Skip to content

Commit f04ae5f

Browse files
authored
Merge pull request #17 from cal-smith/master
feat(button): add button directive
2 parents 40f0c2d + 4cdbaa1 commit f04ae5f

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

src/button/button.directive.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {
2+
Directive,
3+
HostBinding,
4+
Input,
5+
OnInit
6+
} from "@angular/core";
7+
8+
/**
9+
* A convinence directive for applying styling to a button.
10+
*
11+
* Example:
12+
*
13+
* ```hmtl
14+
* <button ibmButton>A button</button>
15+
* <button ibmButton="secondary">A secondary button</button>
16+
* ```
17+
*
18+
* See the [vanilla carbon docs](http://www.carbondesignsystem.com/components/button/code) for more detail.
19+
*/
20+
@Directive({
21+
selector: "[ibmButton]"
22+
})
23+
export class Button implements OnInit {
24+
/**
25+
* sets the button type
26+
*/
27+
@Input() ibmButton: "primary" | "secondary" | "tertiary" | "ghost" | "danger" | "danger--primary" = "primary";
28+
/**
29+
* Specify the size of the button
30+
*/
31+
@Input() size: "normal" | "sm" = "normal";
32+
// a whole lot of HostBindings ... this way we don't have to touch the elementRef directly
33+
@HostBinding("class") btnClass = "bx--btn";
34+
@HostBinding("class.bx--btn--primary") primary = true;
35+
@HostBinding("class.bx--btn--secondary") secondary = false;
36+
@HostBinding("class.bx--btn--tertiary") tertiary = false;
37+
@HostBinding("class.bx--btn--ghost") ghost = false;
38+
@HostBinding("class.bx--btn--danger") danger = false;
39+
@HostBinding("class.bx--btn--danger--primary") dangerPrimary = false;
40+
@HostBinding("class.bx--btn--sm") smallSize = false;
41+
42+
ngOnInit() {
43+
if (this.size === "sm") {
44+
this.smallSize = true;
45+
}
46+
this.primary = false;
47+
switch (this.ibmButton) {
48+
case "primary": this.primary = true; break;
49+
case "secondary": this.secondary = true; break;
50+
case "tertiary": this.tertiary = true; break;
51+
case "ghost": this.ghost = true; break;
52+
case "danger": this.danger = true; break;
53+
case "danger--primary": this.dangerPrimary = true; break;
54+
default: this.primary = true; break;
55+
}
56+
}
57+
}

src/button/button.module.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { NgModule } from "@angular/core";
2+
import { CommonModule } from "@angular/common";
3+
4+
import { Button } from "./button.directive";
5+
6+
export { Button } from "./button.directive";
7+
8+
@NgModule({
9+
declarations: [Button],
10+
exports: [Button],
11+
imports: [CommonModule]
12+
})
13+
export class ButtonModule { }

src/button/button.stories.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { storiesOf, moduleMetadata } from "@storybook/angular";
2+
import { withNotes } from "@storybook/addon-notes";
3+
import { action } from "@storybook/addon-actions";
4+
import { withKnobs, boolean, object } from "@storybook/addon-knobs/angular";
5+
6+
import { ButtonModule } from "../";
7+
8+
storiesOf("Button", module)
9+
.addDecorator(
10+
moduleMetadata({
11+
imports: [ButtonModule]
12+
})
13+
)
14+
.addDecorator(withKnobs)
15+
.add("Basic", () => ({
16+
template: `
17+
<button ibmButton>A button</button>
18+
<br><br>
19+
<button ibmButton="secondary">A secondary button</button>
20+
<br><br>
21+
<button ibmButton="tertiary">A tertiary button</button>
22+
<br><br>
23+
<button ibmButton="ghost">A ghost button</button>
24+
<br><br>
25+
<button ibmButton="danger">A danger button</button>
26+
<br><br>
27+
<button ibmButton="danger--primary">A primary danger button</button>
28+
`
29+
}))
30+
.add("Small", () => ({
31+
template: `
32+
<button ibmButton size="sm">A button</button>
33+
<br><br>
34+
<button ibmButton="secondary" size="sm">A secondary button</button>
35+
<br><br>
36+
<button ibmButton="tertiary" size="sm">A tertiary button</button>
37+
<br><br>
38+
<button ibmButton="ghost" size="sm">A ghost button</button>
39+
<br><br>
40+
<button ibmButton="danger" size="sm">A danger button</button>
41+
<br><br>
42+
<button ibmButton="danger--primary" size="sm">A primary danger button</button>
43+
`
44+
}));

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export * from "./pill-input/pill-input.module";
1414
export * from "./utils/position";
1515
export * from "./content-switcher/content-switcher.module";
1616
export * from "./accordion/accordion.module";
17+
export * from "./button/button.module";

0 commit comments

Comments
 (0)