@react-aria/selection 3.22.0 → 3.23.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.
@@ -1,7 +1,7 @@
1
- import {isNonContiguousSelectionModifier as $feb5ffebff200149$export$d3e3bd3e26688c04} from "./utils.module.js";
2
- import {focusSafely as $581M0$focusSafely} from "@react-aria/focus";
3
- import {useRouter as $581M0$useRouter, useId as $581M0$useId, isCtrlKeyPressed as $581M0$isCtrlKeyPressed, UPDATE_ACTIVEDESCENDANT as $581M0$UPDATE_ACTIVEDESCENDANT, openLink as $581M0$openLink, mergeProps as $581M0$mergeProps} from "@react-aria/utils";
4
- import {usePress as $581M0$usePress, useLongPress as $581M0$useLongPress} from "@react-aria/interactions";
1
+ import {getCollectionId as $feb5ffebff200149$export$6aeb1680a0ae8741, isNonContiguousSelectionModifier as $feb5ffebff200149$export$d3e3bd3e26688c04} from "./utils.module.js";
2
+ import {focusSafely as $581M0$focusSafely, usePress as $581M0$usePress, useLongPress as $581M0$useLongPress} from "@react-aria/interactions";
3
+ import {useRouter as $581M0$useRouter, useId as $581M0$useId, isCtrlKeyPressed as $581M0$isCtrlKeyPressed, mergeProps as $581M0$mergeProps, openLink as $581M0$openLink} from "@react-aria/utils";
4
+ import {moveVirtualFocus as $581M0$moveVirtualFocus} from "@react-aria/focus";
5
5
  import {useEffect as $581M0$useEffect, useRef as $581M0$useRef} from "react";
6
6
 
