|
| 1 | +import * as React from "react" |
| 2 | +import { cva, type VariantProps } from "class-variance-authority" |
| 3 | +import { Slot } from "@radix-ui/react-slot" |
| 4 | + |
| 5 | +import { BaseLink, type LinkProps } from "@/components/Link" |
| 6 | + |
| 7 | +import { cn } from "@/lib/utils/cn" |
| 8 | +import { type MatomoEventOptions, trackCustomEvent } from "@/lib/utils/matomo" |
| 9 | +import { scrollIntoView } from "@/lib/utils/scrollIntoView" |
| 10 | + |
| 11 | +const buttonVariants = cva( |
| 12 | + "inline-flex gap-2 items-center justify-center rounded border-[1px] border-solid border-current text-primary transition focus-visible:outline focus-visible:outline-4 focus-visible:outline-primary-hover focus-visible:-outline-offset-1 disabled:text-disabled disabled:pointer-events-none hover:text-primary-hover [&[data-secondary='true']]:text-body", |
| 13 | + { |
| 14 | + variants: { |
| 15 | + variant: { |
| 16 | + solid: |
| 17 | + "!text-background bg-primary border-transparent disabled:bg-disabled disabled:text-background hover:text-background hover:bg-primary-hover hover:shadow-button-hover active:shadow-none", |
| 18 | + outline: "hover:shadow-button-hover active:shadow-none", |
| 19 | + ghost: "border-transparent", |
| 20 | + link: "border-transparent font-bold underline py-0 px-1 active:text-primary", |
| 21 | + }, |
| 22 | + size: { |
| 23 | + md: "min-h-10.5 px-4 py-2 [&>svg]:text-2xl", |
| 24 | + sm: "text-xs min-h-[31px] py-1.5 px-2 [&>svg]:text-md", |
| 25 | + }, |
| 26 | + }, |
| 27 | + defaultVariants: { |
| 28 | + variant: "solid", |
| 29 | + size: "md", |
| 30 | + }, |
| 31 | + } |
| 32 | +) |
| 33 | + |
| 34 | +export const checkIsSecondary = ({ |
| 35 | + variant, |
| 36 | + isSecondary, |
| 37 | +}: { |
| 38 | + variant: ButtonVariantProps["variant"] |
| 39 | + isSecondary: boolean |
| 40 | +}) => { |
| 41 | + // These two variants do not have secondary styling, so prevent overrides |
| 42 | + return { |
| 43 | + "data-secondary": |
| 44 | + !["solid", "link"].includes(variant || "solid") && isSecondary, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +type ButtonVariantProps = VariantProps<typeof buttonVariants> |
| 49 | + |
| 50 | +export interface ButtonProps |
| 51 | + extends React.ButtonHTMLAttributes<HTMLButtonElement>, |
| 52 | + ButtonVariantProps { |
| 53 | + asChild?: boolean |
| 54 | + /** |
| 55 | + * Set string value that matches the `id` attribute value used |
| 56 | + * on another element in a given page. Selecting the button will then |
| 57 | + * trigger a scroll to that element. |
| 58 | + */ |
| 59 | + toId?: string |
| 60 | + /** |
| 61 | + * Custom theme prop. If true, `body` color is used instead of |
| 62 | + * `primary` color in the theming. |
| 63 | + * |
| 64 | + * `NOTE`: Does not apply to the `Solid` or `Link` variants |
| 65 | + */ |
| 66 | + isSecondary?: boolean |
| 67 | + customEventOptions?: MatomoEventOptions |
| 68 | +} |
| 69 | + |
| 70 | +const Button = React.forwardRef<HTMLButtonElement, ButtonProps>( |
| 71 | + ( |
| 72 | + { |
| 73 | + className, |
| 74 | + variant, |
| 75 | + size, |
| 76 | + asChild = false, |
| 77 | + isSecondary = false, |
| 78 | + onClick, |
| 79 | + toId, |
| 80 | + customEventOptions, |
| 81 | + ...props |
| 82 | + }, |
| 83 | + ref |
| 84 | + ) => { |
| 85 | + const handleOnClick = (e: React.MouseEvent<HTMLButtonElement>) => { |
| 86 | + toId && scrollIntoView(toId) |
| 87 | + customEventOptions && trackCustomEvent(customEventOptions) |
| 88 | + |
| 89 | + onClick?.(e) |
| 90 | + } |
| 91 | + |
| 92 | + const Comp = asChild ? Slot : "button" |
| 93 | + return ( |
| 94 | + <Comp |
| 95 | + className={cn(buttonVariants({ variant, size, className }))} |
| 96 | + ref={ref} |
| 97 | + onClick={handleOnClick} |
| 98 | + {...checkIsSecondary({ |
| 99 | + variant, |
| 100 | + isSecondary, |
| 101 | + })} |
| 102 | + {...props} |
| 103 | + /> |
| 104 | + ) |
| 105 | + } |
| 106 | +) |
| 107 | +Button.displayName = "Button" |
| 108 | + |
| 109 | +type ButtonLinkProps = ButtonProps & { |
| 110 | + linkProps: LinkProps |
| 111 | + customEventOptions?: MatomoEventOptions |
| 112 | +} |
| 113 | + |
| 114 | +const ButtonLink = React.forwardRef<HTMLAnchorElement, ButtonLinkProps>( |
| 115 | + ({ linkProps, customEventOptions, children, ...buttonProps }, ref) => { |
| 116 | + const handleClick = () => { |
| 117 | + customEventOptions && trackCustomEvent(customEventOptions) |
| 118 | + } |
| 119 | + return ( |
| 120 | + <Button asChild {...buttonProps}> |
| 121 | + <BaseLink |
| 122 | + ref={ref} |
| 123 | + activeStyle={{}} |
| 124 | + // TODO: Redress this override when migrating the link component |
| 125 | + color={ |
| 126 | + buttonProps.variant === "solid" ? "background.base" : undefined |
| 127 | + } |
| 128 | + textDecor="none" |
| 129 | + _hover={{ |
| 130 | + textDecor: "none", |
| 131 | + }} |
| 132 | + {...linkProps} |
| 133 | + onClick={handleClick} |
| 134 | + > |
| 135 | + {children} |
| 136 | + </BaseLink> |
| 137 | + </Button> |
| 138 | + ) |
| 139 | + } |
| 140 | +) |
| 141 | +ButtonLink.displayName = "ButtonLink" |
| 142 | + |
| 143 | +export { |
| 144 | + Button, |
| 145 | + ButtonLink, |
| 146 | + type ButtonLinkProps, |
| 147 | + type ButtonVariantProps, |
| 148 | + buttonVariants, |
| 149 | +} |
0 commit comments