|
| 1 | +import type { Drawer as _Drawer } from '@base-ui/react/drawer'; |
| 2 | +import { |
| 3 | + createContext, |
| 4 | + useCallback, |
| 5 | + useContext, |
| 6 | + useEffect, |
| 7 | + useMemo, |
| 8 | + useRef, |
| 9 | +} from '@wordpress/element'; |
| 10 | + |
| 11 | +// -- Modal context ---------------------------------------------------------- |
| 12 | + |
| 13 | +const DrawerModalContext = |
| 14 | + createContext< _Drawer.Root.Props[ 'modal' ] >( true ); |
| 15 | + |
| 16 | +export function DrawerModalProvider( { |
| 17 | + modal = true, |
| 18 | + children, |
| 19 | +}: { |
| 20 | + modal?: _Drawer.Root.Props[ 'modal' ]; |
| 21 | + children: React.ReactNode; |
| 22 | +} ) { |
| 23 | + return ( |
| 24 | + <DrawerModalContext.Provider value={ modal }> |
| 25 | + { children } |
| 26 | + </DrawerModalContext.Provider> |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +export function useDrawerModal() { |
| 31 | + return useContext( DrawerModalContext ); |
| 32 | +} |
| 33 | + |
| 34 | +// -- Validation context (dev-only) ------------------------------------------ |
| 35 | + |
| 36 | +/** |
| 37 | + * Whether validation is enabled. This is a build-time constant that allows |
| 38 | + * bundlers to tree-shake all validation code in production builds. |
| 39 | + */ |
| 40 | +const VALIDATION_ENABLED = process.env.NODE_ENV !== 'production'; |
| 41 | + |
| 42 | +type DrawerValidationContextType = { |
| 43 | + registerTitle: ( element: HTMLElement | null ) => void; |
| 44 | +}; |
| 45 | + |
| 46 | +const DrawerValidationContext = VALIDATION_ENABLED |
| 47 | + ? createContext< DrawerValidationContextType | null >( null ) |
| 48 | + : ( null as unknown as React.Context< DrawerValidationContextType | null > ); |
| 49 | + |
| 50 | +function useDrawerValidationContextDev() { |
| 51 | + return useContext( DrawerValidationContext ); |
| 52 | +} |
| 53 | + |
| 54 | +function useDrawerValidationContextProd() { |
| 55 | + return null; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Hook to access the drawer validation context. |
| 60 | + * Returns null in production or if not within a Drawer.Popup. |
| 61 | + */ |
| 62 | +export const useDrawerValidationContext = VALIDATION_ENABLED |
| 63 | + ? useDrawerValidationContextDev |
| 64 | + : useDrawerValidationContextProd; |
| 65 | + |
| 66 | +function DrawerValidationProviderDev( { |
| 67 | + children, |
| 68 | +}: { |
| 69 | + children: React.ReactNode; |
| 70 | +} ) { |
| 71 | + const titleElementRef = useRef< HTMLElement | null >( null ); |
| 72 | + |
| 73 | + const registerTitle = useCallback( ( element: HTMLElement | null ) => { |
| 74 | + titleElementRef.current = element; |
| 75 | + }, [] ); |
| 76 | + |
| 77 | + const contextValue = useMemo( |
| 78 | + () => ( { registerTitle } ), |
| 79 | + [ registerTitle ] |
| 80 | + ); |
| 81 | + |
| 82 | + useEffect( () => { |
| 83 | + const titleElement = titleElementRef.current; |
| 84 | + |
| 85 | + if ( ! titleElement ) { |
| 86 | + throw new Error( |
| 87 | + 'Drawer: Missing <Drawer.Title>. ' + |
| 88 | + 'For accessibility, every drawer requires a title. ' + |
| 89 | + 'If needed, the title can be visually hidden but must not be omitted.' |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + const textContent = titleElement.textContent?.trim(); |
| 94 | + if ( ! textContent ) { |
| 95 | + throw new Error( |
| 96 | + 'Drawer: <Drawer.Title> cannot be empty. ' + |
| 97 | + 'Provide meaningful text content for the drawer title.' |
| 98 | + ); |
| 99 | + } |
| 100 | + }, [] ); |
| 101 | + |
| 102 | + return ( |
| 103 | + <DrawerValidationContext.Provider value={ contextValue }> |
| 104 | + { children } |
| 105 | + </DrawerValidationContext.Provider> |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +function DrawerValidationProviderProd( { |
| 110 | + children, |
| 111 | +}: { |
| 112 | + children: React.ReactNode; |
| 113 | +} ) { |
| 114 | + return <>{ children }</>; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Provider component that validates Drawer.Title presence in development mode. |
| 119 | + * In production, this component is a no-op and just renders children. |
| 120 | + */ |
| 121 | +export const DrawerValidationProvider = VALIDATION_ENABLED |
| 122 | + ? DrawerValidationProviderDev |
| 123 | + : DrawerValidationProviderProd; |
0 commit comments