Skip to content

Commit

Permalink
feat(button): add example button variants with tailwind
Browse files Browse the repository at this point in the history
  • Loading branch information
dcwither committed May 27, 2020
1 parent e3a03a7 commit 6f6ccd5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 23 deletions.
3 changes: 2 additions & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
},
"dependencies": {
"@chanzuckerberg/czedi-kit-styles": "^0.0.0",
"@chanzuckerberg/czedi-kit-tokens": "^0.0.0"
"@chanzuckerberg/czedi-kit-tokens": "^0.0.0",
"classnames": "^2.2.6"
},
"devDependencies": {
"@babel/core": "^7.6.4",
Expand Down
23 changes: 15 additions & 8 deletions packages/components/src/button/button.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { axe } from "jest-axe";
import { render } from "@testing-library/react";
import { text } from "./button.stories";
import { variants } from "./button.stories";

describe("<Button />", () => {
it("renders the component", () => {
const { container } = render(text());
const { container } = render(variants());
expect(container.firstChild).toMatchInlineSnapshot(`
<button
class="czedi-btn "
>
Hello Button
</button>
<div>
<button
class="czedi-btn czedi-btn-primary"
>
Hello Button
</button>
<button
class="czedi-btn czedi-btn-secondary"
>
Secondary Button
</button>
</div>
`);
});

it("passes accessibility checks", async () => {
const { container } = render(text());
const { container } = render(variants());
const results = await axe(container);
expect(results).toHaveNoViolations();
});
Expand Down
11 changes: 11 additions & 0 deletions packages/components/src/button/button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ export const text = (): JSX.Element => (
<Button onClick={action("clicked")}>Hello Button</Button>
);

export const variants = (): JSX.Element => (
<div>
<Button onClick={action("clicked")} variant="primary">
Hello Button
</Button>
<Button onClick={action("clicked")} variant="secondary">
Secondary Button
</Button>
</div>
);

export const emoji = (): JSX.Element => (
<Button onClick={action("clicked")}>
<span aria-label="so cool">😀 😎 👍 💯</span>
Expand Down
30 changes: 22 additions & 8 deletions packages/components/src/button/button.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import * as React from "react";

function Button(
props: React.DetailedHTMLProps<
React.ButtonHTMLAttributes<HTMLButtonElement>,
HTMLButtonElement
>
): JSX.Element {
const { className = "", ...rest } = props;
return <button className={`czedi-btn ${className}`} {...rest}></button>;
import cx from "classnames";

// Temporary props for button to test out css options for variants
type ButtonProps = React.DetailedHTMLProps<
React.ButtonHTMLAttributes<HTMLButtonElement>,
HTMLButtonElement
> & {
variant: "primary" | "secondary";
};

function Button(props: ButtonProps): JSX.Element {
const { className = "", variant, ...rest } = props;
const classNames = cx("czedi-btn", className, {
"czedi-btn-primary": variant === "primary",
"czedi-btn-secondary": variant === "secondary",
});

return <button className={classNames} {...rest}></button>;
}

Button.defaultProps = {
variant: "primary",
};

export default Button;
22 changes: 16 additions & 6 deletions packages/styles/src/button/button.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
@import "./_variables.scss";
@import "@chanzuckerberg/czedi-kit-tokens/scss/_variables.scss";

.btn {
@apply font-bold py-2 px-4 rounded bg-blue-500 text-white;
@apply font-bold py-2 px-4 rounded text-white;
}

.btn:hover {
@apply bg-blue-700;
.btn,
.btn-primary {
@apply bg-blue-300;

&:hover {
@apply bg-blue-400;
}
}

.btn-secondary {
@apply bg-gray-600;

&:hover {
@apply bg-gray-800;
}
}

0 comments on commit 6f6ccd5

Please sign in to comment.