7
7
  /*
@@ -47,7 +47,7 @@ function $880e95eb8b93ba9a$export$ecf600387e221c37(options) {
47
47
  };
48
48
  // Focus the associated DOM node when this item becomes the focusedKey
49
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 UPDATE_ACTIVEDESCENDANT and immediately sets
50
+ // However, if this is a useEffect, it runs twice and dispatches two blur events and immediately sets
51
51
  // aria-activeDescendant in useAutocomplete... I've worked around this for now
52
52
  (0, $581M0$useEffect)(()=>{
53
53
  let isFocused = key === manager.focusedKey;
@@ -55,14 +55,7 @@ function $880e95eb8b93ba9a$export$ecf600387e221c37(options) {
55
55
  if (!shouldUseVirtualFocus) {
56
56
  if (focus) focus();
57
57
  else if (document.activeElement !== ref.current && ref.current) (0, $581M0$focusSafely)(ref.current);
58
- } else {
59
- var _ref_current;
60
- let updateActiveDescendant = new CustomEvent((0, $581M0$UPDATE_ACTIVEDESCENDANT), {
61
- cancelable: true,
62
- bubbles: true
63
- });
64
- (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.dispatchEvent(updateActiveDescendant);
65
- }
58
+ } else (0, $581M0$moveVirtualFocus)(ref.current);
66
59
  }
67
60
  // eslint-disable-next-line react-hooks/exhaustive-deps
68
61
  }, [
@@ -160,8 +153,24 @@ function $880e95eb8b93ba9a$export$ecf600387e221c37(options) {
160
153
  }
161
154
  };
162
155
  }
156
+ itemProps['data-collection'] = (0, $feb5ffebff200149$export$6aeb1680a0ae8741)(manager.collection);
163
157
  itemProps['data-key'] = key;
164
158
  itemPressProps.preventFocusOnPress = shouldUseVirtualFocus;
159
+ // When using virtual focus, make sure the focused key gets updated on press.
160
+ if (shouldUseVirtualFocus) itemPressProps = (0, $581M0$mergeProps)(itemPressProps, {
161
+ onPressStart (e) {
162
+ if (e.pointerType !== 'touch') {
163
+ manager.setFocused(true);
164
+ manager.setFocusedKey(key);
165
+ }
166
+ },
167
+ onPress (e) {
168
+ if (e.pointerType === 'touch') {
169
+ manager.setFocused(true);
170
+ manager.setFocusedKey(key);
171
+ }
172
+ }
173
+ });
165
174
  let { pressProps: pressProps, isPressed: isPressed } = (0, $581M0$usePress)(itemPressProps);
166
175
  // Double clicking with a mouse with selectionBehavior = 'replace' performs an action.
167
176
  let onDoubleClick = hasSecondaryAction ? (e)=>{
@@ -196,12 +205,15 @@ function $880e95eb8b93ba9a$export$ecf600387e221c37(options) {
196
205
  if (!(0, $581M0$openLink).isOpening) e.preventDefault();
197
206
  } : undefined;
198
207
  return {
199
- itemProps: (0, $581M0$mergeProps)(itemProps, allowsSelection || hasPrimaryAction ? pressProps : {}, longPressEnabled ? longPressProps : {}, {
208
+ itemProps: (0, $581M0$mergeProps)(itemProps, allowsSelection || hasPrimaryAction || shouldUseVirtualFocus ? pressProps : {}, longPressEnabled ? longPressProps : {}, {
200
209
  onDoubleClick: onDoubleClick,
201
210
  onDragStartCapture: onDragStartCapture,
202
211
  onClick: onClick,
203
212
  id: id
204
- }),
213
+ }, // Prevent DOM focus from moving on mouse down when using virtual focus
214
+ shouldUseVirtualFocus ? {
215
+ onMouseDown: (e)=>e.preventDefault()
216
+ } : undefined),
205
217
  isPressed: isPressed,
206
218
  isSelected: manager.isSelected(key),
207
219
  isFocused: manager.isFocused && manager.focusedKey === key,
@@ -1 +1 @@
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,gBAAQ;IACrB,KAAK,CAAA,GAAA,YAAI,EAAE;IACX,IAAI,WAAW,CAAC;QACd,IAAI,EAAE,WAAW,KAAK,cAAc,CAAA,GAAA,yCAA+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,IAAI,CAAC,IAAI,OAAO,EAAE,GAAG,UAAU,IAAI,EAAE,UAAU,aAAa;oBACnE,qFAAqF;oBACrF,QAAQ,eAAe,CAAC,QAAQ,YAAY;oBAC5C;gBACF,OAA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IACxB,OAAO,CAAA,kBAAA,4BAAA,MAAO,GAAG,MAAK;AACxB;AAEA,SAAS;IACP,IAAI,QAAQ,OAAO,KAAK;IACxB,OAAO,CAAA,kBAAA,4BAAA,MAAO,GAAG,MAAK,OAAO,CAAA,kBAAA,4BAAA,MAAO,IAAI,MAAK;AAC/C","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 {DOMAttributes, DOMProps, FocusableElement, Key, LongPressEvent, PointerType, PressEvent, RefObject} from '@react-types/shared';\nimport {focusSafely} from '@react-aria/focus';\nimport {isCtrlKeyPressed, mergeProps, openLink, UPDATE_ACTIVEDESCENDANT, useId, useRouter} from '@react-aria/utils';\nimport {isNonContiguousSelectionModifier} from './utils';\nimport {MultipleSelectionManager} from '@react-stately/selection';\nimport {PressProps, useLongPress, usePress} from '@react-aria/interactions';\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 UPDATE_ACTIVEDESCENDANT 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 (document.activeElement !== ref.current && ref.current) {\n focusSafely(ref.current);\n }\n } else {\n let updateActiveDescendant = new CustomEvent(UPDATE_ACTIVEDESCENDANT, {\n cancelable: true,\n bubbles: true\n });\n\n ref.current?.dispatchEvent(updateActiveDescendant);\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 (e.target === 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 // 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 hasLinkAction = manager.isLink(key) && linkBehavior !== 'selection' && linkBehavior !== 'none';\n let allowsSelection = !isDisabled && manager.canSelectItem(key) && !isLinkOverride;\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\n let performAction = (e) => {\n if (onAction) {\n onAction();\n }\n\n if (hasLinkAction && ref.current) {\n let itemProps = manager.getItemProps(key);\n router.open(ref.current, e, itemProps.href, itemProps.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: PressProps = {};\n if (shouldSelectOnPressUp) {\n itemPressProps.onPressStart = (e) => {\n modality.current = e.pointerType;\n longPressEnabledOnPressStart.current = longPressEnabled;\n if (e.pointerType === 'keyboard' && (!hasAction || isSelectionKey())) {\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()) {\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()))\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()) ||\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-key'] = key;\n itemPressProps.preventFocusOnPress = shouldUseVirtualFocus;\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 = 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 ? pressProps : {},\n longPressEnabled ? longPressProps : {},\n {onDoubleClick, onDragStartCapture, onClick, id}\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() {\n let event = window.event as KeyboardEvent;\n return event?.key === 'Enter';\n}\n\nfunction isSelectionKey() {\n let event = window.event as KeyboardEvent;\n return event?.key === ' ' || event?.code === 'Space';\n}\n"],"names":[],"version":3,"file":"useSelectableItem.module.js.map"}
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,gBAAQ;IACrB,KAAK,CAAA,GAAA,YAAI,EAAE;IACX,IAAI,WAAW,CAAC;QACd,IAAI,EAAE,WAAW,KAAK,cAAc,CAAA,GAAA,yCAA+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,IAAI,CAAC,IAAI,OAAO,EAAE,GAAG,UAAU,IAAI,EAAE,UAAU,aAAa;oBACnE,qFAAqF;oBACrF,QAAQ,eAAe,CAAC,QAAQ,YAAY;oBAC5C;gBACF,OAAO,IAAI,iBAAiB,cAAc,iBAAiB,QACzD;YAEJ;YAEA,IAAI,QAAQ,aAAa,KAAK;gBAC5B,IAAI,QAAQ,UAAU,CAAC,QAAQ,CAAC,QAAQ,sBAAsB,EAC5D,QAAQ,eAAe,CAAC;qBAExB,QAAQ,gBAAgB,CAAC;mBAEtB,IAAI,KAAK,EAAE,QAAQ,EACxB,QAAQ,eAAe,CAAC;iBACnB,IAAI,QAAQ,iBAAiB,KAAK,YAAa,KAA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ACA;IACF;AACF;AAEA,SAAS;IACP,IAAI,QAAQ,OAAO,KAAK;IACxB,OAAO,CAAA,kBAAA,4BAAA,MAAO,GAAG,MAAK;AACxB;AAEA,SAAS;IACP,IAAI,QAAQ,OAAO,KAAK;IACxB,OAAO,CAAA,kBAAA,4BAAA,MAAO,GAAG,MAAK,OAAO,CAAA,kBAAA,4BAAA,MAAO,IAAI,MAAK;AAC/C","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 {DOMAttributes, DOMProps, FocusableElement, Key, LongPressEvent, PointerType, PressEvent, RefObject} from '@react-types/shared';\nimport {focusSafely, PressProps, useLongPress, usePress} from '@react-aria/interactions';\nimport {getCollectionId, isNonContiguousSelectionModifier} from './utils';\nimport {isCtrlKeyPressed, mergeProps, openLink, useId, useRouter} from '@react-aria/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 (document.activeElement !== 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 (e.target === 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 // 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 hasLinkAction = manager.isLink(key) && linkBehavior !== 'selection' && linkBehavior !== 'none';\n let allowsSelection = !isDisabled && manager.canSelectItem(key) && !isLinkOverride;\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\n let performAction = (e) => {\n if (onAction) {\n onAction();\n }\n\n if (hasLinkAction && ref.current) {\n let itemProps = manager.getItemProps(key);\n router.open(ref.current, e, itemProps.href, itemProps.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: PressProps = {};\n if (shouldSelectOnPressUp) {\n itemPressProps.onPressStart = (e) => {\n modality.current = e.pointerType;\n longPressEnabledOnPressStart.current = longPressEnabled;\n if (e.pointerType === 'keyboard' && (!hasAction || isSelectionKey())) {\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()) {\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()))\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()) ||\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 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 = 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 ? 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() {\n let event = window.event as KeyboardEvent;\n return event?.key === 'Enter';\n}\n\nfunction isSelectionKey() {\n let event = window.event as KeyboardEvent;\n return event?.key === ' ' || event?.code === 'Space';\n}\n"],"names":[],"version":3,"file":"useSelectableItem.module.js.map"}
@@ -6,6 +6,9 @@ function $parcel$export(e, n, v, s) {
6
6
  }
7
7
 
8
8
  $parcel$export(module.exports, "isNonContiguousSelectionModifier", () => $ee0bdf4faa47f2a8$export$d3e3bd3e26688c04);
9
+ $parcel$export(module.exports, "getItemElement", () => $ee0bdf4faa47f2a8$export$c3d8340acf92597f);
10
+ $parcel$export(module.exports, "useCollectionId", () => $ee0bdf4faa47f2a8$export$881eb0d9f3605d9d);
11
+ $parcel$export(module.exports, "getCollectionId", () => $ee0bdf4faa47f2a8$export$6aeb1680a0ae8741);
9
12
  /*
10
13
  * Copyright 2020 Adobe. All rights reserved.
11
14
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
@@ -22,6 +25,22 @@ function $ee0bdf4faa47f2a8$export$d3e3bd3e26688c04(e) {
22
25
  // On Windows and Ubuntu, Alt + Space has a system wide meaning.
23
26
  return (0, $gP8Dl$reactariautils.isAppleDevice)() ? e.altKey : e.ctrlKey;
24
27
  }
28
+ function $ee0bdf4faa47f2a8$export$c3d8340acf92597f(collectionRef, key) {
29
+ var _collectionRef_current, _collectionRef_current1;
30
+ let selector = `[data-key="${CSS.escape(String(key))}"]`;
31
+ let collection = (_collectionRef_current = collectionRef.current) === null || _collectionRef_current === void 0 ? void 0 : _collectionRef_current.dataset.collection;
32
+ if (collection) selector = `[data-collection="${CSS.escape(collection)}"]${selector}`;
33
+ return (_collectionRef_current1 = collectionRef.current) === null || _collectionRef_current1 === void 0 ? void 0 : _collectionRef_current1.querySelector(selector);
34
+ }
35
+ const $ee0bdf4faa47f2a8$var$collectionMap = new WeakMap();
36
+ function $ee0bdf4faa47f2a8$export$881eb0d9f3605d9d(collection) {
37
+ let id = (0, $gP8Dl$reactariautils.useId)();
38
+ $ee0bdf4faa47f2a8$var$collectionMap.set(collection, id);
39
+ return id;
40
+ }
41
+ function $ee0bdf4faa47f2a8$export$6aeb1680a0ae8741(collection) {
42
+ return $ee0bdf4faa47f2a8$var$collectionMap.get(collection);
43
+ }
25
44
 
26
45
 
27
46
  //# sourceMappingURL=utils.main.js.map
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AAUM,SAAS,0CAAiC,CAAQ;IACvD,qFAAqF;IACrF,gEAAgE;IAChE,OAAO,CAAA,GAAA,mCAAY,MAAM,EAAE,MAAM,GAAG,EAAE,OAAO;AAC/C","sources":["packages/@react-aria/selection/src/utils.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 {isAppleDevice} from '@react-aria/utils';\n\ninterface Event {\n altKey: boolean,\n ctrlKey: boolean,\n metaKey: boolean\n}\n\nexport function isNonContiguousSelectionModifier(e: Event) {\n // Ctrl + Arrow Up/Arrow Down has a system wide meaning on macOS, so use Alt instead.\n // On Windows and Ubuntu, Alt + Space has a system wide meaning.\n return isAppleDevice() ? e.altKey : e.ctrlKey;\n}\n"],"names":[],"version":3,"file":"utils.main.js.map"}
1
+ {"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;CAUC;AAYM,SAAS,0CAAiC,CAAQ;IACvD,qFAAqF;IACrF,gEAAgE;IAChE,OAAO,CAAA,GAAA,mCAAY,MAAM,EAAE,MAAM,GAAG,EAAE,OAAO;AAC/C;AAEO,SAAS,0CAAe,aAA4C,EAAE,GAAQ;QAElE,wBAIV;IALP,IAAI,WAAW,CAAC,WAAW,EAAE,IAAI,MAAM,CAAC,OAAO,MAAM,EAAE,CAAC;IACxD,IAAI,cAAa,yBAAA,cAAc,OAAO,cAArB,6CAAA,uBAAuB,OAAO,CAAC,UAAU;IAC1D,IAAI,YACF,WAAW,CAAC,kBAAkB,EAAE,IAAI,MAAM,CAAC,YAAY,EAAE,EAAE,UAAU;IAEvE,QAAO,0BAAA,cAAc,OAAO,cAArB,8CAAA,wBAAuB,aAAa,CAAC;AAC9C;AAEA,MAAM,sCAAgB,IAAI;AACnB,SAAS,0CAAgB,UAA2B;IACzD,IAAI,KAAK,CAAA,GAAA,2BAAI;IACb,oCAAc,GAAG,CAAC,YAAY;IAC9B,OAAO;AACT;AAEO,SAAS,0CAAgB,UAA2B;IACzD,OAAO,oCAAc,GAAG,CAAC;AAC3B","sources":["packages/@react-aria/selection/src/utils.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 {Collection, Key} from '@react-types/shared';\nimport {isAppleDevice, useId} from '@react-aria/utils';\nimport {RefObject} from 'react';\n\ninterface Event {\n altKey: boolean,\n ctrlKey: boolean,\n metaKey: boolean\n}\n\nexport function isNonContiguousSelectionModifier(e: Event) {\n // Ctrl + Arrow Up/Arrow Down has a system wide meaning on macOS, so use Alt instead.\n // On Windows and Ubuntu, Alt + Space has a system wide meaning.\n return isAppleDevice() ? e.altKey : e.ctrlKey;\n}\n\nexport function getItemElement(collectionRef: RefObject<HTMLElement | null>, key: Key) {\n let selector = `[data-key=\"${CSS.escape(String(key))}\"]`;\n let collection = collectionRef.current?.dataset.collection;\n if (collection) {\n selector = `[data-collection=\"${CSS.escape(collection)}\"]${selector}`;\n }\n return collectionRef.current?.querySelector(selector);\n}\n\nconst collectionMap = new WeakMap();\nexport function useCollectionId(collection: Collection<any>) {\n let id = useId();\n collectionMap.set(collection, id);\n return id;\n}\n\nexport function getCollectionId(collection: Collection<any>) {\n return collectionMap.get(collection)!;\n}\n"],"names":[],"version":3,"file":"utils.main.js.map"}
package/dist/utils.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import {isAppleDevice as $jUnAJ$isAppleDevice} from "@react-aria/utils";
1
+ import {isAppleDevice as $jUnAJ$isAppleDevice, useId as $jUnAJ$useId} from "@react-aria/utils";
2
2
 
3
3
  /*
4
4
  * Copyright 2020 Adobe. All rights reserved.
@@ -16,7 +16,23 @@ function $feb5ffebff200149$export$d3e3bd3e26688c04(e) {
16
16
  // On Windows and Ubuntu, Alt + Space has a system wide meaning.
17
17
  return (0, $jUnAJ$isAppleDevice)() ? e.altKey : e.ctrlKey;
18
18
  }
19
+ function $feb5ffebff200149$export$c3d8340acf92597f(collectionRef, key) {
20
+ var _collectionRef_current, _collectionRef_current1;
21
+ let selector = `[data-key="${CSS.escape(String(key))}"]`;
22
+ let collection = (_collectionRef_current = collectionRef.current) === null || _collectionRef_current === void 0 ? void 0 : _collectionRef_current.dataset.collection;
23
+ if (collection) selector = `[data-collection="${CSS.escape(collection)}"]${selector}`;
24
+ return (_collectionRef_current1 = collectionRef.current) === null || _collectionRef_current1 === void 0 ? void 0 : _collectionRef_current1.querySelector(selector);
25
+ }
26
+ const $feb5ffebff200149$var$collectionMap = new WeakMap();
27
+ function $feb5ffebff200149$export$881eb0d9f3605d9d(collection) {
28
+ let id = (0, $jUnAJ$useId)();
29
+ $feb5ffebff200149$var$collectionMap.set(collection, id);
30
+ return id;
31
+ }
32
+ function $feb5ffebff200149$export$6aeb1680a0ae8741(collection) {
33
+ return $feb5ffebff200149$var$collectionMap.get(collection);
34
+ }
19
35
 
20
36
 
21
- export {$feb5ffebff200149$export$d3e3bd3e26688c04 as isNonContiguousSelectionModifier};
37
+ export {$feb5ffebff200149$export$d3e3bd3e26688c04 as isNonContiguousSelectionModifier, $feb5ffebff200149$export$c3d8340acf92597f as getItemElement, $feb5ffebff200149$export$881eb0d9f3605d9d as useCollectionId, $feb5ffebff200149$export$6aeb1680a0ae8741 as getCollectionId};
22
38
  //# sourceMappingURL=utils.module.js.map
@@ -1,4 +1,4 @@
1
- import {isAppleDevice as $jUnAJ$isAppleDevice} from "@react-aria/utils";
1
+ import {isAppleDevice as $jUnAJ$isAppleDevice, useId as $jUnAJ$useId} from "@react-aria/utils";
2
2
 
3
3
  /*
4
4
  * Copyright 2020 Adobe. All rights reserved.
@@ -16,7 +16,23 @@ function $feb5ffebff200149$export$d3e3bd3e26688c04(e) {
16
16
  // On Windows and Ubuntu, Alt + Space has a system wide meaning.
17
17
  return (0, $jUnAJ$isAppleDevice)() ? e.altKey : e.ctrlKey;
18
18
  }
19
+ function $feb5ffebff200149$export$c3d8340acf92597f(collectionRef, key) {
20
+ var _collectionRef_current, _collectionRef_current1;
21
+ let selector = `[data-key="${CSS.escape(String(key))}"]`;
22
+ let collection = (_collectionRef_current = collectionRef.current) === null || _collectionRef_current === void 0 ? void 0 : _collectionRef_current.dataset.collection;
23
+ if (collection) selector = `[data-collection="${CSS.escape(collection)}"]${selector}`;
24
+ return (_collectionRef_current1 = collectionRef.current) === null || _collectionRef_current1 === void 0 ? void 0 : _collectionRef_current1.querySelector(selector);
25
+ }
26
+ const $feb5ffebff200149$var$collectionMap = new WeakMap();
27
+ function $feb5ffebff200149$export$881eb0d9f3605d9d(collection) {
28
+ let id = (0, $jUnAJ$useId)();
29
+ $feb5ffebff200149$var$collectionMap.set(collection, id);
30
+ return id;
31
+ }
32
+ function $feb5ffebff200149$export$6aeb1680a0ae8741(collection) {
33
+ return $feb5ffebff200149$var$collectionMap.get(collection);
34
+ }
19
35
 
20
36
 
21
- export {$feb5ffebff200149$export$d3e3bd3e26688c04 as isNonContiguousSelectionModifier};
37
+ export {$feb5ffebff200149$export$d3e3bd3e26688c04 as isNonContiguousSelectionModifier, $feb5ffebff200149$export$c3d8340acf92597f as getItemElement, $feb5ffebff200149$export$881eb0d9f3605d9d as useCollectionId, $feb5ffebff200149$export$6aeb1680a0ae8741 as getCollectionId};
22
38
  //# sourceMappingURL=utils.module.js.map
@@ -1 +1 @@
1
- {"mappings":";;AAAA;;;;;;;;;;CAUC;AAUM,SAAS,0CAAiC,CAAQ;IACvD,qFAAqF;IACrF,gEAAgE;IAChE,OAAO,CAAA,GAAA,oBAAY,MAAM,EAAE,MAAM,GAAG,EAAE,OAAO;AAC/C","sources":["packages/@react-aria/selection/src/utils.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 {isAppleDevice} from '@react-aria/utils';\n\ninterface Event {\n altKey: boolean,\n ctrlKey: boolean,\n metaKey: boolean\n}\n\nexport function isNonContiguousSelectionModifier(e: Event) {\n // Ctrl + Arrow Up/Arrow Down has a system wide meaning on macOS, so use Alt instead.\n // On Windows and Ubuntu, Alt + Space has a system wide meaning.\n return isAppleDevice() ? e.altKey : e.ctrlKey;\n}\n"],"names":[],"version":3,"file":"utils.module.js.map"}
1
+ {"mappings":";;AAAA;;;;;;;;;;CAUC;AAYM,SAAS,0CAAiC,CAAQ;IACvD,qFAAqF;IACrF,gEAAgE;IAChE,OAAO,CAAA,GAAA,oBAAY,MAAM,EAAE,MAAM,GAAG,EAAE,OAAO;AAC/C;AAEO,SAAS,0CAAe,aAA4C,EAAE,GAAQ;QAElE,wBAIV;IALP,IAAI,WAAW,CAAC,WAAW,EAAE,IAAI,MAAM,CAAC,OAAO,MAAM,EAAE,CAAC;IACxD,IAAI,cAAa,yBAAA,cAAc,OAAO,cAArB,6CAAA,uBAAuB,OAAO,CAAC,UAAU;IAC1D,IAAI,YACF,WAAW,CAAC,kBAAkB,EAAE,IAAI,MAAM,CAAC,YAAY,EAAE,EAAE,UAAU;IAEvE,QAAO,0BAAA,cAAc,OAAO,cAArB,8CAAA,wBAAuB,aAAa,CAAC;AAC9C;AAEA,MAAM,sCAAgB,IAAI;AACnB,SAAS,0CAAgB,UAA2B;IACzD,IAAI,KAAK,CAAA,GAAA,YAAI;IACb,oCAAc,GAAG,CAAC,YAAY;IAC9B,OAAO;AACT;AAEO,SAAS,0CAAgB,UAA2B;IACzD,OAAO,oCAAc,GAAG,CAAC;AAC3B","sources":["packages/@react-aria/selection/src/utils.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 {Collection, Key} from '@react-types/shared';\nimport {isAppleDevice, useId} from '@react-aria/utils';\nimport {RefObject} from 'react';\n\ninterface Event {\n altKey: boolean,\n ctrlKey: boolean,\n metaKey: boolean\n}\n\nexport function isNonContiguousSelectionModifier(e: Event) {\n // Ctrl + Arrow Up/Arrow Down has a system wide meaning on macOS, so use Alt instead.\n // On Windows and Ubuntu, Alt + Space has a system wide meaning.\n return isAppleDevice() ? e.altKey : e.ctrlKey;\n}\n\nexport function getItemElement(collectionRef: RefObject<HTMLElement | null>, key: Key) {\n let selector = `[data-key=\"${CSS.escape(String(key))}\"]`;\n let collection = collectionRef.current?.dataset.collection;\n if (collection) {\n selector = `[data-collection=\"${CSS.escape(collection)}\"]${selector}`;\n }\n return collectionRef.current?.querySelector(selector);\n}\n\nconst collectionMap = new WeakMap();\nexport function useCollectionId(collection: Collection<any>) {\n let id = useId();\n collectionMap.set(collection, id);\n return id;\n}\n\nexport function getCollectionId(collection: Collection<any>) {\n return collectionMap.get(collection)!;\n}\n"],"names":[],"version":3,"file":"utils.module.js.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-aria/selection",
3
- "version": "3.22.0",
3
+ "version": "3.23.1",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -22,12 +22,12 @@
22
22
  "url": "https://github.com/adobe/react-spectrum"
23
23
  },
24
24
  "dependencies": {
25
- "@react-aria/focus": "^3.19.1",
26
- "@react-aria/i18n": "^3.12.5",
27
- "@react-aria/interactions": "^3.23.0",
28
- "@react-aria/utils": "^3.27.0",
29
- "@react-stately/selection": "^3.19.0",
30
- "@react-types/shared": "^3.27.0",
25
+ "@react-aria/focus": "^3.20.1",
26
+ "@react-aria/i18n": "^3.12.7",
27
+ "@react-aria/interactions": "^3.24.1",
28
+ "@react-aria/utils": "^3.28.1",
29
+ "@react-stately/selection": "^3.20.0",
30
+ "@react-types/shared": "^3.28.0",
31
31
  "@swc/helpers": "^0.5.0"
32
32
  },
33
33
  "peerDependencies": {
@@ -37,5 +37,5 @@
37
37
  "publishConfig": {
38
38
  "access": "public"
39
39
  },
40
- "gitHead": "09e7f44bebdc9d89122926b2b439a0a38a2814ea"
40
+ "gitHead": "9c4ebbc0c1972cc880febc29de995ca58caa3ba4"
41
41
  }
@@ -10,6 +10,7 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
+ import {getItemElement} from './utils';
13
14
  import {Key, LayoutDelegate, Rect, RefObject, Size} from '@react-types/shared';
14
15
 
15
16
  export class DOMLayoutDelegate implements LayoutDelegate {
@@ -24,7 +25,7 @@ export class DOMLayoutDelegate implements LayoutDelegate {
24
25
  if (!container) {
25
26
  return null;
26
27
  }
27
- let item = key != null ? container.querySelector(`[data-key="${CSS.escape(key.toString())}"]`) : null;
28
+ let item = key != null ? getItemElement(this.ref, key) : null;
28
29
  if (!item) {
29
30
  return null;
30
31
  }
@@ -10,13 +10,13 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {CLEAR_FOCUS_EVENT, FOCUS_EVENT, focusWithoutScrolling, isCtrlKeyPressed, mergeProps, scrollIntoView, scrollIntoViewport, UPDATE_ACTIVEDESCENDANT, useEffectEvent, useEvent, useRouter, useUpdateLayoutEffect} from '@react-aria/utils';
13
+ import {CLEAR_FOCUS_EVENT, FOCUS_EVENT, focusWithoutScrolling, isCtrlKeyPressed, mergeProps, scrollIntoView, scrollIntoViewport, useEffectEvent, useEvent, useRouter, useUpdateLayoutEffect} from '@react-aria/utils';
14
14
  import {DOMAttributes, FocusableElement, FocusStrategy, Key, KeyboardDelegate, RefObject} from '@react-types/shared';
15
15
  import {flushSync} from 'react-dom';
16
16
  import {FocusEvent, KeyboardEvent, useEffect, useRef} from 'react';
17
- import {focusSafely, getFocusableTreeWalker} from '@react-aria/focus';
18
- import {getInteractionModality} from '@react-aria/interactions';
19
- import {isNonContiguousSelectionModifier} from './utils';
17
+ import {focusSafely, getInteractionModality} from '@react-aria/interactions';
18
+ import {getFocusableTreeWalker, moveVirtualFocus} from '@react-aria/focus';
19
+ import {getItemElement, isNonContiguousSelectionModifier, useCollectionId} from './utils';
20
20
  import {MultipleSelectionManager} from '@react-stately/selection';
21
21
  import {useLocale} from '@react-aria/i18n';
22
22
  import {useTypeSelect} from './useTypeSelect';
@@ -140,7 +140,7 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
140
140
  manager.setFocusedKey(key, childFocus);
141
141
  });
142
142
 
143
- let item = scrollRef.current?.querySelector(`[data-key="${CSS.escape(key.toString())}"]`);
143
+ let item = getItemElement(ref, key);
144
144
  let itemProps = manager.getItemProps(key);
145
145
  if (item) {
146
146
  router.open(item, e, itemProps.href, itemProps.routerOptions);
@@ -342,12 +342,11 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
342
342
  }
343
343
 
344
344
  manager.setFocused(true);
345
-
346
345
  if (manager.focusedKey == null) {
347
- let navigateToFirstKey = (key: Key | undefined | null) => {
346
+ let navigateToKey = (key: Key | undefined | null) => {
348
347
  if (key != null) {
349
348
  manager.setFocusedKey(key);
350
- if (selectOnFocus) {
349
+ if (selectOnFocus && !manager.isSelected(key)) {
351
350
  manager.replaceSelection(key);
352
351
  }
353
352
  }
@@ -357,9 +356,9 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
357
356
  // and either focus the first or last item accordingly.
358
357
  let relatedTarget = e.relatedTarget as Element;
359
358
  if (relatedTarget && (e.currentTarget.compareDocumentPosition(relatedTarget) & Node.DOCUMENT_POSITION_FOLLOWING)) {
360
- navigateToFirstKey(manager.lastSelectedKey ?? delegate.getLastKey?.());
359
+ navigateToKey(manager.lastSelectedKey ?? delegate.getLastKey?.());
361
360
  } else {
362
- navigateToFirstKey(manager.firstSelectedKey ?? delegate.getFirstKey?.());
361
+ navigateToKey(manager.firstSelectedKey ?? delegate.getFirstKey?.());
363
362
  }
364
363
  } else if (!isVirtualized && scrollRef.current) {
365
364
  // Restore the scroll position to what it was before.
@@ -369,10 +368,10 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
369
368
 
370
369
  if (manager.focusedKey != null && scrollRef.current) {
371
370
  // Refocus and scroll the focused item into view if it exists within the scrollable region.
372
- let element = scrollRef.current.querySelector(`[data-key="${CSS.escape(manager.focusedKey.toString())}"]`) as HTMLElement;
373
- if (element) {
371
+ let element = getItemElement(ref, manager.focusedKey);
372
+ if (element instanceof HTMLElement) {
374
373
  // This prevents a flash of focus on the first/last element in the collection, or the collection itself.
375
- if (!element.contains(document.activeElement)) {
374
+ if (!element.contains(document.activeElement) && !shouldUseVirtualFocus) {
376
375
  focusWithoutScrolling(element);
377
376
  }
378
377
 
@@ -414,12 +413,7 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
414
413
 
415
414
  // If no focusable items exist in the list, make sure to clear any activedescendant that may still exist
416
415
  if (keyToFocus == null) {
417
- ref.current?.dispatchEvent(
418
- new CustomEvent(UPDATE_ACTIVEDESCENDANT, {
419
- cancelable: true,
420
- bubbles: true
421
- })
422
- );
416
+ moveVirtualFocus(ref.current);
423
417
 
424
418
  // If there wasn't a focusable key but the collection had items, then that means we aren't in an intermediate load state and all keys are disabled.
425
419
  // Reset shouldVirtualFocusFirst so that we don't erronously autofocus an item when the collection is filtered again.
@@ -455,13 +449,16 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
455
449
  resetFocusFirstFlag();
456
450
  }, [manager.focusedKey, resetFocusFirstFlag]);
457
451
 
458
- useEvent(ref, CLEAR_FOCUS_EVENT, !shouldUseVirtualFocus ? undefined : (e) => {
452
+ useEvent(ref, CLEAR_FOCUS_EVENT, !shouldUseVirtualFocus ? undefined : (e: any) => {
459
453
  e.stopPropagation();
460
454
  manager.setFocused(false);
461
- manager.setFocusedKey(null);
455
+ if (e.detail?.clearFocusKey) {
456
+ manager.setFocusedKey(null);
457
+ }
462
458
  });
463
459
 
464
460
  const autoFocusRef = useRef(autoFocus);
461
+ const didAutoFocusRef = useRef(false);
465
462
  useEffect(() => {
466
463
  if (autoFocusRef.current) {
467
464
  let focusedKey: Key | null = null;
@@ -491,23 +488,28 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
491
488
  if (focusedKey == null && !shouldUseVirtualFocus && ref.current) {
492
489
  focusSafely(ref.current);
493
490
  }
491
+
492
+ // Wait until the collection has items to autofocus.
493
+ if (manager.collection.size > 0) {
494
+ autoFocusRef.current = false;
495
+ didAutoFocusRef.current = true;
496
+ }
494
497
  }
495
- // eslint-disable-next-line react-hooks/exhaustive-deps
496
- }, []);
498
+ });
497
499
 
498
500
  // Scroll the focused element into view when the focusedKey changes.
499
501
  let lastFocusedKey = useRef(manager.focusedKey);
500
502
  useEffect(() => {
501
- if (manager.isFocused && manager.focusedKey != null && (manager.focusedKey !== lastFocusedKey.current || autoFocusRef.current) && scrollRef.current && ref.current) {
503
+ if (manager.isFocused && manager.focusedKey != null && (manager.focusedKey !== lastFocusedKey.current || didAutoFocusRef.current) && scrollRef.current && ref.current) {
502
504
  let modality = getInteractionModality();
503
- let element = ref.current.querySelector(`[data-key="${CSS.escape(manager.focusedKey.toString())}"]`) as HTMLElement;
504
- if (!element) {
505
+ let element = getItemElement(ref, manager.focusedKey);
506
+ if (!(element instanceof HTMLElement)) {
505
507
  // If item element wasn't found, return early (don't update autoFocusRef and lastFocusedKey).
506
508
  // The collection may initially be empty (e.g. virtualizer), so wait until the element exists.
507
509
  return;
508
510
  }
509
511
 
510
- if (modality === 'keyboard' || autoFocusRef.current) {
512
+ if (modality === 'keyboard' || didAutoFocusRef.current) {
511
513
  scrollIntoView(scrollRef.current, element);
512
514
 
513
515
  // Avoid scroll in iOS VO, since it may cause overlay to close (i.e. RAC submenu)
@@ -523,7 +525,7 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
523
525
  }
524
526
 
525
527
  lastFocusedKey.current = manager.focusedKey;
526
- autoFocusRef.current = false;
528
+ didAutoFocusRef.current = false;
527
529
  });
528
530
 
529
531
  // Intercept FocusScope restoration since virtualized collections can reuse DOM nodes.
@@ -559,14 +561,13 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
559
561
  let tabIndex: number | undefined = undefined;
560
562
  if (!shouldUseVirtualFocus) {
561
563
  tabIndex = manager.focusedKey == null ? 0 : -1;
562
- } else {
563
- tabIndex = -1;
564
564
  }
565
565
 
566
+ let collectionId = useCollectionId(manager.collection);
566
567
  return {
567
- collectionProps: {
568
- ...handlers,
569
- tabIndex
570
- }
568
+ collectionProps: mergeProps(handlers, {
569
+ tabIndex,
570
+ 'data-collection': collectionId
571
+ })
571
572
  };
572
573
  }
@@ -11,11 +11,11 @@
11
11
  */
