@hanzogui/dismissable 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/cjs/Dismissable.cjs +287 -0
- package/dist/cjs/Dismissable.native.js +65 -0
- package/dist/cjs/Dismissable.native.js.map +1 -0
- package/dist/cjs/DismissableProps.cjs +16 -0
- package/dist/cjs/DismissableProps.native.js +19 -0
- package/dist/cjs/DismissableProps.native.js.map +1 -0
- package/dist/cjs/index.cjs +18 -0
- package/dist/cjs/index.native.js +21 -0
- package/dist/cjs/index.native.js.map +1 -0
- package/dist/esm/Dismissable.mjs +246 -0
- package/dist/esm/Dismissable.mjs.map +1 -0
- package/dist/esm/Dismissable.native.js +22 -0
- package/dist/esm/Dismissable.native.js.map +1 -0
- package/dist/esm/DismissableProps.mjs +2 -0
- package/dist/esm/DismissableProps.mjs.map +1 -0
- package/dist/esm/DismissableProps.native.js +2 -0
- package/dist/esm/DismissableProps.native.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/index.native.js +2 -0
- package/dist/esm/index.native.js.map +1 -0
- package/dist/jsx/Dismissable.mjs +246 -0
- package/dist/jsx/Dismissable.mjs.map +1 -0
- package/dist/jsx/Dismissable.native.js +65 -0
- package/dist/jsx/Dismissable.native.js.map +1 -0
- package/dist/jsx/DismissableProps.mjs +2 -0
- package/dist/jsx/DismissableProps.mjs.map +1 -0
- package/dist/jsx/DismissableProps.native.js +19 -0
- package/dist/jsx/DismissableProps.native.js.map +1 -0
- package/dist/jsx/index.js +2 -0
- package/dist/jsx/index.js.map +1 -0
- package/dist/jsx/index.mjs +2 -0
- package/dist/jsx/index.mjs.map +1 -0
- package/dist/jsx/index.native.js +21 -0
- package/dist/jsx/index.native.js.map +1 -0
- package/package.json +53 -0
- package/src/Dismissable.native.tsx +39 -0
- package/src/Dismissable.tsx +507 -0
- package/src/DismissableProps.tsx +63 -0
- package/src/index.ts +1 -0
- package/types/Dismissable.d.ts +42 -0
- package/types/Dismissable.native.d.ts +10 -0
- package/types/DismissableProps.d.ts +57 -0
- package/types/index.d.ts +2 -0
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
// forked from radix-ui
|
|
2
|
+
// https://github.com/radix-ui/primitives/blob/cfd8dcba5fa6a0e751486af418d05a7b88a7f541/packages/react/dismissable-layer/src/DismissableLayer.tsx#L324
|
|
3
|
+
|
|
4
|
+
import { useComposedRefs } from '@hanzogui/compose-refs'
|
|
5
|
+
import { Slot, GuiElement, View, composeEventHandlers } from '@hanzogui/core'
|
|
6
|
+
import { useEscapeKeydown } from '@hanzogui/use-escape-keydown'
|
|
7
|
+
import { useEvent } from '@hanzogui/use-event'
|
|
8
|
+
import * as React from 'react'
|
|
9
|
+
import * as ReactDOM from 'react-dom'
|
|
10
|
+
|
|
11
|
+
import type { DismissableBranchProps, DismissableProps } from './DismissableProps'
|
|
12
|
+
|
|
13
|
+
export function dispatchDiscreteCustomEvent<E extends CustomEvent>(
|
|
14
|
+
target: E['target'],
|
|
15
|
+
event: E
|
|
16
|
+
) {
|
|
17
|
+
if (target) ReactDOM.flushSync(() => target.dispatchEvent(event))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/* -------------------------------------------------------------------------------------------------
|
|
21
|
+
* Dismissable
|
|
22
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
23
|
+
|
|
24
|
+
const DISMISSABLE_LAYER_NAME = 'Dismissable'
|
|
25
|
+
const CONTEXT_UPDATE = 'dismissable.update'
|
|
26
|
+
const POINTER_DOWN_OUTSIDE = 'dismissable.pointerDownOutside'
|
|
27
|
+
const FOCUS_OUTSIDE = 'dismissable.focusOutside'
|
|
28
|
+
|
|
29
|
+
let originalBodyPointerEvents: string
|
|
30
|
+
|
|
31
|
+
// global layer tracking
|
|
32
|
+
const globalLayers = new Set<HTMLElement>()
|
|
33
|
+
const layerChangeListeners = new Set<() => void>()
|
|
34
|
+
|
|
35
|
+
// track if any layer has disableOutsidePointerEvents - only then do we need position updates
|
|
36
|
+
let layersWithPointerEventsDisabledCount = 0
|
|
37
|
+
|
|
38
|
+
function notifyLayerChange() {
|
|
39
|
+
for (const listener of layerChangeListeners) {
|
|
40
|
+
listener()
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* returns the number of active dismissable layers
|
|
46
|
+
* useful for non-React contexts (e.g. escape key handlers)
|
|
47
|
+
*/
|
|
48
|
+
export function getDismissableLayerCount(): number {
|
|
49
|
+
return globalLayers.size
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* debug helper - logs what elements are registered as dismissable layers
|
|
54
|
+
*/
|
|
55
|
+
export function debugDismissableLayers(): HTMLElement[] {
|
|
56
|
+
const layers = Array.from(globalLayers)
|
|
57
|
+
console.log('[Dismissable] Active layers:', layers.length, layers)
|
|
58
|
+
return layers
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* hook that returns true when any dismissable layer is active
|
|
63
|
+
* re-renders when the state changes
|
|
64
|
+
* uses module-level globals, not React context, so works anywhere in tree
|
|
65
|
+
*/
|
|
66
|
+
export function useHasDismissableLayers(): boolean {
|
|
67
|
+
const [count, setCount] = React.useState(() => globalLayers.size)
|
|
68
|
+
|
|
69
|
+
React.useEffect(() => {
|
|
70
|
+
setCount(globalLayers.size)
|
|
71
|
+
const update = () => setCount(globalLayers.size)
|
|
72
|
+
layerChangeListeners.add(update)
|
|
73
|
+
return () => {
|
|
74
|
+
layerChangeListeners.delete(update)
|
|
75
|
+
}
|
|
76
|
+
}, [])
|
|
77
|
+
|
|
78
|
+
return count > 0
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const DismissableContext = React.createContext({
|
|
82
|
+
layers: new Set<HTMLElement>(),
|
|
83
|
+
layersWithOutsidePointerEventsDisabled: new Set<HTMLElement>(),
|
|
84
|
+
branches: new Set<HTMLElement>(),
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* hook to check if a DOM element is inside an active dismissable layer
|
|
89
|
+
* useful for custom escape handling - if inside a dismissable, you may want to defer
|
|
90
|
+
*/
|
|
91
|
+
export function useIsInsideDismissable(ref: React.RefObject<HTMLElement | null>) {
|
|
92
|
+
const context = React.useContext(DismissableContext)
|
|
93
|
+
const [isInside, setIsInside] = React.useState(false)
|
|
94
|
+
|
|
95
|
+
React.useEffect(() => {
|
|
96
|
+
const check = () => {
|
|
97
|
+
const el = ref.current
|
|
98
|
+
if (!el) {
|
|
99
|
+
setIsInside(false)
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
for (const layer of context.layers) {
|
|
103
|
+
if (layer.contains(el)) {
|
|
104
|
+
setIsInside(true)
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
setIsInside(false)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
check()
|
|
112
|
+
document.addEventListener(CONTEXT_UPDATE, check)
|
|
113
|
+
return () => document.removeEventListener(CONTEXT_UPDATE, check)
|
|
114
|
+
}, [context.layers, ref])
|
|
115
|
+
|
|
116
|
+
return isInside
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* hook to check if there are dismissable layers above a given element
|
|
121
|
+
* returns the count of layers that are ancestors of the element
|
|
122
|
+
*/
|
|
123
|
+
export function useDismissableLayersAbove(ref: React.RefObject<HTMLElement | null>) {
|
|
124
|
+
const context = React.useContext(DismissableContext)
|
|
125
|
+
const [count, setCount] = React.useState(0)
|
|
126
|
+
|
|
127
|
+
React.useEffect(() => {
|
|
128
|
+
const check = () => {
|
|
129
|
+
const el = ref.current
|
|
130
|
+
if (!el) {
|
|
131
|
+
setCount(0)
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
let above = 0
|
|
135
|
+
for (const layer of context.layers) {
|
|
136
|
+
if (layer.contains(el)) {
|
|
137
|
+
above++
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
setCount(above)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
check()
|
|
144
|
+
document.addEventListener(CONTEXT_UPDATE, check)
|
|
145
|
+
return () => document.removeEventListener(CONTEXT_UPDATE, check)
|
|
146
|
+
}, [context.layers, ref])
|
|
147
|
+
|
|
148
|
+
return count
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const Dismissable = React.forwardRef<
|
|
152
|
+
HTMLElement,
|
|
153
|
+
DismissableProps & { asChild?: boolean }
|
|
154
|
+
>((props, forwardedRef) => {
|
|
155
|
+
const {
|
|
156
|
+
disableOutsidePointerEvents = false,
|
|
157
|
+
forceUnmount,
|
|
158
|
+
onEscapeKeyDown,
|
|
159
|
+
onPointerDownOutside,
|
|
160
|
+
onFocusOutside,
|
|
161
|
+
onInteractOutside,
|
|
162
|
+
onDismiss,
|
|
163
|
+
asChild,
|
|
164
|
+
children,
|
|
165
|
+
branches: branchesProp,
|
|
166
|
+
...layerProps
|
|
167
|
+
} = props
|
|
168
|
+
const Comp = asChild ? Slot : View
|
|
169
|
+
const context = React.useContext(DismissableContext)
|
|
170
|
+
const [node, setNode] = React.useState<HTMLElement | null>(null)
|
|
171
|
+
const [, force] = React.useState({})
|
|
172
|
+
const composedRefs = useComposedRefs(forwardedRef, (node: HTMLElement | null) =>
|
|
173
|
+
setNode(node)
|
|
174
|
+
)
|
|
175
|
+
const layers = Array.from(context.layers)
|
|
176
|
+
|
|
177
|
+
const [highestLayerWithOutsidePointerEventsDisabled] = [
|
|
178
|
+
...context.layersWithOutsidePointerEventsDisabled,
|
|
179
|
+
].slice(-1)
|
|
180
|
+
const highestLayerWithOutsidePointerEventsDisabledIndex = layers.indexOf(
|
|
181
|
+
highestLayerWithOutsidePointerEventsDisabled
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
const index = node ? layers.indexOf(node) : -1
|
|
185
|
+
const isBodyPointerEventsDisabled =
|
|
186
|
+
context.layersWithOutsidePointerEventsDisabled.size > 0
|
|
187
|
+
const isPointerEventsEnabled =
|
|
188
|
+
index >= highestLayerWithOutsidePointerEventsDisabledIndex
|
|
189
|
+
|
|
190
|
+
const pointerDownOutside = usePointerDownOutside((event) => {
|
|
191
|
+
const target = event.target as HTMLElement
|
|
192
|
+
// check prop-based branches first (scoped to this dismissable), then global branches
|
|
193
|
+
const branches = branchesProp || context.branches
|
|
194
|
+
const isPointerDownOnBranch = [...branches].some((branch) => branch.contains(target))
|
|
195
|
+
if (!isPointerEventsEnabled || isPointerDownOnBranch) return
|
|
196
|
+
onPointerDownOutside?.(event)
|
|
197
|
+
onInteractOutside?.(event)
|
|
198
|
+
if (!event.defaultPrevented) onDismiss?.()
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
const focusOutside = useFocusOutside((event) => {
|
|
202
|
+
const target = event.target as HTMLElement
|
|
203
|
+
// check prop-based branches first (scoped to this dismissable), then global branches
|
|
204
|
+
const branches = branchesProp || context.branches
|
|
205
|
+
const isFocusInBranch = [...branches].some((branch) => branch.contains(target))
|
|
206
|
+
if (isFocusInBranch) return
|
|
207
|
+
onFocusOutside?.(event)
|
|
208
|
+
onInteractOutside?.(event)
|
|
209
|
+
if (!event.defaultPrevented) onDismiss?.()
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
// track forceUnmount in a ref so escape handler can check it
|
|
213
|
+
const forceUnmountRef = React.useRef(forceUnmount)
|
|
214
|
+
React.useEffect(() => {
|
|
215
|
+
forceUnmountRef.current = forceUnmount
|
|
216
|
+
}, [forceUnmount])
|
|
217
|
+
|
|
218
|
+
useEscapeKeydown((event) => {
|
|
219
|
+
// skip if this layer is force-unmounted (e.g. dialog closed but still mounted)
|
|
220
|
+
if (forceUnmountRef.current) return
|
|
221
|
+
// Check layers at callback time, not render time, to avoid stale closures
|
|
222
|
+
const currentLayers = Array.from(context.layers)
|
|
223
|
+
const currentIndex = node ? currentLayers.indexOf(node) : -1
|
|
224
|
+
const isHighestLayer = currentIndex === currentLayers.length - 1
|
|
225
|
+
if (!isHighestLayer) return
|
|
226
|
+
onEscapeKeyDown?.(event)
|
|
227
|
+
if (!event.defaultPrevented && onDismiss) {
|
|
228
|
+
event.preventDefault()
|
|
229
|
+
onDismiss()
|
|
230
|
+
}
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
React.useEffect(() => {
|
|
234
|
+
if (!node) return
|
|
235
|
+
// don't add to layers when force-unmounted (dialog closed but still mounted)
|
|
236
|
+
if (forceUnmount) return
|
|
237
|
+
if (disableOutsidePointerEvents) {
|
|
238
|
+
if (context.layersWithOutsidePointerEventsDisabled.size === 0) {
|
|
239
|
+
originalBodyPointerEvents = document.body.style.pointerEvents
|
|
240
|
+
document.body.style.pointerEvents = 'none'
|
|
241
|
+
}
|
|
242
|
+
context.layersWithOutsidePointerEventsDisabled.add(node)
|
|
243
|
+
layersWithPointerEventsDisabledCount++
|
|
244
|
+
}
|
|
245
|
+
context.layers.add(node)
|
|
246
|
+
globalLayers.add(node)
|
|
247
|
+
// only dispatch position update when pointer-events tracking is active
|
|
248
|
+
if (disableOutsidePointerEvents || layersWithPointerEventsDisabledCount > 0) {
|
|
249
|
+
dispatchUpdate()
|
|
250
|
+
}
|
|
251
|
+
notifyLayerChange()
|
|
252
|
+
return () => {
|
|
253
|
+
if (disableOutsidePointerEvents) {
|
|
254
|
+
if (context.layersWithOutsidePointerEventsDisabled.size === 1) {
|
|
255
|
+
document.body.style.pointerEvents = originalBodyPointerEvents
|
|
256
|
+
}
|
|
257
|
+
// decrement AFTER dispatch so other layers still re-render
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}, [node, disableOutsidePointerEvents, forceUnmount, context])
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* We purposefully prevent combining this effect with the `disableOutsidePointerEvents` effect
|
|
264
|
+
* because a change to `disableOutsidePointerEvents` would remove this layer from the stack
|
|
265
|
+
* and add it to the end again so the layering order wouldn't be _creation order_.
|
|
266
|
+
* We only want them to be removed from context stacks when unmounted.
|
|
267
|
+
*/
|
|
268
|
+
React.useEffect(() => {
|
|
269
|
+
if (forceUnmount) return
|
|
270
|
+
return () => {
|
|
271
|
+
if (!node) return
|
|
272
|
+
const hadPointerEventsDisabled =
|
|
273
|
+
context.layersWithOutsidePointerEventsDisabled.has(node)
|
|
274
|
+
context.layers.delete(node)
|
|
275
|
+
context.layersWithOutsidePointerEventsDisabled.delete(node)
|
|
276
|
+
globalLayers.delete(node)
|
|
277
|
+
// only dispatch position update when pointer-events tracking is active
|
|
278
|
+
if (layersWithPointerEventsDisabledCount > 0) {
|
|
279
|
+
dispatchUpdate()
|
|
280
|
+
}
|
|
281
|
+
notifyLayerChange()
|
|
282
|
+
// decrement count AFTER dispatch so other layers see count > 0 and re-render
|
|
283
|
+
if (hadPointerEventsDisabled) {
|
|
284
|
+
layersWithPointerEventsDisabledCount--
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}, [node, context, forceUnmount])
|
|
288
|
+
|
|
289
|
+
React.useEffect(() => {
|
|
290
|
+
const handleUpdate = () => {
|
|
291
|
+
// only force re-render if we need to track layer positions for pointer-events
|
|
292
|
+
// this avoids N^2 re-renders when multiple dismissables mount/unmount
|
|
293
|
+
if (layersWithPointerEventsDisabledCount > 0) {
|
|
294
|
+
force({})
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
document.addEventListener(CONTEXT_UPDATE, handleUpdate)
|
|
298
|
+
return () => document.removeEventListener(CONTEXT_UPDATE, handleUpdate)
|
|
299
|
+
}, [])
|
|
300
|
+
|
|
301
|
+
return (
|
|
302
|
+
<Comp
|
|
303
|
+
{...layerProps}
|
|
304
|
+
// @ts-ignore
|
|
305
|
+
ref={composedRefs}
|
|
306
|
+
{...(!asChild && {
|
|
307
|
+
display: 'contents',
|
|
308
|
+
})}
|
|
309
|
+
pointerEvents={
|
|
310
|
+
isBodyPointerEventsDisabled
|
|
311
|
+
? isPointerEventsEnabled
|
|
312
|
+
? 'auto'
|
|
313
|
+
: 'none'
|
|
314
|
+
: undefined
|
|
315
|
+
}
|
|
316
|
+
onFocusCapture={composeEventHandlers(
|
|
317
|
+
props.onFocusCapture,
|
|
318
|
+
focusOutside.onFocusCapture
|
|
319
|
+
)}
|
|
320
|
+
onBlurCapture={composeEventHandlers(
|
|
321
|
+
props.onBlurCapture,
|
|
322
|
+
focusOutside.onBlurCapture
|
|
323
|
+
)}
|
|
324
|
+
onPointerDownCapture={composeEventHandlers(
|
|
325
|
+
props.onPointerDownCapture,
|
|
326
|
+
pointerDownOutside.onPointerDownCapture
|
|
327
|
+
)}
|
|
328
|
+
>
|
|
329
|
+
{children}
|
|
330
|
+
</Comp>
|
|
331
|
+
)
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
Dismissable.displayName = DISMISSABLE_LAYER_NAME
|
|
335
|
+
|
|
336
|
+
/* -------------------------------------------------------------------------------------------------
|
|
337
|
+
* DismissableBranch
|
|
338
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
339
|
+
|
|
340
|
+
const BRANCH_NAME = 'DismissableBranch'
|
|
341
|
+
|
|
342
|
+
const DismissableBranch = React.forwardRef<GuiElement, DismissableBranchProps>(
|
|
343
|
+
(props, forwardedRef) => {
|
|
344
|
+
const { branches: branchesProp, ...rest } = props
|
|
345
|
+
const context = React.useContext(DismissableContext)
|
|
346
|
+
const ref = React.useRef<GuiElement>(null)
|
|
347
|
+
const composedRefs = useComposedRefs(forwardedRef, ref)
|
|
348
|
+
|
|
349
|
+
React.useEffect(() => {
|
|
350
|
+
const node = ref.current
|
|
351
|
+
if (!(node instanceof HTMLElement)) return
|
|
352
|
+
// use prop-based branches if provided, otherwise fall back to global context
|
|
353
|
+
const branches = branchesProp || context.branches
|
|
354
|
+
if (node && branches) {
|
|
355
|
+
branches.add(node)
|
|
356
|
+
return () => {
|
|
357
|
+
branches.delete(node)
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}, [branchesProp, context.branches])
|
|
361
|
+
|
|
362
|
+
return <View asChild="except-style" {...rest} ref={composedRefs} />
|
|
363
|
+
}
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
DismissableBranch.displayName = BRANCH_NAME
|
|
367
|
+
|
|
368
|
+
/* -----------------------------------------------------------------------------------------------*/
|
|
369
|
+
|
|
370
|
+
export type PointerDownOutsideEvent = CustomEvent<{ originalEvent: PointerEvent }>
|
|
371
|
+
export type FocusOutsideEvent = CustomEvent<{ originalEvent: FocusEvent }>
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Listens for `pointerdown` outside a react subtree. We use `pointerdown` rather than `pointerup`
|
|
375
|
+
* to mimic layer dismissing behaviour present in OS.
|
|
376
|
+
* Returns props to pass to the node we want to check for outside events.
|
|
377
|
+
*/
|
|
378
|
+
function usePointerDownOutside(
|
|
379
|
+
onPointerDownOutside?: (event: PointerDownOutsideEvent) => void
|
|
380
|
+
) {
|
|
381
|
+
const handlePointerDownOutside = useEvent(onPointerDownOutside) as EventListener
|
|
382
|
+
const isPointerInsideReactTreeRef = React.useRef(false)
|
|
383
|
+
const handleClickRef = React.useRef(() => {})
|
|
384
|
+
|
|
385
|
+
React.useEffect(() => {
|
|
386
|
+
const handlePointerDown = (event: PointerEvent) => {
|
|
387
|
+
if (event.target && !isPointerInsideReactTreeRef.current) {
|
|
388
|
+
const eventDetail = { originalEvent: event }
|
|
389
|
+
|
|
390
|
+
function handleAndDispatchPointerDownOutsideEvent() {
|
|
391
|
+
handleAndDispatchCustomEvent(
|
|
392
|
+
POINTER_DOWN_OUTSIDE,
|
|
393
|
+
handlePointerDownOutside,
|
|
394
|
+
eventDetail,
|
|
395
|
+
{ discrete: true }
|
|
396
|
+
)
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* On touch devices, we need to wait for a click event because browsers implement
|
|
401
|
+
* a ~350ms delay between the time the user stops touching the display and when the
|
|
402
|
+
* browser executres events. We need to ensure we don't reactivate pointer-events within
|
|
403
|
+
* this timeframe otherwise the browser may execute events that should have been prevented.
|
|
404
|
+
*
|
|
405
|
+
* Additionally, this also lets us deal automatically with cancellations when a click event
|
|
406
|
+
* isn't raised because the page was considered scrolled/drag-scrolled, long-pressed, etc.
|
|
407
|
+
*
|
|
408
|
+
* This is why we also continuously remove the previous listener, because we cannot be
|
|
409
|
+
* certain that it was raised, and therefore cleaned-up.
|
|
410
|
+
*/
|
|
411
|
+
if (event.pointerType === 'touch') {
|
|
412
|
+
document.removeEventListener('click', handleClickRef.current)
|
|
413
|
+
handleClickRef.current = handleAndDispatchPointerDownOutsideEvent
|
|
414
|
+
document.addEventListener('click', handleClickRef.current, { once: true })
|
|
415
|
+
} else {
|
|
416
|
+
handleAndDispatchPointerDownOutsideEvent()
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
isPointerInsideReactTreeRef.current = false
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* if this hook executes in a component that mounts via a `pointerdown` event, the event
|
|
423
|
+
* would bubble up to the document and trigger a `pointerDownOutside` event. We avoid
|
|
424
|
+
* this by delaying the event listener registration on the document.
|
|
425
|
+
* This is not React specific, but rather how the DOM works, ie:
|
|
426
|
+
* ```
|
|
427
|
+
* button.addEventListener('pointerdown', () => {
|
|
428
|
+
* console.log('I will log');
|
|
429
|
+
* document.addEventListener('pointerdown', () => {
|
|
430
|
+
* console.log('I will also log');
|
|
431
|
+
* })
|
|
432
|
+
* });
|
|
433
|
+
*/
|
|
434
|
+
const timerId = setTimeout(() => {
|
|
435
|
+
document.addEventListener('pointerdown', handlePointerDown)
|
|
436
|
+
}, 0)
|
|
437
|
+
return () => {
|
|
438
|
+
window.clearTimeout(timerId)
|
|
439
|
+
document.removeEventListener('pointerdown', handlePointerDown)
|
|
440
|
+
document.removeEventListener('click', handleClickRef.current)
|
|
441
|
+
}
|
|
442
|
+
}, [handlePointerDownOutside])
|
|
443
|
+
|
|
444
|
+
return {
|
|
445
|
+
// ensures we check React component tree (not just DOM tree)
|
|
446
|
+
onPointerDownCapture: () => {
|
|
447
|
+
isPointerInsideReactTreeRef.current = true
|
|
448
|
+
},
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Listens for when focus happens outside a react subtree.
|
|
454
|
+
* Returns props to pass to the root (node) of the subtree we want to check.
|
|
455
|
+
*/
|
|
456
|
+
function useFocusOutside(onFocusOutside?: (event: FocusOutsideEvent) => void) {
|
|
457
|
+
const handleFocusOutside = useEvent(onFocusOutside) as EventListener
|
|
458
|
+
const isFocusInsideReactTreeRef = React.useRef(false)
|
|
459
|
+
|
|
460
|
+
React.useEffect(() => {
|
|
461
|
+
const handleFocus = (event: FocusEvent) => {
|
|
462
|
+
if (event.target && !isFocusInsideReactTreeRef.current) {
|
|
463
|
+
const eventDetail = { originalEvent: event }
|
|
464
|
+
handleAndDispatchCustomEvent(FOCUS_OUTSIDE, handleFocusOutside, eventDetail, {
|
|
465
|
+
discrete: false,
|
|
466
|
+
})
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
document.addEventListener('focusin', handleFocus)
|
|
470
|
+
return () => document.removeEventListener('focusin', handleFocus)
|
|
471
|
+
}, [handleFocusOutside])
|
|
472
|
+
|
|
473
|
+
return {
|
|
474
|
+
onFocusCapture: () => {
|
|
475
|
+
isFocusInsideReactTreeRef.current = true
|
|
476
|
+
},
|
|
477
|
+
onBlurCapture: () => {
|
|
478
|
+
isFocusInsideReactTreeRef.current = false
|
|
479
|
+
},
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function dispatchUpdate() {
|
|
484
|
+
const event = new CustomEvent(CONTEXT_UPDATE)
|
|
485
|
+
document.dispatchEvent(event)
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function handleAndDispatchCustomEvent<E extends CustomEvent, OriginalEvent extends Event>(
|
|
489
|
+
name: string,
|
|
490
|
+
handler: ((event: E) => void) | undefined,
|
|
491
|
+
detail: { originalEvent: OriginalEvent } & (E extends CustomEvent<infer D> ? D : never),
|
|
492
|
+
{ discrete }: { discrete: boolean }
|
|
493
|
+
) {
|
|
494
|
+
const target = detail.originalEvent.target
|
|
495
|
+
const event = new CustomEvent(name, { bubbles: false, cancelable: true, detail })
|
|
496
|
+
if (handler) target.addEventListener(name, handler as EventListener, { once: true })
|
|
497
|
+
|
|
498
|
+
if (discrete) {
|
|
499
|
+
dispatchDiscreteCustomEvent(target, event)
|
|
500
|
+
} else {
|
|
501
|
+
target.dispatchEvent(event)
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
export { Dismissable, DismissableBranch }
|
|
506
|
+
|
|
507
|
+
export type { DismissableProps }
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type React from 'react'
|
|
2
|
+
|
|
3
|
+
import type { FocusOutsideEvent, PointerDownOutsideEvent } from './Dismissable'
|
|
4
|
+
|
|
5
|
+
export interface DismissableProps {
|
|
6
|
+
/**
|
|
7
|
+
* When `true`, hover/focus/click interactions will be disabled on elements outside
|
|
8
|
+
* the `Dismissable`. Users will need to click twice on outside elements to
|
|
9
|
+
* interact with them: once to close the `Dismissable`, and again to trigger the element.
|
|
10
|
+
*/
|
|
11
|
+
disableOutsidePointerEvents?: boolean
|
|
12
|
+
/**
|
|
13
|
+
* Optional Set of branch elements that should not trigger dismissal.
|
|
14
|
+
* Pass the same Set to DismissableBranch components to scope them to this Dismissable.
|
|
15
|
+
*/
|
|
16
|
+
branches?: Set<HTMLElement>
|
|
17
|
+
/**
|
|
18
|
+
* Event handler called when the escape key is down.
|
|
19
|
+
* Can be prevented.
|
|
20
|
+
*/
|
|
21
|
+
onEscapeKeyDown?: React.KeyboardEventHandler
|
|
22
|
+
/**
|
|
23
|
+
* Event handler called when the a `pointerdown` event happens outside of the `Dismissable`.
|
|
24
|
+
* Can be prevented.
|
|
25
|
+
*/
|
|
26
|
+
onPointerDownOutside?: (event: PointerDownOutsideEvent) => void
|
|
27
|
+
/**
|
|
28
|
+
* Event handler called when the focus moves outside of the `Dismissable`.
|
|
29
|
+
* Can be prevented.
|
|
30
|
+
*/
|
|
31
|
+
onFocusOutside?: (event: FocusOutsideEvent) => void
|
|
32
|
+
/**
|
|
33
|
+
* Event handler called when an interaction happens outside the `Dismissable`.
|
|
34
|
+
* Specifically, when a `pointerdown` event happens outside or focus moves outside of it.
|
|
35
|
+
* Can be prevented.
|
|
36
|
+
*/
|
|
37
|
+
onInteractOutside?: (event: PointerDownOutsideEvent | FocusOutsideEvent) => void
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Handler called when the `Dismissable` should be dismissed
|
|
41
|
+
*/
|
|
42
|
+
onDismiss?: () => void
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* When using animations on exit, may want to simualte force unmount early
|
|
46
|
+
*/
|
|
47
|
+
forceUnmount?: boolean
|
|
48
|
+
|
|
49
|
+
onPointerDownCapture?: any
|
|
50
|
+
onBlurCapture?: any
|
|
51
|
+
onFocusCapture?: any
|
|
52
|
+
|
|
53
|
+
children?: React.ReactNode
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface DismissableBranchProps {
|
|
57
|
+
children?: React.ReactNode
|
|
58
|
+
/**
|
|
59
|
+
* Optional Set to register this branch with.
|
|
60
|
+
* Pass the same Set to the Dismissable to scope this branch to that specific layer.
|
|
61
|
+
*/
|
|
62
|
+
branches?: Set<HTMLElement>
|
|
63
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Dismissable'
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { GuiElement } from '@gui/core';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import type { DismissableBranchProps, DismissableProps } from './DismissableProps';
|
|
4
|
+
export declare function dispatchDiscreteCustomEvent<E extends CustomEvent>(target: E['target'], event: E): void;
|
|
5
|
+
/**
|
|
6
|
+
* returns the number of active dismissable layers
|
|
7
|
+
* useful for non-React contexts (e.g. escape key handlers)
|
|
8
|
+
*/
|
|
9
|
+
export declare function getDismissableLayerCount(): number;
|
|
10
|
+
/**
|
|
11
|
+
* debug helper - logs what elements are registered as dismissable layers
|
|
12
|
+
*/
|
|
13
|
+
export declare function debugDismissableLayers(): HTMLElement[];
|
|
14
|
+
/**
|
|
15
|
+
* hook that returns true when any dismissable layer is active
|
|
16
|
+
* re-renders when the state changes
|
|
17
|
+
* uses module-level globals, not React context, so works anywhere in tree
|
|
18
|
+
*/
|
|
19
|
+
export declare function useHasDismissableLayers(): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* hook to check if a DOM element is inside an active dismissable layer
|
|
22
|
+
* useful for custom escape handling - if inside a dismissable, you may want to defer
|
|
23
|
+
*/
|
|
24
|
+
export declare function useIsInsideDismissable(ref: React.RefObject<HTMLElement | null>): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* hook to check if there are dismissable layers above a given element
|
|
27
|
+
* returns the count of layers that are ancestors of the element
|
|
28
|
+
*/
|
|
29
|
+
export declare function useDismissableLayersAbove(ref: React.RefObject<HTMLElement | null>): number;
|
|
30
|
+
declare const Dismissable: React.ForwardRefExoticComponent<DismissableProps & {
|
|
31
|
+
asChild?: boolean;
|
|
32
|
+
} & React.RefAttributes<HTMLElement>>;
|
|
33
|
+
declare const DismissableBranch: React.ForwardRefExoticComponent<DismissableBranchProps & React.RefAttributes<GuiElement>>;
|
|
34
|
+
export type PointerDownOutsideEvent = CustomEvent<{
|
|
35
|
+
originalEvent: PointerEvent;
|
|
36
|
+
}>;
|
|
37
|
+
export type FocusOutsideEvent = CustomEvent<{
|
|
38
|
+
originalEvent: FocusEvent;
|
|
39
|
+
}>;
|
|
40
|
+
export { Dismissable, DismissableBranch };
|
|
41
|
+
export type { DismissableProps };
|
|
42
|
+
//# sourceMappingURL=Dismissable.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { DismissableBranchProps, DismissableProps } from './DismissableProps';
|
|
3
|
+
export declare function dispatchDiscreteCustomEvent<E extends CustomEvent>(_target: E['target'], _event: E): void;
|
|
4
|
+
export declare function getDismissableLayerCount(): number;
|
|
5
|
+
export declare function useHasDismissableLayers(): boolean;
|
|
6
|
+
export declare function useIsInsideDismissable(_ref: React.RefObject<HTMLElement | null>): boolean;
|
|
7
|
+
export declare function useDismissableLayersAbove(_ref: React.RefObject<HTMLElement | null>): number;
|
|
8
|
+
export declare const Dismissable: React.ForwardRefExoticComponent<DismissableProps & React.RefAttributes<unknown>>;
|
|
9
|
+
export declare const DismissableBranch: React.ForwardRefExoticComponent<DismissableBranchProps & React.RefAttributes<unknown>>;
|
|
10
|
+
//# sourceMappingURL=Dismissable.native.d.ts.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import type { FocusOutsideEvent, PointerDownOutsideEvent } from './Dismissable';
|
|
3
|
+
export interface DismissableProps {
|
|
4
|
+
/**
|
|
5
|
+
* When `true`, hover/focus/click interactions will be disabled on elements outside
|
|
6
|
+
* the `Dismissable`. Users will need to click twice on outside elements to
|
|
7
|
+
* interact with them: once to close the `Dismissable`, and again to trigger the element.
|
|
8
|
+
*/
|
|
9
|
+
disableOutsidePointerEvents?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Optional Set of branch elements that should not trigger dismissal.
|
|
12
|
+
* Pass the same Set to DismissableBranch components to scope them to this Dismissable.
|
|
13
|
+
*/
|
|
14
|
+
branches?: Set<HTMLElement>;
|
|
15
|
+
/**
|
|
16
|
+
* Event handler called when the escape key is down.
|
|
17
|
+
* Can be prevented.
|
|
18
|
+
*/
|
|
19
|
+
onEscapeKeyDown?: React.KeyboardEventHandler;
|
|
20
|
+
/**
|
|
21
|
+
* Event handler called when the a `pointerdown` event happens outside of the `Dismissable`.
|
|
22
|
+
* Can be prevented.
|
|
23
|
+
*/
|
|
24
|
+
onPointerDownOutside?: (event: PointerDownOutsideEvent) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Event handler called when the focus moves outside of the `Dismissable`.
|
|
27
|
+
* Can be prevented.
|
|
28
|
+
*/
|
|
29
|
+
onFocusOutside?: (event: FocusOutsideEvent) => void;
|
|
30
|
+
/**
|
|
31
|
+
* Event handler called when an interaction happens outside the `Dismissable`.
|
|
32
|
+
* Specifically, when a `pointerdown` event happens outside or focus moves outside of it.
|
|
33
|
+
* Can be prevented.
|
|
34
|
+
*/
|
|
35
|
+
onInteractOutside?: (event: PointerDownOutsideEvent | FocusOutsideEvent) => void;
|
|
36
|
+
/**
|
|
37
|
+
* Handler called when the `Dismissable` should be dismissed
|
|
38
|
+
*/
|
|
39
|
+
onDismiss?: () => void;
|
|
40
|
+
/**
|
|
41
|
+
* When using animations on exit, may want to simualte force unmount early
|
|
42
|
+
*/
|
|
43
|
+
forceUnmount?: boolean;
|
|
44
|
+
onPointerDownCapture?: any;
|
|
45
|
+
onBlurCapture?: any;
|
|
46
|
+
onFocusCapture?: any;
|
|
47
|
+
children?: React.ReactNode;
|
|
48
|
+
}
|
|
49
|
+
export interface DismissableBranchProps {
|
|
50
|
+
children?: React.ReactNode;
|
|
51
|
+
/**
|
|
52
|
+
* Optional Set to register this branch with.
|
|
53
|
+
* Pass the same Set to the Dismissable to scope this branch to that specific layer.
|
|
54
|
+
*/
|
|
55
|
+
branches?: Set<HTMLElement>;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=DismissableProps.d.ts.map
|
package/types/index.d.ts
ADDED