-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
DismissableLayer.ts
310 lines (266 loc) · 9.67 KB
/
DismissableLayer.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import {
useComposedRefs,
useEscapeKeydown,
useForwardRef,
} from '@oku-ui/use-composable'
import type { OkuElement, PrimitiveProps } from '@oku-ui/primitive'
import { Primitive, primitiveProps } from '@oku-ui/primitive'
import type { Ref } from 'vue'
import {
computed,
defineComponent,
h,
nextTick,
onMounted,
onUnmounted,
ref,
shallowReactive,
toRefs,
watchEffect,
} from 'vue'
import { composeEventHandlers } from '@oku-ui/utils'
import {
dispatchUpdate,
scopeDismissableLayerProps,
useFocusoutSide,
usePointerdownOutside,
} from './util'
export const DismissableLayerContext = shallowReactive({
layersRoot: new Set<DismissableLayerElement>(),
layersWithOutsidePointerEventsDisabled: new Set<DismissableLayerElement>(),
branches: new Set<DismissableLayerElement>(),
})
export const INJECT_UPDATE = 'dismissableLayer.update'
export const POINTER_DOWN_OUTSIDE = 'dismissableLayer.pointerDownOutside'
export const FOCUS_OUTSIDE = 'dismissableLayer.focusOutside'
let originalBodyPointerEvents: string
export const DISMISSABLE_NAME = 'OkuDismissableLayer'
export const DismissableLayerProvideKey = Symbol('DismissableLayerProvide')
export type DismissableLayerNativeElement = OkuElement<'div'>
export type DismissableLayerElement = HTMLDivElement
export type DismissableLayerProvideValue = {
layers: Ref<Set<DismissableLayerElement>>
layersWithOutsidePointerEventsDisabled: Ref<Set<DismissableLayerElement>>
branches: Ref<Set<DismissableLayerElement>>
}
export type PointerdownOutsideEvent = CustomEvent<{
originalEvent: PointerEvent
}>
export type FocusoutSideEvent = CustomEvent<{ originalEvent: FocusEvent }>
export type FocusCaptureEvent = CustomEvent<{ originalEvent: FocusEvent }>
export type FocusBlurCaptureEvent = CustomEvent<{ originalEvent: FocusEvent }>
export type PointerdownCaptureEvent = CustomEvent<{
originalEvent: PointerEvent
}>
export interface DismissableLayerProps extends PrimitiveProps {
/**
* When `true`, hover/focus/click interactions will be disabled on elements outside
* the `DismissableLayer`. Users will need to click twice on outside elements to
* interact with them: once to close the `DismissableLayer`, and again to trigger the element.
*/
disableOutsidePointerEvents?: boolean
}
export type DismissableLayerEmits = {
/**
* Event handler called when the escape key is down.
* Can be prevented.
*/
escapeKeyDown: [event: KeyboardEvent]
/**
* Event handler called when the a `pointerdown` event happens outside of the `DismissableLayer`.
* Can be prevented.
*/
pointerdownOutside: [event: PointerdownOutsideEvent]
/**
* Event handler called when the focus moves outside of the `DismissableLayer`.
* Can be prevented.
*/
focusoutSide: [event: FocusoutSideEvent]
/**
* Event handler called when an interaction happens outside the `DismissableLayer`.
* Specifically, when a `pointerdown` event happens outside or focus moves outside of it.
* Can be prevented.
*/
interactOutside: [event: PointerdownOutsideEvent | FocusoutSideEvent]
/**
* Handler called when the `DismissableLayer` should be dismissed
*/
dismiss: []
focusCapture: [event: FocusCaptureEvent]
blurCapture: [event: FocusBlurCaptureEvent]
pointerdownCapture: [event: PointerdownCaptureEvent]
}
export const dismissableLayerProps = {
props: {
disableOutsidePointerEvents: {
type: Boolean,
default: false,
},
},
emits: {
// eslint-disable-next-line unused-imports/no-unused-vars
escapeKeyDown: (event: KeyboardEvent) => true,
// eslint-disable-next-line unused-imports/no-unused-vars
pointerdownOutside: (event: PointerdownOutsideEvent) => true,
// eslint-disable-next-line unused-imports/no-unused-vars
focusoutSide: (event: FocusoutSideEvent) => true,
// eslint-disable-next-line unused-imports/no-unused-vars
interactOutside: (event: PointerdownOutsideEvent | FocusoutSideEvent) => true,
dismiss: () => true,
// eslint-disable-next-line unused-imports/no-unused-vars
focusCapture: (event: FocusCaptureEvent) => true,
// eslint-disable-next-line unused-imports/no-unused-vars
blurCapture: (event: FocusBlurCaptureEvent) => true,
// eslint-disable-next-line unused-imports/no-unused-vars
pointerdownCapture: (event: PointerdownCaptureEvent) => true,
},
}
const DismissableLayer = defineComponent({
name: DISMISSABLE_NAME,
inheritAttrs: false,
props: {
...dismissableLayerProps.props,
...primitiveProps,
...scopeDismissableLayerProps,
},
emits: dismissableLayerProps.emits,
setup(props, { attrs, emit, slots }) {
const { disableOutsidePointerEvents, asChild } = toRefs(props)
const provide = DismissableLayerContext
const node = ref<HTMLDivElement | null>(null)
const ownerDocument = computed(
() => node.value?.ownerDocument ?? globalThis?.document,
)
const forwardedRef = useForwardRef()
const composedRefs = useComposedRefs(node, forwardedRef)
const layers = computed(() => Array.from(provide.layersRoot))
const highestLayerWithOutsidePointerEventsDisabled = computed(() => {
const [highestLayerWithOutsidePointerEventsDisabled] = [
...provide.layersWithOutsidePointerEventsDisabled,
].slice(-1)
return highestLayerWithOutsidePointerEventsDisabled
})
const index = computed(() => {
return node.value ? layers.value.indexOf(node.value) : -1
})
const highestLayerWithOutsidePointerEventsDisabledIndex = computed(() =>
layers.value.indexOf(highestLayerWithOutsidePointerEventsDisabled.value),
)
const isPointerEventsEnabled = computed(() => index.value >= highestLayerWithOutsidePointerEventsDisabledIndex.value)
const isBodyPointerEventsDisabled = computed(
() => provide.layersWithOutsidePointerEventsDisabled.size > 0,
)
const pointerdownOutside = usePointerdownOutside((event) => {
const target = event.target as HTMLElement
const isPointerDownOnBranch = [...provide.branches].some(branch =>
branch.contains(target),
)
if (!isPointerEventsEnabled.value || isPointerDownOnBranch)
return
emit('pointerdownOutside', event)
emit('interactOutside', event)
if (!event.defaultPrevented)
emit('dismiss')
}, ownerDocument.value)
const focusoutSide = useFocusoutSide((event) => {
const target = event.target as HTMLElement
const isFocusInBranch = [...provide.branches].some(branch =>
branch.contains(target),
)
if (isFocusInBranch)
return
emit('focusoutSide', event)
emit('interactOutside', event)
if (!event.defaultPrevented)
emit('dismiss')
}, ownerDocument.value)
useEscapeKeydown((event) => {
const isHighestLayer = index.value === provide.layersRoot.size - 1
if (!isHighestLayer)
return
emit('escapeKeyDown', event)
if (!event.defaultPrevented)
emit('dismiss')
}, ownerDocument.value)
watchEffect(async (onInvalidate) => {
if (!node.value)
return
if (disableOutsidePointerEvents.value) {
if (provide.layersWithOutsidePointerEventsDisabled.size === 0) {
originalBodyPointerEvents
= ownerDocument.value.body.style.pointerEvents
ownerDocument.value.body.style.pointerEvents = 'none'
}
await nextTick()
provide.layersWithOutsidePointerEventsDisabled.add(node.value)
}
provide.layersRoot.add(node.value)
dispatchUpdate()
onInvalidate(() => {
if (
disableOutsidePointerEvents.value
&& provide.layersWithOutsidePointerEventsDisabled.size === 1
) {
ownerDocument.value.body.style.pointerEvents
= originalBodyPointerEvents
}
})
})
/**
* We purposefully prevent combining this effect with the `disableOutsidePointerEvents` effect
* because a change to `disableOutsidePointerEvents` would remove this layer from the stack
* and add it to the end again so the layering order wouldn't be _creation order_.
* We only want them to be removed from inject stacks when unmounted.
*/
watchEffect((onInvalidate) => {
onInvalidate(() => {
if (!node.value)
return
provide.layersRoot.delete(node.value)
provide.layersWithOutsidePointerEventsDisabled.delete(node.value)
dispatchUpdate()
})
})
const handleUpdate = () => { }
onMounted(() => {
document.addEventListener(INJECT_UPDATE, handleUpdate)
})
onUnmounted(() => {
document.removeEventListener(INJECT_UPDATE, handleUpdate)
})
const originalReturn = () =>
h(
Primitive.div,
{
...attrs,
ref: composedRefs,
asChild: asChild.value,
style: {
pointerEvents: isBodyPointerEventsDisabled.value
? isPointerEventsEnabled.value
? 'auto'
: 'none'
: undefined,
...(attrs.style as CSSPropertyRule),
},
onFocusCapture: composeEventHandlers<FocusCaptureEvent>((e) => {
emit('focusCapture', e)
}, focusoutSide.onFocusCapture),
onBlurCapture: composeEventHandlers<FocusBlurCaptureEvent>((e) => {
emit('blurCapture', e)
}, focusoutSide.onBlurCapture),
onPointerdownCapture: composeEventHandlers<PointerdownCaptureEvent>(
(e) => {
emit('pointerdownCapture', e)
},
pointerdownOutside.onPointerdownCapture,
),
}, slots,
)
return originalReturn
},
})
export const OkuDismissableLayer = DismissableLayer as typeof DismissableLayer &
(new () => {
$props: DismissableLayerNativeElement
})