diff options
author | Eric Bailey <git@esb.lol> | 2024-03-04 15:37:11 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-04 15:37:11 -0600 |
commit | 6c9d6f5b05953988cb4fb1556bf435805479e07e (patch) | |
tree | 98a19db445461f6c6007680aa0a8ede2e58634f1 /src/state/dialogs/index.tsx | |
parent | ebd279ed6839965af5e033ed7f430aa7401fd77d (diff) | |
download | voidsky-6c9d6f5b05953988cb4fb1556bf435805479e07e.tar.zst |
Improve dialogs a11y (#3094)
* Improve a11y on ios * Format * Remove android * Fix android
Diffstat (limited to 'src/state/dialogs/index.tsx')
-rw-r--r-- | src/state/dialogs/index.tsx | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/src/state/dialogs/index.tsx b/src/state/dialogs/index.tsx index 9fc70c178..90aaca4f8 100644 --- a/src/state/dialogs/index.tsx +++ b/src/state/dialogs/index.tsx @@ -13,20 +13,20 @@ const DialogContext = React.createContext<{ * The currently open dialogs, referenced by their IDs, generated from * `useId`. */ - openDialogs: React.MutableRefObject<Set<string>> + openDialogs: string[] }>({ activeDialogs: { current: new Map(), }, - openDialogs: { - current: new Set(), - }, + openDialogs: [], }) const DialogControlContext = React.createContext<{ closeAllDialogs(): boolean + setDialogIsOpen(id: string, isOpen: boolean): void }>({ closeAllDialogs: () => false, + setDialogIsOpen: () => {}, }) export function useDialogStateContext() { @@ -41,15 +41,31 @@ export function Provider({children}: React.PropsWithChildren<{}>) { const activeDialogs = React.useRef< Map<string, React.MutableRefObject<DialogControlRefProps>> >(new Map()) - const openDialogs = React.useRef<Set<string>>(new Set()) + const [openDialogs, setOpenDialogs] = React.useState<string[]>([]) const closeAllDialogs = React.useCallback(() => { activeDialogs.current.forEach(dialog => dialog.current.close()) - return openDialogs.current.size > 0 - }, []) + return openDialogs.length > 0 + }, [openDialogs]) + + const setDialogIsOpen = React.useCallback( + (id: string, isOpen: boolean) => { + setOpenDialogs(prev => { + const filtered = prev.filter(dialogId => dialogId !== id) as string[] + return isOpen ? [...filtered, id] : filtered + }) + }, + [setOpenDialogs], + ) - const context = React.useMemo(() => ({activeDialogs, openDialogs}), []) - const controls = React.useMemo(() => ({closeAllDialogs}), [closeAllDialogs]) + const context = React.useMemo( + () => ({activeDialogs, openDialogs}), + [openDialogs], + ) + const controls = React.useMemo( + () => ({closeAllDialogs, setDialogIsOpen}), + [closeAllDialogs, setDialogIsOpen], + ) return ( <DialogContext.Provider value={context}> |