@react-aria/selection 3.12.1 → 3.13.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.
@@ -0,0 +1,673 @@
1
+ import {useRef as $eCAIO$useRef, useEffect as $eCAIO$useEffect, useMemo as $eCAIO$useMemo} from "react";
2
+ import {getFocusableTreeWalker as $eCAIO$getFocusableTreeWalker, focusSafely as $eCAIO$focusSafely} from "@react-aria/focus";
3
+ import {focusWithoutScrolling as $eCAIO$focusWithoutScrolling, useEvent as $eCAIO$useEvent, scrollIntoView as $eCAIO$scrollIntoView, scrollIntoViewport as $eCAIO$scrollIntoViewport, mergeProps as $eCAIO$mergeProps, isAppleDevice as $eCAIO$isAppleDevice, isMac as $eCAIO$isMac} from "@react-aria/utils";
4
+ import {getInteractionModality as $eCAIO$getInteractionModality, usePress as $eCAIO$usePress, useLongPress as $eCAIO$useLongPress} from "@react-aria/interactions";
5
+ import {useLocale as $eCAIO$useLocale, useCollator as $eCAIO$useCollator} from "@react-aria/i18n";
6
+
7
+ /*
8
+ * Copyright 2020 Adobe. All rights reserved.
9
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License. You may obtain a copy
11
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software distributed under
14
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
15
+ * OF ANY KIND, either express or implied. See the License for the specific language
16
+ * governing permissions and limitations under the License.
17
+ */ /*
18
+ * Copyright 2020 Adobe. All rights reserved.
19
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
20
+ * you may not use this file except in compliance with the License. You may obtain a copy
21
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
22
+ *
23
+ * Unless required by applicable law or agreed to in writing, software distributed under
24
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
25
+ * OF ANY KIND, either express or implied. See the License for the specific language
26
+ * governing permissions and limitations under the License.
27
+ */
28
+
29
+
30
+
31
+ /*
32
+ * Copyright 2020 Adobe. All rights reserved.
33
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
34
+ * you may not use this file except in compliance with the License. You may obtain a copy
35
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
36
+ *
37
+ * Unless required by applicable law or agreed to in writing, software distributed under
38
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
39
+ * OF ANY KIND, either express or implied. See the License for the specific language
40
+ * governing permissions and limitations under the License.
41
+ */
42
+
43
+ function $feb5ffebff200149$export$d3e3bd3e26688c04(e) {
44
+ // Ctrl + Arrow Up/Arrow Down has a system wide meaning on macOS, so use Alt instead.
45
+ // On Windows and Ubuntu, Alt + Space has a system wide meaning.
46
+ return (0, $eCAIO$isAppleDevice)() ? e.altKey : e.ctrlKey;
47
+ }
48
+ function $feb5ffebff200149$export$16792effe837dba3(e) {
49
+ if ((0, $eCAIO$isMac)()) return e.metaKey;
50
+ return e.ctrlKey;
51
+ }
52
+
53
+
54
+
55
+ /*
56
+ * Copyright 2020 Adobe. All rights reserved.
57
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
58
+ * you may not use this file except in compliance with the License. You may obtain a copy
59
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
60
+ *
61
+ * Unless required by applicable law or agreed to in writing, software distributed under
62
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
63
+ * OF ANY KIND, either express or implied. See the License for the specific language
64
+ * governing permissions and limitations under the License.
65
+ */
66
+ /**
67
+ * Controls how long to wait before clearing the typeahead buffer.
68
+ */ const $fb3050f43d946246$var$TYPEAHEAD_DEBOUNCE_WAIT_MS = 1000; // 1 second
69
+ function $fb3050f43d946246$export$e32c88dfddc6e1d8(options) {
70
+ let { keyboardDelegate: keyboardDelegate , selectionManager: selectionManager , onTypeSelect: onTypeSelect } = options;
71
+ let state = (0, $eCAIO$useRef)({
72
+ search: "",
73
+ timeout: null
74
+ }).current;
75
+ let onKeyDown = (e)=>{
76
+ let character = $fb3050f43d946246$var$getStringForKey(e.key);
77
+ if (!character || e.ctrlKey || e.metaKey) return;
78
+ // Do not propagate the Spacebar event if it's meant to be part of the search.
79
+ // When we time out, the search term becomes empty, hence the check on length.
80
+ // Trimming is to account for the case of pressing the Spacebar more than once,
81
+ // which should cycle through the selection/deselection of the focused item.
82
+ if (character === " " && state.search.trim().length > 0) {
83
+ e.preventDefault();
84
+ if (!("continuePropagation" in e)) e.stopPropagation();
85
+ }
86
+ state.search += character;
87
+ // Use the delegate to find a key to focus.
88
+ // Prioritize items after the currently focused item, falling back to searching the whole list.
89
+ let key = keyboardDelegate.getKeyForSearch(state.search, selectionManager.focusedKey);
90
+ // If no key found, search from the top.
91
+ if (key == null) key = keyboardDelegate.getKeyForSearch(state.search);
92
+ if (key != null) {
93
+ selectionManager.setFocusedKey(key);
94
+ if (onTypeSelect) onTypeSelect(key);
95
+ }
96
+ clearTimeout(state.timeout);
97
+ state.timeout = setTimeout(()=>{
98
+ state.search = "";
99
+ }, $fb3050f43d946246$var$TYPEAHEAD_DEBOUNCE_WAIT_MS);
100
+ };
101
+ return {
102
+ typeSelectProps: {
103
+ // Using a capturing listener to catch the keydown event before
104
+ // other hooks in order to handle the Spacebar event.
105
+ onKeyDownCapture: keyboardDelegate.getKeyForSearch ? onKeyDown : null
106
+ }
107
+ };
108
+ }
109
+ function $fb3050f43d946246$var$getStringForKey(key) {
110
+ // If the key is of length 1, it is an ASCII value.
111
+ // Otherwise, if there are no ASCII characters in the key name,
112
+ // it is a Unicode character.
113
+ // See https://www.w3.org/TR/uievents-key/
114
+ if (key.length === 1 || !/^[A-Z]/i.test(key)) return key;
115
+ return "";
116
+ }
117
+
118
+
119
+ function $ae20dd8cbca75726$export$d6daf82dcd84e87c(options) {
120
+ let { selectionManager: manager , keyboardDelegate: delegate , ref: ref , autoFocus: autoFocus = false , shouldFocusWrap: shouldFocusWrap = false , disallowEmptySelection: disallowEmptySelection = false , disallowSelectAll: disallowSelectAll = false , selectOnFocus: selectOnFocus = manager.selectionBehavior === "replace" , disallowTypeAhead: disallowTypeAhead = false , shouldUseVirtualFocus: shouldUseVirtualFocus , allowsTabNavigation: allowsTabNavigation = false , isVirtualized: isVirtualized , scrollRef: // If no scrollRef is provided, assume the collection ref is the scrollable region
121
+ scrollRef = ref } = options;
122
+ let { direction: direction } = (0, $eCAIO$useLocale)();
123
+ let onKeyDown = (e)=>{
124
+ // Prevent option + tab from doing anything since it doesn't move focus to the cells, only buttons/checkboxes
125
+ if (e.altKey && e.key === "Tab") e.preventDefault();
126
+ // Keyboard events bubble through portals. Don't handle keyboard events
127
+ // for elements outside the collection (e.g. menus).
128
+ if (!ref.current.contains(e.target)) return;
129
+ const navigateToKey = (key, childFocus)=>{
130
+ if (key != null) {
131
+ manager.setFocusedKey(key, childFocus);
132
+ if (e.shiftKey && manager.selectionMode === "multiple") manager.extendSelection(key);
133
+ else if (selectOnFocus && !(0, $feb5ffebff200149$export$d3e3bd3e26688c04)(e)) manager.replaceSelection(key);
134
+ }
135
+ };
136
+ switch(e.key){
137
+ case "ArrowDown":
138
+ if (delegate.getKeyBelow) {
139
+ var _delegate_getFirstKey, _delegate_getFirstKey1;
140
+ e.preventDefault();
141
+ let nextKey = manager.focusedKey != null ? delegate.getKeyBelow(manager.focusedKey) : (_delegate_getFirstKey = delegate.getFirstKey) === null || _delegate_getFirstKey === void 0 ? void 0 : _delegate_getFirstKey.call(delegate);
142
+ if (nextKey == null && shouldFocusWrap) nextKey = (_delegate_getFirstKey1 = delegate.getFirstKey) === null || _delegate_getFirstKey1 === void 0 ? void 0 : _delegate_getFirstKey1.call(delegate, manager.focusedKey);
143
+ navigateToKey(nextKey);
144
+ }
145
+ break;
146
+ case "ArrowUp":
147
+ if (delegate.getKeyAbove) {
148
+ var _delegate_getLastKey, _delegate_getLastKey1;
149
+ e.preventDefault();
150
+ let nextKey1 = manager.focusedKey != null ? delegate.getKeyAbove(manager.focusedKey) : (_delegate_getLastKey = delegate.getLastKey) === null || _delegate_getLastKey === void 0 ? void 0 : _delegate_getLastKey.call(delegate);
151
+ if (nextKey1 == null && shouldFocusWrap) nextKey1 = (_delegate_getLastKey1 = delegate.getLastKey) === null || _delegate_getLastKey1 === void 0 ? void 0 : _delegate_getLastKey1.call(delegate, manager.focusedKey);
152
+ navigateToKey(nextKey1);
153
+ }
154
+ break;
155
+ case "ArrowLeft":
156
+ if (delegate.getKeyLeftOf) {
157
+ e.preventDefault();
158
+ let nextKey2 = delegate.getKeyLeftOf(manager.focusedKey);
159
+ navigateToKey(nextKey2, direction === "rtl" ? "first" : "last");
160
+ }
161
+ break;
162
+ case "ArrowRight":
163
+ if (delegate.getKeyRightOf) {
164
+ e.preventDefault();
165
+ let nextKey3 = delegate.getKeyRightOf(manager.focusedKey);
166
+ navigateToKey(nextKey3, direction === "rtl" ? "last" : "first");
167
+ }
168
+ break;
169
+ case "Home":
170
+ if (delegate.getFirstKey) {
171
+ e.preventDefault();
172
+ let firstKey = delegate.getFirstKey(manager.focusedKey, (0, $feb5ffebff200149$export$16792effe837dba3)(e));
173
+ manager.setFocusedKey(firstKey);
174
+ if ((0, $feb5ffebff200149$export$16792effe837dba3)(e) && e.shiftKey && manager.selectionMode === "multiple") manager.extendSelection(firstKey);
175
+ else if (selectOnFocus) manager.replaceSelection(firstKey);
176
+ }
177
+ break;
178
+ case "End":
179
+ if (delegate.getLastKey) {
180
+ e.preventDefault();
181
+ let lastKey = delegate.getLastKey(manager.focusedKey, (0, $feb5ffebff200149$export$16792effe837dba3)(e));
182
+ manager.setFocusedKey(lastKey);
183
+ if ((0, $feb5ffebff200149$export$16792effe837dba3)(e) && e.shiftKey && manager.selectionMode === "multiple") manager.extendSelection(lastKey);
184
+ else if (selectOnFocus) manager.replaceSelection(lastKey);
185
+ }
186
+ break;
187
+ case "PageDown":
188
+ if (delegate.getKeyPageBelow) {
189
+ e.preventDefault();
190
+ let nextKey4 = delegate.getKeyPageBelow(manager.focusedKey);
191
+ navigateToKey(nextKey4);
192
+ }
193
+ break;
194
+ case "PageUp":
195
+ if (delegate.getKeyPageAbove) {
196
+ e.preventDefault();
197
+ let nextKey5 = delegate.getKeyPageAbove(manager.focusedKey);
198
+ navigateToKey(nextKey5);
199
+ }
200
+ break;
201
+ case "a":
202
+ if ((0, $feb5ffebff200149$export$16792effe837dba3)(e) && manager.selectionMode === "multiple" && disallowSelectAll !== true) {
203
+ e.preventDefault();
204
+ manager.selectAll();
205
+ }
206
+ break;
207
+ case "Escape":
208
+ e.preventDefault();
209
+ if (!disallowEmptySelection) manager.clearSelection();
210
+ break;
211
+ case "Tab":
212
+ if (!allowsTabNavigation) {
213
+ // There may be elements that are "tabbable" inside a collection (e.g. in a grid cell).
214
+ // However, collections should be treated as a single tab stop, with arrow key navigation internally.
215
+ // We don't control the rendering of these, so we can't override the tabIndex to prevent tabbing.
216
+ // Instead, we handle the Tab key, and move focus manually to the first/last tabbable element
217
+ // in the collection, so that the browser default behavior will apply starting from that element
218
+ // rather than the currently focused one.
219
+ if (e.shiftKey) ref.current.focus();
220
+ else {
221
+ let walker = (0, $eCAIO$getFocusableTreeWalker)(ref.current, {
222
+ tabbable: true
223
+ });
224
+ let next;
225
+ let last;
226
+ do {
227
+ last = walker.lastChild();
228
+ if (last) next = last;
229
+ }while (last);
230
+ if (next && !next.contains(document.activeElement)) (0, $eCAIO$focusWithoutScrolling)(next);
231
+ }
232
+ break;
233
+ }
234
+ }
235
+ };
236
+ // Store the scroll position so we can restore it later.
237
+ let scrollPos = (0, $eCAIO$useRef)({
238
+ top: 0,
239
+ left: 0
240
+ });
241
+ (0, $eCAIO$useEvent)(scrollRef, "scroll", isVirtualized ? null : ()=>{
242
+ scrollPos.current = {
243
+ top: scrollRef.current.scrollTop,
244
+ left: scrollRef.current.scrollLeft
245
+ };
246
+ });
247
+ let onFocus = (e)=>{
248
+ if (manager.isFocused) {
249
+ // If a focus event bubbled through a portal, reset focus state.
250
+ if (!e.currentTarget.contains(e.target)) manager.setFocused(false);
251
+ return;
252
+ }
253
+ // Focus events can bubble through portals. Ignore these events.
254
+ if (!e.currentTarget.contains(e.target)) return;
255
+ manager.setFocused(true);
256
+ if (manager.focusedKey == null) {
257
+ let navigateToFirstKey = (key)=>{
258
+ if (key != null) {
259
+ manager.setFocusedKey(key);
260
+ if (selectOnFocus) manager.replaceSelection(key);
261
+ }
262
+ };
263
+ // If the user hasn't yet interacted with the collection, there will be no focusedKey set.
264
+ // Attempt to detect whether the user is tabbing forward or backward into the collection
265
+ // and either focus the first or last item accordingly.
266
+ let relatedTarget = e.relatedTarget;
267
+ var _manager_lastSelectedKey, _manager_firstSelectedKey;
268
+ if (relatedTarget && e.currentTarget.compareDocumentPosition(relatedTarget) & Node.DOCUMENT_POSITION_FOLLOWING) navigateToFirstKey((_manager_lastSelectedKey = manager.lastSelectedKey) !== null && _manager_lastSelectedKey !== void 0 ? _manager_lastSelectedKey : delegate.getLastKey());
269
+ else navigateToFirstKey((_manager_firstSelectedKey = manager.firstSelectedKey) !== null && _manager_firstSelectedKey !== void 0 ? _manager_firstSelectedKey : delegate.getFirstKey());
270
+ } else if (!isVirtualized) {
271
+ // Restore the scroll position to what it was before.
272
+ scrollRef.current.scrollTop = scrollPos.current.top;
273
+ scrollRef.current.scrollLeft = scrollPos.current.left;
274
+ // Refocus and scroll the focused item into view if it exists within the scrollable region.
275
+ let element = scrollRef.current.querySelector(`[data-key="${manager.focusedKey}"]`);
276
+ if (element) {
277
+ // This prevents a flash of focus on the first/last element in the collection
278
+ (0, $eCAIO$focusWithoutScrolling)(element);
279
+ (0, $eCAIO$scrollIntoView)(scrollRef.current, element);
280
+ }
281
+ }
282
+ };
283
+ let onBlur = (e)=>{
284
+ // Don't set blurred and then focused again if moving focus within the collection.
285
+ if (!e.currentTarget.contains(e.relatedTarget)) manager.setFocused(false);
286
+ };
287
+ const autoFocusRef = (0, $eCAIO$useRef)(autoFocus);
288
+ (0, $eCAIO$useEffect)(()=>{
289
+ if (autoFocusRef.current) {
290
+ let focusedKey = null;
291
+ // Check focus strategy to determine which item to focus
292
+ if (autoFocus === "first") focusedKey = delegate.getFirstKey();
293
+ if (autoFocus === "last") focusedKey = delegate.getLastKey();
294
+ // If there are any selected keys, make the first one the new focus target
295
+ let selectedKeys = manager.selectedKeys;
296
+ if (selectedKeys.size) focusedKey = selectedKeys.values().next().value;
297
+ manager.setFocused(true);
298
+ manager.setFocusedKey(focusedKey);
299
+ // If no default focus key is selected, focus the collection itself.
300
+ if (focusedKey == null && !shouldUseVirtualFocus) (0, $eCAIO$focusSafely)(ref.current);
301
+ }
302
+ autoFocusRef.current = false;
303
+ // eslint-disable-next-line react-hooks/exhaustive-deps
304
+ }, []);
305
+ // If not virtualized, scroll the focused element into view when the focusedKey changes.
306
+ // When virtualized, Virtualizer handles this internally.
307
+ (0, $eCAIO$useEffect)(()=>{
308
+ let modality = (0, $eCAIO$getInteractionModality)();
309
+ if (!isVirtualized && manager.isFocused && manager.focusedKey != null && (scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current)) {
310
+ let element = scrollRef.current.querySelector(`[data-key="${manager.focusedKey}"]`);
311
+ if (element) {
312
+ (0, $eCAIO$scrollIntoView)(scrollRef.current, element);
313
+ if (modality === "keyboard") (0, $eCAIO$scrollIntoViewport)(element, {
314
+ containingElement: ref.current
315
+ });
316
+ }
317
+ }
318
+ }, [
319
+ isVirtualized,
320
+ scrollRef,
321
+ manager.focusedKey,
322
+ manager.isFocused,
323
+ ref
324
+ ]);
325
+ let handlers = {
326
+ onKeyDown: onKeyDown,
327
+ onFocus: onFocus,
328
+ onBlur: onBlur,
329
+ onMouseDown (e) {
330
+ // Ignore events that bubbled through portals.
331
+ if (scrollRef.current === e.target) // Prevent focus going to the collection when clicking on the scrollbar.
332
+ e.preventDefault();
333
+ }
334
+ };
335
+ let { typeSelectProps: typeSelectProps } = (0, $fb3050f43d946246$export$e32c88dfddc6e1d8)({
336
+ keyboardDelegate: delegate,
337
+ selectionManager: manager
338
+ });
339
+ if (!disallowTypeAhead) handlers = (0, $eCAIO$mergeProps)(typeSelectProps, handlers);
340
+ // If nothing is focused within the collection, make the collection itself tabbable.
341
+ // This will be marshalled to either the first or last item depending on where focus came from.
342
+ // If using virtual focus, don't set a tabIndex at all so that VoiceOver on iOS 14 doesn't try
343
+ // to move real DOM focus to the element anyway.
344
+ let tabIndex;
345
+ if (!shouldUseVirtualFocus) tabIndex = manager.focusedKey == null ? 0 : -1;
346
+ return {
347
+ collectionProps: {
348
+ ...handlers,
349
+ tabIndex: tabIndex
350
+ }
351
+ };
352
+ }
353
+
354
+
355
+ /*
356
+ * Copyright 2020 Adobe. All rights reserved.
357
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
358
+ * you may not use this file except in compliance with the License. You may obtain a copy
359
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
360
+ *
361
+ * Unless required by applicable law or agreed to in writing, software distributed under
362
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
363
+ * OF ANY KIND, either express or implied. See the License for the specific language
364
+ * governing permissions and limitations under the License.
365
+ */
366
+
367
+
368
+
369
+
370
+ function $880e95eb8b93ba9a$export$ecf600387e221c37(options) {
371
+ let { selectionManager: manager , key: key , ref: ref , shouldSelectOnPressUp: shouldSelectOnPressUp , isVirtualized: isVirtualized , shouldUseVirtualFocus: shouldUseVirtualFocus , focus: focus , isDisabled: isDisabled , onAction: onAction , allowsDifferentPressOrigin: allowsDifferentPressOrigin } = options;
372
+ let onSelect = (e)=>{
373
+ if (e.pointerType === "keyboard" && (0, $feb5ffebff200149$export$d3e3bd3e26688c04)(e)) manager.toggleSelection(key);
374
+ else {
375
+ if (manager.selectionMode === "none") return;
376
+ if (manager.selectionMode === "single") {
377
+ if (manager.isSelected(key) && !manager.disallowEmptySelection) manager.toggleSelection(key);
378
+ else manager.replaceSelection(key);
379
+ } else if (e && e.shiftKey) manager.extendSelection(key);
380
+ else if (manager.selectionBehavior === "toggle" || e && ((0, $feb5ffebff200149$export$16792effe837dba3)(e) || e.pointerType === "touch" || e.pointerType === "virtual")) // if touch or virtual (VO) then we just want to toggle, otherwise it's impossible to multi select because they don't have modifier keys
381
+ manager.toggleSelection(key);
382
+ else manager.replaceSelection(key);
383
+ }
384
+ };
385
+ // Focus the associated DOM node when this item becomes the focusedKey
386
+ (0, $eCAIO$useEffect)(()=>{
387
+ let isFocused = key === manager.focusedKey;
388
+ if (isFocused && manager.isFocused && !shouldUseVirtualFocus && document.activeElement !== ref.current) {
389
+ if (focus) focus();
390
+ else (0, $eCAIO$focusSafely)(ref.current);
391
+ }
392
+ // eslint-disable-next-line react-hooks/exhaustive-deps
393
+ }, [
394
+ ref,
395
+ key,
396
+ manager.focusedKey,
397
+ manager.childFocusStrategy,
398
+ manager.isFocused,
399
+ shouldUseVirtualFocus
400
+ ]);
401
+ isDisabled = isDisabled || manager.isDisabled(key);
402
+ // Set tabIndex to 0 if the element is focused, or -1 otherwise so that only the last focused
403
+ // item is tabbable. If using virtual focus, don't set a tabIndex at all so that VoiceOver
404
+ // on iOS 14 doesn't try to move real DOM focus to the item anyway.
405
+ let itemProps = {};
406
+ if (!shouldUseVirtualFocus && !isDisabled) itemProps = {
407
+ tabIndex: key === manager.focusedKey ? 0 : -1,
408
+ onFocus (e) {
409
+ if (e.target === ref.current) manager.setFocusedKey(key);
410
+ }
411
+ };
412
+ else if (isDisabled) itemProps.onMouseDown = (e)=>{
413
+ // Prevent focus going to the body when clicking on a disabled item.
414
+ e.preventDefault();
415
+ };
416
+ // With checkbox selection, onAction (i.e. navigation) becomes primary, and occurs on a single click of the row.
417
+ // Clicking the checkbox enters selection mode, after which clicking anywhere on any row toggles selection for that row.
418
+ // With highlight selection, onAction is secondary, and occurs on double click. Single click selects the row.
419
+ // With touch, onAction occurs on single tap, and long press enters selection mode.
420
+ let allowsSelection = !isDisabled && manager.canSelectItem(key);
421
+ let allowsActions = onAction && !isDisabled;
422
+ let hasPrimaryAction = allowsActions && (manager.selectionBehavior === "replace" ? !allowsSelection : manager.isEmpty);
423
+ let hasSecondaryAction = allowsActions && allowsSelection && manager.selectionBehavior === "replace";
424
+ let hasAction = hasPrimaryAction || hasSecondaryAction;
425
+ let modality = (0, $eCAIO$useRef)(null);
426
+ let longPressEnabled = hasAction && allowsSelection;
427
+ let longPressEnabledOnPressStart = (0, $eCAIO$useRef)(false);
428
+ let hadPrimaryActionOnPressStart = (0, $eCAIO$useRef)(false);
429
+ // By default, selection occurs on pointer down. This can be strange if selecting an
430
+ // item causes the UI to disappear immediately (e.g. menus).
431
+ // If shouldSelectOnPressUp is true, we use onPressUp instead of onPressStart.
432
+ // onPress requires a pointer down event on the same element as pointer up. For menus,
433
+ // we want to be able to have the pointer down on the trigger that opens the menu and
434
+ // the pointer up on the menu item rather than requiring a separate press.
435
+ // For keyboard events, selection still occurs on key down.
436
+ let itemPressProps = {};
437
+ if (shouldSelectOnPressUp) {
438
+ itemPressProps.onPressStart = (e)=>{
439
+ modality.current = e.pointerType;
440
+ longPressEnabledOnPressStart.current = longPressEnabled;
441
+ if (e.pointerType === "keyboard" && (!hasAction || $880e95eb8b93ba9a$var$isSelectionKey())) onSelect(e);
442
+ };
443
+ // If allowsDifferentPressOrigin, make selection happen on pressUp (e.g. open menu on press down, selection on menu item happens on press up.)
444
+ // Otherwise, have selection happen onPress (prevents listview row selection when clicking on interactable elements in the row)
445
+ if (!allowsDifferentPressOrigin) itemPressProps.onPress = (e)=>{
446
+ if (hasPrimaryAction || hasSecondaryAction && e.pointerType !== "mouse") {
447
+ if (e.pointerType === "keyboard" && !$880e95eb8b93ba9a$var$isActionKey()) return;
448
+ onAction();
449
+ } else if (e.pointerType !== "keyboard") onSelect(e);
450
+ };
451
+ else {
452
+ itemPressProps.onPressUp = (e)=>{
453
+ if (e.pointerType !== "keyboard") onSelect(e);
454
+ };
455
+ itemPressProps.onPress = hasPrimaryAction ? ()=>onAction() : null;
456
+ }
457
+ } else {
458
+ itemPressProps.onPressStart = (e)=>{
459
+ modality.current = e.pointerType;
460
+ longPressEnabledOnPressStart.current = longPressEnabled;
461
+ hadPrimaryActionOnPressStart.current = hasPrimaryAction;
462
+ // Select on mouse down unless there is a primary action which will occur on mouse up.
463
+ // For keyboard, select on key down. If there is an action, the Space key selects on key down,
464
+ // and the Enter key performs onAction on key up.
465
+ if (e.pointerType === "mouse" && !hasPrimaryAction || e.pointerType === "keyboard" && (!onAction || $880e95eb8b93ba9a$var$isSelectionKey())) onSelect(e);
466
+ };
467
+ itemPressProps.onPress = (e)=>{
468
+ // Selection occurs on touch up. Primary actions always occur on pointer up.
469
+ // Both primary and secondary actions occur on Enter key up. The only exception
470
+ // is secondary actions, which occur on double click with a mouse.
471
+ if (e.pointerType === "touch" || e.pointerType === "pen" || e.pointerType === "virtual" || e.pointerType === "keyboard" && hasAction && $880e95eb8b93ba9a$var$isActionKey() || e.pointerType === "mouse" && hadPrimaryActionOnPressStart.current) {
472
+ if (hasAction) onAction();
473
+ else onSelect(e);
474
+ }
475
+ };
476
+ }
477
+ if (!isVirtualized) itemProps["data-key"] = key;
478
+ itemPressProps.preventFocusOnPress = shouldUseVirtualFocus;
479
+ let { pressProps: pressProps , isPressed: isPressed } = (0, $eCAIO$usePress)(itemPressProps);
480
+ // Double clicking with a mouse with selectionBehavior = 'replace' performs an action.
481
+ let onDoubleClick = hasSecondaryAction ? (e)=>{
482
+ if (modality.current === "mouse") {
483
+ e.stopPropagation();
484
+ e.preventDefault();
485
+ onAction();
486
+ }
487
+ } : undefined;
488
+ // Long pressing an item with touch when selectionBehavior = 'replace' switches the selection behavior
489
+ // to 'toggle'. This changes the single tap behavior from performing an action (i.e. navigating) to
490
+ // selecting, and may toggle the appearance of a UI affordance like checkboxes on each item.
491
+ let { longPressProps: longPressProps } = (0, $eCAIO$useLongPress)({
492
+ isDisabled: !longPressEnabled,
493
+ onLongPress (e) {
494
+ if (e.pointerType === "touch") {
495
+ onSelect(e);
496
+ manager.setSelectionBehavior("toggle");
497
+ }
498
+ }
499
+ });
500
+ // Prevent native drag and drop on long press if we also select on long press.
501
+ // Once the user is in selection mode, they can long press again to drag.
502
+ // Use a capturing listener to ensure this runs before useDrag, regardless of
503
+ // the order the props get merged.
504
+ let onDragStartCapture = (e)=>{
505
+ if (modality.current === "touch" && longPressEnabledOnPressStart.current) e.preventDefault();
506
+ };
507
+ return {
508
+ itemProps: (0, $eCAIO$mergeProps)(itemProps, allowsSelection || hasPrimaryAction ? pressProps : {}, longPressEnabled ? longPressProps : {}, {
509
+ onDoubleClick: onDoubleClick,
510
+ onDragStartCapture: onDragStartCapture
511
+ }),
512
+ isPressed: isPressed,
513
+ isSelected: manager.isSelected(key),
514
+ isDisabled: isDisabled,
515
+ allowsSelection: allowsSelection,
516
+ hasAction: hasAction
517
+ };
518
+ }
519
+ function $880e95eb8b93ba9a$var$isActionKey() {
520
+ let event = window.event;
521
+ return (event === null || event === void 0 ? void 0 : event.key) === "Enter";
522
+ }
523
+ function $880e95eb8b93ba9a$var$isSelectionKey() {
524
+ let event = window.event;
525
+ return (event === null || event === void 0 ? void 0 : event.key) === " " || (event === null || event === void 0 ? void 0 : event.code) === "Space";
526
+ }
527
+
528
+
529
+ /*
530
+ * Copyright 2020 Adobe. All rights reserved.
531
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
532
+ * you may not use this file except in compliance with the License. You may obtain a copy
533
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
534
+ *
535
+ * Unless required by applicable law or agreed to in writing, software distributed under
536
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
537
+ * OF ANY KIND, either express or implied. See the License for the specific language
538
+ * governing permissions and limitations under the License.
539
+ */
540
+ /*
541
+ * Copyright 2020 Adobe. All rights reserved.
542
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
543
+ * you may not use this file except in compliance with the License. You may obtain a copy
544
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
545
+ *
546
+ * Unless required by applicable law or agreed to in writing, software distributed under
547
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
548
+ * OF ANY KIND, either express or implied. See the License for the specific language
549
+ * governing permissions and limitations under the License.
550
+ */ class $2a25aae57d74318e$export$a05409b8bb224a5a {
551
+ getKeyBelow(key) {
552
+ key = this.collection.getKeyAfter(key);
553
+ while(key != null){
554
+ let item = this.collection.getItem(key);
555
+ if (item.type === "item" && !this.disabledKeys.has(key)) return key;
556
+ key = this.collection.getKeyAfter(key);
557
+ }
558
+ }
559
+ getKeyAbove(key) {
560
+ key = this.collection.getKeyBefore(key);
561
+ while(key != null){
562
+ let item = this.collection.getItem(key);
563
+ if (item.type === "item" && !this.disabledKeys.has(key)) return key;
564
+ key = this.collection.getKeyBefore(key);
565
+ }
566
+ }
567
+ getFirstKey() {
568
+ let key = this.collection.getFirstKey();
569
+ while(key != null){
570
+ let item = this.collection.getItem(key);
571
+ if (item.type === "item" && !this.disabledKeys.has(key)) return key;
572
+ key = this.collection.getKeyAfter(key);
573
+ }
574
+ }
575
+ getLastKey() {
576
+ let key = this.collection.getLastKey();
577
+ while(key != null){
578
+ let item = this.collection.getItem(key);
579
+ if (item.type === "item" && !this.disabledKeys.has(key)) return key;
580
+ key = this.collection.getKeyBefore(key);
581
+ }
582
+ }
583
+ getItem(key) {
584
+ return this.ref.current.querySelector(`[data-key="${key}"]`);
585
+ }
586
+ getKeyPageAbove(key) {
587
+ let menu = this.ref.current;
588
+ let item = this.getItem(key);
589
+ if (!item) return null;
590
+ let pageY = Math.max(0, item.offsetTop + item.offsetHeight - menu.offsetHeight);
591
+ while(item && item.offsetTop > pageY){
592
+ key = this.getKeyAbove(key);
593
+ item = this.getItem(key);
594
+ }
595
+ return key;
596
+ }
597
+ getKeyPageBelow(key) {
598
+ let menu = this.ref.current;
599
+ let item = this.getItem(key);
600
+ if (!item) return null;
601
+ let pageY = Math.min(menu.scrollHeight, item.offsetTop - item.offsetHeight + menu.offsetHeight);
602
+ while(item && item.offsetTop < pageY){
603
+ key = this.getKeyBelow(key);
604
+ item = this.getItem(key);
605
+ }
606
+ return key;
607
+ }
608
+ getKeyForSearch(search, fromKey) {
609
+ if (!this.collator) return null;
610
+ let collection = this.collection;
611
+ let key = fromKey || this.getFirstKey();
612
+ while(key != null){
613
+ let item = collection.getItem(key);
614
+ let substring = item.textValue.slice(0, search.length);
615
+ if (item.textValue && this.collator.compare(substring, search) === 0) return key;
616
+ key = this.getKeyBelow(key);
617
+ }
618
+ return null;
619
+ }
620
+ constructor(collection, disabledKeys, ref, collator){
621
+ this.collection = collection;
622
+ this.disabledKeys = disabledKeys;
623
+ this.ref = ref;
624
+ this.collator = collator;
625
+ }
626
+ }
627
+
628
+
629
+
630
+
631
+ function $982254629710d113$export$b95089534ab7c1fd(props) {
632
+ let { selectionManager: selectionManager , collection: collection , disabledKeys: disabledKeys , ref: ref , keyboardDelegate: keyboardDelegate , autoFocus: autoFocus , shouldFocusWrap: shouldFocusWrap , isVirtualized: isVirtualized , disallowEmptySelection: disallowEmptySelection , selectOnFocus: selectOnFocus = selectionManager.selectionBehavior === "replace" , disallowTypeAhead: disallowTypeAhead , shouldUseVirtualFocus: shouldUseVirtualFocus , allowsTabNavigation: allowsTabNavigation } = props;
633
+ // By default, a KeyboardDelegate is provided which uses the DOM to query layout information (e.g. for page up/page down).
634
+ // When virtualized, the layout object will be passed in as a prop and override this.
635
+ let collator = (0, $eCAIO$useCollator)({
636
+ usage: "search",
637
+ sensitivity: "base"
638
+ });
639
+ let disabledBehavior = selectionManager.disabledBehavior;
640
+ let delegate = (0, $eCAIO$useMemo)(()=>keyboardDelegate || new (0, $2a25aae57d74318e$export$a05409b8bb224a5a)(collection, disabledBehavior === "selection" ? new Set() : disabledKeys, ref, collator), [
641
+ keyboardDelegate,
642
+ collection,
643
+ disabledKeys,
644
+ ref,
645
+ collator,
646
+ disabledBehavior
647
+ ]);
648
+ let { collectionProps: collectionProps } = (0, $ae20dd8cbca75726$export$d6daf82dcd84e87c)({
649
+ ref: ref,
650
+ selectionManager: selectionManager,
651
+ keyboardDelegate: delegate,
652
+ autoFocus: autoFocus,
653
+ shouldFocusWrap: shouldFocusWrap,
654
+ disallowEmptySelection: disallowEmptySelection,
655
+ selectOnFocus: selectOnFocus,
656
+ disallowTypeAhead: disallowTypeAhead,
657
+ shouldUseVirtualFocus: shouldUseVirtualFocus,
658
+ allowsTabNavigation: allowsTabNavigation,
659
+ isVirtualized: isVirtualized,
660
+ scrollRef: ref
661
+ });
662
+ return {
663
+ listProps: collectionProps
664
+ };
665
+ }
666
+
667
+
668
+
669
+
670
+
671
+
672
+ export {$ae20dd8cbca75726$export$d6daf82dcd84e87c as useSelectableCollection, $880e95eb8b93ba9a$export$ecf600387e221c37 as useSelectableItem, $982254629710d113$export$b95089534ab7c1fd as useSelectableList, $2a25aae57d74318e$export$a05409b8bb224a5a as ListKeyboardDelegate, $fb3050f43d946246$export$e32c88dfddc6e1d8 as useTypeSelect};
673
+ //# sourceMappingURL=module.js.map