12
12
 
13
13
  import {DOMAttributes, DOMProps, FocusableElement, Key, LongPressEvent, PointerType, PressEvent, RefObject} from '@react-types/shared';
14
- import {focusSafely} from '@react-aria/focus';
15
- import {isCtrlKeyPressed, mergeProps, openLink, UPDATE_ACTIVEDESCENDANT, useId, useRouter} from '@react-aria/utils';
16
- import {isNonContiguousSelectionModifier} from './utils';
14
+ import {focusSafely, PressProps, useLongPress, usePress} from '@react-aria/interactions';
15
+ import {getCollectionId, isNonContiguousSelectionModifier} from './utils';
16
+ import {isCtrlKeyPressed, mergeProps, openLink, useId, useRouter} from '@react-aria/utils';
17
+ import {moveVirtualFocus} from '@react-aria/focus';
17
18
  import {MultipleSelectionManager} from '@react-stately/selection';
18
- import {PressProps, useLongPress, usePress} from '@react-aria/interactions';
19
19
  import {useEffect, useRef} from 'react';
20
20
 
21
21
  export interface SelectableItemOptions extends DOMProps {
@@ -161,7 +161,7 @@ export function useSelectableItem(options: SelectableItemOptions): SelectableIte
161
161
 
162
162
  // Focus the associated DOM node when this item becomes the focusedKey
163
163
  // TODO: can't make this useLayoutEffect bacause it breaks menus inside dialogs
164
- // However, if this is a useEffect, it runs twice and dispatches two UPDATE_ACTIVEDESCENDANT and immediately sets
164
+ // However, if this is a useEffect, it runs twice and dispatches two blur events and immediately sets
165
165
  // aria-activeDescendant in useAutocomplete... I've worked around this for now
166
166
  useEffect(() => {
167
167
  let isFocused = key === manager.focusedKey;
@@ -173,12 +173,7 @@ export function useSelectableItem(options: SelectableItemOptions): SelectableIte
173
173
  focusSafely(ref.current);
174
174
  }
175
175
  } else {
176
- let updateActiveDescendant = new CustomEvent(UPDATE_ACTIVEDESCENDANT, {
177
- cancelable: true,
178
- bubbles: true
179
- });
180
-
181
- ref.current?.dispatchEvent(updateActiveDescendant);
176
+ moveVirtualFocus(ref.current);
182
177
  }
183
178
  }
