diff options
author | Jan-Olof Eriksson <jan-olof.eriksson@iki.fi> | 2024-03-11 14:52:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-11 14:52:33 +0200 |
commit | 4a2251f48bd51d8bd427d21f0dd697c8d43d7856 (patch) | |
tree | 17b7d68f0a19f54313607257de79accc7b6daf4c /src/state/dialogs/index.tsx | |
parent | cbc65247ae57502356165a5d51270bd8d19fd9a5 (diff) | |
parent | 596e744d4177d3be6defeef68f202a70baaf6e37 (diff) | |
download | voidsky-4a2251f48bd51d8bd427d21f0dd697c8d43d7856.tar.zst |
Merge branch 'bluesky-social:main' into main
Diffstat (limited to 'src/state/dialogs/index.tsx')
-rw-r--r-- | src/state/dialogs/index.tsx | 60 |
1 files changed, 49 insertions, 11 deletions
diff --git a/src/state/dialogs/index.tsx b/src/state/dialogs/index.tsx index 9fc70c178..951105a50 100644 --- a/src/state/dialogs/index.tsx +++ b/src/state/dialogs/index.tsx @@ -1,8 +1,9 @@ import React from 'react' +import {SharedValue, useSharedValue} from 'react-native-reanimated' import {DialogControlRefProps} from '#/components/Dialog' import {Provider as GlobalDialogsProvider} from '#/components/dialogs/Context' -const DialogContext = React.createContext<{ +interface IDialogContext { /** * The currently active `useDialogControl` hooks. */ @@ -14,19 +15,24 @@ const DialogContext = React.createContext<{ * `useId`. */ openDialogs: React.MutableRefObject<Set<string>> -}>({ - activeDialogs: { - current: new Map(), - }, - openDialogs: { - current: new Set(), - }, -}) + /** + * The counterpart to `accessibilityViewIsModal` for Android. This property + * applies to the parent of all non-modal views, and prevents TalkBack from + * navigating within content beneath an open dialog. + * + * @see https://reactnative.dev/docs/accessibility#importantforaccessibility-android + */ + importantForAccessibility: SharedValue<'auto' | 'no-hide-descendants'> +} + +const DialogContext = React.createContext<IDialogContext>({} as IDialogContext) const DialogControlContext = React.createContext<{ closeAllDialogs(): boolean + setDialogIsOpen(id: string, isOpen: boolean): void }>({ closeAllDialogs: () => false, + setDialogIsOpen: () => {}, }) export function useDialogStateContext() { @@ -42,14 +48,46 @@ export function Provider({children}: React.PropsWithChildren<{}>) { Map<string, React.MutableRefObject<DialogControlRefProps>> >(new Map()) const openDialogs = React.useRef<Set<string>>(new Set()) + const importantForAccessibility = useSharedValue< + 'auto' | 'no-hide-descendants' + >('auto') const closeAllDialogs = React.useCallback(() => { activeDialogs.current.forEach(dialog => dialog.current.close()) return openDialogs.current.size > 0 }, []) - const context = React.useMemo(() => ({activeDialogs, openDialogs}), []) - const controls = React.useMemo(() => ({closeAllDialogs}), [closeAllDialogs]) + const setDialogIsOpen = React.useCallback( + (id: string, isOpen: boolean) => { + if (isOpen) { + openDialogs.current.add(id) + importantForAccessibility.value = 'no-hide-descendants' + } else { + openDialogs.current.delete(id) + if (openDialogs.current.size < 1) { + importantForAccessibility.value = 'auto' + } + } + }, + [importantForAccessibility], + ) + + const context = React.useMemo<IDialogContext>( + () => ({ + activeDialogs: { + current: new Map(), + }, + openDialogs: { + current: new Set(), + }, + importantForAccessibility, + }), + [importantForAccessibility], + ) + const controls = React.useMemo( + () => ({closeAllDialogs, setDialogIsOpen}), + [closeAllDialogs, setDialogIsOpen], + ) return ( <DialogContext.Provider value={context}> |