Skip to content

Commit

Permalink
Merge pull request #17 from cal-smith/master
Browse files Browse the repository at this point in the history
feat(button): add button directive
  • Loading branch information
cal-smith authored Aug 22, 2018
2 parents 40f0c2d + 4cdbaa1 commit f04ae5f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/button/button.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
Directive,
HostBinding,
Input,
OnInit
} from "@angular/core";

/**
* A convinence directive for applying styling to a button.
*
* Example:
*
* ```hmtl
* <button ibmButton>A button</button>
* <button ibmButton="secondary">A secondary button</button>
* ```
*
* See the [vanilla carbon docs](http://www.carbondesignsystem.com/components/button/code) for more detail.
*/
@Directive({
selector: "[ibmButton]"
})
export class Button implements OnInit {
/**
* sets the button type
*/
@Input() ibmButton: "primary" | "secondary" | "tertiary" | "ghost" | "danger" | "danger--primary" = "primary";
/**
* Specify the size of the button
*/
@Input() size: "normal" | "sm" = "normal";
// a whole lot of HostBindings ... this way we don't have to touch the elementRef directly
@HostBinding("class") btnClass = "bx--btn";
@HostBinding("class.bx--btn--primary") primary = true;
@HostBinding("class.bx--btn--secondary") secondary = false;
@HostBinding("class.bx--btn--tertiary") tertiary = false;
@HostBinding("class.bx--btn--ghost") ghost = false;
@HostBinding("class.bx--btn--danger") danger = false;
@HostBinding("class.bx--btn--danger--primary") dangerPrimary = false;
@HostBinding("class.bx--btn--sm") smallSize = false;

ngOnInit() {
if (this.size === "sm") {
this.smallSize = true;
}
this.primary = false;
switch (this.ibmButton) {
case "primary": this.primary = true; break;
case "secondary": this.secondary = true; break;
case "tertiary": this.tertiary = true; break;
case "ghost": this.ghost = true; break;
case "danger": this.danger = true; break;
case "danger--primary": this.dangerPrimary = true; break;
default: this.primary = true; break;
}
}
}
13 changes: 13 additions & 0 deletions src/button/button.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";

import { Button } from "./button.directive";

export { Button } from "./button.directive";

@NgModule({
declarations: [Button],
exports: [Button],
imports: [CommonModule]
})
export class ButtonModule { }
44 changes: 44 additions & 0 deletions src/button/button.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { storiesOf, moduleMetadata } from "@storybook/angular";
import { withNotes } from "@storybook/addon-notes";
import { action } from "@storybook/addon-actions";
import { withKnobs, boolean, object } from "@storybook/addon-knobs/angular";

import { ButtonModule } from "../";

storiesOf("Button", module)
.addDecorator(
moduleMetadata({
imports: [ButtonModule]
})
)
.addDecorator(withKnobs)
.add("Basic", () => ({
template: `
<button ibmButton>A button</button>
<br><br>
<button ibmButton="secondary">A secondary button</button>
<br><br>
<button ibmButton="tertiary">A tertiary button</button>
<br><br>
<button ibmButton="ghost">A ghost button</button>
<br><br>
<button ibmButton="danger">A danger button</button>
<br><br>
<button ibmButton="danger--primary">A primary danger button</button>
`
}))
.add("Small", () => ({
template: `
<button ibmButton size="sm">A button</button>
<br><br>
<button ibmButton="secondary" size="sm">A secondary button</button>
<br><br>
<button ibmButton="tertiary" size="sm">A tertiary button</button>
<br><br>
<button ibmButton="ghost" size="sm">A ghost button</button>
<br><br>
<button ibmButton="danger" size="sm">A danger button</button>
<br><br>
<button ibmButton="danger--primary" size="sm">A primary danger button</button>
`
}));
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from "./pill-input/pill-input.module";
export * from "./utils/position";
export * from "./content-switcher/content-switcher.module";
export * from "./accordion/accordion.module";
export * from "./button/button.module";

0 comments on commit f04ae5f

Please sign in to comment.