@react-aria/selection 3.27.2 → 3.28.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.
Files changed (52) hide show
  1. package/dist/import.mjs +7 -7
  2. package/dist/main.js +12 -12
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +7 -7
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +10 -0
  7. package/package.json +15 -17
  8. package/src/index.ts +10 -10
  9. package/dist/DOMLayoutDelegate.main.js +0 -59
  10. package/dist/DOMLayoutDelegate.main.js.map +0 -1
  11. package/dist/DOMLayoutDelegate.mjs +0 -54
  12. package/dist/DOMLayoutDelegate.module.js +0 -54
  13. package/dist/DOMLayoutDelegate.module.js.map +0 -1
  14. package/dist/ListKeyboardDelegate.main.js +0 -197
  15. package/dist/ListKeyboardDelegate.main.js.map +0 -1
  16. package/dist/ListKeyboardDelegate.mjs +0 -192
  17. package/dist/ListKeyboardDelegate.module.js +0 -192
  18. package/dist/ListKeyboardDelegate.module.js.map +0 -1
  19. package/dist/types.d.ts +0 -258
  20. package/dist/types.d.ts.map +0 -1
  21. package/dist/useSelectableCollection.main.js +0 -404
  22. package/dist/useSelectableCollection.main.js.map +0 -1
  23. package/dist/useSelectableCollection.mjs +0 -399
  24. package/dist/useSelectableCollection.module.js +0 -399
  25. package/dist/useSelectableCollection.module.js.map +0 -1
  26. package/dist/useSelectableItem.main.js +0 -263
  27. package/dist/useSelectableItem.main.js.map +0 -1
  28. package/dist/useSelectableItem.mjs +0 -258
  29. package/dist/useSelectableItem.module.js +0 -258
  30. package/dist/useSelectableItem.module.js.map +0 -1
  31. package/dist/useSelectableList.main.js +0 -63
  32. package/dist/useSelectableList.main.js.map +0 -1
  33. package/dist/useSelectableList.mjs +0 -58
  34. package/dist/useSelectableList.module.js +0 -58
  35. package/dist/useSelectableList.module.js.map +0 -1
  36. package/dist/useTypeSelect.main.js +0 -77
  37. package/dist/useTypeSelect.main.js.map +0 -1
  38. package/dist/useTypeSelect.mjs +0 -72
  39. package/dist/useTypeSelect.module.js +0 -72
  40. package/dist/useTypeSelect.module.js.map +0 -1
  41. package/dist/utils.main.js +0 -46
  42. package/dist/utils.main.js.map +0 -1
  43. package/dist/utils.mjs +0 -38
  44. package/dist/utils.module.js +0 -38
  45. package/dist/utils.module.js.map +0 -1
  46. package/src/DOMLayoutDelegate.ts +0 -61
  47. package/src/ListKeyboardDelegate.ts +0 -291
  48. package/src/useSelectableCollection.ts +0 -594
  49. package/src/useSelectableItem.ts +0 -422
  50. package/src/useSelectableList.ts +0 -85
  51. package/src/useTypeSelect.ts +0 -117
  52. package/src/utils.ts +0 -47