184
179
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -320,8 +315,28 @@ export function useSelectableItem(options: SelectableItemOptions): SelectableIte
320
315
  };
321
316
  }
322
317
 
318
+ itemProps['data-collection'] = getCollectionId(manager.collection);
323
319
  itemProps['data-key'] = key;
324
320
  itemPressProps.preventFocusOnPress = shouldUseVirtualFocus;
321
+
322
+ // When using virtual focus, make sure the focused key gets updated on press.
323
+ if (shouldUseVirtualFocus) {
324
+ itemPressProps = mergeProps(itemPressProps, {
325
+ onPressStart(e) {
326
+ if (e.pointerType !== 'touch') {
327
+ manager.setFocused(true);
328
+ manager.setFocusedKey(key);
329
+ }
330
+ },
331
+ onPress(e) {
332
+ if (e.pointerType === 'touch') {
333
+ manager.setFocused(true);
334
+ manager.setFocusedKey(key);
335
+ }
336
+ }
337
+ });
338
+ }
339
+
325
340
  let {pressProps, isPressed} = usePress(itemPressProps);
326
341
 
327
342
  // Double clicking with a mouse with selectionBehavior = 'replace' performs an action.
@@ -367,9 +382,11 @@ export function useSelectableItem(options: SelectableItemOptions): SelectableIte
367
382
  return {
368
383
  itemProps: mergeProps(
369
384
  itemProps,
370
- allowsSelection || hasPrimaryAction ? pressProps : {},
385
+ allowsSelection || hasPrimaryAction || shouldUseVirtualFocus ? pressProps : {},
371
386
  longPressEnabled ? longPressProps : {},
372
- {onDoubleClick, onDragStartCapture, onClick, id}
387
+ {onDoubleClick, onDragStartCapture, onClick, id},
388
+ // Prevent DOM focus from moving on mouse down when using virtual focus
389
+ shouldUseVirtualFocus ? {onMouseDown: e => e.preventDefault()} : undefined
373
390
  ),
374
391
  isPressed,
375
392
  isSelected: manager.isSelected(key),