|
| 1 | +import React, {createContext, useContext, useRef} from 'react'; |
| 2 | +import type {View} from 'react-native'; |
| 3 | + |
| 4 | +type LabelEntry = {id: number; text: string}; |
| 5 | + |
| 6 | +type DialogLabelData = { |
| 7 | + containerRef: React.RefObject<View | null>; |
| 8 | + isInsideDialog: boolean; |
| 9 | +}; |
| 10 | + |
| 11 | +type DialogLabelActions = { |
| 12 | + pushLabel: (text: string) => number; |
| 13 | + popLabel: (id: number) => void; |
| 14 | + claimInitialFocus: () => boolean; |
| 15 | +}; |
| 16 | + |
| 17 | +const DialogLabelDataContext = createContext<DialogLabelData>({ |
| 18 | + containerRef: {current: null}, |
| 19 | + isInsideDialog: false, |
| 20 | +}); |
| 21 | + |
| 22 | +const DialogLabelActionsContext = createContext<DialogLabelActions>({ |
| 23 | + pushLabel: () => 0, |
| 24 | + popLabel: () => {}, |
| 25 | + claimInitialFocus: () => false, |
| 26 | +}); |
| 27 | + |
| 28 | +type DialogLabelProviderProps = { |
| 29 | + children: React.ReactNode; |
| 30 | + containerRef: React.RefObject<View | null>; |
| 31 | +}; |
| 32 | + |
| 33 | +function DialogLabelProvider({children, containerRef}: DialogLabelProviderProps) { |
| 34 | + const nextIdRef = useRef(0); |
| 35 | + const labelStackRef = useRef<LabelEntry[]>([]); |
| 36 | + const initialFocusClaimedRef = useRef(false); |
| 37 | + |
| 38 | + const updateContainerLabel = () => { |
| 39 | + const top = labelStackRef.current.at(-1); |
| 40 | + const node = containerRef.current as unknown as HTMLElement | null; |
| 41 | + if (!node || typeof node.setAttribute !== 'function') { |
| 42 | + return; |
| 43 | + } |
| 44 | + if (top?.text) { |
| 45 | + node.setAttribute('aria-label', top.text); |
| 46 | + } else { |
| 47 | + node.removeAttribute('aria-label'); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + const pushLabel = (text: string): number => { |
| 52 | + const id = nextIdRef.current++; |
| 53 | + labelStackRef.current = [...labelStackRef.current, {id, text}]; |
| 54 | + initialFocusClaimedRef.current = false; |
| 55 | + updateContainerLabel(); |
| 56 | + return id; |
| 57 | + }; |
| 58 | + |
| 59 | + const popLabel = (id: number) => { |
| 60 | + labelStackRef.current = labelStackRef.current.filter((entry) => entry.id !== id); |
| 61 | + updateContainerLabel(); |
| 62 | + }; |
| 63 | + |
| 64 | + const claimInitialFocus = (): boolean => { |
| 65 | + if (initialFocusClaimedRef.current) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + initialFocusClaimedRef.current = true; |
| 69 | + return true; |
| 70 | + }; |
| 71 | + |
| 72 | + const data: DialogLabelData = { |
| 73 | + containerRef, |
| 74 | + isInsideDialog: true, |
| 75 | + }; |
| 76 | + |
| 77 | + const actions: DialogLabelActions = { |
| 78 | + pushLabel, |
| 79 | + popLabel, |
| 80 | + claimInitialFocus, |
| 81 | + }; |
| 82 | + |
| 83 | + return ( |
| 84 | + <DialogLabelDataContext.Provider value={data}> |
| 85 | + <DialogLabelActionsContext.Provider value={actions}>{children}</DialogLabelActionsContext.Provider> |
| 86 | + </DialogLabelDataContext.Provider> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +function useDialogLabelData(): DialogLabelData { |
| 91 | + return useContext(DialogLabelDataContext); |
| 92 | +} |
| 93 | + |
| 94 | +function useDialogLabelActions(): DialogLabelActions { |
| 95 | + return useContext(DialogLabelActionsContext); |
| 96 | +} |
| 97 | + |
| 98 | +export {DialogLabelProvider, useDialogLabelData, useDialogLabelActions}; |
0 commit comments