@@ -1,263 +0,0 @@
1
- var $ee0bdf4faa47f2a8$exports = require("./utils.main.js");
2
- var $i4XHw$reactariautils = require("@react-aria/utils");
3
- var $i4XHw$reactariainteractions = require("@react-aria/interactions");
4
- var $i4XHw$reactariafocus = require("@react-aria/focus");
5
- var $i4XHw$react = require("react");
6
-
7
-
8
- function $parcel$export(e, n, v, s) {
9
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
10
- }
11
-
12
- $parcel$export(module.exports, "useSelectableItem", () => $433b1145b0781e10$export$ecf600387e221c37);
13
- /*
14
- * Copyright 2020 Adobe. All rights reserved.
15
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
16
- * you may not use this file except in compliance with the License. You may obtain a copy
17
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
18
- *
19
- * Unless required by applicable law or agreed to in writing, software distributed under
20
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
21
- * OF ANY KIND, either express or implied. See the License for the specific language
22
- * governing permissions and limitations under the License.
23
- */
24
-
25
-
26
-
27
-
28
- function $433b1145b0781e10$export$ecf600387e221c37(options) {
29
- let { id: id, selectionManager: manager, key: key, ref: ref, shouldSelectOnPressUp: shouldSelectOnPressUp, shouldUseVirtualFocus: shouldUseVirtualFocus, focus: focus, isDisabled: isDisabled, onAction: onAction, allowsDifferentPressOrigin: allowsDifferentPressOrigin, linkBehavior: linkBehavior = 'action' } = options;
30
- let router = (0, $i4XHw$reactariautils.useRouter)();
31
- id = (0, $i4XHw$reactariautils.useId)(id);
32
- let onSelect = (e)=>{
33
- if (e.pointerType === 'keyboard' && (0, $ee0bdf4faa47f2a8$exports.isNonContiguousSelectionModifier)(e)) manager.toggleSelection(key);
34
- else {
35
- if (manager.selectionMode === 'none') return;
36
- if (manager.isLink(key)) {
37
- if (linkBehavior === 'selection' && ref.current) {
38
- let itemProps = manager.getItemProps(key);
39
- router.open(ref.current, e, itemProps.href, itemProps.routerOptions);
40
- // Always set selected keys back to what they were so that select and combobox close.
41
- manager.setSelectedKeys(manager.selectedKeys);
42
- return;
43
- } else if (linkBehavior === 'override' || linkBehavior === 'none') return;
44
- }
45
- if (manager.selectionMode === 'single') {
46
- if (manager.isSelected(key) && !manager.disallowEmptySelection) manager.toggleSelection(key);
47
- else manager.replaceSelection(key);
48
- } else if (e && e.shiftKey) manager.extendSelection(key);
49
- else if (manager.selectionBehavior === 'toggle' || e && ((0, $i4XHw$reactariautils.isCtrlKeyPressed)(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
50
- manager.toggleSelection(key);
51
- else manager.replaceSelection(key);
52
- }
53
- };
54
- // Focus the associated DOM node when this item becomes the focusedKey
55
- // TODO: can't make this useLayoutEffect bacause it breaks menus inside dialogs
56
- // However, if this is a useEffect, it runs twice and dispatches two blur events and immediately sets
57
- // aria-activeDescendant in useAutocomplete... I've worked around this for now
58
- (0, $i4XHw$react.useEffect)(()=>{
59
- let isFocused = key === manager.focusedKey;
60
- if (isFocused && manager.isFocused) {
61
- if (!shouldUseVirtualFocus) {
62
- if (focus) focus();
63
- else if ((0, $i4XHw$reactariautils.getActiveElement)() !== ref.current && ref.current) (0, $i4XHw$reactariainteractions.focusSafely)(ref.current);
64
- } else (0, $i4XHw$reactariafocus.moveVirtualFocus)(ref.current);
65
- }
66
- // eslint-disable-next-line react-hooks/exhaustive-deps
67
- }, [
68
- ref,
69
- key,
70
- manager.focusedKey,
71
- manager.childFocusStrategy,
72
- manager.isFocused,
73
- shouldUseVirtualFocus
74
- ]);
75
- isDisabled = isDisabled || manager.isDisabled(key);
76
- // Set tabIndex to 0 if the element is focused, or -1 otherwise so that only the last focused
77
- // item is tabbable. If using virtual focus, don't set a tabIndex at all so that VoiceOver
78
- // on iOS 14 doesn't try to move real DOM focus to the item anyway.
79
- let itemProps = {};
80
- if (!shouldUseVirtualFocus && !isDisabled) itemProps = {
81
- tabIndex: key === manager.focusedKey ? 0 : -1,
82
- onFocus (e) {
83
- if ((0, $i4XHw$reactariautils.getEventTarget)(e) === ref.current) manager.setFocusedKey(key);
84
- }
85
- };
86
- else if (isDisabled) itemProps.onMouseDown = (e)=>{
87
- // Prevent focus going to the body when clicking on a disabled item.
88
- e.preventDefault();
89
- };
90
- (0, $i4XHw$react.useEffect)(()=>{
91
- if (isDisabled && manager.focusedKey === key) manager.setFocusedKey(null);
92
- }, [
93
- manager,
94
- isDisabled,
95
- key
96
- ]);
97
- // With checkbox selection, onAction (i.e. navigation) becomes primary, and occurs on a single click of the row.
98
- // Clicking the checkbox enters selection mode, after which clicking anywhere on any row toggles selection for that row.
99
- // With highlight selection, onAction is secondary, and occurs on double click. Single click selects the row.
100
- // With touch, onAction occurs on single tap, and long press enters selection mode.
101
- let isLinkOverride = manager.isLink(key) && linkBehavior === 'override';
102
- let isActionOverride = onAction && options['UNSTABLE_itemBehavior'] === 'action';
103
- let hasLinkAction = manager.isLink(key) && linkBehavior !== 'selection' && linkBehavior !== 'none';
104
- let allowsSelection = !isDisabled && manager.canSelectItem(key) && !isLinkOverride && !isActionOverride;
105
- let allowsActions = (onAction || hasLinkAction) && !isDisabled;
106
- let hasPrimaryAction = allowsActions && (manager.selectionBehavior === 'replace' ? !allowsSelection : !allowsSelection || manager.isEmpty);
107
- let hasSecondaryAction = allowsActions && allowsSelection && manager.selectionBehavior === 'replace';
108
- let hasAction = hasPrimaryAction || hasSecondaryAction;
109
- let modality = (0, $i4XHw$react.useRef)(null);
110
- let longPressEnabled = hasAction && allowsSelection;
111
- let longPressEnabledOnPressStart = (0, $i4XHw$react.useRef)(false);
112
- let hadPrimaryActionOnPressStart = (0, $i4XHw$react.useRef)(false);
113
- let collectionItemProps = manager.getItemProps(key);
114
- let performAction = (e)=>{
115
- if (onAction) {
116
- var _ref_current;
117
- onAction();
118
- (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.dispatchEvent(new CustomEvent('react-aria-item-action', {
119
- bubbles: true
120
- }));
121
- }
122
- if (hasLinkAction && ref.current) router.open(ref.current, e, collectionItemProps.href, collectionItemProps.routerOptions);
123
- };
124
- // By default, selection occurs on pointer down. This can be strange if selecting an
125
- // item causes the UI to disappear immediately (e.g. menus).
126
- // If shouldSelectOnPressUp is true, we use onPressUp instead of onPressStart.
127
- // onPress requires a pointer down event on the same element as pointer up. For menus,
128
- // we want to be able to have the pointer down on the trigger that opens the menu and
129
- // the pointer up on the menu item rather than requiring a separate press.
130
- // For keyboard events, selection still occurs on key down.
131
- let itemPressProps = {
132
- ref: ref
133
- };
134
- if (shouldSelectOnPressUp) {
135
- itemPressProps.onPressStart = (e)=>{
136
- modality.current = e.pointerType;
137
- longPressEnabledOnPressStart.current = longPressEnabled;
138
- if (e.pointerType === 'keyboard' && (!hasAction || $433b1145b0781e10$var$isSelectionKey(e.key))) onSelect(e);
139
- };
140
- // If allowsDifferentPressOrigin and interacting with mouse, make selection happen on pressUp (e.g. open menu on press down, selection on menu item happens on press up.)
141
- // Otherwise, have selection happen onPress (prevents listview row selection when clicking on interactable elements in the row)
142
- if (!allowsDifferentPressOrigin) itemPressProps.onPress = (e)=>{
143
- if (hasPrimaryAction || hasSecondaryAction && e.pointerType !== 'mouse') {
144
- if (e.pointerType === 'keyboard' && !$433b1145b0781e10$var$isActionKey(e.key)) return;
145
- performAction(e);
146
- } else if (e.pointerType !== 'keyboard' && allowsSelection) onSelect(e);
147
- };
148
- else {
149
- itemPressProps.onPressUp = hasPrimaryAction ? undefined : (e)=>{
150
- if (e.pointerType === 'mouse' && allowsSelection) onSelect(e);
151
- };
152
- itemPressProps.onPress = hasPrimaryAction ? performAction : (e)=>{
153
- if (e.pointerType !== 'keyboard' && e.pointerType !== 'mouse' && allowsSelection) onSelect(e);
154
- };
155
- }
156
- } else {
157
- itemPressProps.onPressStart = (e)=>{
158
- modality.current = e.pointerType;
159
- longPressEnabledOnPressStart.current = longPressEnabled;
160
- hadPrimaryActionOnPressStart.current = hasPrimaryAction;
161
- // Select on mouse down unless there is a primary action which will occur on mouse up.
162
- // For keyboard, select on key down. If there is an action, the Space key selects on key down,
163
- // and the Enter key performs onAction on key up.
164
- if (allowsSelection && (e.pointerType === 'mouse' && !hasPrimaryAction || e.pointerType === 'keyboard' && (!allowsActions || $433b1145b0781e10$var$isSelectionKey(e.key)))) onSelect(e);
165
- };
166
- itemPressProps.onPress = (e)=>{
167
- // Selection occurs on touch up. Primary actions always occur on pointer up.
168
- // Both primary and secondary actions occur on Enter key up. The only exception
169
- // is secondary actions, which occur on double click with a mouse.
170
- if (e.pointerType === 'touch' || e.pointerType === 'pen' || e.pointerType === 'virtual' || e.pointerType === 'keyboard' && hasAction && $433b1145b0781e10$var$isActionKey(e.key) || e.pointerType === 'mouse' && hadPrimaryActionOnPressStart.current) {
171
- if (hasAction) performAction(e);
172
- else if (allowsSelection) onSelect(e);
173
- }
174
- };
175
- }
176
- itemProps['data-collection'] = (0, $ee0bdf4faa47f2a8$exports.getCollectionId)(manager.collection);
177
- itemProps['data-key'] = key;
178
- itemPressProps.preventFocusOnPress = shouldUseVirtualFocus;
179
- // When using virtual focus, make sure the focused key gets updated on press.
180
- if (shouldUseVirtualFocus) itemPressProps = (0, $i4XHw$reactariautils.mergeProps)(itemPressProps, {
181
- onPressStart (e) {
182
- if (e.pointerType !== 'touch') {
183
- manager.setFocused(true);
184
- manager.setFocusedKey(key);
185
- }
186
- },
187
- onPress (e) {
188
- if (e.pointerType === 'touch') {
189
- manager.setFocused(true);
190
- manager.setFocusedKey(key);
191
- }
192
- }
193
- });
194
- if (collectionItemProps) {
195
- for (let key of [
196
- 'onPressStart',
197
- 'onPressEnd',
198
- 'onPressChange',
199
- 'onPress',
200
- 'onPressUp',
201
- 'onClick'
202
- ])if (collectionItemProps[key]) itemPressProps[key] = (0, $i4XHw$reactariautils.chain)(itemPressProps[key], collectionItemProps[key]);
203
- }
204
- let { pressProps: pressProps, isPressed: isPressed } = (0, $i4XHw$reactariainteractions.usePress)(itemPressProps);
205
- // Double clicking with a mouse with selectionBehavior = 'replace' performs an action.
206
- let onDoubleClick = hasSecondaryAction ? (e)=>{
207
- if (modality.current === 'mouse') {
208
- e.stopPropagation();
209
- e.preventDefault();
210
- performAction(e);
211
- }
212
- } : undefined;
213
- // Long pressing an item with touch when selectionBehavior = 'replace' switches the selection behavior
214
- // to 'toggle'. This changes the single tap behavior from performing an action (i.e. navigating) to
215
- // selecting, and may toggle the appearance of a UI affordance like checkboxes on each item.
216
- let { longPressProps: longPressProps } = (0, $i4XHw$reactariainteractions.useLongPress)({
217
- isDisabled: !longPressEnabled,
218
- onLongPress (e) {
219
- if (e.pointerType === 'touch') {
220
- onSelect(e);
221
- manager.setSelectionBehavior('toggle');
222
- }
223
- }
224
- });
225
- // Prevent native drag and drop on long press if we also select on long press.
226
- // Once the user is in selection mode, they can long press again to drag.
227
- // Use a capturing listener to ensure this runs before useDrag, regardless of
228
- // the order the props get merged.
229
- let onDragStartCapture = (e)=>{
230
- if (modality.current === 'touch' && longPressEnabledOnPressStart.current) e.preventDefault();
231
- };
232
- // Prevent default on link clicks so that we control exactly
233
- // when they open (to match selection behavior).
234
- let onClick = linkBehavior !== 'none' && manager.isLink(key) ? (e)=>{
235
- if (!(0, $i4XHw$reactariautils.openLink).isOpening) e.preventDefault();
236
- } : undefined;
237
- return {
238
- itemProps: (0, $i4XHw$reactariautils.mergeProps)(itemProps, allowsSelection || hasPrimaryAction || shouldUseVirtualFocus && !isDisabled ? pressProps : {}, longPressEnabled ? longPressProps : {}, {
239
- onDoubleClick: onDoubleClick,
240
- onDragStartCapture: onDragStartCapture,
241
- onClick: onClick,
242
- id: id
243
- }, // Prevent DOM focus from moving on mouse down when using virtual focus
244
- shouldUseVirtualFocus ? {
245
- onMouseDown: (e)=>e.preventDefault()
246
- } : undefined),
247
- isPressed: isPressed,
248
- isSelected: manager.isSelected(key),
249
- isFocused: manager.isFocused && manager.focusedKey === key,
250
- isDisabled: isDisabled,
251
- allowsSelection: allowsSelection,
252
- hasAction: hasAction
253
- };
254
- }
255
- function $433b1145b0781e10$var$isActionKey(key) {
256
- return key === 'Enter';
257
- }
258
- function $433b1145b0781e10$var$isSelectionKey(key) {
259
- return key === ' ';
260
- }
261
-
262
-
263
- //# sourceMappingURL=useSelectableItem.main.js.map
@@ -1 +0,0 @@
1
- {"mappings":";;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;AAkGM,SAAS,0CAAkB,OAA8B;IAC9D,IAAI,MACF,EAAE,EACF,kBAAkB,OAAO,OACzB,GAAG,OACH,GAAG,yBACH,qBAAqB,yBACrB,qBAAqB,SACrB,KAAK,cACL,UAAU,YACV,QAAQ,8BACR,0BAA0B,gBAC1B,eAAe,UAChB,GAAG;IACJ,IAAI,SAAS,CAAA,GAAA,+BAAQ;IACrB,KAAK,CAAA,GAAA,2BAAI,EAAE;IACX,IAAI,WAAW,CAAC;QACd,IAAI,EAAE,WAAW,KAAK,cAAc,CAAA,GAAA,0DAA+B,EAAE,IACnE,QAAQ,eAAe,CAAC;aACnB;YACL,IAAI,QAAQ,aAAa,KAAK,QAC5B;YAGF,IAAI,QAAQ,MAAM,CAAC,MAAM;gBACvB,IAAI,iBAAiB,eAAe,IAAI,OAAO,EAAE;oBAC/C,IAAI,YAAY,QAAQ,YAAY,CAAC;oBACrC,OAAO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","sources":["packages/@react-aria/selection/src/useSelectableItem.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {chain, getActiveElement, getEventTarget, isCtrlKeyPressed, mergeProps, openLink, useId, useRouter} from '@react-aria/utils';\nimport {DOMAttributes, DOMProps, FocusableElement, Key, LongPressEvent, PointerType, PressEvent, RefObject} from '@react-types/shared';\nimport {focusSafely, PressHookProps, useLongPress, usePress} from '@react-aria/interactions';\nimport {getCollectionId, isNonContiguousSelectionModifier} from './utils';\nimport {moveVirtualFocus} from '@react-aria/focus';\nimport {MultipleSelectionManager} from '@react-stately/selection';\nimport {useEffect, useRef} from 'react';\n\nexport interface SelectableItemOptions extends DOMProps {\n /**\n * An interface for reading and updating multiple selection state.\n */\n selectionManager: MultipleSelectionManager,\n /**\n * A unique key for the item.\n */\n key: Key,\n /**\n * Ref to the item.\n */\n ref: RefObject<FocusableElement | null>,\n /**\n * By default, selection occurs on pointer down. This can be strange if selecting an\n * item causes the UI to disappear immediately (e.g. menus).\n */\n shouldSelectOnPressUp?: boolean,\n /**\n * Whether selection requires the pointer/mouse down and up events to occur on the same target or triggers selection on\n * the target of the pointer/mouse up event.\n */\n allowsDifferentPressOrigin?: boolean,\n /**\n * Whether the option is contained in a virtual scroller.\n */\n isVirtualized?: boolean,\n /**\n * Function to focus the item.\n */\n focus?: () => void,\n /**\n * Whether the option should use virtual focus instead of being focused directly.\n */\n shouldUseVirtualFocus?: boolean,\n /** Whether the item is disabled. */\n isDisabled?: boolean,\n /**\n * Handler that is called when a user performs an action on the item. The exact user event depends on\n * the collection's `selectionBehavior` prop and the interaction modality.\n */\n onAction?: () => void,\n /**\n * The behavior of links in the collection.\n * - 'action': link behaves like onAction.\n * - 'selection': link follows selection interactions (e.g. if URL drives selection).\n * - 'override': links override all other interactions (link items are not selectable).\n * - 'none': links are disabled for both selection and actions (e.g. handled elsewhere).\n * @default 'action'\n */\n linkBehavior?: 'action' | 'selection' | 'override' | 'none'\n}\n\nexport interface SelectableItemStates {\n /** Whether the item is currently in a pressed state. */\n isPressed: boolean,\n /** Whether the item is currently selected. */\n isSelected: boolean,\n /** Whether the item is currently focused. */\n isFocused: boolean,\n /**\n * Whether the item is non-interactive, i.e. both selection and actions are disabled and the item may\n * not be focused. Dependent on `disabledKeys` and `disabledBehavior`.\n */\n isDisabled: boolean,\n /**\n * Whether the item may be selected, dependent on `selectionMode`, `disabledKeys`, and `disabledBehavior`.\n */\n allowsSelection: boolean,\n /**\n * Whether the item has an action, dependent on `onAction`, `disabledKeys`,\n * and `disabledBehavior`. It may also change depending on the current selection state\n * of the list (e.g. when selection is primary). This can be used to enable or disable hover\n * styles or other visual indications of interactivity.\n */\n hasAction: boolean\n}\n\nexport interface SelectableItemAria extends SelectableItemStates {\n /**\n * Props to be spread on the item root node.\n */\n itemProps: DOMAttributes\n}\n\n/**\n * Handles interactions with an item in a selectable collection.\n */\nexport function useSelectableItem(options: SelectableItemOptions): SelectableItemAria {\n let {\n id,\n selectionManager: manager,\n key,\n ref,\n shouldSelectOnPressUp,\n shouldUseVirtualFocus,\n focus,\n isDisabled,\n onAction,\n allowsDifferentPressOrigin,\n linkBehavior = 'action'\n } = options;\n let router = useRouter();\n id = useId(id);\n let onSelect = (e: PressEvent | LongPressEvent | PointerEvent) => {\n if (e.pointerType === 'keyboard' && isNonContiguousSelectionModifier(e)) {\n manager.toggleSelection(key);\n } else {\n if (manager.selectionMode === 'none') {\n return;\n }\n\n if (manager.isLink(key)) {\n if (linkBehavior === 'selection' && ref.current) {\n let itemProps = manager.getItemProps(key);\n router.open(ref.current, e, itemProps.href, itemProps.routerOptions);\n // Always set selected keys back to what they were so that select and combobox close.\n manager.setSelectedKeys(manager.selectedKeys);\n return;\n } else if (linkBehavior === 'override' || linkBehavior === 'none') {\n return;\n }\n }\n\n if (manager.selectionMode === 'single') {\n if (manager.isSelected(key) && !manager.disallowEmptySelection) {\n manager.toggleSelection(key);\n } else {\n manager.replaceSelection(key);\n }\n } else if (e && e.shiftKey) {\n manager.extendSelection(key);\n } else if (manager.selectionBehavior === 'toggle' || (e && (isCtrlKeyPressed(e) || e.pointerType === 'touch' || e.pointerType === 'virtual'))) {\n // 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\n manager.toggleSelection(key);\n } else {\n manager.replaceSelection(key);\n }\n }\n };\n\n // Focus the associated DOM node when this item becomes the focusedKey\n // TODO: can't make this useLayoutEffect bacause it breaks menus inside dialogs\n // However, if this is a useEffect, it runs twice and dispatches two blur events and immediately sets\n // aria-activeDescendant in useAutocomplete... I've worked around this for now\n useEffect(() => {\n let isFocused = key === manager.focusedKey;\n if (isFocused && manager.isFocused) {\n if (!shouldUseVirtualFocus) {\n if (focus) {\n focus();\n } else if (getActiveElement() !== ref.current && ref.current) {\n focusSafely(ref.current);\n }\n } else {\n moveVirtualFocus(ref.current);\n }\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [ref, key, manager.focusedKey, manager.childFocusStrategy, manager.isFocused, shouldUseVirtualFocus]);\n\n isDisabled = isDisabled || manager.isDisabled(key);\n // Set tabIndex to 0 if the element is focused, or -1 otherwise so that only the last focused\n // item is tabbable. If using virtual focus, don't set a tabIndex at all so that VoiceOver\n // on iOS 14 doesn't try to move real DOM focus to the item anyway.\n let itemProps: SelectableItemAria['itemProps'] = {};\n if (!shouldUseVirtualFocus && !isDisabled) {\n itemProps = {\n tabIndex: key === manager.focusedKey ? 0 : -1,\n onFocus(e) {\n if (getEventTarget(e) === ref.current) {\n manager.setFocusedKey(key);\n }\n }\n };\n } else if (isDisabled) {\n itemProps.onMouseDown = (e) => {\n // Prevent focus going to the body when clicking on a disabled item.\n e.preventDefault();\n };\n }\n\n useEffect(() => {\n if (isDisabled && manager.focusedKey === key) {\n manager.setFocusedKey(null);\n }\n }, [manager, isDisabled, key]);\n\n // With checkbox selection, onAction (i.e. navigation) becomes primary, and occurs on a single click of the row.\n // Clicking the checkbox enters selection mode, after which clicking anywhere on any row toggles selection for that row.\n // With highlight selection, onAction is secondary, and occurs on double click. Single click selects the row.\n // With touch, onAction occurs on single tap, and long press enters selection mode.\n let isLinkOverride = manager.isLink(key) && linkBehavior === 'override';\n let isActionOverride = onAction && options['UNSTABLE_itemBehavior'] === 'action';\n let hasLinkAction = manager.isLink(key) && linkBehavior !== 'selection' && linkBehavior !== 'none';\n let allowsSelection = !isDisabled && manager.canSelectItem(key) && !isLinkOverride && !isActionOverride;\n let allowsActions = (onAction || hasLinkAction) && !isDisabled;\n let hasPrimaryAction = allowsActions && (\n manager.selectionBehavior === 'replace'\n ? !allowsSelection\n : !allowsSelection || manager.isEmpty\n );\n let hasSecondaryAction = allowsActions && allowsSelection && manager.selectionBehavior === 'replace';\n let hasAction = hasPrimaryAction || hasSecondaryAction;\n let modality = useRef<PointerType | null>(null);\n\n let longPressEnabled = hasAction && allowsSelection;\n let longPressEnabledOnPressStart = useRef(false);\n let hadPrimaryActionOnPressStart = useRef(false);\n let collectionItemProps = manager.getItemProps(key);\n\n let performAction = (e) => {\n if (onAction) {\n onAction();\n ref.current?.dispatchEvent(new CustomEvent('react-aria-item-action', {bubbles: true}));\n }\n\n if (hasLinkAction && ref.current) {\n router.open(ref.current, e, collectionItemProps.href, collectionItemProps.routerOptions);\n }\n };\n\n // By default, selection occurs on pointer down. This can be strange if selecting an\n // item causes the UI to disappear immediately (e.g. menus).\n // If shouldSelectOnPressUp is true, we use onPressUp instead of onPressStart.\n // onPress requires a pointer down event on the same element as pointer up. For menus,\n // we want to be able to have the pointer down on the trigger that opens the menu and\n // the pointer up on the menu item rather than requiring a separate press.\n // For keyboard events, selection still occurs on key down.\n let itemPressProps: PressHookProps = {ref};\n if (shouldSelectOnPressUp) {\n itemPressProps.onPressStart = (e) => {\n modality.current = e.pointerType;\n longPressEnabledOnPressStart.current = longPressEnabled;\n if (e.pointerType === 'keyboard' && (!hasAction || isSelectionKey(e.key))) {\n onSelect(e);\n }\n };\n\n // If allowsDifferentPressOrigin and interacting with mouse, make selection happen on pressUp (e.g. open menu on press down, selection on menu item happens on press up.)\n // Otherwise, have selection happen onPress (prevents listview row selection when clicking on interactable elements in the row)\n if (!allowsDifferentPressOrigin) {\n itemPressProps.onPress = (e) => {\n if (hasPrimaryAction || (hasSecondaryAction && e.pointerType !== 'mouse')) {\n if (e.pointerType === 'keyboard' && !isActionKey(e.key)) {\n return;\n }\n\n performAction(e);\n } else if (e.pointerType !== 'keyboard' && allowsSelection) {\n onSelect(e);\n }\n };\n } else {\n itemPressProps.onPressUp = hasPrimaryAction ? undefined : (e) => {\n if (e.pointerType === 'mouse' && allowsSelection) {\n onSelect(e);\n }\n };\n\n itemPressProps.onPress = hasPrimaryAction ? performAction : (e) => {\n if (e.pointerType !== 'keyboard' && e.pointerType !== 'mouse' && allowsSelection) {\n onSelect(e);\n }\n };\n }\n } else {\n itemPressProps.onPressStart = (e) => {\n modality.current = e.pointerType;\n longPressEnabledOnPressStart.current = longPressEnabled;\n hadPrimaryActionOnPressStart.current = hasPrimaryAction;\n\n // Select on mouse down unless there is a primary action which will occur on mouse up.\n // For keyboard, select on key down. If there is an action, the Space key selects on key down,\n // and the Enter key performs onAction on key up.\n if (\n allowsSelection && (\n (e.pointerType === 'mouse' && !hasPrimaryAction) ||\n (e.pointerType === 'keyboard' && (!allowsActions || isSelectionKey(e.key)))\n )\n ) {\n onSelect(e);\n }\n };\n\n itemPressProps.onPress = (e) => {\n // Selection occurs on touch up. Primary actions always occur on pointer up.\n // Both primary and secondary actions occur on Enter key up. The only exception\n // is secondary actions, which occur on double click with a mouse.\n if (\n e.pointerType === 'touch' ||\n e.pointerType === 'pen' ||\n e.pointerType === 'virtual' ||\n (e.pointerType === 'keyboard' && hasAction && isActionKey(e.key)) ||\n (e.pointerType === 'mouse' && hadPrimaryActionOnPressStart.current)\n ) {\n if (hasAction) {\n performAction(e);\n } else if (allowsSelection) {\n onSelect(e);\n }\n }\n };\n }\n\n itemProps['data-collection'] = getCollectionId(manager.collection);\n itemProps['data-key'] = key;\n itemPressProps.preventFocusOnPress = shouldUseVirtualFocus;\n\n // When using virtual focus, make sure the focused key gets updated on press.\n if (shouldUseVirtualFocus) {\n itemPressProps = mergeProps(itemPressProps, {\n onPressStart(e) {\n if (e.pointerType !== 'touch') {\n manager.setFocused(true);\n manager.setFocusedKey(key);\n }\n },\n onPress(e) {\n if (e.pointerType === 'touch') {\n manager.setFocused(true);\n manager.setFocusedKey(key);\n }\n }\n });\n }\n\n if (collectionItemProps) {\n for (let key of ['onPressStart', 'onPressEnd', 'onPressChange', 'onPress', 'onPressUp', 'onClick']) {\n if (collectionItemProps[key]) {\n itemPressProps[key] = chain(itemPressProps[key], collectionItemProps[key]);\n }\n }\n }\n\n let {pressProps, isPressed} = usePress(itemPressProps);\n\n // Double clicking with a mouse with selectionBehavior = 'replace' performs an action.\n let onDoubleClick = hasSecondaryAction ? (e) => {\n if (modality.current === 'mouse') {\n e.stopPropagation();\n e.preventDefault();\n performAction(e);\n }\n } : undefined;\n\n // Long pressing an item with touch when selectionBehavior = 'replace' switches the selection behavior\n // to 'toggle'. This changes the single tap behavior from performing an action (i.e. navigating) to\n // selecting, and may toggle the appearance of a UI affordance like checkboxes on each item.\n let {longPressProps} = useLongPress({\n isDisabled: !longPressEnabled,\n onLongPress(e) {\n if (e.pointerType === 'touch') {\n onSelect(e);\n manager.setSelectionBehavior('toggle');\n }\n }\n });\n\n // Prevent native drag and drop on long press if we also select on long press.\n // Once the user is in selection mode, they can long press again to drag.\n // Use a capturing listener to ensure this runs before useDrag, regardless of\n // the order the props get merged.\n let onDragStartCapture = e => {\n if (modality.current === 'touch' && longPressEnabledOnPressStart.current) {\n e.preventDefault();\n }\n };\n\n // Prevent default on link clicks so that we control exactly\n // when they open (to match selection behavior).\n let onClick = linkBehavior !== 'none' && manager.isLink(key) ? e => {\n if (!(openLink as any).isOpening) {\n e.preventDefault();\n }\n } : undefined;\n\n return {\n itemProps: mergeProps(\n itemProps,\n allowsSelection || hasPrimaryAction || (shouldUseVirtualFocus && !isDisabled) ? pressProps : {},\n longPressEnabled ? longPressProps : {},\n {onDoubleClick, onDragStartCapture, onClick, id},\n // Prevent DOM focus from moving on mouse down when using virtual focus\n shouldUseVirtualFocus ? {onMouseDown: e => e.preventDefault()} : undefined\n ),\n isPressed,\n isSelected: manager.isSelected(key),\n isFocused: manager.isFocused && manager.focusedKey === key,\n isDisabled,\n allowsSelection,\n hasAction\n };\n}\n\nfunction isActionKey(key: string | undefined) {\n return key === 'Enter';\n}\n\nfunction isSelectionKey(key: string | undefined) {\n return key === ' ';\n}\n"],"names":[],"version":3,"file":"useSelectableItem.main.js.map"}
@@ -1,258 +0,0 @@
1
- import {getCollectionId as $feb5ffebff200149$export$6aeb1680a0ae8741, isNonContiguousSelectionModifier as $feb5ffebff200149$export$d3e3bd3e26688c04} from "./utils.mjs";
2
- import {useRouter as $581M0$useRouter, useId as $581M0$useId, isCtrlKeyPressed as $581M0$isCtrlKeyPressed, getActiveElement as $581M0$getActiveElement, getEventTarget as $581M0$getEventTarget, mergeProps as $581M0$mergeProps, chain as $581M0$chain, openLink as $581M0$openLink} from "@react-aria/utils";
3
- import {focusSafely as $581M0$focusSafely, usePress as $581M0$usePress, useLongPress as $581M0$useLongPress} from "@react-aria/interactions";
4
- import {moveVirtualFocus as $581M0$moveVirtualFocus} from "@react-aria/focus";
5
- import {useEffect as $581M0$useEffect, useRef as $581M0$useRef} from "react";
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
-
19
-
20
-
21
-
22
- function $880e95eb8b93ba9a$export$ecf600387e221c37(options) {
23
- let { id: id, selectionManager: manager, key: key, ref: ref, shouldSelectOnPressUp: shouldSelectOnPressUp, shouldUseVirtualFocus: shouldUseVirtualFocus, focus: focus, isDisabled: isDisabled, onAction: onAction, allowsDifferentPressOrigin: allowsDifferentPressOrigin, linkBehavior: linkBehavior = 'action' } = options;
24
- let router = (0, $581M0$useRouter)();
25
- id = (0, $581M0$useId)(id);
26
- let onSelect = (e)=>{
27
- if (e.pointerType === 'keyboard' && (0, $feb5ffebff200149$export$d3e3bd3e26688c04)(e)) manager.toggleSelection(key);
28
- else {
29
- if (manager.selectionMode === 'none') return;
30
- if (manager.isLink(key)) {
31
- if (linkBehavior === 'selection' && ref.current) {
32
- let itemProps = manager.getItemProps(key);
33
- router.open(ref.current, e, itemProps.href, itemProps.routerOptions);
34
- // Always set selected keys back to what they were so that select and combobox close.
35
- manager.setSelectedKeys(manager.selectedKeys);
36
- return;
37
- } else if (linkBehavior === 'override' || linkBehavior === 'none') return;
38
- }
39
- if (manager.selectionMode === 'single') {
40
- if (manager.isSelected(key) && !manager.disallowEmptySelection) manager.toggleSelection(key);
41
- else manager.replaceSelection(key);
42
- } else if (e && e.shiftKey) manager.extendSelection(key);
43
- else if (manager.selectionBehavior === 'toggle' || e && ((0, $581M0$isCtrlKeyPressed)(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
44
- manager.toggleSelection(key);
45
- else manager.replaceSelection(key);
46
- }
47
- };
48
- // Focus the associated DOM node when this item becomes the focusedKey
49
- // TODO: can't make this useLayoutEffect bacause it breaks menus inside dialogs
50
- // However, if this is a useEffect, it runs twice and dispatches two blur events and immediately sets
51
- // aria-activeDescendant in useAutocomplete... I've worked around this for now
52
- (0, $581M0$useEffect)(()=>{
53
- let isFocused = key === manager.focusedKey;
54
- if (isFocused && manager.isFocused) {
55
- if (!shouldUseVirtualFocus) {
56
- if (focus) focus();
57
- else if ((0, $581M0$getActiveElement)() !== ref.current && ref.current) (0, $581M0$focusSafely)(ref.current);
58
- } else (0, $581M0$moveVirtualFocus)(ref.current);
59
- }
60
- // eslint-disable-next-line react-hooks/exhaustive-deps
61
- }, [
62
- ref,
63
- key,
64
- manager.focusedKey,
65
- manager.childFocusStrategy,
66
- manager.isFocused,
67
- shouldUseVirtualFocus
68
- ]);
69
- isDisabled = isDisabled || manager.isDisabled(key);
70
- // Set tabIndex to 0 if the element is focused, or -1 otherwise so that only the last focused
71
- // item is tabbable. If using virtual focus, don't set a tabIndex at all so that VoiceOver
72
- // on iOS 14 doesn't try to move real DOM focus to the item anyway.
73
- let itemProps = {};
74
- if (!shouldUseVirtualFocus && !isDisabled) itemProps = {
75
- tabIndex: key === manager.focusedKey ? 0 : -1,
76
- onFocus (e) {
77
- if ((0, $581M0$getEventTarget)(e) === ref.current) manager.setFocusedKey(key);
78
- }
79
- };
80
- else if (isDisabled) itemProps.onMouseDown = (e)=>{
81
- // Prevent focus going to the body when clicking on a disabled item.
82
- e.preventDefault();
83
- };
84
- (0, $581M0$useEffect)(()=>{
85
- if (isDisabled && manager.focusedKey === key) manager.setFocusedKey(null);
86
- }, [
87
- manager,
88
- isDisabled,
89
- key
90
- ]);
91
- // With checkbox selection, onAction (i.e. navigation) becomes primary, and occurs on a single click of the row.
92
- // Clicking the checkbox enters selection mode, after which clicking anywhere on any row toggles selection for that row.
93
- // With highlight selection, onAction is secondary, and occurs on double click. Single click selects the row.
94
- // With touch, onAction occurs on single tap, and long press enters selection mode.
95
- let isLinkOverride = manager.isLink(key) && linkBehavior === 'override';
96
- let isActionOverride = onAction && options['UNSTABLE_itemBehavior'] === 'action';
97
- let hasLinkAction = manager.isLink(key) && linkBehavior !== 'selection' && linkBehavior !== 'none';
98
- let allowsSelection = !isDisabled && manager.canSelectItem(key) && !isLinkOverride && !isActionOverride;
99
- let allowsActions = (onAction || hasLinkAction) && !isDisabled;
100
- let hasPrimaryAction = allowsActions && (manager.selectionBehavior === 'replace' ? !allowsSelection : !allowsSelection || manager.isEmpty);
101
- let hasSecondaryAction = allowsActions && allowsSelection && manager.selectionBehavior === 'replace';
102
- let hasAction = hasPrimaryAction || hasSecondaryAction;
103
- let modality = (0, $581M0$useRef)(null);
104
- let longPressEnabled = hasAction && allowsSelection;
105
- let longPressEnabledOnPressStart = (0, $581M0$useRef)(false);
106
- let hadPrimaryActionOnPressStart = (0, $581M0$useRef)(false);
107
- let collectionItemProps = manager.getItemProps(key);
108
- let performAction = (e)=>{
109
- if (onAction) {
110
- var _ref_current;
111
- onAction();
112
- (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.dispatchEvent(new CustomEvent('react-aria-item-action', {
113
- bubbles: true
114
- }));
115
- }
116
- if (hasLinkAction && ref.current) router.open(ref.current, e, collectionItemProps.href, collectionItemProps.routerOptions);
117
- };
118
- // By default, selection occurs on pointer down. This can be strange if selecting an
119
- // item causes the UI to disappear immediately (e.g. menus).
120
- // If shouldSelectOnPressUp is true, we use onPressUp instead of onPressStart.
121
- // onPress requires a pointer down event on the same element as pointer up. For menus,
122
- // we want to be able to have the pointer down on the trigger that opens the menu and
123
- // the pointer up on the menu item rather than requiring a separate press.
124
- // For keyboard events, selection still occurs on key down.
125
- let itemPressProps = {
126
- ref: ref
127
- };
128
- if (shouldSelectOnPressUp) {
129
- itemPressProps.onPressStart = (e)=>{
130
- modality.current = e.pointerType;
131
- longPressEnabledOnPressStart.current = longPressEnabled;
132
- if (e.pointerType === 'keyboard' && (!hasAction || $880e95eb8b93ba9a$var$isSelectionKey(e.key))) onSelect(e);
133
- };
134
- // If allowsDifferentPressOrigin and interacting with mouse, make selection happen on pressUp (e.g. open menu on press down, selection on menu item happens on press up.)
135
- // Otherwise, have selection happen onPress (prevents listview row selection when clicking on interactable elements in the row)
136
- if (!allowsDifferentPressOrigin) itemPressProps.onPress = (e)=>{
137
- if (hasPrimaryAction || hasSecondaryAction && e.pointerType !== 'mouse') {
138
- if (e.pointerType === 'keyboard' && !$880e95eb8b93ba9a$var$isActionKey(e.key)) return;
139
- performAction(e);
140
- } else if (e.pointerType !== 'keyboard' && allowsSelection) onSelect(e);
141
- };
142
- else {
143
- itemPressProps.onPressUp = hasPrimaryAction ? undefined : (e)=>{
144
- if (e.pointerType === 'mouse' && allowsSelection) onSelect(e);
145
- };
146
- itemPressProps.onPress = hasPrimaryAction ? performAction : (e)=>{
147
- if (e.pointerType !== 'keyboard' && e.pointerType !== 'mouse' && allowsSelection) onSelect(e);
148
- };
149
- }
150
- } else {
151
- itemPressProps.onPressStart = (e)=>{
152
- modality.current = e.pointerType;
153
- longPressEnabledOnPressStart.current = longPressEnabled;
154
- hadPrimaryActionOnPressStart.current = hasPrimaryAction;
155
- // Select on mouse down unless there is a primary action which will occur on mouse up.
156
- // For keyboard, select on key down. If there is an action, the Space key selects on key down,
157
- // and the Enter key performs onAction on key up.
158
- if (allowsSelection && (e.pointerType === 'mouse' && !hasPrimaryAction || e.pointerType === 'keyboard' && (!allowsActions || $880e95eb8b93ba9a$var$isSelectionKey(e.key)))) onSelect(e);
159
- };
160
- itemPressProps.onPress = (e)=>{
161
- // Selection occurs on touch up. Primary actions always occur on pointer up.
162
- // Both primary and secondary actions occur on Enter key up. The only exception
163
- // is secondary actions, which occur on double click with a mouse.
164
- if (e.pointerType === 'touch' || e.pointerType === 'pen' || e.pointerType === 'virtual' || e.pointerType === 'keyboard' && hasAction && $880e95eb8b93ba9a$var$isActionKey(e.key) || e.pointerType === 'mouse' && hadPrimaryActionOnPressStart.current) {
165
- if (hasAction) performAction(e);
166
- else if (allowsSelection) onSelect(e);
167
- }
168
- };
169
- }
170
- itemProps['data-collection'] = (0, $feb5ffebff200149$export$6aeb1680a0ae8741)(manager.collection);
171
- itemProps['data-key'] = key;
172
- itemPressProps.preventFocusOnPress = shouldUseVirtualFocus;
173
- // When using virtual focus, make sure the focused key gets updated on press.
174
- if (shouldUseVirtualFocus) itemPressProps = (0, $581M0$mergeProps)(itemPressProps, {
175
- onPressStart (e) {
176
- if (e.pointerType !== 'touch') {
177
- manager.setFocused(true);
178
- manager.setFocusedKey(key);
179
- }
180
- },
181
- onPress (e) {
182
- if (e.pointerType === 'touch') {
183
- manager.setFocused(true);
184
- manager.setFocusedKey(key);
185
- }
186
- }
187
- });
188
- if (collectionItemProps) {
189
- for (let key of [
190
- 'onPressStart',
191
- 'onPressEnd',
192
- 'onPressChange',
193
- 'onPress',
194
- 'onPressUp',
195
- 'onClick'
196
- ])if (collectionItemProps[key]) itemPressProps[key] = (0, $581M0$chain)(itemPressProps[key], collectionItemProps[key]);
197
- }
198
- let { pressProps: pressProps, isPressed: isPressed } = (0, $581M0$usePress)(itemPressProps);
199
- // Double clicking with a mouse with selectionBehavior = 'replace' performs an action.
200
- let onDoubleClick = hasSecondaryAction ? (e)=>{
201
- if (modality.current === 'mouse') {
202
- e.stopPropagation();
203
- e.preventDefault();
204
- performAction(e);
205
- }
206
- } : undefined;
207
- // Long pressing an item with touch when selectionBehavior = 'replace' switches the selection behavior
208
- // to 'toggle'. This changes the single tap behavior from performing an action (i.e. navigating) to
209
- // selecting, and may toggle the appearance of a UI affordance like checkboxes on each item.
210
- let { longPressProps: longPressProps } = (0, $581M0$useLongPress)({
211
- isDisabled: !longPressEnabled,
212
- onLongPress (e) {
213
- if (e.pointerType === 'touch') {
214
- onSelect(e);
215
- manager.setSelectionBehavior('toggle');
216
- }
217
- }
218
- });
219
- // Prevent native drag and drop on long press if we also select on long press.
220
- // Once the user is in selection mode, they can long press again to drag.
221
- // Use a capturing listener to ensure this runs before useDrag, regardless of
222
- // the order the props get merged.
223
- let onDragStartCapture = (e)=>{
224
- if (modality.current === 'touch' && longPressEnabledOnPressStart.current) e.preventDefault();
225
- };
226
- // Prevent default on link clicks so that we control exactly
227
- // when they open (to match selection behavior).
228
- let onClick = linkBehavior !== 'none' && manager.isLink(key) ? (e)=>{
229
- if (!(0, $581M0$openLink).isOpening) e.preventDefault();
230
- } : undefined;
231
- return {
232
- itemProps: (0, $581M0$mergeProps)(itemProps, allowsSelection || hasPrimaryAction || shouldUseVirtualFocus && !isDisabled ? pressProps : {}, longPressEnabled ? longPressProps : {}, {
233
- onDoubleClick: onDoubleClick,
234
- onDragStartCapture: onDragStartCapture,
235
- onClick: onClick,
236
- id: id
237
- }, // Prevent DOM focus from moving on mouse down when using virtual focus
238
- shouldUseVirtualFocus ? {
239
- onMouseDown: (e)=>e.preventDefault()
240
- } : undefined),
241
- isPressed: isPressed,
242
- isSelected: manager.isSelected(key),
243
- isFocused: manager.isFocused && manager.focusedKey === key,
244
- isDisabled: isDisabled,
245
- allowsSelection: allowsSelection,
246
- hasAction: hasAction
247
- };
248
- }
249
- function $880e95eb8b93ba9a$var$isActionKey(key) {
250
- return key === 'Enter';
251
- }
252
- function $880e95eb8b93ba9a$var$isSelectionKey(key) {
253
- return key === ' ';
254
- }
255
-
256
-
257
- export {$880e95eb8b93ba9a$export$ecf600387e221c37 as useSelectableItem};
258
- //# sourceMappingURL=useSelectableItem.module.js.map