2024-06-15 00:55:43 +03:00

815 lines
43 KiB
JavaScript

import {flushSync as $eCAIO$flushSync} from "react-dom";
import {useRef as $eCAIO$useRef, useEffect as $eCAIO$useEffect, useMemo as $eCAIO$useMemo} from "react";
import {getFocusableTreeWalker as $eCAIO$getFocusableTreeWalker, focusSafely as $eCAIO$focusSafely} from "@react-aria/focus";
import {useRouter as $eCAIO$useRouter, focusWithoutScrolling as $eCAIO$focusWithoutScrolling, useEvent as $eCAIO$useEvent, scrollIntoViewport as $eCAIO$scrollIntoViewport, scrollIntoView as $eCAIO$scrollIntoView, mergeProps as $eCAIO$mergeProps, isAppleDevice as $eCAIO$isAppleDevice, isMac as $eCAIO$isMac, openLink as $eCAIO$openLink, isScrollable as $eCAIO$isScrollable} from "@react-aria/utils";
import {getInteractionModality as $eCAIO$getInteractionModality, usePress as $eCAIO$usePress, useLongPress as $eCAIO$useLongPress} from "@react-aria/interactions";
import {useLocale as $eCAIO$useLocale, useCollator as $eCAIO$useCollator} from "@react-aria/i18n";
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/ /*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
function $feb5ffebff200149$export$d3e3bd3e26688c04(e) {
// Ctrl + Arrow Up/Arrow Down has a system wide meaning on macOS, so use Alt instead.
// On Windows and Ubuntu, Alt + Space has a system wide meaning.
return (0, $eCAIO$isAppleDevice)() ? e.altKey : e.ctrlKey;
}
function $feb5ffebff200149$export$16792effe837dba3(e) {
if ((0, $eCAIO$isMac)()) return e.metaKey;
return e.ctrlKey;
}
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/**
* Controls how long to wait before clearing the typeahead buffer.
*/ const $fb3050f43d946246$var$TYPEAHEAD_DEBOUNCE_WAIT_MS = 1000; // 1 second
function $fb3050f43d946246$export$e32c88dfddc6e1d8(options) {
let { keyboardDelegate: keyboardDelegate, selectionManager: selectionManager, onTypeSelect: onTypeSelect } = options;
let state = (0, $eCAIO$useRef)({
search: "",
timeout: null
}).current;
let onKeyDown = (e)=>{
let character = $fb3050f43d946246$var$getStringForKey(e.key);
if (!character || e.ctrlKey || e.metaKey || !e.currentTarget.contains(e.target)) return;
// Do not propagate the Spacebar event if it's meant to be part of the search.
// When we time out, the search term becomes empty, hence the check on length.
// Trimming is to account for the case of pressing the Spacebar more than once,
// which should cycle through the selection/deselection of the focused item.
if (character === " " && state.search.trim().length > 0) {
e.preventDefault();
if (!("continuePropagation" in e)) e.stopPropagation();
}
state.search += character;
// Use the delegate to find a key to focus.
// Prioritize items after the currently focused item, falling back to searching the whole list.
let key = keyboardDelegate.getKeyForSearch(state.search, selectionManager.focusedKey);
// If no key found, search from the top.
if (key == null) key = keyboardDelegate.getKeyForSearch(state.search);
if (key != null) {
selectionManager.setFocusedKey(key);
if (onTypeSelect) onTypeSelect(key);
}
clearTimeout(state.timeout);
state.timeout = setTimeout(()=>{
state.search = "";
}, $fb3050f43d946246$var$TYPEAHEAD_DEBOUNCE_WAIT_MS);
};
return {
typeSelectProps: {
// Using a capturing listener to catch the keydown event before
// other hooks in order to handle the Spacebar event.
onKeyDownCapture: keyboardDelegate.getKeyForSearch ? onKeyDown : null
}
};
}
function $fb3050f43d946246$var$getStringForKey(key) {
// If the key is of length 1, it is an ASCII value.
// Otherwise, if there are no ASCII characters in the key name,
// it is a Unicode character.
// See https://www.w3.org/TR/uievents-key/
if (key.length === 1 || !/^[A-Z]/i.test(key)) return key;
return "";
}
function $ae20dd8cbca75726$export$d6daf82dcd84e87c(options) {
let { selectionManager: manager, keyboardDelegate: delegate, ref: ref, autoFocus: autoFocus = false, shouldFocusWrap: shouldFocusWrap = false, disallowEmptySelection: disallowEmptySelection = false, disallowSelectAll: disallowSelectAll = false, selectOnFocus: selectOnFocus = manager.selectionBehavior === "replace", disallowTypeAhead: disallowTypeAhead = false, shouldUseVirtualFocus: shouldUseVirtualFocus, allowsTabNavigation: allowsTabNavigation = false, isVirtualized: isVirtualized, scrollRef: // If no scrollRef is provided, assume the collection ref is the scrollable region
scrollRef = ref, linkBehavior: linkBehavior = "action" } = options;
let { direction: direction } = (0, $eCAIO$useLocale)();
let router = (0, $eCAIO$useRouter)();
let onKeyDown = (e)=>{
// Prevent option + tab from doing anything since it doesn't move focus to the cells, only buttons/checkboxes
if (e.altKey && e.key === "Tab") e.preventDefault();
// Keyboard events bubble through portals. Don't handle keyboard events
// for elements outside the collection (e.g. menus).
if (!ref.current.contains(e.target)) return;
const navigateToKey = (key, childFocus)=>{
if (key != null) {
if (manager.isLink(key) && linkBehavior === "selection" && selectOnFocus && !(0, $feb5ffebff200149$export$d3e3bd3e26688c04)(e)) {
// Set focused key and re-render synchronously to bring item into view if needed.
(0, $eCAIO$flushSync)(()=>{
manager.setFocusedKey(key, childFocus);
});
let item = scrollRef.current.querySelector(`[data-key="${CSS.escape(key.toString())}"]`);
router.open(item, e);
return;
}
manager.setFocusedKey(key, childFocus);
if (manager.isLink(key) && linkBehavior === "override") return;
if (e.shiftKey && manager.selectionMode === "multiple") manager.extendSelection(key);
else if (selectOnFocus && !(0, $feb5ffebff200149$export$d3e3bd3e26688c04)(e)) manager.replaceSelection(key);
}
};
switch(e.key){
case "ArrowDown":
if (delegate.getKeyBelow) {
var _delegate_getFirstKey, _delegate_getFirstKey1;
e.preventDefault();
let nextKey = manager.focusedKey != null ? delegate.getKeyBelow(manager.focusedKey) : (_delegate_getFirstKey = delegate.getFirstKey) === null || _delegate_getFirstKey === void 0 ? void 0 : _delegate_getFirstKey.call(delegate);
if (nextKey == null && shouldFocusWrap) nextKey = (_delegate_getFirstKey1 = delegate.getFirstKey) === null || _delegate_getFirstKey1 === void 0 ? void 0 : _delegate_getFirstKey1.call(delegate, manager.focusedKey);
navigateToKey(nextKey);
}
break;
case "ArrowUp":
if (delegate.getKeyAbove) {
var _delegate_getLastKey, _delegate_getLastKey1;
e.preventDefault();
let nextKey = manager.focusedKey != null ? delegate.getKeyAbove(manager.focusedKey) : (_delegate_getLastKey = delegate.getLastKey) === null || _delegate_getLastKey === void 0 ? void 0 : _delegate_getLastKey.call(delegate);
if (nextKey == null && shouldFocusWrap) nextKey = (_delegate_getLastKey1 = delegate.getLastKey) === null || _delegate_getLastKey1 === void 0 ? void 0 : _delegate_getLastKey1.call(delegate, manager.focusedKey);
navigateToKey(nextKey);
}
break;
case "ArrowLeft":
if (delegate.getKeyLeftOf) {
var _delegate_getFirstKey2, _delegate_getLastKey2;
e.preventDefault();
let nextKey = delegate.getKeyLeftOf(manager.focusedKey);
if (nextKey == null && shouldFocusWrap) nextKey = direction === "rtl" ? (_delegate_getFirstKey2 = delegate.getFirstKey) === null || _delegate_getFirstKey2 === void 0 ? void 0 : _delegate_getFirstKey2.call(delegate, manager.focusedKey) : (_delegate_getLastKey2 = delegate.getLastKey) === null || _delegate_getLastKey2 === void 0 ? void 0 : _delegate_getLastKey2.call(delegate, manager.focusedKey);
navigateToKey(nextKey, direction === "rtl" ? "first" : "last");
}
break;
case "ArrowRight":
if (delegate.getKeyRightOf) {
var _delegate_getLastKey3, _delegate_getFirstKey3;
e.preventDefault();
let nextKey = delegate.getKeyRightOf(manager.focusedKey);
if (nextKey == null && shouldFocusWrap) nextKey = direction === "rtl" ? (_delegate_getLastKey3 = delegate.getLastKey) === null || _delegate_getLastKey3 === void 0 ? void 0 : _delegate_getLastKey3.call(delegate, manager.focusedKey) : (_delegate_getFirstKey3 = delegate.getFirstKey) === null || _delegate_getFirstKey3 === void 0 ? void 0 : _delegate_getFirstKey3.call(delegate, manager.focusedKey);
navigateToKey(nextKey, direction === "rtl" ? "last" : "first");
}
break;
case "Home":
if (delegate.getFirstKey) {
e.preventDefault();
let firstKey = delegate.getFirstKey(manager.focusedKey, (0, $feb5ffebff200149$export$16792effe837dba3)(e));
manager.setFocusedKey(firstKey);
if ((0, $feb5ffebff200149$export$16792effe837dba3)(e) && e.shiftKey && manager.selectionMode === "multiple") manager.extendSelection(firstKey);
else if (selectOnFocus) manager.replaceSelection(firstKey);
}
break;
case "End":
if (delegate.getLastKey) {
e.preventDefault();
let lastKey = delegate.getLastKey(manager.focusedKey, (0, $feb5ffebff200149$export$16792effe837dba3)(e));
manager.setFocusedKey(lastKey);
if ((0, $feb5ffebff200149$export$16792effe837dba3)(e) && e.shiftKey && manager.selectionMode === "multiple") manager.extendSelection(lastKey);
else if (selectOnFocus) manager.replaceSelection(lastKey);
}
break;
case "PageDown":
if (delegate.getKeyPageBelow) {
e.preventDefault();
let nextKey = delegate.getKeyPageBelow(manager.focusedKey);
navigateToKey(nextKey);
}
break;
case "PageUp":
if (delegate.getKeyPageAbove) {
e.preventDefault();
let nextKey = delegate.getKeyPageAbove(manager.focusedKey);
navigateToKey(nextKey);
}
break;
case "a":
if ((0, $feb5ffebff200149$export$16792effe837dba3)(e) && manager.selectionMode === "multiple" && disallowSelectAll !== true) {
e.preventDefault();
manager.selectAll();
}
break;
case "Escape":
e.preventDefault();
if (!disallowEmptySelection) manager.clearSelection();
break;
case "Tab":
if (!allowsTabNavigation) {
// There may be elements that are "tabbable" inside a collection (e.g. in a grid cell).
// However, collections should be treated as a single tab stop, with arrow key navigation internally.
// We don't control the rendering of these, so we can't override the tabIndex to prevent tabbing.
// Instead, we handle the Tab key, and move focus manually to the first/last tabbable element
// in the collection, so that the browser default behavior will apply starting from that element
// rather than the currently focused one.
if (e.shiftKey) ref.current.focus();
else {
let walker = (0, $eCAIO$getFocusableTreeWalker)(ref.current, {
tabbable: true
});
let next;
let last;
do {
last = walker.lastChild();
if (last) next = last;
}while (last);
if (next && !next.contains(document.activeElement)) (0, $eCAIO$focusWithoutScrolling)(next);
}
break;
}
}
};
// Store the scroll position so we can restore it later.
let scrollPos = (0, $eCAIO$useRef)({
top: 0,
left: 0
});
(0, $eCAIO$useEvent)(scrollRef, "scroll", isVirtualized ? null : ()=>{
scrollPos.current = {
top: scrollRef.current.scrollTop,
left: scrollRef.current.scrollLeft
};
});
let onFocus = (e)=>{
if (manager.isFocused) {
// If a focus event bubbled through a portal, reset focus state.
if (!e.currentTarget.contains(e.target)) manager.setFocused(false);
return;
}
// Focus events can bubble through portals. Ignore these events.
if (!e.currentTarget.contains(e.target)) return;
manager.setFocused(true);
if (manager.focusedKey == null) {
let navigateToFirstKey = (key)=>{
if (key != null) {
manager.setFocusedKey(key);
if (selectOnFocus) manager.replaceSelection(key);
}
};
// If the user hasn't yet interacted with the collection, there will be no focusedKey set.
// Attempt to detect whether the user is tabbing forward or backward into the collection
// and either focus the first or last item accordingly.
let relatedTarget = e.relatedTarget;
var _manager_lastSelectedKey, _manager_firstSelectedKey;
if (relatedTarget && e.currentTarget.compareDocumentPosition(relatedTarget) & Node.DOCUMENT_POSITION_FOLLOWING) navigateToFirstKey((_manager_lastSelectedKey = manager.lastSelectedKey) !== null && _manager_lastSelectedKey !== void 0 ? _manager_lastSelectedKey : delegate.getLastKey());
else navigateToFirstKey((_manager_firstSelectedKey = manager.firstSelectedKey) !== null && _manager_firstSelectedKey !== void 0 ? _manager_firstSelectedKey : delegate.getFirstKey());
} else if (!isVirtualized) {
// Restore the scroll position to what it was before.
scrollRef.current.scrollTop = scrollPos.current.top;
scrollRef.current.scrollLeft = scrollPos.current.left;
}
if (!isVirtualized && manager.focusedKey != null) {
// Refocus and scroll the focused item into view if it exists within the scrollable region.
let element = scrollRef.current.querySelector(`[data-key="${CSS.escape(manager.focusedKey.toString())}"]`);
if (element) {
// This prevents a flash of focus on the first/last element in the collection, or the collection itself.
if (!element.contains(document.activeElement)) (0, $eCAIO$focusWithoutScrolling)(element);
let modality = (0, $eCAIO$getInteractionModality)();
if (modality === "keyboard") (0, $eCAIO$scrollIntoViewport)(element, {
containingElement: ref.current
});
}
}
};
let onBlur = (e)=>{
// Don't set blurred and then focused again if moving focus within the collection.
if (!e.currentTarget.contains(e.relatedTarget)) manager.setFocused(false);
};
const autoFocusRef = (0, $eCAIO$useRef)(autoFocus);
(0, $eCAIO$useEffect)(()=>{
if (autoFocusRef.current) {
let focusedKey = null;
// Check focus strategy to determine which item to focus
if (autoFocus === "first") focusedKey = delegate.getFirstKey();
if (autoFocus === "last") focusedKey = delegate.getLastKey();
// If there are any selected keys, make the first one the new focus target
let selectedKeys = manager.selectedKeys;
if (selectedKeys.size) {
for (let key of selectedKeys)if (manager.canSelectItem(key)) {
focusedKey = key;
break;
}
}
manager.setFocused(true);
manager.setFocusedKey(focusedKey);
// If no default focus key is selected, focus the collection itself.
if (focusedKey == null && !shouldUseVirtualFocus) (0, $eCAIO$focusSafely)(ref.current);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// If not virtualized, scroll the focused element into view when the focusedKey changes.
// When virtualized, Virtualizer handles this internally.
let lastFocusedKey = (0, $eCAIO$useRef)(manager.focusedKey);
(0, $eCAIO$useEffect)(()=>{
let modality = (0, $eCAIO$getInteractionModality)();
if (manager.isFocused && manager.focusedKey != null && (scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current)) {
let element = scrollRef.current.querySelector(`[data-key="${CSS.escape(manager.focusedKey.toString())}"]`);
if (element && (modality === "keyboard" || autoFocusRef.current)) {
if (!isVirtualized) (0, $eCAIO$scrollIntoView)(scrollRef.current, element);
// Avoid scroll in iOS VO, since it may cause overlay to close (i.e. RAC submenu)
if (modality !== "virtual") (0, $eCAIO$scrollIntoViewport)(element, {
containingElement: ref.current
});
}
}
// If the focused key becomes null (e.g. the last item is deleted), focus the whole collection.
if (manager.isFocused && manager.focusedKey == null && lastFocusedKey.current != null) (0, $eCAIO$focusSafely)(ref.current);
lastFocusedKey.current = manager.focusedKey;
autoFocusRef.current = false;
}, [
isVirtualized,
scrollRef,
manager.focusedKey,
manager.isFocused,
ref
]);
let handlers = {
onKeyDown: onKeyDown,
onFocus: onFocus,
onBlur: onBlur,
onMouseDown (e) {
// Ignore events that bubbled through portals.
if (scrollRef.current === e.target) // Prevent focus going to the collection when clicking on the scrollbar.
e.preventDefault();
}
};
let { typeSelectProps: typeSelectProps } = (0, $fb3050f43d946246$export$e32c88dfddc6e1d8)({
keyboardDelegate: delegate,
selectionManager: manager
});
if (!disallowTypeAhead) handlers = (0, $eCAIO$mergeProps)(typeSelectProps, handlers);
// If nothing is focused within the collection, make the collection itself tabbable.
// This will be marshalled to either the first or last item depending on where focus came from.
// If using virtual focus, don't set a tabIndex at all so that VoiceOver on iOS 14 doesn't try
// to move real DOM focus to the element anyway.
let tabIndex;
if (!shouldUseVirtualFocus) tabIndex = manager.focusedKey == null ? 0 : -1;
return {
collectionProps: {
...handlers,
tabIndex: tabIndex
}
};
}
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
function $880e95eb8b93ba9a$export$ecf600387e221c37(options) {
let { selectionManager: manager, key: key, ref: ref, shouldSelectOnPressUp: shouldSelectOnPressUp, shouldUseVirtualFocus: shouldUseVirtualFocus, focus: focus, isDisabled: isDisabled, onAction: onAction, allowsDifferentPressOrigin: allowsDifferentPressOrigin, linkBehavior: linkBehavior = "action" } = options;
let router = (0, $eCAIO$useRouter)();
let onSelect = (e)=>{
if (e.pointerType === "keyboard" && (0, $feb5ffebff200149$export$d3e3bd3e26688c04)(e)) manager.toggleSelection(key);
else {
if (manager.selectionMode === "none") return;
if (manager.isLink(key)) {
if (linkBehavior === "selection") {
router.open(ref.current, e);
// Always set selected keys back to what they were so that select and combobox close.
manager.setSelectedKeys(manager.selectedKeys);
return;
} else if (linkBehavior === "override" || linkBehavior === "none") return;
}
if (manager.selectionMode === "single") {
if (manager.isSelected(key) && !manager.disallowEmptySelection) manager.toggleSelection(key);
else manager.replaceSelection(key);
} else if (e && e.shiftKey) manager.extendSelection(key);
else if (manager.selectionBehavior === "toggle" || e && ((0, $feb5ffebff200149$export$16792effe837dba3)(e) || e.pointerType === "touch" || e.pointerType === "virtual")) // if touch or virtual (VO) then we just want to toggle, otherwise it's impossible to multi select because they don't have modifier keys
manager.toggleSelection(key);
else manager.replaceSelection(key);
}
};
// Focus the associated DOM node when this item becomes the focusedKey
(0, $eCAIO$useEffect)(()=>{
let isFocused = key === manager.focusedKey;
if (isFocused && manager.isFocused && !shouldUseVirtualFocus) {
if (focus) focus();
else if (document.activeElement !== ref.current) (0, $eCAIO$focusSafely)(ref.current);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
ref,
key,
manager.focusedKey,
manager.childFocusStrategy,
manager.isFocused,
shouldUseVirtualFocus
]);
isDisabled = isDisabled || manager.isDisabled(key);
// Set tabIndex to 0 if the element is focused, or -1 otherwise so that only the last focused
// item is tabbable. If using virtual focus, don't set a tabIndex at all so that VoiceOver
// on iOS 14 doesn't try to move real DOM focus to the item anyway.
let itemProps = {};
if (!shouldUseVirtualFocus && !isDisabled) itemProps = {
tabIndex: key === manager.focusedKey ? 0 : -1,
onFocus (e) {
if (e.target === ref.current) manager.setFocusedKey(key);
}
};
else if (isDisabled) itemProps.onMouseDown = (e)=>{
// Prevent focus going to the body when clicking on a disabled item.
e.preventDefault();
};
// With checkbox selection, onAction (i.e. navigation) becomes primary, and occurs on a single click of the row.
// Clicking the checkbox enters selection mode, after which clicking anywhere on any row toggles selection for that row.
// With highlight selection, onAction is secondary, and occurs on double click. Single click selects the row.
// With touch, onAction occurs on single tap, and long press enters selection mode.
let isLinkOverride = manager.isLink(key) && linkBehavior === "override";
let hasLinkAction = manager.isLink(key) && linkBehavior !== "selection" && linkBehavior !== "none";
let allowsSelection = !isDisabled && manager.canSelectItem(key) && !isLinkOverride;
let allowsActions = (onAction || hasLinkAction) && !isDisabled;
let hasPrimaryAction = allowsActions && (manager.selectionBehavior === "replace" ? !allowsSelection : !allowsSelection || manager.isEmpty);
let hasSecondaryAction = allowsActions && allowsSelection && manager.selectionBehavior === "replace";
let hasAction = hasPrimaryAction || hasSecondaryAction;
let modality = (0, $eCAIO$useRef)(null);
let longPressEnabled = hasAction && allowsSelection;
let longPressEnabledOnPressStart = (0, $eCAIO$useRef)(false);
let hadPrimaryActionOnPressStart = (0, $eCAIO$useRef)(false);
let performAction = (e)=>{
if (onAction) onAction();
if (hasLinkAction) router.open(ref.current, e);
};
// By default, selection occurs on pointer down. This can be strange if selecting an
// item causes the UI to disappear immediately (e.g. menus).
// If shouldSelectOnPressUp is true, we use onPressUp instead of onPressStart.
// onPress requires a pointer down event on the same element as pointer up. For menus,
// we want to be able to have the pointer down on the trigger that opens the menu and
// the pointer up on the menu item rather than requiring a separate press.
// For keyboard events, selection still occurs on key down.
let itemPressProps = {};
if (shouldSelectOnPressUp) {
itemPressProps.onPressStart = (e)=>{
modality.current = e.pointerType;
longPressEnabledOnPressStart.current = longPressEnabled;
if (e.pointerType === "keyboard" && (!hasAction || $880e95eb8b93ba9a$var$isSelectionKey())) onSelect(e);
};
// If allowsDifferentPressOrigin, make selection happen on pressUp (e.g. open menu on press down, selection on menu item happens on press up.)
// Otherwise, have selection happen onPress (prevents listview row selection when clicking on interactable elements in the row)
if (!allowsDifferentPressOrigin) itemPressProps.onPress = (e)=>{
if (hasPrimaryAction || hasSecondaryAction && e.pointerType !== "mouse") {
if (e.pointerType === "keyboard" && !$880e95eb8b93ba9a$var$isActionKey()) return;
performAction(e);
} else if (e.pointerType !== "keyboard" && allowsSelection) onSelect(e);
};
else {
itemPressProps.onPressUp = hasPrimaryAction ? null : (e)=>{
if (e.pointerType !== "keyboard" && allowsSelection) onSelect(e);
};
itemPressProps.onPress = hasPrimaryAction ? performAction : null;
}
} else {
itemPressProps.onPressStart = (e)=>{
modality.current = e.pointerType;
longPressEnabledOnPressStart.current = longPressEnabled;
hadPrimaryActionOnPressStart.current = hasPrimaryAction;
// Select on mouse down unless there is a primary action which will occur on mouse up.
// For keyboard, select on key down. If there is an action, the Space key selects on key down,
// and the Enter key performs onAction on key up.
if (allowsSelection && (e.pointerType === "mouse" && !hasPrimaryAction || e.pointerType === "keyboard" && (!allowsActions || $880e95eb8b93ba9a$var$isSelectionKey()))) onSelect(e);
};
itemPressProps.onPress = (e)=>{
// Selection occurs on touch up. Primary actions always occur on pointer up.
// Both primary and secondary actions occur on Enter key up. The only exception
// is secondary actions, which occur on double click with a mouse.
if (e.pointerType === "touch" || e.pointerType === "pen" || e.pointerType === "virtual" || e.pointerType === "keyboard" && hasAction && $880e95eb8b93ba9a$var$isActionKey() || e.pointerType === "mouse" && hadPrimaryActionOnPressStart.current) {
if (hasAction) performAction(e);
else if (allowsSelection) onSelect(e);
}
};
}
itemProps["data-key"] = key;
itemPressProps.preventFocusOnPress = shouldUseVirtualFocus;
let { pressProps: pressProps, isPressed: isPressed } = (0, $eCAIO$usePress)(itemPressProps);
// Double clicking with a mouse with selectionBehavior = 'replace' performs an action.
let onDoubleClick = hasSecondaryAction ? (e)=>{
if (modality.current === "mouse") {
e.stopPropagation();
e.preventDefault();
performAction(e);
}
} : undefined;
// Long pressing an item with touch when selectionBehavior = 'replace' switches the selection behavior
// to 'toggle'. This changes the single tap behavior from performing an action (i.e. navigating) to
// selecting, and may toggle the appearance of a UI affordance like checkboxes on each item.
let { longPressProps: longPressProps } = (0, $eCAIO$useLongPress)({
isDisabled: !longPressEnabled,
onLongPress (e) {
if (e.pointerType === "touch") {
onSelect(e);
manager.setSelectionBehavior("toggle");
}
}
});
// Prevent native drag and drop on long press if we also select on long press.
// Once the user is in selection mode, they can long press again to drag.
// Use a capturing listener to ensure this runs before useDrag, regardless of
// the order the props get merged.
let onDragStartCapture = (e)=>{
if (modality.current === "touch" && longPressEnabledOnPressStart.current) e.preventDefault();
};
// Prevent default on link clicks so that we control exactly
// when they open (to match selection behavior).
let onClick = manager.isLink(key) ? (e)=>{
if (!(0, $eCAIO$openLink).isOpening) e.preventDefault();
} : undefined;
return {
itemProps: (0, $eCAIO$mergeProps)(itemProps, allowsSelection || hasPrimaryAction ? pressProps : {}, longPressEnabled ? longPressProps : {}, {
onDoubleClick: onDoubleClick,
onDragStartCapture: onDragStartCapture,
onClick: onClick
}),
isPressed: isPressed,
isSelected: manager.isSelected(key),
isFocused: manager.isFocused && manager.focusedKey === key,
isDisabled: isDisabled,
allowsSelection: allowsSelection,
hasAction: hasAction
};
}
function $880e95eb8b93ba9a$var$isActionKey() {
let event = window.event;
return (event === null || event === void 0 ? void 0 : event.key) === "Enter";
}
function $880e95eb8b93ba9a$var$isSelectionKey() {
let event = window.event;
return (event === null || event === void 0 ? void 0 : event.key) === " " || (event === null || event === void 0 ? void 0 : event.code) === "Space";
}
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
class $2a25aae57d74318e$export$a05409b8bb224a5a {
getNextKey(key) {
key = this.collection.getKeyAfter(key);
while(key != null){
let item = this.collection.getItem(key);
if (item.type === "item" && !this.disabledKeys.has(key)) return key;
key = this.collection.getKeyAfter(key);
}
return null;
}
getPreviousKey(key) {
key = this.collection.getKeyBefore(key);
while(key != null){
let item = this.collection.getItem(key);
if (item.type === "item" && !this.disabledKeys.has(key)) return key;
key = this.collection.getKeyBefore(key);
}
return null;
}
findKey(key, nextKey, shouldSkip) {
let item = this.getItem(key);
if (!item) return null;
// Find the item above or below in the same column.
let prevRect = item.getBoundingClientRect();
do {
key = nextKey(key);
item = this.getItem(key);
}while (item && shouldSkip(prevRect, item.getBoundingClientRect()));
return key;
}
isSameRow(prevRect, itemRect) {
return prevRect.top === itemRect.top || prevRect.left !== itemRect.left;
}
isSameColumn(prevRect, itemRect) {
return prevRect.left === itemRect.left || prevRect.top !== itemRect.top;
}
getKeyBelow(key) {
if (this.layout === "grid" && this.orientation === "vertical") return this.findKey(key, (key)=>this.getNextKey(key), this.isSameRow);
else return this.getNextKey(key);
}
getKeyAbove(key) {
if (this.layout === "grid" && this.orientation === "vertical") return this.findKey(key, (key)=>this.getPreviousKey(key), this.isSameRow);
else return this.getPreviousKey(key);
}
getNextColumn(key, right) {
return right ? this.getPreviousKey(key) : this.getNextKey(key);
}
getKeyRightOf(key) {
if (this.layout === "grid") {
if (this.orientation === "vertical") return this.getNextColumn(key, this.direction === "rtl");
else return this.findKey(key, (key)=>this.getNextColumn(key, this.direction === "rtl"), this.isSameColumn);
} else if (this.orientation === "horizontal") return this.getNextColumn(key, this.direction === "rtl");
return null;
}
getKeyLeftOf(key) {
if (this.layout === "grid") {
if (this.orientation === "vertical") return this.getNextColumn(key, this.direction === "ltr");
else return this.findKey(key, (key)=>this.getNextColumn(key, this.direction === "ltr"), this.isSameColumn);
} else if (this.orientation === "horizontal") return this.getNextColumn(key, this.direction === "ltr");
return null;
}
getFirstKey() {
let key = this.collection.getFirstKey();
while(key != null){
let item = this.collection.getItem(key);
if ((item === null || item === void 0 ? void 0 : item.type) === "item" && !this.disabledKeys.has(key)) return key;
key = this.collection.getKeyAfter(key);
}
return null;
}
getLastKey() {
let key = this.collection.getLastKey();
while(key != null){
let item = this.collection.getItem(key);
if (item.type === "item" && !this.disabledKeys.has(key)) return key;
key = this.collection.getKeyBefore(key);
}
return null;
}
getItem(key) {
return this.ref.current.querySelector(`[data-key="${CSS.escape(key.toString())}"]`);
}
getKeyPageAbove(key) {
let menu = this.ref.current;
let item = this.getItem(key);
if (!item) return null;
if (!(0, $eCAIO$isScrollable)(menu)) return this.getFirstKey();
let containerRect = menu.getBoundingClientRect();
let itemRect = item.getBoundingClientRect();
if (this.orientation === "horizontal") {
let containerX = containerRect.x - menu.scrollLeft;
let pageX = Math.max(0, itemRect.x - containerX + itemRect.width - containerRect.width);
while(item && itemRect.x - containerX > pageX){
key = this.getKeyAbove(key);
item = key == null ? null : this.getItem(key);
itemRect = item === null || item === void 0 ? void 0 : item.getBoundingClientRect();
}
} else {
let containerY = containerRect.y - menu.scrollTop;
let pageY = Math.max(0, itemRect.y - containerY + itemRect.height - containerRect.height);
while(item && itemRect.y - containerY > pageY){
key = this.getKeyAbove(key);
item = key == null ? null : this.getItem(key);
itemRect = item === null || item === void 0 ? void 0 : item.getBoundingClientRect();
}
}
return key !== null && key !== void 0 ? key : this.getFirstKey();
}
getKeyPageBelow(key) {
let menu = this.ref.current;
let item = this.getItem(key);
if (!item) return null;
if (!(0, $eCAIO$isScrollable)(menu)) return this.getLastKey();
let containerRect = menu.getBoundingClientRect();
let itemRect = item.getBoundingClientRect();
if (this.orientation === "horizontal") {
let containerX = containerRect.x - menu.scrollLeft;
let pageX = Math.min(menu.scrollWidth, itemRect.x - containerX - itemRect.width + containerRect.width);
while(item && itemRect.x - containerX < pageX){
key = this.getKeyBelow(key);
item = key == null ? null : this.getItem(key);
itemRect = item === null || item === void 0 ? void 0 : item.getBoundingClientRect();
}
} else {
let containerY = containerRect.y - menu.scrollTop;
let pageY = Math.min(menu.scrollHeight, itemRect.y - containerY - itemRect.height + containerRect.height);
while(item && itemRect.y - containerY < pageY){
key = this.getKeyBelow(key);
item = key == null ? null : this.getItem(key);
itemRect = item === null || item === void 0 ? void 0 : item.getBoundingClientRect();
}
}
return key !== null && key !== void 0 ? key : this.getLastKey();
}
getKeyForSearch(search, fromKey) {
if (!this.collator) return null;
let collection = this.collection;
let key = fromKey || this.getFirstKey();
while(key != null){
let item = collection.getItem(key);
let substring = item.textValue.slice(0, search.length);
if (item.textValue && this.collator.compare(substring, search) === 0) return key;
key = this.getKeyBelow(key);
}
return null;
}
constructor(...args){
if (args.length === 1) {
let opts = args[0];
this.collection = opts.collection;
this.ref = opts.ref;
this.collator = opts.collator;
this.disabledKeys = opts.disabledKeys || new Set();
this.orientation = opts.orientation;
this.direction = opts.direction;
this.layout = opts.layout || "stack";
} else {
this.collection = args[0];
this.disabledKeys = args[1];
this.ref = args[2];
this.collator = args[3];
this.layout = "stack";
this.orientation = "vertical";
}
// If this is a vertical stack, remove the left/right methods completely
// so they aren't called by useDroppableCollection.
if (this.layout === "stack" && this.orientation === "vertical") {
this.getKeyLeftOf = undefined;
this.getKeyRightOf = undefined;
}
}
}
function $982254629710d113$export$b95089534ab7c1fd(props) {
let { selectionManager: selectionManager, collection: collection, disabledKeys: disabledKeys, ref: ref, keyboardDelegate: keyboardDelegate } = props;
// By default, a KeyboardDelegate is provided which uses the DOM to query layout information (e.g. for page up/page down).
// When virtualized, the layout object will be passed in as a prop and override this.
let collator = (0, $eCAIO$useCollator)({
usage: "search",
sensitivity: "base"
});
let disabledBehavior = selectionManager.disabledBehavior;
let delegate = (0, $eCAIO$useMemo)(()=>keyboardDelegate || new (0, $2a25aae57d74318e$export$a05409b8bb224a5a)(collection, disabledBehavior === "selection" ? new Set() : disabledKeys, ref, collator), [
keyboardDelegate,
collection,
disabledKeys,
ref,
collator,
disabledBehavior
]);
let { collectionProps: collectionProps } = (0, $ae20dd8cbca75726$export$d6daf82dcd84e87c)({
...props,
ref: ref,
selectionManager: selectionManager,
keyboardDelegate: delegate
});
return {
listProps: collectionProps
};
}
export {$ae20dd8cbca75726$export$d6daf82dcd84e87c as useSelectableCollection, $880e95eb8b93ba9a$export$ecf600387e221c37 as useSelectableItem, $982254629710d113$export$b95089534ab7c1fd as useSelectableList, $2a25aae57d74318e$export$a05409b8bb224a5a as ListKeyboardDelegate, $fb3050f43d946246$export$e32c88dfddc6e1d8 as useTypeSelect};
//# sourceMappingURL=module.js.map