@astryxdesign/core 0.7.7-canary.550ff21 → 0.7.7-canary.59d7d10
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/dist/DateInput/Wheel.d.ts +17 -1
- package/dist/DateInput/Wheel.d.ts.map +1 -1
- package/dist/DateInput/Wheel.js +179 -19
- package/dist/Drawer/useDrawerDialogPresence.d.ts.map +1 -1
- package/dist/Drawer/useDrawerDialogPresence.js +4 -0
- package/dist/DropdownMenu/DropdownMenu.d.ts.map +1 -1
- package/dist/DropdownMenu/DropdownMenu.js +20 -4
- package/dist/Layer/layerClickBoundary.d.ts +7 -1
- package/dist/Layer/layerClickBoundary.d.ts.map +1 -1
- package/dist/Layer/layerClickBoundary.js +2 -2
- package/dist/Layer/usePressPin.d.ts +95 -0
- package/dist/Layer/usePressPin.d.ts.map +1 -0
- package/dist/Layer/usePressPin.js +365 -0
- package/dist/Layer/useTouchTrigger.d.ts +23 -1
- package/dist/Layer/useTouchTrigger.d.ts.map +1 -1
- package/dist/Layer/useTouchTrigger.js +45 -7
- package/dist/Popover/usePopover.d.ts.map +1 -1
- package/dist/Popover/usePopover.js +56 -7
- package/dist/ProgressBar/ProgressBar.d.ts +1 -1
- package/dist/ProgressBar/ProgressBar.d.ts.map +1 -1
- package/dist/ProgressBar/ProgressBar.js +33 -6
- package/dist/Switch/Switch.d.ts +1 -1
- package/dist/Switch/Switch.d.ts.map +1 -1
- package/dist/Switch/Switch.js +200 -46
- package/dist/Switch/switchFill.d.ts +31 -0
- package/dist/Switch/switchFill.d.ts.map +1 -0
- package/dist/Switch/switchFill.js +88 -0
- package/dist/Switch/switchTouch.d.ts +81 -0
- package/dist/Switch/switchTouch.d.ts.map +1 -0
- package/dist/Switch/switchTouch.js +133 -0
- package/dist/Switch/useSwitchTouch.d.ts +285 -0
- package/dist/Switch/useSwitchTouch.d.ts.map +1 -0
- package/dist/Switch/useSwitchTouch.js +997 -0
- package/dist/Tooltip/Tooltip.d.ts +30 -6
- package/dist/Tooltip/Tooltip.d.ts.map +1 -1
- package/dist/Tooltip/Tooltip.js +5 -0
- package/dist/Tooltip/index.d.ts +1 -1
- package/dist/Tooltip/index.d.ts.map +1 -1
- package/dist/Tooltip/useTooltip.d.ts +31 -5
- package/dist/Tooltip/useTooltip.d.ts.map +1 -1
- package/dist/Tooltip/useTooltip.js +59 -18
- package/dist/Typeahead/Typeahead.d.ts.map +1 -1
- package/dist/Typeahead/Typeahead.js +26 -15
- package/dist/astryx.css +46 -2
- package/dist/gestures/GestureHost.d.ts.map +1 -1
- package/dist/gestures/GestureHost.js +4 -3
- package/dist/gestures/PressTracker.d.ts +6 -0
- package/dist/gestures/PressTracker.d.ts.map +1 -1
- package/dist/gestures/PressTracker.js +3 -0
- package/dist/gestures/types.d.ts +6 -0
- package/dist/gestures/types.d.ts.map +1 -1
- package/dist/gestures/usePressTracker.d.ts.map +1 -1
- package/dist/gestures/usePressTracker.js +1 -0
- package/dist/hooks/useFocusTrap.d.ts +15 -0
- package/dist/hooks/useFocusTrap.d.ts.map +1 -1
- package/dist/hooks/useFocusTrap.js +13 -2
- package/dist/motion/uikit/index.d.ts +6 -4
- package/dist/motion/uikit/index.d.ts.map +1 -1
- package/dist/motion/uikit/index.js +6 -4
- package/dist/motion/uikit/progressBar.d.ts +27 -7
- package/dist/motion/uikit/progressBar.d.ts.map +1 -1
- package/dist/motion/uikit/progressBar.js +31 -11
- package/dist/motion/uikit/springs.d.ts +13 -29
- package/dist/motion/uikit/springs.d.ts.map +1 -1
- package/dist/motion/uikit/springs.js +16 -30
- package/dist/motion/uikit/switch.d.ts +166 -0
- package/dist/motion/uikit/switch.d.ts.map +1 -0
- package/dist/motion/uikit/switch.js +186 -0
- package/dist/privateAttributes/pressHoldsDrag.stylex.d.ts +18 -0
- package/dist/privateAttributes/pressHoldsDrag.stylex.d.ts.map +1 -0
- package/dist/privateAttributes/pressHoldsDrag.stylex.js +28 -0
- package/dist/utils/pressFeedback.d.ts.map +1 -1
- package/dist/utils/pressFeedback.js +5 -0
- package/package.json +4 -4
- package/src/DateInput/Wheel.tsx +174 -14
- package/src/Drawer/useDrawerDialogPresence.ts +4 -0
- package/src/DropdownMenu/DropdownMenu.tsx +28 -6
- package/src/Layer/layerClickBoundary.ts +2 -2
- package/src/Layer/usePressPin.ts +516 -0
- package/src/Layer/useTouchTrigger.ts +56 -6
- package/src/Popover/usePopover.tsx +71 -9
- package/src/ProgressBar/ProgressBar.doc.mjs +4 -2
- package/src/ProgressBar/ProgressBar.tsx +67 -7
- package/src/Switch/Switch.doc.mjs +3 -3
- package/src/Switch/Switch.tsx +412 -86
- package/src/Switch/switchFill.ts +106 -0
- package/src/Switch/switchTouch.ts +194 -0
- package/src/Switch/useSwitchTouch.ts +1117 -0
- package/src/Tooltip/Tooltip.doc.mjs +21 -6
- package/src/Tooltip/Tooltip.tsx +42 -5
- package/src/Tooltip/index.ts +1 -0
- package/src/Tooltip/useTooltip.doc.mjs +4 -2
- package/src/Tooltip/useTooltip.tsx +84 -21
- package/src/Typeahead/Typeahead.tsx +29 -15
- package/src/gestures/GestureHost.ts +7 -3
- package/src/gestures/PressTracker.ts +10 -0
- package/src/gestures/types.ts +6 -0
- package/src/gestures/usePressTracker.ts +2 -0
- package/src/hooks/useFocusTrap.ts +26 -2
- package/src/motion/MEASUREMENTS.md +29 -27
- package/src/motion/README.md +2 -1
- package/src/motion/uikit/index.ts +5 -4
- package/src/motion/uikit/progressBar.ts +31 -11
- package/src/motion/uikit/springs.ts +16 -30
- package/src/motion/uikit/switch.ts +174 -0
- package/src/privateAttributes/pressHoldsDrag.stylex.ts +28 -0
- package/src/utils/pressFeedback.ts +5 -0
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file usePressPin.ts
|
|
7
|
+
* @input A hover layer's trigger, open state, show and hide, and its
|
|
8
|
+
* `triggerBehavior` and `touchTrigger` options
|
|
9
|
+
* @output usePressPin, the press toggle that pins a hover layer open;
|
|
10
|
+
* isPressPinTrigger; stopPressPinClick; the LayerTriggerBehavior type
|
|
11
|
+
* @position Core-internal: one call per hover layer, useTooltip's. It owns
|
|
12
|
+
* the layer's touch trigger, so the host makes no second touch-trigger call.
|
|
13
|
+
*
|
|
14
|
+
* SYNC: When modified, update:
|
|
15
|
+
* - /packages/core/src/Tooltip/useTooltip.tsx
|
|
16
|
+
* - /packages/core/src/Layer/usePressPin.test.tsx
|
|
17
|
+
*/
|
|
18
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
19
|
+
import { useAnnounce } from "../hooks/useAnnounce.js";
|
|
20
|
+
import { useDevWarning } from "../hooks/useDevWarning.js";
|
|
21
|
+
import { stopInReactTree } from "./layerClickBoundary.js";
|
|
22
|
+
import { useTouchTriggerInternal } from "./useTouchTrigger.js";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* How the trigger opens the layer. `adaptive`, the default, previews it on
|
|
26
|
+
* hover and focus, and a tap opens it unless the trigger acts. While
|
|
27
|
+
* `touchTrigger` remains, it decides those taps. `hover` previews it on hover
|
|
28
|
+
* and focus, and no touch or pen tap opens it. `toggle` pins it on every
|
|
29
|
+
* press of a button trigger.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/** The clicks a press pin answered, so the wrapper can stop them (`stopPressPinClick`). */
|
|
33
|
+
const pressPinClicks = new WeakSet();
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The layer component's wrapper handler. It stops a press that pinned or
|
|
37
|
+
* closed the layer in the React tree only, after the trigger's own handlers.
|
|
38
|
+
* No clickable row, card, or container around the trigger then receives it,
|
|
39
|
+
* and document and window listeners still do. An anchored trigger has no
|
|
40
|
+
* wrapper, so a plain `onClick` element around it still receives the press.
|
|
41
|
+
*/
|
|
42
|
+
export function stopPressPinClick(event) {
|
|
43
|
+
if (pressPinClicks.has(event.nativeEvent)) {
|
|
44
|
+
stopInReactTree(event);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Whether presses on this element can pin a layer: a button, by role. */
|
|
49
|
+
export function isPressPinTrigger(element) {
|
|
50
|
+
const role = element.getAttribute('role');
|
|
51
|
+
if (role != null && role !== '') {
|
|
52
|
+
return role === 'button';
|
|
53
|
+
}
|
|
54
|
+
return element.tagName === 'BUTTON';
|
|
55
|
+
}
|
|
56
|
+
function restoreAttribute(element, name, value) {
|
|
57
|
+
if (value == null) {
|
|
58
|
+
element.removeAttribute(name);
|
|
59
|
+
} else {
|
|
60
|
+
element.setAttribute(name, value);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function writeAttribute(element, name, value) {
|
|
64
|
+
if (element.getAttribute(name) !== value) {
|
|
65
|
+
restoreAttribute(element, name, value);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Pins a hover layer open on a press of a button trigger, under
|
|
71
|
+
* `triggerBehavior="toggle"`. The same press closes it, and so do Escape, a
|
|
72
|
+
* press or scroll outside, and focus that leaves the trigger and the layer.
|
|
73
|
+
* The press stops at the trigger. A trigger that is not a button gets a
|
|
74
|
+
* development warning and keeps tap-only opening.
|
|
75
|
+
*/
|
|
76
|
+
export function usePressPin(options) {
|
|
77
|
+
const {
|
|
78
|
+
triggerBehavior,
|
|
79
|
+
isOpen,
|
|
80
|
+
isLayerOpen,
|
|
81
|
+
layerId
|
|
82
|
+
} = options;
|
|
83
|
+
const isToggleMode = triggerBehavior === 'toggle';
|
|
84
|
+
|
|
85
|
+
// DOM listeners read the latest options and announcer through refs. The
|
|
86
|
+
// announcer is the layer's own: inside a modal, the modal's region.
|
|
87
|
+
const optionsRef = useRef(options);
|
|
88
|
+
optionsRef.current = options;
|
|
89
|
+
const announce = useAnnounce();
|
|
90
|
+
const announceRef = useRef(announce);
|
|
91
|
+
announceRef.current = announce;
|
|
92
|
+
const isPinnedRef = useRef(false);
|
|
93
|
+
const [isPinned, setIsPinnedState] = useState(false);
|
|
94
|
+
const setPinned = useCallback(next => {
|
|
95
|
+
isPinnedRef.current = next;
|
|
96
|
+
setIsPinnedState(next);
|
|
97
|
+
}, []);
|
|
98
|
+
|
|
99
|
+
// The last offered element lets a `triggerBehavior` change rewire it.
|
|
100
|
+
const triggerRef = useRef(null);
|
|
101
|
+
const offeredRef = useRef(null);
|
|
102
|
+
// A primitive: re-attaching the same trigger renders nothing.
|
|
103
|
+
const [offeredKind, setOfferedKind] = useState('none');
|
|
104
|
+
// The trigger's own aria-expanded and aria-controls, restored on unwiring.
|
|
105
|
+
const ownStateRef = useRef(null);
|
|
106
|
+
|
|
107
|
+
// The touch trigger's outside dismissal (a press or scroll outside) closes
|
|
108
|
+
// a pinned layer through the pin, so the pin drops with it.
|
|
109
|
+
const hideFromTouch = useCallback(() => {
|
|
110
|
+
const current = optionsRef.current;
|
|
111
|
+
if (current.triggerBehavior === 'toggle' && triggerRef.current != null) {
|
|
112
|
+
if (current.isOpen !== undefined) {
|
|
113
|
+
current.report(false);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (isPinnedRef.current) {
|
|
117
|
+
setPinned(false);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
current.hide();
|
|
121
|
+
}, [setPinned]);
|
|
122
|
+
const {
|
|
123
|
+
isTouchPointerRef,
|
|
124
|
+
isTouchInteraction,
|
|
125
|
+
handlePointerEnter,
|
|
126
|
+
handlePointerDown,
|
|
127
|
+
clearTapOpen,
|
|
128
|
+
armOutsideDismiss
|
|
129
|
+
} = useTouchTriggerInternal({
|
|
130
|
+
touchTrigger: isToggleMode ? 'tap' : triggerBehavior === 'hover' ? 'none' : options.touchTrigger,
|
|
131
|
+
isEnabled: options.isEnabled,
|
|
132
|
+
isControlled: isOpen !== undefined,
|
|
133
|
+
isOpen: isLayerOpen,
|
|
134
|
+
layerId,
|
|
135
|
+
triggerRef: options.triggerRef,
|
|
136
|
+
show: options.show,
|
|
137
|
+
hide: hideFromTouch
|
|
138
|
+
});
|
|
139
|
+
const isToggleTrigger = useCallback(element => optionsRef.current.triggerBehavior === 'toggle' && element != null && isPressPinTrigger(element), []);
|
|
140
|
+
const close = useCallback(() => {
|
|
141
|
+
const current = optionsRef.current;
|
|
142
|
+
clearTapOpen();
|
|
143
|
+
if (current.isOpen !== undefined) {
|
|
144
|
+
current.report(false);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
setPinned(false);
|
|
148
|
+
current.hide();
|
|
149
|
+
}, [setPinned, clearTapOpen]);
|
|
150
|
+
const announceName = useCallback(() => {
|
|
151
|
+
const current = optionsRef.current;
|
|
152
|
+
const name = current.label?.trim() ?? '';
|
|
153
|
+
if (name !== '') {
|
|
154
|
+
announceRef.current(name, 'polite');
|
|
155
|
+
}
|
|
156
|
+
}, []);
|
|
157
|
+
const toggle = useCallback(() => {
|
|
158
|
+
const current = optionsRef.current;
|
|
159
|
+
if (current.isOpen !== undefined) {
|
|
160
|
+
if (!current.isOpen) {
|
|
161
|
+
armOutsideDismiss();
|
|
162
|
+
announceName();
|
|
163
|
+
} else {
|
|
164
|
+
clearTapOpen();
|
|
165
|
+
}
|
|
166
|
+
current.report(!current.isOpen);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
// A pin counts only while its layer shows; a closed layer pins again.
|
|
170
|
+
if (isPinnedRef.current && current.isLayerOpen) {
|
|
171
|
+
setPinned(false);
|
|
172
|
+
clearTapOpen();
|
|
173
|
+
current.hide();
|
|
174
|
+
} else {
|
|
175
|
+
setPinned(true);
|
|
176
|
+
current.show();
|
|
177
|
+
armOutsideDismiss();
|
|
178
|
+
announceName();
|
|
179
|
+
}
|
|
180
|
+
}, [setPinned, announceName, armOutsideDismiss, clearTapOpen]);
|
|
181
|
+
|
|
182
|
+
// A non-native button gets no click from the browser for a key. So Enter
|
|
183
|
+
// presses it on keydown, and Space on keyup, as on a native button. A click
|
|
184
|
+
// with no pointer (`detail` 0) while Space is down, or in a key press's
|
|
185
|
+
// task, is that same press.
|
|
186
|
+
const keyPressedRef = useRef(false);
|
|
187
|
+
const spaceHeldRef = useRef(false);
|
|
188
|
+
const handlePress = useCallback(event => {
|
|
189
|
+
const current = optionsRef.current;
|
|
190
|
+
if (!current.isEnabled || current.triggerBehavior !== 'toggle') {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
// The press stops at its trigger: no label control, link, or submit.
|
|
194
|
+
event.preventDefault();
|
|
195
|
+
pressPinClicks.add(event);
|
|
196
|
+
if (event.detail === 0 && (keyPressedRef.current || spaceHeldRef.current)) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
toggle();
|
|
200
|
+
}, [toggle]);
|
|
201
|
+
const pressFromKey = useCallback(() => {
|
|
202
|
+
keyPressedRef.current = true;
|
|
203
|
+
setTimeout(() => {
|
|
204
|
+
keyPressedRef.current = false;
|
|
205
|
+
}, 0);
|
|
206
|
+
toggle();
|
|
207
|
+
}, [toggle]);
|
|
208
|
+
const handleKeyDown = useCallback(event => {
|
|
209
|
+
const current = optionsRef.current;
|
|
210
|
+
if (!current.isEnabled || current.triggerBehavior !== 'toggle' || event.isComposing || event.key !== 'Enter' && event.key !== ' ') {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
// The press replaces the key's own action, such as a page scroll on Space.
|
|
214
|
+
event.preventDefault();
|
|
215
|
+
if (event.repeat) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (event.key === ' ') {
|
|
219
|
+
spaceHeldRef.current = true;
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
pressFromKey();
|
|
223
|
+
}, [pressFromKey]);
|
|
224
|
+
// Only a Space that went down on the trigger presses it when it comes up.
|
|
225
|
+
const handleKeyUp = useCallback(event => {
|
|
226
|
+
if (event.key !== ' ' || !spaceHeldRef.current) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
spaceHeldRef.current = false;
|
|
230
|
+
const current = optionsRef.current;
|
|
231
|
+
if (!current.isEnabled || current.triggerBehavior !== 'toggle') {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
pressFromKey();
|
|
235
|
+
}, [pressFromKey]);
|
|
236
|
+
const isHeldOpen = useCallback(() => {
|
|
237
|
+
const current = optionsRef.current;
|
|
238
|
+
if (current.triggerBehavior !== 'toggle' || triggerRef.current == null) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
return current.isOpen !== undefined ? current.isOpen && current.isLayerOpen : isPinnedRef.current && current.isLayerOpen;
|
|
242
|
+
}, []);
|
|
243
|
+
const handleFocusLeave = useCallback(event => {
|
|
244
|
+
// A Space that comes up after focus left presses nothing.
|
|
245
|
+
spaceHeldRef.current = false;
|
|
246
|
+
if (!isHeldOpen()) {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
const next = event.relatedTarget;
|
|
250
|
+
const trigger = triggerRef.current;
|
|
251
|
+
// Focus that leaves for no element is a press's to decide. The trigger
|
|
252
|
+
// can sit in another document, so the check uses its own window.
|
|
253
|
+
const view = trigger?.ownerDocument.defaultView;
|
|
254
|
+
if (view == null || !(next instanceof view.Node)) {
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
const surface = trigger?.ownerDocument.getElementById(optionsRef.current.layerId) ?? null;
|
|
258
|
+
if (trigger?.contains(next) === true || surface?.contains(next) === true) {
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
close();
|
|
262
|
+
return true;
|
|
263
|
+
}, [isHeldOpen, close]);
|
|
264
|
+
const unwire = useCallback(() => {
|
|
265
|
+
const previous = triggerRef.current;
|
|
266
|
+
if (previous == null) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
previous.removeEventListener('click', handlePress);
|
|
270
|
+
previous.removeEventListener('keydown', handleKeyDown);
|
|
271
|
+
previous.removeEventListener('keyup', handleKeyUp);
|
|
272
|
+
previous.removeEventListener('focusout', handleFocusLeave);
|
|
273
|
+
const own = ownStateRef.current;
|
|
274
|
+
if (own != null) {
|
|
275
|
+
restoreAttribute(previous, 'aria-expanded', own.expanded);
|
|
276
|
+
restoreAttribute(previous, 'aria-controls', own.controls);
|
|
277
|
+
}
|
|
278
|
+
ownStateRef.current = null;
|
|
279
|
+
triggerRef.current = null;
|
|
280
|
+
}, [handlePress, handleKeyDown, handleKeyUp, handleFocusLeave]);
|
|
281
|
+
|
|
282
|
+
// The element and mode last settled; the same pair again does nothing.
|
|
283
|
+
const settledRef = useRef(null);
|
|
284
|
+
const wire = useCallback(element => {
|
|
285
|
+
const current = optionsRef.current;
|
|
286
|
+
const settled = settledRef.current;
|
|
287
|
+
if (settled != null && settled.element === element && settled.mode === current.triggerBehavior) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
settledRef.current = {
|
|
291
|
+
element,
|
|
292
|
+
mode: current.triggerBehavior
|
|
293
|
+
};
|
|
294
|
+
unwire();
|
|
295
|
+
if (element == null || current.triggerBehavior !== 'toggle') {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (!isPressPinTrigger(element)) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
element.addEventListener('click', handlePress);
|
|
302
|
+
if (element.tagName !== 'BUTTON') {
|
|
303
|
+
element.addEventListener('keydown', handleKeyDown);
|
|
304
|
+
element.addEventListener('keyup', handleKeyUp);
|
|
305
|
+
}
|
|
306
|
+
// Its own focus-leave listener: `focusTrigger="never"` attaches none.
|
|
307
|
+
element.addEventListener('focusout', handleFocusLeave);
|
|
308
|
+
ownStateRef.current = {
|
|
309
|
+
expanded: element.getAttribute('aria-expanded'),
|
|
310
|
+
controls: element.getAttribute('aria-controls')
|
|
311
|
+
};
|
|
312
|
+
triggerRef.current = element;
|
|
313
|
+
}, [unwire, handlePress, handleKeyDown, handleKeyUp, handleFocusLeave]);
|
|
314
|
+
const attach = useCallback(element => {
|
|
315
|
+
offeredRef.current = element;
|
|
316
|
+
// Hosts detach and re-attach within one commit, so a detach is not kept.
|
|
317
|
+
if (element != null) {
|
|
318
|
+
setOfferedKind(isPressPinTrigger(element) ? 'button' : 'other');
|
|
319
|
+
}
|
|
320
|
+
wire(element);
|
|
321
|
+
}, [wire]);
|
|
322
|
+
const isWired = isToggleMode && offeredKind === 'button';
|
|
323
|
+
useDevWarning(options.component, '`triggerBehavior="toggle"` pins only a button trigger (a <button> or role="button"); this trigger keeps tap-only opening.', isToggleMode && offeredKind === 'other');
|
|
324
|
+
|
|
325
|
+
// A later `triggerBehavior` change rewires here; the ref does not rerun.
|
|
326
|
+
useEffect(() => {
|
|
327
|
+
wire(offeredRef.current);
|
|
328
|
+
}, [triggerBehavior, wire]);
|
|
329
|
+
const unpin = useCallback(() => {
|
|
330
|
+
if (isPinnedRef.current) {
|
|
331
|
+
setPinned(false);
|
|
332
|
+
}
|
|
333
|
+
}, [setPinned]);
|
|
334
|
+
|
|
335
|
+
// Each path that hides a pinned layer drops the pin itself, so a layer that
|
|
336
|
+
// does not show is never reported expanded.
|
|
337
|
+
const isExpanded = (isOpen !== undefined ? isOpen : isPinned) && isLayerOpen;
|
|
338
|
+
|
|
339
|
+
// The trigger's aria-expanded and aria-controls follow the pin. No deps: a
|
|
340
|
+
// host can swap the trigger on any commit; equal values are not rewritten.
|
|
341
|
+
useEffect(() => {
|
|
342
|
+
const trigger = triggerRef.current;
|
|
343
|
+
if (!isToggleMode || !isWired || trigger == null) {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
writeAttribute(trigger, 'aria-expanded', isExpanded ? 'true' : 'false');
|
|
347
|
+
writeAttribute(trigger, 'aria-controls', isLayerOpen ? layerId : null);
|
|
348
|
+
});
|
|
349
|
+
const isPinShown = isToggleMode && isWired && isExpanded;
|
|
350
|
+
return {
|
|
351
|
+
isToggleTrigger,
|
|
352
|
+
isHeldOpen,
|
|
353
|
+
attach,
|
|
354
|
+
unpin,
|
|
355
|
+
handleFocusLeave,
|
|
356
|
+
isPinShown,
|
|
357
|
+
touch: {
|
|
358
|
+
isTouchPointerRef,
|
|
359
|
+
isTouchInteraction,
|
|
360
|
+
handlePointerEnter,
|
|
361
|
+
handlePointerDown,
|
|
362
|
+
clearTapOpen
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
* @input Touch trigger mode, layer open state, trigger ref, show/hide callbacks
|
|
4
4
|
* @output Exports useTouchTrigger hook, isActionTrigger, LayerTouchTrigger type,
|
|
5
5
|
* tapLandsOnDisabledControl, TOUCH_POINTER_TYPES and useTapStop (internal:
|
|
6
|
-
* not re-exported from index)
|
|
6
|
+
* not re-exported from index). Also useTouchTriggerInternal, with the
|
|
7
|
+
* outside dismissal's arm for the press pin (Core-internal).
|
|
7
8
|
* @position Layer hook; shared touch behavior for useTooltip and useHoverCard
|
|
8
9
|
*
|
|
9
10
|
* SYNC: When modified, update:
|
|
@@ -137,6 +138,20 @@ export interface UseTouchTriggerReturn {
|
|
|
137
138
|
/** Forget a tap-open. Call from every other close path (Escape, controlled). */
|
|
138
139
|
clearTapOpen: () => void;
|
|
139
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* useTouchTrigger's return plus the outside dismissal's arm, for Core's own
|
|
143
|
+
* press pin (usePressPin). Not exported from the Layer entry point.
|
|
144
|
+
*
|
|
145
|
+
* @internal
|
|
146
|
+
*/
|
|
147
|
+
export interface UseTouchTriggerInternalReturn extends UseTouchTriggerReturn {
|
|
148
|
+
/**
|
|
149
|
+
* Arms the outside dismissal a tap-open gets: a press that lifts outside
|
|
150
|
+
* the trigger and the layer, or a scroll that starts there, hides it. A
|
|
151
|
+
* press pin arms it too.
|
|
152
|
+
*/
|
|
153
|
+
armOutsideDismiss: () => void;
|
|
154
|
+
}
|
|
140
155
|
/**
|
|
141
156
|
* Touch behavior shared by the hover layers.
|
|
142
157
|
*
|
|
@@ -155,4 +170,11 @@ export interface UseTouchTriggerReturn {
|
|
|
155
170
|
* ```
|
|
156
171
|
*/
|
|
157
172
|
export declare function useTouchTrigger(options: UseTouchTriggerOptions): UseTouchTriggerReturn;
|
|
173
|
+
/**
|
|
174
|
+
* useTouchTrigger, plus the outside dismissal's arm that a press pin uses to
|
|
175
|
+
* close on a press or scroll outside.
|
|
176
|
+
*
|
|
177
|
+
* @internal
|
|
178
|
+
*/
|
|
179
|
+
export declare function useTouchTriggerInternal(options: UseTouchTriggerOptions): UseTouchTriggerInternalReturn;
|
|
158
180
|
//# sourceMappingURL=useTouchTrigger.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTouchTrigger.d.ts","sourceRoot":"","sources":["../../src/Layer/useTouchTrigger.ts"],"names":[],"mappings":"AAIA
|
|
1
|
+
{"version":3,"file":"useTouchTrigger.d.ts","sourceRoot":"","sources":["../../src/Layer/useTouchTrigger.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,OAAO,EAIL,KAAK,UAAU,IAAI,eAAe,EAClC,KAAK,YAAY,IAAI,iBAAiB,EACtC,KAAK,SAAS,EACf,MAAM,OAAO,CAAC;AASf;;;;;;GAMG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,CAAC;AAExD;;;;;;;GAOG;AACH,eAAO,MAAM,mBAAmB,aAA4B,CAAC;AAE7D,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG;IAC1C,aAAa,EAAE,CAAC,KAAK,EAAE,iBAAiB,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC;IAC3D,OAAO,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC;CACpD,CAAC;AACF,wBAAgB,UAAU,CAAC,KAAK,EAAE,WAAW,GAAG;IAC9C,aAAa,EAAE,CAAC,KAAK,EAAE,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AED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,sBAAsB,GAC9B,qBAAqB,CASvB;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,sBAAsB,GAC9B,6BAA6B,CA2N/B"}
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* @input Touch trigger mode, layer open state, trigger ref, show/hide callbacks
|
|
8
8
|
* @output Exports useTouchTrigger hook, isActionTrigger, LayerTouchTrigger type,
|
|
9
9
|
* tapLandsOnDisabledControl, TOUCH_POINTER_TYPES and useTapStop (internal:
|
|
10
|
-
* not re-exported from index)
|
|
10
|
+
* not re-exported from index). Also useTouchTriggerInternal, with the
|
|
11
|
+
* outside dismissal's arm for the press pin (Core-internal).
|
|
11
12
|
* @position Layer hook; shared touch behavior for useTooltip and useHoverCard
|
|
12
13
|
*
|
|
13
14
|
* SYNC: When modified, update:
|
|
@@ -176,6 +177,14 @@ function isEditable(element) {
|
|
|
176
177
|
const attribute = element.getAttribute('contenteditable');
|
|
177
178
|
return attribute != null && attribute !== 'false';
|
|
178
179
|
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* useTouchTrigger's return plus the outside dismissal's arm, for Core's own
|
|
183
|
+
* press pin (usePressPin). Not exported from the Layer entry point.
|
|
184
|
+
*
|
|
185
|
+
* @internal
|
|
186
|
+
*/
|
|
187
|
+
|
|
179
188
|
/**
|
|
180
189
|
* Touch behavior shared by the hover layers.
|
|
181
190
|
*
|
|
@@ -194,6 +203,23 @@ function isEditable(element) {
|
|
|
194
203
|
* ```
|
|
195
204
|
*/
|
|
196
205
|
export function useTouchTrigger(options) {
|
|
206
|
+
const touch = useTouchTriggerInternal(options);
|
|
207
|
+
return {
|
|
208
|
+
isTouchPointerRef: touch.isTouchPointerRef,
|
|
209
|
+
isTouchInteraction: touch.isTouchInteraction,
|
|
210
|
+
handlePointerEnter: touch.handlePointerEnter,
|
|
211
|
+
handlePointerDown: touch.handlePointerDown,
|
|
212
|
+
clearTapOpen: touch.clearTapOpen
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* useTouchTrigger, plus the outside dismissal's arm that a press pin uses to
|
|
218
|
+
* close on a press or scroll outside.
|
|
219
|
+
*
|
|
220
|
+
* @internal
|
|
221
|
+
*/
|
|
222
|
+
export function useTouchTriggerInternal(options) {
|
|
197
223
|
const {
|
|
198
224
|
touchTrigger,
|
|
199
225
|
isEnabled,
|
|
@@ -222,6 +248,9 @@ export function useTouchTrigger(options) {
|
|
|
222
248
|
// pointer-leave, and a controlled one is not ours to close.
|
|
223
249
|
const isTapOpenRef = useRef(false);
|
|
224
250
|
const outsideListenerRef = useRef(null);
|
|
251
|
+
// The documents the outside dismissal listens on: the page's, and the
|
|
252
|
+
// trigger's own when it renders in another document, such as an iframe.
|
|
253
|
+
const outsideDocumentsRef = useRef([]);
|
|
225
254
|
// A touch press on the trigger still waiting for its tap: calling it stops
|
|
226
255
|
// waiting. The next press, a cancel, the tap itself or unmounting calls it.
|
|
227
256
|
const forgetPendingTapRef = useRef(null);
|
|
@@ -233,8 +262,11 @@ export function useTouchTrigger(options) {
|
|
|
233
262
|
return;
|
|
234
263
|
}
|
|
235
264
|
outsideListenerRef.current = null;
|
|
236
|
-
|
|
237
|
-
|
|
265
|
+
for (const doc of outsideDocumentsRef.current) {
|
|
266
|
+
doc.removeEventListener('pointerup', listener, true);
|
|
267
|
+
doc.removeEventListener('pointercancel', listener, true);
|
|
268
|
+
}
|
|
269
|
+
outsideDocumentsRef.current = [];
|
|
238
270
|
}, []);
|
|
239
271
|
|
|
240
272
|
// Capture phase, so a trigger that stops propagation cannot strand an open
|
|
@@ -247,6 +279,8 @@ export function useTouchTrigger(options) {
|
|
|
247
279
|
if (outsideListenerRef.current != null) {
|
|
248
280
|
return;
|
|
249
281
|
}
|
|
282
|
+
const own = triggerRef.current?.ownerDocument ?? document;
|
|
283
|
+
const docs = own === document ? [document] : [document, own];
|
|
250
284
|
const handleOutsidePointerEnd = event => {
|
|
251
285
|
const target = event.target;
|
|
252
286
|
if (target != null) {
|
|
@@ -256,7 +290,7 @@ export function useTouchTrigger(options) {
|
|
|
256
290
|
return;
|
|
257
291
|
}
|
|
258
292
|
// Hover card content is interactive; a tap inside it is not a dismiss.
|
|
259
|
-
if (
|
|
293
|
+
if (docs.some(doc => doc.getElementById(layerIdRef.current)?.contains(target) === true)) {
|
|
260
294
|
return;
|
|
261
295
|
}
|
|
262
296
|
}
|
|
@@ -264,8 +298,11 @@ export function useTouchTrigger(options) {
|
|
|
264
298
|
hideRef.current();
|
|
265
299
|
};
|
|
266
300
|
outsideListenerRef.current = handleOutsidePointerEnd;
|
|
267
|
-
|
|
268
|
-
|
|
301
|
+
outsideDocumentsRef.current = docs;
|
|
302
|
+
for (const doc of docs) {
|
|
303
|
+
doc.addEventListener('pointerup', handleOutsidePointerEnd, true);
|
|
304
|
+
doc.addEventListener('pointercancel', handleOutsidePointerEnd, true);
|
|
305
|
+
}
|
|
269
306
|
}, [triggerRef, disarmOutsideDismiss]);
|
|
270
307
|
useEffect(() => disarmOutsideDismiss, [disarmOutsideDismiss]);
|
|
271
308
|
useEffect(() => () => forgetPendingTapRef.current?.(), []);
|
|
@@ -358,6 +395,7 @@ export function useTouchTrigger(options) {
|
|
|
358
395
|
isTouchInteraction,
|
|
359
396
|
handlePointerEnter,
|
|
360
397
|
handlePointerDown,
|
|
361
|
-
clearTapOpen: disarmOutsideDismiss
|
|
398
|
+
clearTapOpen: disarmOutsideDismiss,
|
|
399
|
+
armOutsideDismiss
|
|
362
400
|
};
|
|
363
401
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePopover.d.ts","sourceRoot":"","sources":["../../src/Popover/usePopover.tsx"],"names":[],"mappings":"AAIA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,EAAiC,KAAK,SAAS,EAAC,MAAM,OAAO,CAAC;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* as stylex from '@stylexjs/stylex';
|
|
20
20
|
import { useLayer } from "../Layer/useLayer.js";
|
|
21
|
-
import {
|
|
21
|
+
import { useFocusTrapInternal } from "../hooks/useFocusTrap.js";
|
|
22
22
|
import { LayerDepthProvider } from "../Layer/LayerDepthContext.js";
|
|
23
23
|
import "../theme/tokens.stylex.js";
|
|
24
24
|
import { colorVars, spacingVars, radiusVars, shadowVars } from "../theme/tokens.stylex.js";
|
|
@@ -28,6 +28,7 @@ import { rtlStyles } from "../utils/index.js";
|
|
|
28
28
|
import { useDevWarning } from "../hooks/useDevWarning.js";
|
|
29
29
|
import { focusOutlineProps } from "../utils/focusOutline.stylex.js";
|
|
30
30
|
import { mergeProps } from "../utils/mergeProps.js";
|
|
31
|
+
import { isFocusDetached } from "../utils/focusReturn.js";
|
|
31
32
|
import { themeProps } from "../utils/themeProps.js";
|
|
32
33
|
import { paddingStyles } from "../Layout/padding.stylex.js";
|
|
33
34
|
import { stableClassName } from "../naming.js";
|
|
@@ -161,30 +162,52 @@ function usePopoverImplementation(options = {}) {
|
|
|
161
162
|
|
|
162
163
|
// Track whether to skip auto-focus for the current open event
|
|
163
164
|
const skipAutoFocusRef = useRef(false);
|
|
165
|
+
// Whether `show` focused a content control within the opening event.
|
|
166
|
+
const hasFocusedInGestureRef = useRef(false);
|
|
167
|
+
|
|
168
|
+
// Set when the layer reports it has shown, so `show` can tell a popover
|
|
169
|
+
// that opened within the call from one whose content mounts later.
|
|
170
|
+
const hasShownRef = useRef(false);
|
|
171
|
+
const handleLayerShow = useCallback(() => {
|
|
172
|
+
hasShownRef.current = true;
|
|
173
|
+
onShow?.();
|
|
174
|
+
}, [onShow]);
|
|
164
175
|
|
|
165
176
|
// Core layer for popover positioning
|
|
166
177
|
const layer = useLayer({
|
|
167
178
|
mode: 'context',
|
|
168
179
|
lightDismiss: hasLightDismiss,
|
|
169
|
-
onShow,
|
|
180
|
+
onShow: handleLayerShow,
|
|
170
181
|
onHide
|
|
171
182
|
});
|
|
172
183
|
|
|
173
184
|
// Focus trap for the popover content. Escape stays registered while light
|
|
174
185
|
// dismiss is on (native popover="auto" closes on Escape regardless), so a
|
|
175
186
|
// host Dialog keeps deferring to this trap instead of double-dismissing.
|
|
187
|
+
// The element focused when `show` ran: initial focus may move inside before
|
|
188
|
+
// the trap activates, so the trap restores focus here instead.
|
|
189
|
+
const openerRef = useRef(null);
|
|
190
|
+
const getRestoreTarget = useCallback(() => openerRef.current, []);
|
|
176
191
|
const {
|
|
177
192
|
containerRef: contentRef,
|
|
178
193
|
focusFirst
|
|
179
|
-
} =
|
|
194
|
+
} = useFocusTrapInternal({
|
|
180
195
|
isActive: layer.isOpen,
|
|
181
|
-
onEscape: hasEscapeDismiss || hasLightDismiss ? layer.hide : undefined
|
|
196
|
+
onEscape: hasEscapeDismiss || hasLightDismiss ? layer.hide : undefined,
|
|
197
|
+
getRestoreTarget
|
|
182
198
|
});
|
|
183
199
|
const focusInitialTarget = useCallback(() => {
|
|
184
200
|
const container = contentRef.current;
|
|
185
201
|
if (!container) {
|
|
186
202
|
return;
|
|
187
203
|
}
|
|
204
|
+
// Focus already on a control inside (set in `onShow`, or a press in the
|
|
205
|
+
// content) is where the user or the caller wants it; moving it would
|
|
206
|
+
// undo that.
|
|
207
|
+
const active = container.ownerDocument.activeElement;
|
|
208
|
+
if (active != null && active !== container && container.contains(active)) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
188
211
|
if (focusFirstContentControl(container)) {
|
|
189
212
|
return;
|
|
190
213
|
}
|
|
@@ -200,16 +223,23 @@ function usePopoverImplementation(options = {}) {
|
|
|
200
223
|
// Reset the per-open focus preference after the popover closes.
|
|
201
224
|
if (!layer.isOpen) {
|
|
202
225
|
skipAutoFocusRef.current = false;
|
|
226
|
+
hasFocusedInGestureRef.current = false;
|
|
203
227
|
return;
|
|
204
228
|
}
|
|
205
229
|
if (hasAutoFocus && !skipAutoFocusRef.current) {
|
|
206
230
|
// Wait a frame so the content is mounted. A popover that closes or
|
|
207
231
|
// unmounts first cancels it: the frame would otherwise move focus into
|
|
208
232
|
// content that is no longer shown, after the close had left it alone.
|
|
209
|
-
const frame = requestAnimationFrame(
|
|
233
|
+
const frame = requestAnimationFrame(() => {
|
|
234
|
+
const doc = contentRef.current?.ownerDocument ?? document;
|
|
235
|
+
if (hasFocusedInGestureRef.current && !isFocusDetached(doc)) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
focusInitialTarget();
|
|
239
|
+
});
|
|
210
240
|
return () => cancelAnimationFrame(frame);
|
|
211
241
|
}
|
|
212
|
-
}, [layer.isOpen, hasAutoFocus, focusInitialTarget]);
|
|
242
|
+
}, [layer.isOpen, hasAutoFocus, focusInitialTarget, contentRef]);
|
|
213
243
|
|
|
214
244
|
// Combined ref for trigger element (layer anchor + our ref). It is keyed on
|
|
215
245
|
// the layer's own ref, not the whole layer: the layer object changes with
|
|
@@ -227,8 +257,27 @@ function usePopoverImplementation(options = {}) {
|
|
|
227
257
|
// Show function with optional focus preservation
|
|
228
258
|
const show = useCallback(showOptions => {
|
|
229
259
|
skipAutoFocusRef.current = showOptions?.skipAutoFocus ?? false;
|
|
260
|
+
hasShownRef.current = false;
|
|
261
|
+
const activeElement = (triggerElementRef.current?.ownerDocument ?? document).activeElement;
|
|
262
|
+
openerRef.current = activeElement != null && !contentRef.current?.contains(activeElement) ? activeElement : null;
|
|
230
263
|
layer.show();
|
|
231
|
-
|
|
264
|
+
// When the content is already mounted, the layer shows it within this
|
|
265
|
+
// call, so initial focus lands within the event that opened it: iOS
|
|
266
|
+
// raises its keyboard only for a focus made while the tap is still
|
|
267
|
+
// being handled. The frame in the effect above stays for content that
|
|
268
|
+
// mounts later.
|
|
269
|
+
// Only a content control already rendered takes focus here; content
|
|
270
|
+
// that renders on open (and the dialog-surface fallback) waits for the
|
|
271
|
+
// effect's frame.
|
|
272
|
+
const container = contentRef.current;
|
|
273
|
+
if (hasShownRef.current && hasAutoFocus && !skipAutoFocusRef.current && container != null && !container.contains(container.ownerDocument.activeElement) && focusFirstContentControl(container)) {
|
|
274
|
+
// Initial focus is done. The effect's frame stays only as a fallback
|
|
275
|
+
// for focus lost to the page (content swapped in the same handler
|
|
276
|
+
// unmounts the control just focused); it must not move focus the user
|
|
277
|
+
// has since moved elsewhere.
|
|
278
|
+
hasFocusedInGestureRef.current = true;
|
|
279
|
+
}
|
|
280
|
+
}, [layer, hasAutoFocus, contentRef]);
|
|
232
281
|
|
|
233
282
|
// Toggle function. Opening delegates to show so every route inherits the
|
|
234
283
|
// same-gesture reopen guard owned by Layer.
|
|
@@ -70,7 +70,7 @@ export interface ProgressBarProps extends BaseProps<HTMLDivElement> {
|
|
|
70
70
|
* When true, renders an animated indeterminate progress indicator.
|
|
71
71
|
* Use when the progress amount is unknown (e.g. loading, processing).
|
|
72
72
|
* The `value` and `hasValueLabel` props are ignored in this mode.
|
|
73
|
-
*
|
|
73
|
+
* Under reduced motion it pulses in place instead of sliding.
|
|
74
74
|
* @default false
|
|
75
75
|
*/
|
|
76
76
|
isIndeterminate?: boolean;
|