@react-aria/selection 3.12.0 → 3.13.0

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