|
| 1 | +import { forwardRef, useMemo } from 'react'; |
| 2 | +import { View } from 'react-native'; |
| 3 | +import { CloseIcon } from '../../helpers/components/close-icon'; |
| 4 | +import { Text } from '../../helpers/components/text'; |
| 5 | +import type { ViewRef } from '../../helpers/types'; |
| 6 | +import { createContext } from '../../helpers/utils'; |
| 7 | +import * as ToastPrimitive from '../../primitives/toast'; |
| 8 | +import { Button } from '../button'; |
| 9 | +import { DISPLAY_NAME } from './toast.constants'; |
| 10 | +import toastStyles, { styleSheet } from './toast.styles'; |
| 11 | +import type { |
| 12 | + ToastActionProps, |
| 13 | + ToastCloseProps, |
| 14 | + ToastContextValue, |
| 15 | + ToastDescriptionProps, |
| 16 | + ToastLabelProps, |
| 17 | + ToastRootProps, |
| 18 | +} from './toast.types'; |
| 19 | + |
| 20 | +const [ToastProvider, useToast] = createContext<ToastContextValue>({ |
| 21 | + name: 'ToastContext', |
| 22 | +}); |
| 23 | + |
| 24 | +// -------------------------------------------------- |
| 25 | + |
| 26 | +const ToastRoot = forwardRef<ViewRef, ToastRootProps>((props, ref) => { |
| 27 | + const { |
| 28 | + children, |
| 29 | + variant = 'default', |
| 30 | + className, |
| 31 | + style, |
| 32 | + ...restProps |
| 33 | + } = props; |
| 34 | + |
| 35 | + const tvStyles = toastStyles.root({ |
| 36 | + className, |
| 37 | + }); |
| 38 | + |
| 39 | + const contextValue = useMemo( |
| 40 | + () => ({ |
| 41 | + variant, |
| 42 | + }), |
| 43 | + [variant] |
| 44 | + ); |
| 45 | + |
| 46 | + return ( |
| 47 | + <ToastProvider value={contextValue}> |
| 48 | + <ToastPrimitive.Root |
| 49 | + ref={ref} |
| 50 | + className={tvStyles} |
| 51 | + style={[styleSheet.root, style]} |
| 52 | + {...restProps} |
| 53 | + > |
| 54 | + {children} |
| 55 | + </ToastPrimitive.Root> |
| 56 | + </ToastProvider> |
| 57 | + ); |
| 58 | +}); |
| 59 | + |
| 60 | +// -------------------------------------------------- |
| 61 | + |
| 62 | +const ToastLabel = forwardRef<View, ToastLabelProps>((props, ref) => { |
| 63 | + const { children, className, ...restProps } = props; |
| 64 | + |
| 65 | + const { variant } = useToast(); |
| 66 | + |
| 67 | + const tvStyles = toastStyles.label({ |
| 68 | + variant, |
| 69 | + className, |
| 70 | + }); |
| 71 | + |
| 72 | + return ( |
| 73 | + <Text ref={ref} className={tvStyles} {...restProps}> |
| 74 | + {children} |
| 75 | + </Text> |
| 76 | + ); |
| 77 | +}); |
| 78 | + |
| 79 | +// -------------------------------------------------- |
| 80 | + |
| 81 | +const ToastDescription = forwardRef<View, ToastDescriptionProps>( |
| 82 | + (props, ref) => { |
| 83 | + const { children, className, ...restProps } = props; |
| 84 | + |
| 85 | + const tvStyles = toastStyles.description({ |
| 86 | + className, |
| 87 | + }); |
| 88 | + |
| 89 | + return ( |
| 90 | + <Text ref={ref} className={tvStyles} {...restProps}> |
| 91 | + {children} |
| 92 | + </Text> |
| 93 | + ); |
| 94 | + } |
| 95 | +); |
| 96 | + |
| 97 | +// -------------------------------------------------- |
| 98 | + |
| 99 | +const ToastAction = forwardRef<View, ToastActionProps>((props, ref) => { |
| 100 | + const { children, variant, size = 'sm', className, ...restProps } = props; |
| 101 | + |
| 102 | + const { variant: toastVariant } = useToast(); |
| 103 | + |
| 104 | + const buttonVariant = useMemo(() => { |
| 105 | + if (variant) return variant; |
| 106 | + |
| 107 | + switch (toastVariant) { |
| 108 | + case 'accent': |
| 109 | + return 'primary'; |
| 110 | + case 'danger': |
| 111 | + return 'destructive'; |
| 112 | + default: |
| 113 | + return 'tertiary'; |
| 114 | + } |
| 115 | + }, [toastVariant, variant]); |
| 116 | + |
| 117 | + const tvStyles = toastStyles.action({ |
| 118 | + variant: toastVariant, |
| 119 | + className, |
| 120 | + }); |
| 121 | + |
| 122 | + return ( |
| 123 | + <Button |
| 124 | + ref={ref} |
| 125 | + variant={buttonVariant} |
| 126 | + size={size} |
| 127 | + className={tvStyles} |
| 128 | + {...restProps} |
| 129 | + > |
| 130 | + {children} |
| 131 | + </Button> |
| 132 | + ); |
| 133 | +}); |
| 134 | + |
| 135 | +// -------------------------------------------------- |
| 136 | + |
| 137 | +const ToastClose = forwardRef<View, ToastCloseProps>((props, ref) => { |
| 138 | + const { children, iconProps, size = 'sm', className, ...restProps } = props; |
| 139 | + |
| 140 | + return ( |
| 141 | + <Button |
| 142 | + ref={ref} |
| 143 | + variant="ghost" |
| 144 | + size={size} |
| 145 | + isIconOnly |
| 146 | + aria-label="Close" |
| 147 | + className={className} |
| 148 | + {...restProps} |
| 149 | + > |
| 150 | + {children ?? ( |
| 151 | + <CloseIcon size={iconProps?.size ?? 16} color={iconProps?.color} /> |
| 152 | + )} |
| 153 | + </Button> |
| 154 | + ); |
| 155 | +}); |
| 156 | + |
| 157 | +// -------------------------------------------------- |
| 158 | + |
| 159 | +ToastRoot.displayName = DISPLAY_NAME.TOAST_ROOT; |
| 160 | +ToastLabel.displayName = DISPLAY_NAME.TOAST_LABEL; |
| 161 | +ToastDescription.displayName = DISPLAY_NAME.TOAST_DESCRIPTION; |
| 162 | +ToastAction.displayName = DISPLAY_NAME.TOAST_ACTION; |
| 163 | +ToastClose.displayName = DISPLAY_NAME.TOAST_CLOSE; |
| 164 | + |
| 165 | +/** |
| 166 | + * Compound Toast component with sub-components |
| 167 | + * |
| 168 | + * @component Toast - Main toast container that displays notification messages with various variants. |
| 169 | + * |
| 170 | + * @component Toast.Label - Title/heading text of the toast notification. |
| 171 | + * |
| 172 | + * @component Toast.Description - Descriptive text content of the toast. |
| 173 | + * |
| 174 | + * @component Toast.Action - Action button within the toast. Variant is automatically determined |
| 175 | + * based on toast variant but can be overridden. |
| 176 | + * |
| 177 | + * @component Toast.Close - Close button for dismissing the toast. Renders as an icon-only button. |
| 178 | + * |
| 179 | + * Props flow from Toast to sub-components via context (variant). |
| 180 | + * |
| 181 | + * @see Full documentation: https://heroui.com/components/toast |
| 182 | + */ |
| 183 | +const CompoundToast = Object.assign(ToastRoot, { |
| 184 | + /** Toast label/title - renders text content */ |
| 185 | + Label: ToastLabel, |
| 186 | + /** Toast description - renders descriptive text */ |
| 187 | + Description: ToastDescription, |
| 188 | + /** Toast action button - renders action with appropriate variant */ |
| 189 | + Action: ToastAction, |
| 190 | + /** Toast close button - renders icon-only close button */ |
| 191 | + Close: ToastClose, |
| 192 | +}); |
| 193 | + |
| 194 | +export { useToast }; |
| 195 | +export default CompoundToast; |
0 commit comments