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