about summary refs log tree commit diff
path: root/src/state/dialogs/index.tsx
blob: 26bb6792fdcac6d53ceb0760e2b04d94285e40d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React from 'react'
import {SharedValue, useSharedValue} from 'react-native-reanimated'

import {DialogControlRefProps} from '#/components/Dialog'
import {Provider as GlobalDialogsProvider} from '#/components/dialogs/Context'

interface IDialogContext {
  /**
   * The currently active `useDialogControl` hooks.
   */
  activeDialogs: React.MutableRefObject<
    Map<string, React.MutableRefObject<DialogControlRefProps>>
  >
  /**
   * The currently open dialogs, referenced by their IDs, generated from
   * `useId`.
   */
  openDialogs: React.MutableRefObject<Set<string>>
  /**
   * 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() {
  return React.useContext(DialogContext)
}

export function useDialogStateControlContext() {
  return React.useContext(DialogControlContext)
}

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 importantForAccessibility = useSharedValue<
    'auto' | 'no-hide-descendants'
  >('auto')

  const closeAllDialogs = React.useCallback(() => {
    openDialogs.current.forEach(id => {
      const dialog = activeDialogs.current.get(id)
      if (dialog) dialog.current.close()
    })
    return openDialogs.current.size > 0
  }, [])

  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,
      openDialogs,
      importantForAccessibility,
    }),
    [importantForAccessibility, activeDialogs, openDialogs],
  )
  const controls = React.useMemo(
    () => ({closeAllDialogs, setDialogIsOpen}),
    [closeAllDialogs, setDialogIsOpen],
  )

  return (
    <DialogContext.Provider value={context}>
      <DialogControlContext.Provider value={controls}>
        <GlobalDialogsProvider>{children}</GlobalDialogsProvider>
      </DialogControlContext.Provider>
    </DialogContext.Provider>
  )
}