-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: button component - improve props, variants, sizing, icons, … (
#486) * refactor: button component - improve props, variants, sizing, icons, customization, and Tailwind IntelliSense * chore: removes duplicate transition-colors inside button component * chore: removes duplicate shadow-sm inside button component * refactor: add new button size "icon-md" * chore: Update changeset * Update .changeset/mighty-balloons-camp.md Co-authored-by: Francisco Jiménez Aguilera <[email protected]> * Update .changeset/mighty-balloons-camp.md Co-authored-by: elessar.eth <[email protected]> * feat: Add IconButton component and update usage in code * refactor(web): encapsulate icon dimensions within the `IconButton` component --------- Co-authored-by: Francisco Jiménez Aguilera <[email protected]> Co-authored-by: elessar.eth <[email protected]>
- Loading branch information
1 parent
4875a48
commit be191ba
Showing
17 changed files
with
236 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@blobscan/web": minor | ||
--- | ||
|
||
Improved button component props, variants and sizing logic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,84 @@ | ||
import type { | ||
ButtonHTMLAttributes, | ||
DOMAttributes, | ||
FC, | ||
HTMLAttributes, | ||
ReactElement, | ||
} from "react"; | ||
import classNames from "classnames"; | ||
import type { ButtonHTMLAttributes, FC } from "react"; | ||
import { cva } from "class-variance-authority"; | ||
import type { VariantProps } from "class-variance-authority"; | ||
import { twMerge } from "tailwind-merge"; | ||
|
||
import type { Size } from "~/types"; | ||
|
||
type Variant = "outline" | "primary" | "icon"; | ||
type VariantStyles = Record< | ||
Variant, | ||
HTMLAttributes<HTMLButtonElement>["className"] | ||
>; | ||
|
||
type ButtonProps = { | ||
disabled?: boolean; | ||
type?: ButtonHTMLAttributes<HTMLButtonElement>["type"]; | ||
variant?: keyof VariantStyles; | ||
className?: HTMLAttributes<HTMLButtonElement>["className"]; | ||
size?: Size; | ||
icon?: ReactElement<{ className?: string }>; | ||
label?: React.ReactNode; | ||
onClick?: DOMAttributes<HTMLButtonElement>["onClick"]; | ||
}; | ||
|
||
const VARIANT_STYLES: VariantStyles = { | ||
primary: ` | ||
bg-accent-light | ||
dark:bg-primary-500 | ||
text-accentContent-light | ||
dark:text-accentContent-dark | ||
hover:bg-accentHighlight-light | ||
dark:hover:bg-accentHighlight-dark | ||
active:bg-accent-light | ||
dark:active:bg-accent-dark | ||
disabled:text-warmGray-400 | ||
dark:disabled:text-coolGray-400 | ||
disabled:bg-warmGray-500 | ||
dark:disabled:bg-coolGray-300 | ||
disabled:border-warmGray-500 | ||
dark:disabled:border-coolGray-300 | ||
const buttonVariants = cva( | ||
` | ||
rounded | ||
text-sm | ||
font-semibold | ||
transition-colors | ||
active:scale-[0.99] | ||
disabled:pointer-events-none | ||
disabled:cursor-default | ||
`, | ||
outline: ` | ||
bg-transparent | ||
dark:bg-transparent | ||
border-accent-light | ||
dark:border-accent-dark | ||
text-accent-light | ||
dark:text-accent-dark | ||
hover:text-content-dark | ||
dark:hover:text-content-dark | ||
hover:bg-accentHighlight-light | ||
dark:hover:bg-accentHighlight-dark | ||
active:bg-accent-light | ||
dark:active:bg-accent-dark | ||
border | ||
{ | ||
variants: { | ||
variant: { | ||
primary: ` | ||
shadow-sm | ||
bg-accent-light | ||
dark:bg-primary-500 | ||
text-accentContent-light | ||
dark:text-accentContent-dark | ||
hover:bg-accentHighlight-light | ||
dark:hover:bg-accentHighlight-dark | ||
active:bg-accent-light | ||
dark:active:bg-accent-dark | ||
disabled:text-contentDisabled-light | ||
disabled:border-accentDisabled-light | ||
dark:disabled:text-contentDisabled-dark | ||
dark:disabled:border-accentDisabled-dark | ||
`, | ||
icon: ` | ||
rounded-full p-2 | ||
text-icon-light | ||
shadow-sm transition-colors | ||
focus-visible:outline | ||
focus-visible:outline-2 | ||
focus-visible:outline-offset-2 | ||
focus-visible:outline-iconHighlight-dark | ||
dark:text-icon-dark | ||
`, | ||
}; | ||
disabled:text-warmGray-400 | ||
dark:disabled:text-coolGray-400 | ||
disabled:bg-warmGray-500 | ||
dark:disabled:bg-coolGray-300 | ||
disabled:border-warmGray-500 | ||
dark:disabled:border-coolGray-300 | ||
`, | ||
outline: ` | ||
shadow-sm | ||
bg-transparent | ||
dark:bg-transparent | ||
border-accent-light | ||
dark:border-accent-dark | ||
text-accent-light | ||
dark:text-accent-dark | ||
hover:text-content-dark | ||
dark:hover:text-content-dark | ||
hover:bg-accentHighlight-light | ||
dark:hover:bg-accentHighlight-dark | ||
active:bg-accent-light | ||
dark:active:bg-accent-dark | ||
border | ||
disabled:text-contentDisabled-light | ||
disabled:border-accentDisabled-light | ||
dark:disabled:text-contentDisabled-dark | ||
dark:disabled:border-accentDisabled-dark | ||
`, | ||
}, | ||
size: { | ||
sm: "px-2 py-1 h-7", | ||
md: "px-4 py-2 h-9", | ||
lg: "px-4 py-3 h-11", | ||
}, | ||
}, | ||
defaultVariants: { | ||
variant: "primary", | ||
size: "md", | ||
}, | ||
} | ||
); | ||
|
||
export const Button: FC<ButtonProps> = function ({ | ||
disabled = false, | ||
type = "button", | ||
className, | ||
icon, | ||
label, | ||
onClick, | ||
size = "md", | ||
variant, | ||
}: ButtonProps) { | ||
type Props = ButtonHTMLAttributes<HTMLButtonElement> & | ||
VariantProps<typeof buttonVariants>; | ||
|
||
const Button: FC<Props> = ({ className, variant, size, ...props }) => { | ||
return ( | ||
<button | ||
disabled={disabled} | ||
type={type} | ||
className={` | ||
${VARIANT_STYLES[variant ?? "primary"]} | ||
${ | ||
variant !== "icon" | ||
? classNames({ | ||
"px-2 py-1": size === "sm", | ||
"px-4 py-2": size === "md", | ||
"px-4 py-3": size === "lg", | ||
}) | ||
: ` | ||
fill-icon-light | ||
text-icon-light | ||
hover:fill-iconHighlight-light | ||
hover:text-iconHighlight-light | ||
dark:fill-icon-dark | ||
dark:text-icon-dark | ||
hover:dark:fill-iconHighlight-dark | ||
hover:dark:text-iconHighlight-dark | ||
${classNames({ | ||
"w-7": size === "sm", | ||
"w-9": size === "md", | ||
"w-11": size === "lg", | ||
"w-13": size === "xl", | ||
})} | ||
` | ||
} | ||
${classNames({ | ||
"h-7": size === "sm", | ||
"h-9": size === "md", | ||
"h-11": size === "lg", | ||
"h-13": size === "xl", | ||
})} | ||
cursor-pointer | ||
rounded | ||
text-sm | ||
font-semibold | ||
shadow-sm | ||
transition-colors | ||
active:scale-[0.99] | ||
disabled:pointer-events-none | ||
disabled:cursor-default | ||
${className} | ||
`} | ||
onClick={onClick} | ||
> | ||
<div className="flex h-full items-center gap-1"> | ||
{icon && <div className="h-full w-full">{icon}</div>} | ||
{label} | ||
</div> | ||
</button> | ||
className={twMerge(buttonVariants({ variant, size }), className)} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export { Button }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { ButtonHTMLAttributes, FC } from "react"; | ||
import { cva } from "class-variance-authority"; | ||
import type { VariantProps } from "class-variance-authority"; | ||
import { twMerge } from "tailwind-merge"; | ||
|
||
const iconVariants = cva( | ||
` | ||
rounded | ||
text-sm | ||
font-semibold | ||
transition-colors | ||
active:scale-[0.99] | ||
disabled:pointer-events-none | ||
disabled:cursor-default | ||
flex | ||
items-center | ||
justify-center | ||
`, | ||
{ | ||
variants: { | ||
variant: { | ||
default: ` | ||
focus-visible:outline | ||
focus-visible:outline-2 | ||
focus-visible:outline-offset-2 | ||
focus-visible:outline-iconHighlight-dark | ||
fill-icon-light | ||
text-icon-light | ||
hover:fill-iconHighlight-light | ||
hover:text-iconHighlight-light | ||
dark:fill-icon-dark | ||
dark:text-icon-dark | ||
hover:dark:fill-iconHighlight-dark | ||
hover:dark:text-iconHighlight-dark | ||
`, | ||
}, | ||
size: { | ||
md: "[&>*]:h-5 [&>*]:w-5", | ||
}, | ||
}, | ||
defaultVariants: { | ||
variant: "default", | ||
size: "md", | ||
}, | ||
} | ||
); | ||
|
||
type Props = ButtonHTMLAttributes<HTMLButtonElement> & | ||
VariantProps<typeof iconVariants>; | ||
|
||
const IconButton: FC<Props> = ({ className, variant, size, ...props }) => { | ||
return ( | ||
<button | ||
className={twMerge(iconVariants({ variant, size }), className)} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export { IconButton }; |
Oops, something went wrong.