|
| 1 | +import { Children } from 'react'; |
| 2 | +import { cva, type VariantProps } from 'cva'; |
| 3 | +import { useLocale, Button } from 'react-aria-components'; |
| 4 | +import { |
| 5 | + Close, |
| 6 | + InfoCircle, |
| 7 | + CheckCircle, |
| 8 | + Warning, |
| 9 | + CloseCircle, |
| 10 | +} from '@obosbbl/grunnmuren-icons-react'; |
| 11 | +import { useState } from 'react'; |
| 12 | + |
| 13 | +// TODO: expand/collapse |
| 14 | +// TODO: add new icons |
| 15 | + |
| 16 | +const iconMap = { |
| 17 | + info: InfoCircle, |
| 18 | + success: CheckCircle, |
| 19 | + warning: Warning, |
| 20 | + danger: CloseCircle, |
| 21 | +}; |
| 22 | + |
| 23 | +const alertVariants = cva({ |
| 24 | + base: [ |
| 25 | + 'grid grid-cols-[auto_1fr_auto] items-center gap-x-2 gap-y-4 rounded-md border-2 px-3 py-2', |
| 26 | + // Heading styles: |
| 27 | + '[&_[data-slot="heading"]]:text-base [&_[data-slot="heading"]]:font-medium [&_[data-slot="heading"]]:leading-7', |
| 28 | + // Content styles: |
| 29 | + '[&:has([data-slot="heading"])_[data-slot="content"]]:col-span-full [&_[data-slot="content"]]:text-sm [&_[data-slot="content"]]:leading-6', |
| 30 | + // Footer styles: |
| 31 | + '[&_[data-slot="footer"]]:col-span-full [&_[data-slot="footer"]]:-mt-[6px] [&_[data-slot="footer"]]:text-xs [&_[data-slot="footer"]]:font-light [&_[data-slot="footer"]]:leading-6', |
| 32 | + ], |
| 33 | + variants: { |
| 34 | + /** |
| 35 | + * The variant of the alert |
| 36 | + * @default info |
| 37 | + */ |
| 38 | + variant: { |
| 39 | + info: 'border-[#1A7FA7] bg-sky-light', |
| 40 | + success: 'border-[#0F9B6E] bg-mint-light', |
| 41 | + warning: 'border-[#C57C13] bg-[#FFF2DE]', |
| 42 | + danger: 'border-[#C0385D] bg-red-light', |
| 43 | + }, |
| 44 | + }, |
| 45 | + defaultVariants: { |
| 46 | + variant: 'info', |
| 47 | + }, |
| 48 | +}); |
| 49 | + |
| 50 | +type Props = VariantProps<typeof alertVariants> & { |
| 51 | + children: React.ReactNode; |
| 52 | + /** |
| 53 | + * The ARIA role for the alertbox. |
| 54 | + */ |
| 55 | + role: 'alert' | 'status' | 'none'; |
| 56 | + /** |
| 57 | + * Controls if the alert can be dismissed with a close button. |
| 58 | + * @default true |
| 59 | + */ |
| 60 | + isDismissable?: boolean; |
| 61 | + /** Additional CSS className for the element. */ |
| 62 | + className?: string; |
| 63 | + /** |
| 64 | + * Controls if the alert is rendered or not. |
| 65 | + * This is used to control the open/closed state of the component; make the component "controlled". |
| 66 | + */ |
| 67 | + isVisible?: boolean; |
| 68 | + /** |
| 69 | + * Callback that should be triggered when a dismissable alert is closed. |
| 70 | + * This is used to control the open/closed state of the component; make the component "controlled". |
| 71 | + */ |
| 72 | + onClose?: () => void; |
| 73 | +}; |
| 74 | + |
| 75 | +const Alertbox = ({ |
| 76 | + children, |
| 77 | + role, |
| 78 | + className, |
| 79 | + variant = 'info', |
| 80 | + isDismissable, |
| 81 | + isVisible: isControlledVisible, |
| 82 | + onClose, |
| 83 | +}: Props) => { |
| 84 | + const Icon = iconMap[variant]; |
| 85 | + |
| 86 | + // Set a default aria-label for the close button and handle translations based on the current locale |
| 87 | + const { locale } = useLocale(); |
| 88 | + let closeLabel = 'Lukk'; |
| 89 | + if (locale === 'sv') closeLabel = 'Stäng'; |
| 90 | + else if (locale === 'en') closeLabel = 'Close'; |
| 91 | + |
| 92 | + const [isUncontrolledVisible, setIsUncontrolledVisible] = useState(true); |
| 93 | + const isVisible = |
| 94 | + isControlledVisible !== undefined |
| 95 | + ? isControlledVisible |
| 96 | + : isUncontrolledVisible; |
| 97 | + |
| 98 | + if (!isVisible) return; |
| 99 | + |
| 100 | + const close = () => { |
| 101 | + setIsUncontrolledVisible(false); |
| 102 | + if (onClose) onClose(); |
| 103 | + }; |
| 104 | + |
| 105 | + const isInDevMode = process.env.NODE_ENV !== 'production'; |
| 106 | + |
| 107 | + if (isInDevMode && onClose && !isDismissable) { |
| 108 | + console.warn( |
| 109 | + 'Passing an `onClose` callback without setting the `isDismissable` prop to `true` will not have any effect.', |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + if (isInDevMode && !children) { |
| 114 | + console.error('`No children was passed to the <AlertBox/>` component.'); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + const [firstChild, ...restChildren] = Children.toArray(children); |
| 119 | + |
| 120 | + return ( |
| 121 | + <div |
| 122 | + className={alertVariants({ |
| 123 | + className, |
| 124 | + variant, |
| 125 | + })} |
| 126 | + // The role prop is required to force consumers to consider and choose the appropriate alertbox role. |
| 127 | + // role="none" will not have any effect on a div, so it can be omitted. |
| 128 | + role={role === 'none' ? undefined : role} |
| 129 | + > |
| 130 | + <Icon /> |
| 131 | + {firstChild} |
| 132 | + {isDismissable && ( |
| 133 | + <Button |
| 134 | + className="-m-2 grid h-11 w-11 place-items-center outline-transparent transition-[outline] duration-200 focus:-outline-offset-8 focus:outline-black" |
| 135 | + onPress={close} |
| 136 | + aria-label={closeLabel} |
| 137 | + > |
| 138 | + <Close /> |
| 139 | + </Button> |
| 140 | + )} |
| 141 | + {restChildren} |
| 142 | + </div> |
| 143 | + ); |
| 144 | +}; |
| 145 | + |
| 146 | +export { type Props as AlertboxProps, Alertbox }; |
0 commit comments