Skip to content

Commit

Permalink
Merge pull request #4 from hotosm/cva-button
Browse files Browse the repository at this point in the history
feat(button,package): add class-variance-authority lib, convert button component to use it
  • Loading branch information
spwoodcock authored Nov 23, 2023
2 parents 8a9eddb + 8e8e0a8 commit 5e2a30c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pnpm install @hotosm/ui
## Usage

```js
import Button from '@hotosm/ui';
import { Button } from '@hotosm/ui';

const HomePage = ({}) => {
return (
Expand Down
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",
},
};
31 changes: 26 additions & 5 deletions src/button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
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" : ""
}`,
buttonStyle({
disabled: props.disabled,
intent: intent,
className: className,
}),
className,
)}
{...rest}
Expand Down

0 comments on commit 5e2a30c

Please sign in to comment.