@octanejs/aria 0.0.1
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/README.md +26 -0
- package/package.json +60 -0
- package/src/index.ts +52 -0
- package/src/interactions/PressResponder.ts +54 -0
- package/src/interactions/Pressable.ts +37 -0
- package/src/interactions/context.ts +20 -0
- package/src/interactions/createEventHandler.ts +94 -0
- package/src/interactions/focusSafely.ts +40 -0
- package/src/interactions/textSelection.ts +96 -0
- package/src/interactions/useFocus.ts +114 -0
- package/src/interactions/useFocusVisible.ts +456 -0
- package/src/interactions/useFocusWithin.ts +165 -0
- package/src/interactions/useFocusable.ts +205 -0
- package/src/interactions/useHover.ts +268 -0
- package/src/interactions/useInteractOutside.ts +161 -0
- package/src/interactions/useKeyboard.ts +50 -0
- package/src/interactions/useLongPress.ts +159 -0
- package/src/interactions/useMove.ts +281 -0
- package/src/interactions/usePress.ts +1249 -0
- package/src/interactions/useScrollWheel.ts +55 -0
- package/src/interactions/utils.ts +212 -0
- package/src/internal.ts +57 -0
- package/src/ssr/SSRProvider.ts +72 -0
- package/src/stately/flags.ts +21 -0
- package/src/stately/index.ts +7 -0
- package/src/stately/utils/number.ts +70 -0
- package/src/stately/utils/useControlledState.ts +75 -0
- package/src/utils/chain.ts +14 -0
- package/src/utils/constants.ts +5 -0
- package/src/utils/domHelpers.ts +36 -0
- package/src/utils/focusWithoutScrolling.ts +83 -0
- package/src/utils/getNonce.ts +53 -0
- package/src/utils/isElementVisible.ts +66 -0
- package/src/utils/isFocusable.ts +57 -0
- package/src/utils/isScrollable.ts +21 -0
- package/src/utils/isVirtualEvent.ts +47 -0
- package/src/utils/mergeProps.ts +76 -0
- package/src/utils/mergeRefs.ts +48 -0
- package/src/utils/openLink.ts +231 -0
- package/src/utils/platform.ts +74 -0
- package/src/utils/runAfterTransition.ts +114 -0
- package/src/utils/shadowdom/DOMFunctions.ts +100 -0
- package/src/utils/shadowdom/ShadowTreeWalker.ts +303 -0
- package/src/utils/useDescription.ts +62 -0
- package/src/utils/useEffectEvent.ts +34 -0
- package/src/utils/useEvent.ts +55 -0
- package/src/utils/useGlobalListeners.ts +91 -0
- package/src/utils/useId.ts +150 -0
- package/src/utils/useLayoutEffect.ts +7 -0
- package/src/utils/useObjectRef.ts +88 -0
- package/src/utils/useSyncRef.ts +39 -0
- package/src/utils/useValueEffect.ts +81 -0
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
// Ported from react-aria (source: .react-spectrum/packages/react-aria/src/interactions/useFocusVisible.ts).
|
|
2
|
+
// octane adaptations:
|
|
3
|
+
// - React's KeyboardEvent/FocusEvent handler types → native events throughout (module-level
|
|
4
|
+
// listeners already operated on native events upstream).
|
|
5
|
+
// - `FOCUS_VISIBLE_INPUT_KEYS` is typed Record<string, boolean> for native `e.key` indexing.
|
|
6
|
+
// - Public-hook slot threading (splitSlot/subSlot) per the binding convention;
|
|
7
|
+
// `useFocusVisibleListener`'s explicit user deps array is passed through unchanged.
|
|
8
|
+
|
|
9
|
+
// Portions of the code in this file are based on code from react.
|
|
10
|
+
// Original licensing for the following can be found in the
|
|
11
|
+
// NOTICE file in the root directory of this source tree.
|
|
12
|
+
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
|
|
13
|
+
|
|
14
|
+
import type { PointerType } from '@react-types/shared';
|
|
15
|
+
import { useEffect, useState } from 'octane';
|
|
16
|
+
|
|
17
|
+
import { S, splitSlot, subSlot } from '../internal';
|
|
18
|
+
import { getActiveElement, getEventTarget } from '../utils/shadowdom/DOMFunctions';
|
|
19
|
+
import { getOwnerDocument, getOwnerWindow } from '../utils/domHelpers';
|
|
20
|
+
import { ignoreFocusEvent } from './utils';
|
|
21
|
+
import { isMac } from '../utils/platform';
|
|
22
|
+
import { isVirtualClick } from '../utils/isVirtualEvent';
|
|
23
|
+
import { openLink } from '../utils/openLink';
|
|
24
|
+
import { useIsSSR } from '../ssr/SSRProvider';
|
|
25
|
+
|
|
26
|
+
export type Modality = 'keyboard' | 'pointer' | 'virtual';
|
|
27
|
+
type HandlerEvent = PointerEvent | MouseEvent | KeyboardEvent | FocusEvent | null;
|
|
28
|
+
type Handler = (modality: Modality, e: HandlerEvent) => void;
|
|
29
|
+
export type FocusVisibleHandler = (isFocusVisible: boolean) => void;
|
|
30
|
+
export interface FocusVisibleProps {
|
|
31
|
+
/** Whether the element is a text input. */
|
|
32
|
+
isTextInput?: boolean;
|
|
33
|
+
/** Whether the element will be auto focused. */
|
|
34
|
+
autoFocus?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface FocusVisibleResult {
|
|
38
|
+
/** Whether keyboard focus is visible globally. */
|
|
39
|
+
isFocusVisible: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let currentModality: null | Modality = null;
|
|
43
|
+
let currentPointerType: PointerType = 'keyboard';
|
|
44
|
+
export const changeHandlers = new Set<Handler>();
|
|
45
|
+
interface GlobalListenerData {
|
|
46
|
+
focus: () => void;
|
|
47
|
+
}
|
|
48
|
+
export let hasSetupGlobalListeners: Map<Window, GlobalListenerData> = new Map<
|
|
49
|
+
Window,
|
|
50
|
+
GlobalListenerData
|
|
51
|
+
>(); // We use a map here to support setting event listeners across multiple document objects.
|
|
52
|
+
let hasEventBeforeFocus = false;
|
|
53
|
+
let hasBlurredWindowRecently = false;
|
|
54
|
+
|
|
55
|
+
// Only Tab or Esc keys will make focus visible on text input elements
|
|
56
|
+
const FOCUS_VISIBLE_INPUT_KEYS: Record<string, boolean> = {
|
|
57
|
+
Tab: true,
|
|
58
|
+
Escape: true,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function triggerChangeHandlers(modality: Modality, e: HandlerEvent) {
|
|
62
|
+
for (let handler of changeHandlers) {
|
|
63
|
+
handler(modality, e);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Helper function to determine if a KeyboardEvent is unmodified and could make keyboard focus
|
|
69
|
+
* styles visible.
|
|
70
|
+
*/
|
|
71
|
+
function isValidKey(e: KeyboardEvent) {
|
|
72
|
+
// Control and Shift keys trigger when navigating back to the tab with keyboard.
|
|
73
|
+
return !(
|
|
74
|
+
e.metaKey ||
|
|
75
|
+
(!isMac() && e.altKey) ||
|
|
76
|
+
e.ctrlKey ||
|
|
77
|
+
e.key === 'Control' ||
|
|
78
|
+
e.key === 'Shift' ||
|
|
79
|
+
e.key === 'Meta'
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function handleKeyboardEvent(e: KeyboardEvent) {
|
|
84
|
+
hasEventBeforeFocus = true;
|
|
85
|
+
if (!(openLink as any).isOpening && isValidKey(e)) {
|
|
86
|
+
currentModality = 'keyboard';
|
|
87
|
+
currentPointerType = 'keyboard';
|
|
88
|
+
triggerChangeHandlers('keyboard', e);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function handlePointerEvent(e: PointerEvent | MouseEvent) {
|
|
93
|
+
currentModality = 'pointer';
|
|
94
|
+
currentPointerType = 'pointerType' in e ? (e.pointerType as PointerType) : 'mouse';
|
|
95
|
+
if (e.type === 'mousedown' || e.type === 'pointerdown') {
|
|
96
|
+
hasEventBeforeFocus = true;
|
|
97
|
+
triggerChangeHandlers('pointer', e);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function handleClickEvent(e: MouseEvent) {
|
|
102
|
+
if (!(openLink as any).isOpening && isVirtualClick(e)) {
|
|
103
|
+
hasEventBeforeFocus = true;
|
|
104
|
+
currentModality = 'virtual';
|
|
105
|
+
currentPointerType = 'virtual';
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function handleFocusEvent(e: FocusEvent) {
|
|
110
|
+
// Firefox fires two extra focus events when the user first clicks into an iframe:
|
|
111
|
+
// first on the window, then on the document. We ignore these events so they don't
|
|
112
|
+
// cause keyboard focus rings to appear.
|
|
113
|
+
let ownerWindow = getOwnerWindow(getEventTarget(e) as Element);
|
|
114
|
+
let ownerDocument = getOwnerDocument(getEventTarget(e) as Element);
|
|
115
|
+
if (
|
|
116
|
+
getEventTarget(e) === ownerWindow ||
|
|
117
|
+
getEventTarget(e) === ownerDocument ||
|
|
118
|
+
ignoreFocusEvent ||
|
|
119
|
+
!e.isTrusted
|
|
120
|
+
) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// If a focus event occurs without a preceding keyboard or pointer event, switch to virtual modality.
|
|
125
|
+
// This occurs, for example, when navigating a form with the next/previous buttons on iOS.
|
|
126
|
+
if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
|
|
127
|
+
currentModality = 'virtual';
|
|
128
|
+
currentPointerType = 'virtual';
|
|
129
|
+
triggerChangeHandlers('virtual', e);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
hasEventBeforeFocus = false;
|
|
133
|
+
hasBlurredWindowRecently = false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function handleWindowBlur() {
|
|
137
|
+
if (ignoreFocusEvent) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// When the window is blurred, reset state. This is necessary when tabbing out of the window,
|
|
142
|
+
// for example, since a subsequent focus event won't be fired.
|
|
143
|
+
hasEventBeforeFocus = false;
|
|
144
|
+
hasBlurredWindowRecently = true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Setup global event listeners to control when keyboard focus style should be visible.
|
|
149
|
+
*/
|
|
150
|
+
function setupGlobalFocusEvents(element?: HTMLElement | null) {
|
|
151
|
+
if (typeof window === 'undefined' || typeof document === 'undefined') {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const windowObject = getOwnerWindow(element);
|
|
156
|
+
const documentObject = getOwnerDocument(element);
|
|
157
|
+
|
|
158
|
+
if (hasSetupGlobalListeners.get(windowObject)) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Programmatic focus() calls shouldn't affect the current input modality.
|
|
163
|
+
// However, we need to detect other cases when a focus event occurs without
|
|
164
|
+
// a preceding user event (e.g. screen reader focus). Overriding the focus
|
|
165
|
+
// method on HTMLElement.prototype is a bit hacky, but works.
|
|
166
|
+
// defineProperty (not assignment) so this works even if `focus` is currently
|
|
167
|
+
// a getter-only accessor — e.g. when @testing-library/user-event's setup()
|
|
168
|
+
// has instrumented it. Plain assignment throws in that case.
|
|
169
|
+
let focus = windowObject.HTMLElement.prototype.focus;
|
|
170
|
+
Reflect.defineProperty(windowObject.HTMLElement.prototype, 'focus', {
|
|
171
|
+
configurable: true,
|
|
172
|
+
writable: true,
|
|
173
|
+
value: function (this: HTMLElement) {
|
|
174
|
+
hasEventBeforeFocus = true;
|
|
175
|
+
focus.apply(this, arguments as unknown as [options?: FocusOptions | undefined]);
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
documentObject.addEventListener('keydown', handleKeyboardEvent, true);
|
|
180
|
+
documentObject.addEventListener('keyup', handleKeyboardEvent, true);
|
|
181
|
+
documentObject.addEventListener('click', handleClickEvent, true);
|
|
182
|
+
|
|
183
|
+
// Register focus events on the window so they are sure to happen
|
|
184
|
+
// before the framework's event listeners (registered on the document).
|
|
185
|
+
windowObject.addEventListener('focus', handleFocusEvent, true);
|
|
186
|
+
windowObject.addEventListener('blur', handleWindowBlur, false);
|
|
187
|
+
|
|
188
|
+
if (typeof PointerEvent !== 'undefined') {
|
|
189
|
+
documentObject.addEventListener('pointerdown', handlePointerEvent, true);
|
|
190
|
+
documentObject.addEventListener('pointermove', handlePointerEvent, true);
|
|
191
|
+
documentObject.addEventListener('pointerup', handlePointerEvent, true);
|
|
192
|
+
} else if (process.env.NODE_ENV === 'test') {
|
|
193
|
+
documentObject.addEventListener('mousedown', handlePointerEvent, true);
|
|
194
|
+
documentObject.addEventListener('mousemove', handlePointerEvent, true);
|
|
195
|
+
documentObject.addEventListener('mouseup', handlePointerEvent, true);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Add unmount handler
|
|
199
|
+
windowObject.addEventListener(
|
|
200
|
+
'beforeunload',
|
|
201
|
+
() => {
|
|
202
|
+
tearDownWindowFocusTracking(element);
|
|
203
|
+
},
|
|
204
|
+
{ once: true },
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
hasSetupGlobalListeners.set(windowObject, { focus });
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const tearDownWindowFocusTracking = (element?: HTMLElement | null, loadListener?: () => void) => {
|
|
211
|
+
const windowObject = getOwnerWindow(element);
|
|
212
|
+
const documentObject = getOwnerDocument(element);
|
|
213
|
+
if (loadListener) {
|
|
214
|
+
documentObject.removeEventListener('DOMContentLoaded', loadListener);
|
|
215
|
+
}
|
|
216
|
+
if (!hasSetupGlobalListeners.has(windowObject)) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
Reflect.defineProperty(windowObject.HTMLElement.prototype, 'focus', {
|
|
220
|
+
configurable: true,
|
|
221
|
+
writable: true,
|
|
222
|
+
value: hasSetupGlobalListeners.get(windowObject)!.focus,
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
documentObject.removeEventListener('keydown', handleKeyboardEvent, true);
|
|
226
|
+
documentObject.removeEventListener('keyup', handleKeyboardEvent, true);
|
|
227
|
+
documentObject.removeEventListener('click', handleClickEvent, true);
|
|
228
|
+
|
|
229
|
+
windowObject.removeEventListener('focus', handleFocusEvent, true);
|
|
230
|
+
windowObject.removeEventListener('blur', handleWindowBlur, false);
|
|
231
|
+
|
|
232
|
+
if (typeof PointerEvent !== 'undefined') {
|
|
233
|
+
documentObject.removeEventListener('pointerdown', handlePointerEvent, true);
|
|
234
|
+
documentObject.removeEventListener('pointermove', handlePointerEvent, true);
|
|
235
|
+
documentObject.removeEventListener('pointerup', handlePointerEvent, true);
|
|
236
|
+
} else if (process.env.NODE_ENV === 'test') {
|
|
237
|
+
documentObject.removeEventListener('mousedown', handlePointerEvent, true);
|
|
238
|
+
documentObject.removeEventListener('mousemove', handlePointerEvent, true);
|
|
239
|
+
documentObject.removeEventListener('mouseup', handlePointerEvent, true);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
hasSetupGlobalListeners.delete(windowObject);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* EXPERIMENTAL
|
|
247
|
+
* Adds a window (i.e. iframe) to the list of windows that are being tracked for focus visible.
|
|
248
|
+
*
|
|
249
|
+
* Sometimes apps render portions of their tree into an iframe. In this case, we cannot accurately
|
|
250
|
+
* track if the focus is visible because we cannot see interactions inside the iframe. If you have
|
|
251
|
+
* this in your application's architecture, then this function will attach event listeners inside
|
|
252
|
+
* the iframe. You should call `addWindowFocusTracking` with an element from inside the window you
|
|
253
|
+
* wish to add. We'll retrieve the relevant elements based on that. Note, you do not need to call
|
|
254
|
+
* this for the default window, as we call it for you.
|
|
255
|
+
*
|
|
256
|
+
* When you are ready to stop listening, but you do not wish to unmount the iframe, you may call the
|
|
257
|
+
* cleanup function returned by `addWindowFocusTracking`. Otherwise, when you unmount the iframe,
|
|
258
|
+
* all listeners and state will be cleaned up automatically for you.
|
|
259
|
+
*
|
|
260
|
+
* @param element @default document.body - The element provided will be used to get the window to
|
|
261
|
+
* add.
|
|
262
|
+
* @returns A function to remove the event listeners and cleanup the state.
|
|
263
|
+
*/
|
|
264
|
+
export function addWindowFocusTracking(element?: HTMLElement | null): () => void {
|
|
265
|
+
const documentObject = getOwnerDocument(element);
|
|
266
|
+
let loadListener: (() => void) | undefined;
|
|
267
|
+
if (documentObject.readyState !== 'loading') {
|
|
268
|
+
setupGlobalFocusEvents(element);
|
|
269
|
+
} else {
|
|
270
|
+
loadListener = () => {
|
|
271
|
+
setupGlobalFocusEvents(element);
|
|
272
|
+
};
|
|
273
|
+
documentObject.addEventListener('DOMContentLoaded', loadListener);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return () => tearDownWindowFocusTracking(element, loadListener);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Server-side rendering does not have the document object defined
|
|
280
|
+
if (typeof document !== 'undefined') {
|
|
281
|
+
addWindowFocusTracking();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* If true, keyboard focus is visible.
|
|
286
|
+
*/
|
|
287
|
+
export function isFocusVisible(): boolean {
|
|
288
|
+
return currentModality !== 'pointer';
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function getInteractionModality(): Modality | null {
|
|
292
|
+
return currentModality;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function setInteractionModality(modality: Modality): void {
|
|
296
|
+
currentModality = modality;
|
|
297
|
+
currentPointerType = modality === 'pointer' ? 'mouse' : modality;
|
|
298
|
+
triggerChangeHandlers(modality, null);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** @private */
|
|
302
|
+
export function getPointerType(): PointerType {
|
|
303
|
+
return currentPointerType;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Keeps state of the current modality.
|
|
308
|
+
*/
|
|
309
|
+
export function useInteractionModality(): Modality | null;
|
|
310
|
+
// Slot-threading form: sibling ported hooks pass their derived sub-slot as the trailing arg.
|
|
311
|
+
export function useInteractionModality(slot: symbol | undefined): Modality | null;
|
|
312
|
+
export function useInteractionModality(...args: any[]): Modality | null {
|
|
313
|
+
const [, slotArg] = splitSlot(args);
|
|
314
|
+
const slot = slotArg ?? S('useInteractionModality');
|
|
315
|
+
|
|
316
|
+
setupGlobalFocusEvents();
|
|
317
|
+
|
|
318
|
+
let [modality, setModality] = useState(currentModality, subSlot(slot, 'modality'));
|
|
319
|
+
useEffect(
|
|
320
|
+
() => {
|
|
321
|
+
let handler = () => {
|
|
322
|
+
setModality(currentModality);
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
changeHandlers.add(handler);
|
|
326
|
+
return () => {
|
|
327
|
+
changeHandlers.delete(handler);
|
|
328
|
+
};
|
|
329
|
+
},
|
|
330
|
+
[],
|
|
331
|
+
subSlot(slot, 'subscribe'),
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
return useIsSSR(subSlot(slot, 'ssr')) ? null : modality;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const nonTextInputTypes = new Set([
|
|
338
|
+
'checkbox',
|
|
339
|
+
'radio',
|
|
340
|
+
'range',
|
|
341
|
+
'color',
|
|
342
|
+
'file',
|
|
343
|
+
'image',
|
|
344
|
+
'button',
|
|
345
|
+
'submit',
|
|
346
|
+
'reset',
|
|
347
|
+
]);
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* If this is attached to text input component, return if the event is a focus event (Tab/Escape
|
|
351
|
+
* keys pressed) so that focus visible style can be properly set.
|
|
352
|
+
*/
|
|
353
|
+
function isKeyboardFocusEvent(isTextInput: boolean, modality: Modality, e: HandlerEvent) {
|
|
354
|
+
let eventTarget = e ? (getEventTarget(e) as Element) : undefined;
|
|
355
|
+
let document = getOwnerDocument(eventTarget);
|
|
356
|
+
let ownerWindow = getOwnerWindow(eventTarget);
|
|
357
|
+
const IHTMLInputElement =
|
|
358
|
+
typeof ownerWindow !== 'undefined' ? ownerWindow.HTMLInputElement : HTMLInputElement;
|
|
359
|
+
const IHTMLTextAreaElement =
|
|
360
|
+
typeof ownerWindow !== 'undefined' ? ownerWindow.HTMLTextAreaElement : HTMLTextAreaElement;
|
|
361
|
+
const IHTMLElement = typeof ownerWindow !== 'undefined' ? ownerWindow.HTMLElement : HTMLElement;
|
|
362
|
+
const IKeyboardEvent =
|
|
363
|
+
typeof ownerWindow !== 'undefined' ? ownerWindow.KeyboardEvent : KeyboardEvent;
|
|
364
|
+
|
|
365
|
+
// For keyboard events that occur on a non-input element that will move focus into input element (aka ArrowLeft going from Datepicker button to the main input group)
|
|
366
|
+
// we need to rely on the user passing isTextInput into here. This way we can skip toggling focus visiblity for said input element
|
|
367
|
+
let activeElement = getActiveElement(document);
|
|
368
|
+
isTextInput =
|
|
369
|
+
isTextInput ||
|
|
370
|
+
(activeElement instanceof IHTMLInputElement && !nonTextInputTypes.has(activeElement.type)) ||
|
|
371
|
+
activeElement instanceof IHTMLTextAreaElement ||
|
|
372
|
+
(activeElement instanceof IHTMLElement && activeElement.isContentEditable);
|
|
373
|
+
return !(
|
|
374
|
+
isTextInput &&
|
|
375
|
+
modality === 'keyboard' &&
|
|
376
|
+
e instanceof IKeyboardEvent &&
|
|
377
|
+
!FOCUS_VISIBLE_INPUT_KEYS[e.key]
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Manages focus visible state for the page, and subscribes individual components for updates.
|
|
383
|
+
*/
|
|
384
|
+
export function useFocusVisible(props?: FocusVisibleProps): FocusVisibleResult;
|
|
385
|
+
// Slot-threading form: sibling ported hooks pass their derived sub-slot as the trailing arg.
|
|
386
|
+
export function useFocusVisible(
|
|
387
|
+
props: FocusVisibleProps | undefined,
|
|
388
|
+
slot: symbol | undefined,
|
|
389
|
+
): FocusVisibleResult;
|
|
390
|
+
export function useFocusVisible(...args: any[]): FocusVisibleResult {
|
|
391
|
+
const [user, slotArg] = splitSlot(args);
|
|
392
|
+
const slot = slotArg ?? S('useFocusVisible');
|
|
393
|
+
const props = (user[0] as FocusVisibleProps) ?? {};
|
|
394
|
+
|
|
395
|
+
let { isTextInput, autoFocus } = props;
|
|
396
|
+
let [isFocusVisibleState, setFocusVisible] = useState(
|
|
397
|
+
autoFocus || isFocusVisible(),
|
|
398
|
+
subSlot(slot, 'state'),
|
|
399
|
+
);
|
|
400
|
+
useFocusVisibleListener(
|
|
401
|
+
(isFocusVisible) => {
|
|
402
|
+
setFocusVisible(isFocusVisible);
|
|
403
|
+
},
|
|
404
|
+
[isTextInput],
|
|
405
|
+
{ isTextInput },
|
|
406
|
+
subSlot(slot, 'listener'),
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
return { isFocusVisible: isFocusVisibleState };
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Listens for trigger change and reports if focus is visible (i.e., modality is not pointer).
|
|
414
|
+
*/
|
|
415
|
+
export function useFocusVisibleListener(
|
|
416
|
+
fn: FocusVisibleHandler,
|
|
417
|
+
deps: ReadonlyArray<any>,
|
|
418
|
+
opts?: { enabled?: boolean; isTextInput?: boolean },
|
|
419
|
+
): void;
|
|
420
|
+
// Slot-threading form: sibling ported hooks pass their derived sub-slot as the trailing arg.
|
|
421
|
+
export function useFocusVisibleListener(
|
|
422
|
+
fn: FocusVisibleHandler,
|
|
423
|
+
deps: ReadonlyArray<any>,
|
|
424
|
+
opts: { enabled?: boolean; isTextInput?: boolean } | undefined,
|
|
425
|
+
slot: symbol | undefined,
|
|
426
|
+
): void;
|
|
427
|
+
export function useFocusVisibleListener(...args: any[]): void {
|
|
428
|
+
const [user, slotArg] = splitSlot(args);
|
|
429
|
+
const slot = slotArg ?? S('useFocusVisibleListener');
|
|
430
|
+
const fn = user[0] as FocusVisibleHandler;
|
|
431
|
+
const deps = user[1] as any[];
|
|
432
|
+
const opts = user[2] as { enabled?: boolean; isTextInput?: boolean } | undefined;
|
|
433
|
+
|
|
434
|
+
setupGlobalFocusEvents();
|
|
435
|
+
|
|
436
|
+
useEffect(
|
|
437
|
+
() => {
|
|
438
|
+
if (opts?.enabled === false) {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
let handler = (modality: Modality, e: HandlerEvent) => {
|
|
442
|
+
// We want to early return for any keyboard events that occur inside text inputs EXCEPT for Tab and Escape
|
|
443
|
+
if (!isKeyboardFocusEvent(!!opts?.isTextInput, modality, e)) {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
fn(isFocusVisible());
|
|
447
|
+
};
|
|
448
|
+
changeHandlers.add(handler);
|
|
449
|
+
return () => {
|
|
450
|
+
changeHandlers.delete(handler);
|
|
451
|
+
};
|
|
452
|
+
},
|
|
453
|
+
deps,
|
|
454
|
+
subSlot(slot, 'listen'),
|
|
455
|
+
);
|
|
456
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// Ported from react-aria (source: .react-spectrum/packages/react-aria/src/interactions/useFocusWithin.ts).
|
|
2
|
+
// octane adaptations:
|
|
3
|
+
// - Handlers receive NATIVE FocusEvents (React's FocusEvent type → native FocusEvent);
|
|
4
|
+
// `DOMAttributes` from '@react-types/shared' becomes a local structural alias.
|
|
5
|
+
// - `e.currentTarget` is EventTarget-typed on native events; casts to Element where
|
|
6
|
+
// upstream relied on React's element-typed currentTarget.
|
|
7
|
+
// - Public-hook slot threading (splitSlot/subSlot) per the binding convention.
|
|
8
|
+
|
|
9
|
+
// Portions of the code in this file are based on code from react.
|
|
10
|
+
// Original licensing for the following can be found in the
|
|
11
|
+
// NOTICE file in the root directory of this source tree.
|
|
12
|
+
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
|
|
13
|
+
|
|
14
|
+
import { useCallback, useRef } from 'octane';
|
|
15
|
+
|
|
16
|
+
import { S, splitSlot, subSlot } from '../internal';
|
|
17
|
+
import { createSyntheticEvent, setEventTarget, useSyntheticBlurEvent } from './utils';
|
|
18
|
+
import { getActiveElement, getEventTarget, nodeContains } from '../utils/shadowdom/DOMFunctions';
|
|
19
|
+
import { getOwnerDocument } from '../utils/domHelpers';
|
|
20
|
+
import { useGlobalListeners } from '../utils/useGlobalListeners';
|
|
21
|
+
|
|
22
|
+
// octane adaptation: minimal structural DOMAttributes (upstream's drags React attribute types).
|
|
23
|
+
export type DOMAttributes = Record<string, any>;
|
|
24
|
+
|
|
25
|
+
export interface FocusWithinProps {
|
|
26
|
+
/** Whether the focus within events should be disabled. */
|
|
27
|
+
isDisabled?: boolean;
|
|
28
|
+
/** Handler that is called when the target element or a descendant receives focus. */
|
|
29
|
+
onFocusWithin?: (e: FocusEvent) => void;
|
|
30
|
+
/** Handler that is called when the target element and all descendants lose focus. */
|
|
31
|
+
onBlurWithin?: (e: FocusEvent) => void;
|
|
32
|
+
/** Handler that is called when the the focus within state changes. */
|
|
33
|
+
onFocusWithinChange?: (isFocusWithin: boolean) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface FocusWithinResult {
|
|
37
|
+
/** Props to spread onto the target element. */
|
|
38
|
+
focusWithinProps: DOMAttributes;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Handles focus events for the target and its descendants.
|
|
43
|
+
*/
|
|
44
|
+
export function useFocusWithin(props: FocusWithinProps): FocusWithinResult;
|
|
45
|
+
// Slot-threading form: sibling ported hooks pass their derived sub-slot as the trailing arg.
|
|
46
|
+
export function useFocusWithin(
|
|
47
|
+
props: FocusWithinProps,
|
|
48
|
+
slot: symbol | undefined,
|
|
49
|
+
): FocusWithinResult;
|
|
50
|
+
export function useFocusWithin(...args: any[]): FocusWithinResult {
|
|
51
|
+
const [user, slotArg] = splitSlot(args);
|
|
52
|
+
const slot = slotArg ?? S('useFocusWithin');
|
|
53
|
+
const props = user[0] as FocusWithinProps;
|
|
54
|
+
|
|
55
|
+
let { isDisabled, onBlurWithin, onFocusWithin, onFocusWithinChange } = props;
|
|
56
|
+
let state = useRef(
|
|
57
|
+
{
|
|
58
|
+
isFocusWithin: false,
|
|
59
|
+
},
|
|
60
|
+
subSlot(slot, 'state'),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
let { addGlobalListener, removeAllGlobalListeners } = useGlobalListeners(subSlot(slot, 'global'));
|
|
64
|
+
|
|
65
|
+
let onBlur = useCallback(
|
|
66
|
+
(e: FocusEvent) => {
|
|
67
|
+
// Ignore events bubbling through portals.
|
|
68
|
+
if (!nodeContains(e.currentTarget as Element, getEventTarget(e) as Element)) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// We don't want to trigger onBlurWithin and then immediately onFocusWithin again
|
|
73
|
+
// when moving focus inside the element. Only trigger if the currentTarget doesn't
|
|
74
|
+
// include the relatedTarget (where focus is moving).
|
|
75
|
+
if (
|
|
76
|
+
state.current.isFocusWithin &&
|
|
77
|
+
!nodeContains(e.currentTarget as Element, e.relatedTarget as Element)
|
|
78
|
+
) {
|
|
79
|
+
state.current.isFocusWithin = false;
|
|
80
|
+
removeAllGlobalListeners();
|
|
81
|
+
|
|
82
|
+
if (onBlurWithin) {
|
|
83
|
+
onBlurWithin(e);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (onFocusWithinChange) {
|
|
87
|
+
onFocusWithinChange(false);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
[onBlurWithin, onFocusWithinChange, state, removeAllGlobalListeners],
|
|
92
|
+
subSlot(slot, 'blur'),
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
let onSyntheticFocus = useSyntheticBlurEvent(onBlur, subSlot(slot, 'syntheticBlur'));
|
|
96
|
+
let onFocus = useCallback(
|
|
97
|
+
(e: FocusEvent) => {
|
|
98
|
+
// Ignore events bubbling through portals.
|
|
99
|
+
if (!nodeContains(e.currentTarget as Element, getEventTarget(e) as Element)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Double check that document.activeElement actually matches e.target in case a previously chained
|
|
104
|
+
// focus handler already moved focus somewhere else.
|
|
105
|
+
let eventTarget = getEventTarget(e);
|
|
106
|
+
const ownerDocument = getOwnerDocument(eventTarget as Element);
|
|
107
|
+
const activeElement = getActiveElement(ownerDocument);
|
|
108
|
+
if (!state.current.isFocusWithin && activeElement === eventTarget) {
|
|
109
|
+
if (onFocusWithin) {
|
|
110
|
+
onFocusWithin(e);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (onFocusWithinChange) {
|
|
114
|
+
onFocusWithinChange(true);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
state.current.isFocusWithin = true;
|
|
118
|
+
onSyntheticFocus(e);
|
|
119
|
+
|
|
120
|
+
// Browsers don't fire blur events when elements are removed from the DOM.
|
|
121
|
+
// However, if a focus event occurs outside the element we're tracking, we
|
|
122
|
+
// can manually fire onBlur.
|
|
123
|
+
let currentTarget = e.currentTarget as Element;
|
|
124
|
+
addGlobalListener(
|
|
125
|
+
ownerDocument,
|
|
126
|
+
'focus',
|
|
127
|
+
(e: FocusEvent) => {
|
|
128
|
+
let eventTarget = getEventTarget(e);
|
|
129
|
+
if (
|
|
130
|
+
state.current.isFocusWithin &&
|
|
131
|
+
!nodeContains(currentTarget, eventTarget as Element)
|
|
132
|
+
) {
|
|
133
|
+
let nativeEvent = new ownerDocument.defaultView!.FocusEvent('blur', {
|
|
134
|
+
relatedTarget: eventTarget,
|
|
135
|
+
});
|
|
136
|
+
setEventTarget(nativeEvent, currentTarget);
|
|
137
|
+
let event = createSyntheticEvent(nativeEvent);
|
|
138
|
+
onBlur(event);
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
{ capture: true },
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
[onFocusWithin, onFocusWithinChange, onSyntheticFocus, addGlobalListener, onBlur],
|
|
146
|
+
subSlot(slot, 'focus'),
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
if (isDisabled) {
|
|
150
|
+
return {
|
|
151
|
+
focusWithinProps: {
|
|
152
|
+
// These cannot be null, that would conflict in mergeProps
|
|
153
|
+
onFocus: undefined,
|
|
154
|
+
onBlur: undefined,
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
focusWithinProps: {
|
|
161
|
+
onFocus,
|
|
162
|
+
onBlur,
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|