Skip to content

Commit

Permalink
feat(button,package): add class-variance-authority lib, convert butto…
Browse files Browse the repository at this point in the history
…n component to use it
  • Loading branch information
JoltCode committed Nov 21, 2023
1 parent 8a9eddb commit 5ba127d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"react": "^18.2.0",
"tailwind-merge": "^1.14.0"
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/button/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ ButtonStory.argTypes = {
},
defaultValue: false,
},
intent: {
options: ["primary", "secondary"],
control: {
type: "select",
labels: {
primary: "Primary",
secondary: "Secondary",
}
},
defaultValue: "primary",
}
};
27 changes: 21 additions & 6 deletions src/button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import { cn } from "@/utils/merge.js";
import {cva, VariantProps} from "class-variance-authority";

export interface ButtonProps extends React.HTMLAttributes<HTMLButtonElement> {
export interface ButtonProps extends React.HTMLAttributes<HTMLButtonElement>, VariantProps<typeof buttonStyle> {
disabled?: boolean;
}

const buttonStyle = cva("bg-primary text-white py-3 px-6 rounded leading-[1.15]", {
variants: {
intent: {
primary: "bg-primary text-white",
secondary: "bg-secondary text-white",
},
disabled: {
true: "opacity-50 cursor-not-allowed",
false: "",
}
}
})

export const Button = (props: ButtonProps) => {
const { className, ...rest } = props;
const { className, intent, ...rest } = props;

return (
<button
className={cn(
`bg-primary text-white py-3 px-6 rounded leading-[1.15] ${
props.disabled ? "opacity-50 cursor-not-allowed" : ""
}`,
className={cn(buttonStyle({
disabled: props.disabled,
intent: intent,
className: className,
}),
className,
)}
{...rest}
Expand Down

0 comments on commit 5ba127d

Please sign in to comment.