@floating-ui/react
Advanced tools
Comparing version
@@ -1223,2 +1223,9 @@ import { AlignedPlacement } from '@floating-ui/react-dom'; | ||
/** | ||
* Allows to specify the orientation of the parent list, which is used to | ||
* determine the direction of the navigation. | ||
* This is useful when list navigation is used within a Composite, | ||
* as the hook can't determine the orientation of the parent list automatically. | ||
*/ | ||
parentOrientation?: UseListNavigationProps['orientation']; | ||
/** | ||
* Whether the direction of the floating element’s navigation is in RTL | ||
@@ -1225,0 +1232,0 @@ * layout. |
@@ -1,11 +0,120 @@ | ||
import type * as React from 'react'; | ||
/// <reference types="react" /> | ||
import { Dimensions } from '@floating-ui/react-dom'; | ||
import { FocusableElement } from 'tabbable'; | ||
import * as React from 'react'; | ||
import type { UseFloatingReturn } from '@floating-ui/react-dom'; | ||
import useModernLayoutEffect from 'use-isomorphic-layout-effect'; | ||
import type { VirtualElement } from '@floating-ui/react-dom'; | ||
export declare function activeElement(doc: Document): Element | null; | ||
declare type AnyFunction = (...args: any[]) => any; | ||
export declare function contains(parent?: Element | null, child?: Element | null): boolean; | ||
declare interface ContextData { | ||
openEvent?: Event; | ||
floatingContext?: FloatingContext; | ||
/** @deprecated use `onTypingChange` prop in `useTypeahead` */ | ||
typing?: boolean; | ||
[key: string]: any; | ||
} | ||
/** For each cell index, gets the item index that occupies that cell */ | ||
export declare function createGridCellMap(sizes: Dimensions[], cols: number, dense: boolean): (number | undefined)[]; | ||
export declare function disableFocusInside(container: HTMLElement): void; | ||
export declare function enableFocusInside(container: HTMLElement): void; | ||
declare interface ExtendedElements<RT> { | ||
reference: ReferenceType | null; | ||
floating: HTMLElement | null; | ||
domReference: NarrowedElement<RT> | null; | ||
} | ||
declare interface ExtendedRefs<RT> { | ||
reference: React.MutableRefObject<ReferenceType | null>; | ||
floating: React.MutableRefObject<HTMLElement | null>; | ||
domReference: React.MutableRefObject<NarrowedElement<RT> | null>; | ||
setReference(node: RT | null): void; | ||
setFloating(node: HTMLElement | null): void; | ||
setPositionReference(node: ReferenceType | null): void; | ||
} | ||
export declare function findNonDisabledListIndex(listRef: React.MutableRefObject<Array<HTMLElement | null>>, { startingIndex, decrement, disabledIndices, amount, }?: { | ||
startingIndex?: number; | ||
decrement?: boolean; | ||
disabledIndices?: Array<number>; | ||
amount?: number; | ||
}): number; | ||
declare type FloatingContext<RT extends ReferenceType = ReferenceType> = Omit<UseFloatingReturn<RT>, 'refs' | 'elements'> & { | ||
open: boolean; | ||
onOpenChange(open: boolean, event?: Event, reason?: OpenChangeReason): void; | ||
events: FloatingEvents; | ||
dataRef: React.MutableRefObject<ContextData>; | ||
nodeId: string | undefined; | ||
floatingId: string | undefined; | ||
refs: ExtendedRefs<RT>; | ||
elements: ExtendedElements<RT>; | ||
}; | ||
declare interface FloatingEvents { | ||
emit<T extends string>(event: T, data?: any): void; | ||
on(event: string, handler: (data: any) => void): void; | ||
off(event: string, handler: (data: any) => void): void; | ||
} | ||
declare interface FloatingNodeType<RT extends ReferenceType = ReferenceType> { | ||
id: string | undefined; | ||
parentId: string | null; | ||
context?: FloatingContext<RT>; | ||
} | ||
export declare function getDeepestNode<RT extends ReferenceType = ReferenceType>(nodes: Array<FloatingNodeType<RT>>, id: string | undefined): FloatingNodeType<RT> | undefined; | ||
export declare function getDocument(node: Element | null): Document; | ||
export declare function getFloatingFocusElement(floatingElement: HTMLElement | null | undefined): HTMLElement | null; | ||
/** Gets cell index of an item's corner or -1 when index is -1. */ | ||
export declare function getGridCellIndexOfCorner(index: number, sizes: Dimensions[], cellMap: (number | undefined)[], cols: number, corner: 'tl' | 'tr' | 'bl' | 'br'): number; | ||
/** Gets all cell indices that correspond to the specified indices */ | ||
export declare function getGridCellIndices(indices: (number | undefined)[], cellMap: (number | undefined)[]): number[]; | ||
export declare function getGridNavigatedIndex(listRef: React.MutableRefObject<Array<HTMLElement | null>>, { event, orientation, loop, rtl, cols, disabledIndices, minIndex, maxIndex, prevIndex, stopEvent: stop, }: { | ||
event: React.KeyboardEvent; | ||
orientation: 'horizontal' | 'vertical' | 'both'; | ||
loop: boolean; | ||
rtl: boolean; | ||
cols: number; | ||
disabledIndices: Array<number> | undefined; | ||
minIndex: number; | ||
maxIndex: number; | ||
prevIndex: number; | ||
stopEvent?: boolean; | ||
}): number; | ||
export declare function getMaxListIndex(listRef: React.MutableRefObject<Array<HTMLElement | null>>, disabledIndices: Array<number> | undefined): number; | ||
export declare function getMinListIndex(listRef: React.MutableRefObject<Array<HTMLElement | null>>, disabledIndices: Array<number> | undefined): number; | ||
export declare function getNextTabbable(referenceElement: Element | null): FocusableElement | null; | ||
export declare function getNodeAncestors<RT extends ReferenceType = ReferenceType>(nodes: Array<FloatingNodeType<RT>>, id: string | undefined): FloatingNodeType<RT>[]; | ||
export declare function getNodeChildren<RT extends ReferenceType = ReferenceType>(nodes: Array<FloatingNodeType<RT>>, id: string | undefined, onlyOpenChildren?: boolean): FloatingNodeType<RT>[]; | ||
export declare function getPlatform(): string; | ||
export declare function getPreviousTabbable(referenceElement: Element | null): FocusableElement | null; | ||
export declare const getTabbableOptions: () => { | ||
readonly getShadowRoot: true; | ||
readonly displayCheck: "none" | "full"; | ||
}; | ||
export declare function getTarget(event: Event): EventTarget | null; | ||
@@ -17,6 +126,12 @@ | ||
export declare function isDifferentGridRow(index: number, cols: number, prevRow: number): boolean; | ||
export declare function isEventTargetWithin(event: Event, node: Node | null | undefined): boolean; | ||
export declare function isIndexOutOfListBounds(listRef: React.MutableRefObject<Array<HTMLElement | null>>, index: number): boolean; | ||
export declare function isJSDOM(): boolean; | ||
export declare function isListIndexDisabled(listRef: React.MutableRefObject<Array<HTMLElement | null>>, index: number, disabledIndices?: Array<number>): boolean; | ||
export declare function isMac(): boolean; | ||
@@ -26,2 +141,4 @@ | ||
export declare function isOutsideEvent(event: FocusEvent | React.FocusEvent, container?: Element): boolean; | ||
export declare function isReactEvent(event: any): event is React.SyntheticEvent; | ||
@@ -41,6 +158,18 @@ | ||
export declare function matchesFocusVisible(element: Element | null): boolean; | ||
declare type NarrowedElement<T> = T extends Element ? T : Element; | ||
declare type OpenChangeReason = 'outside-press' | 'escape-key' | 'ancestor-scroll' | 'reference-press' | 'click' | 'hover' | 'focus' | 'focus-out' | 'list-navigation' | 'safe-polygon'; | ||
declare type ReferenceType = Element | VirtualElement; | ||
export declare function stopEvent(event: Event | React.SyntheticEvent): void; | ||
export declare const TYPEABLE_SELECTOR: string; | ||
export declare function useEffectEvent<T extends AnyFunction>(callback?: T): T; | ||
export declare function useLatestRef<T>(value: T): React.MutableRefObject<T>; | ||
export { useModernLayoutEffect } | ||
export { } |
import { isShadowRoot, isHTMLElement } from '@floating-ui/utils/dom'; | ||
import * as React from 'react'; | ||
import { useLayoutEffect, useEffect } from 'react'; | ||
import { floor } from '@floating-ui/utils'; | ||
import { tabbable } from 'tabbable'; | ||
// Avoid Chrome DevTools blue warning. | ||
function getPlatform() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData != null && uaData.platform) { | ||
return uaData.platform; | ||
} | ||
return navigator.platform; | ||
} | ||
function getUserAgent() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData && Array.isArray(uaData.brands)) { | ||
return uaData.brands.map(_ref => { | ||
let { | ||
brand, | ||
version | ||
} = _ref; | ||
return brand + "/" + version; | ||
}).join(' '); | ||
} | ||
return navigator.userAgent; | ||
} | ||
function isSafari() { | ||
// Chrome DevTools does not complain about navigator.vendor | ||
return /apple/i.test(navigator.vendor); | ||
} | ||
function isAndroid() { | ||
const re = /android/i; | ||
return re.test(getPlatform()) || re.test(getUserAgent()); | ||
} | ||
function isMac() { | ||
return getPlatform().toLowerCase().startsWith('mac') && !navigator.maxTouchPoints; | ||
} | ||
function isJSDOM() { | ||
return getUserAgent().includes('jsdom/'); | ||
} | ||
const FOCUSABLE_ATTRIBUTE = 'data-floating-ui-focusable'; | ||
const TYPEABLE_SELECTOR = "input:not([type='hidden']):not([disabled])," + "[contenteditable]:not([contenteditable='false']),textarea:not([disabled])"; | ||
const ARROW_LEFT = 'ArrowLeft'; | ||
const ARROW_RIGHT = 'ArrowRight'; | ||
const ARROW_UP = 'ArrowUp'; | ||
const ARROW_DOWN = 'ArrowDown'; | ||
function activeElement(doc) { | ||
@@ -37,24 +84,116 @@ let activeElement = doc.activeElement; | ||
} | ||
// Avoid Chrome DevTools blue warning. | ||
function getPlatform() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData != null && uaData.platform) { | ||
return uaData.platform; | ||
function getTarget(event) { | ||
if ('composedPath' in event) { | ||
return event.composedPath()[0]; | ||
} | ||
return navigator.platform; | ||
// TS thinks `event` is of type never as it assumes all browsers support | ||
// `composedPath()`, but browsers without shadow DOM don't. | ||
return event.target; | ||
} | ||
function getUserAgent() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData && Array.isArray(uaData.brands)) { | ||
return uaData.brands.map(_ref => { | ||
let { | ||
brand, | ||
version | ||
} = _ref; | ||
return brand + "/" + version; | ||
}).join(' '); | ||
function isEventTargetWithin(event, node) { | ||
if (node == null) { | ||
return false; | ||
} | ||
return navigator.userAgent; | ||
if ('composedPath' in event) { | ||
return event.composedPath().includes(node); | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support composedPath, but browsers without shadow dom don't | ||
const e = event; | ||
return e.target != null && node.contains(e.target); | ||
} | ||
function isRootElement(element) { | ||
return element.matches('html,body'); | ||
} | ||
function getDocument(node) { | ||
return (node == null ? void 0 : node.ownerDocument) || document; | ||
} | ||
function isTypeableElement(element) { | ||
return isHTMLElement(element) && element.matches(TYPEABLE_SELECTOR); | ||
} | ||
function isTypeableCombobox(element) { | ||
if (!element) return false; | ||
return element.getAttribute('role') === 'combobox' && isTypeableElement(element); | ||
} | ||
function matchesFocusVisible(element) { | ||
// We don't want to block focus from working with `visibleOnly` | ||
// (JSDOM doesn't match `:focus-visible` when the element has `:focus`) | ||
if (!element || isJSDOM()) return true; | ||
try { | ||
return element.matches(':focus-visible'); | ||
} catch (_e) { | ||
return true; | ||
} | ||
} | ||
function getFloatingFocusElement(floatingElement) { | ||
if (!floatingElement) { | ||
return null; | ||
} | ||
// Try to find the element that has `{...getFloatingProps()}` spread on it. | ||
// This indicates the floating element is acting as a positioning wrapper, and | ||
// so focus should be managed on the child element with the event handlers and | ||
// aria props. | ||
return floatingElement.hasAttribute(FOCUSABLE_ATTRIBUTE) ? floatingElement : floatingElement.querySelector("[" + FOCUSABLE_ATTRIBUTE + "]") || floatingElement; | ||
} | ||
function getNodeChildren(nodes, id, onlyOpenChildren) { | ||
if (onlyOpenChildren === void 0) { | ||
onlyOpenChildren = true; | ||
} | ||
let allChildren = nodes.filter(node => { | ||
var _node$context; | ||
return node.parentId === id && ((_node$context = node.context) == null ? void 0 : _node$context.open); | ||
}); | ||
let currentChildren = allChildren; | ||
while (currentChildren.length) { | ||
currentChildren = onlyOpenChildren ? nodes.filter(node => { | ||
var _currentChildren; | ||
return (_currentChildren = currentChildren) == null ? void 0 : _currentChildren.some(n => { | ||
var _node$context2; | ||
return node.parentId === n.id && ((_node$context2 = node.context) == null ? void 0 : _node$context2.open); | ||
}); | ||
}) : nodes; | ||
allChildren = allChildren.concat(currentChildren); | ||
} | ||
return allChildren; | ||
} | ||
function getDeepestNode(nodes, id) { | ||
let deepestNodeId; | ||
let maxDepth = -1; | ||
function findDeepest(nodeId, depth) { | ||
if (depth > maxDepth) { | ||
deepestNodeId = nodeId; | ||
maxDepth = depth; | ||
} | ||
const children = getNodeChildren(nodes, nodeId); | ||
children.forEach(child => { | ||
findDeepest(child.id, depth + 1); | ||
}); | ||
} | ||
findDeepest(id, 0); | ||
return nodes.find(node => node.id === deepestNodeId); | ||
} | ||
function getNodeAncestors(nodes, id) { | ||
var _nodes$find; | ||
let allAncestors = []; | ||
let currentParentId = (_nodes$find = nodes.find(node => node.id === id)) == null ? void 0 : _nodes$find.parentId; | ||
while (currentParentId) { | ||
const currentNode = nodes.find(node => node.id === currentParentId); | ||
currentParentId = currentNode == null ? void 0 : currentNode.parentId; | ||
if (currentNode) { | ||
allAncestors = allAncestors.concat(currentNode); | ||
} | ||
} | ||
return allAncestors; | ||
} | ||
function stopEvent(event) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
} | ||
function isReactEvent(event) { | ||
return 'nativeEvent' in event; | ||
} | ||
// License: https://github.com/adobe/react-spectrum/blob/b35d5c02fe900badccd0cf1a8f23bb593419f238/packages/@react-aria/utils/src/isVirtualEvent.ts | ||
@@ -78,16 +217,2 @@ function isVirtualClick(event) { | ||
} | ||
function isSafari() { | ||
// Chrome DevTools does not complain about navigator.vendor | ||
return /apple/i.test(navigator.vendor); | ||
} | ||
function isAndroid() { | ||
const re = /android/i; | ||
return re.test(getPlatform()) || re.test(getUserAgent()); | ||
} | ||
function isMac() { | ||
return getPlatform().toLowerCase().startsWith('mac') && !navigator.maxTouchPoints; | ||
} | ||
function isJSDOM() { | ||
return getUserAgent().includes('jsdom/'); | ||
} | ||
function isMouseLikePointerType(pointerType, strict) { | ||
@@ -102,45 +227,320 @@ // On some Linux machines with Chromium, mouse inputs return a `pointerType` | ||
} | ||
function isReactEvent(event) { | ||
return 'nativeEvent' in event; | ||
var index = typeof document !== 'undefined' ? useLayoutEffect : useEffect; | ||
// https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379 | ||
const SafeReact = { | ||
...React | ||
}; | ||
function useLatestRef(value) { | ||
const ref = React.useRef(value); | ||
index(() => { | ||
ref.current = value; | ||
}); | ||
return ref; | ||
} | ||
function isRootElement(element) { | ||
return element.matches('html,body'); | ||
const useInsertionEffect = SafeReact.useInsertionEffect; | ||
const useSafeInsertionEffect = useInsertionEffect || (fn => fn()); | ||
function useEffectEvent(callback) { | ||
const ref = React.useRef(() => { | ||
if (process.env.NODE_ENV !== "production") { | ||
throw new Error('Cannot call an event handler while rendering.'); | ||
} | ||
}); | ||
useSafeInsertionEffect(() => { | ||
ref.current = callback; | ||
}); | ||
return React.useCallback(function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return ref.current == null ? void 0 : ref.current(...args); | ||
}, []); | ||
} | ||
function getDocument(node) { | ||
return (node == null ? void 0 : node.ownerDocument) || document; | ||
function isDifferentGridRow(index, cols, prevRow) { | ||
return Math.floor(index / cols) !== prevRow; | ||
} | ||
function isEventTargetWithin(event, node) { | ||
if (node == null) { | ||
return false; | ||
function isIndexOutOfListBounds(listRef, index) { | ||
return index < 0 || index >= listRef.current.length; | ||
} | ||
function getMinListIndex(listRef, disabledIndices) { | ||
return findNonDisabledListIndex(listRef, { | ||
disabledIndices | ||
}); | ||
} | ||
function getMaxListIndex(listRef, disabledIndices) { | ||
return findNonDisabledListIndex(listRef, { | ||
decrement: true, | ||
startingIndex: listRef.current.length, | ||
disabledIndices | ||
}); | ||
} | ||
function findNonDisabledListIndex(listRef, _temp) { | ||
let { | ||
startingIndex = -1, | ||
decrement = false, | ||
disabledIndices, | ||
amount = 1 | ||
} = _temp === void 0 ? {} : _temp; | ||
let index = startingIndex; | ||
do { | ||
index += decrement ? -amount : amount; | ||
} while (index >= 0 && index <= listRef.current.length - 1 && isListIndexDisabled(listRef, index, disabledIndices)); | ||
return index; | ||
} | ||
function getGridNavigatedIndex(listRef, _ref) { | ||
let { | ||
event, | ||
orientation, | ||
loop, | ||
rtl, | ||
cols, | ||
disabledIndices, | ||
minIndex, | ||
maxIndex, | ||
prevIndex, | ||
stopEvent: stop = false | ||
} = _ref; | ||
let nextIndex = prevIndex; | ||
if (event.key === ARROW_UP) { | ||
stop && stopEvent(event); | ||
if (prevIndex === -1) { | ||
nextIndex = maxIndex; | ||
} else { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: nextIndex, | ||
amount: cols, | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
if (loop && (prevIndex - cols < minIndex || nextIndex < 0)) { | ||
const col = prevIndex % cols; | ||
const maxCol = maxIndex % cols; | ||
const offset = maxIndex - (maxCol - col); | ||
if (maxCol === col) { | ||
nextIndex = maxIndex; | ||
} else { | ||
nextIndex = maxCol > col ? offset : offset - cols; | ||
} | ||
} | ||
} | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
if ('composedPath' in event) { | ||
return event.composedPath().includes(node); | ||
if (event.key === ARROW_DOWN) { | ||
stop && stopEvent(event); | ||
if (prevIndex === -1) { | ||
nextIndex = minIndex; | ||
} else { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
amount: cols, | ||
disabledIndices | ||
}); | ||
if (loop && prevIndex + cols > maxIndex) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex % cols - cols, | ||
amount: cols, | ||
disabledIndices | ||
}); | ||
} | ||
} | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support composedPath, but browsers without shadow dom don't | ||
const e = event; | ||
return e.target != null && node.contains(e.target); | ||
// Remains on the same row/column. | ||
if (orientation === 'both') { | ||
const prevRow = floor(prevIndex / cols); | ||
if (event.key === (rtl ? ARROW_LEFT : ARROW_RIGHT)) { | ||
stop && stopEvent(event); | ||
if (prevIndex % cols !== cols - 1) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
disabledIndices | ||
}); | ||
if (loop && isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} | ||
} else if (loop) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} | ||
if (isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
if (event.key === (rtl ? ARROW_RIGHT : ARROW_LEFT)) { | ||
stop && stopEvent(event); | ||
if (prevIndex % cols !== 0) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
if (loop && isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex + (cols - prevIndex % cols), | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
} | ||
} else if (loop) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex + (cols - prevIndex % cols), | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
} | ||
if (isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
const lastRow = floor(maxIndex / cols) === prevRow; | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
if (loop && lastRow) { | ||
nextIndex = event.key === (rtl ? ARROW_RIGHT : ARROW_LEFT) ? maxIndex : findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} else { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
} | ||
return nextIndex; | ||
} | ||
function getTarget(event) { | ||
if ('composedPath' in event) { | ||
return event.composedPath()[0]; | ||
/** For each cell index, gets the item index that occupies that cell */ | ||
function createGridCellMap(sizes, cols, dense) { | ||
const cellMap = []; | ||
let startIndex = 0; | ||
sizes.forEach((_ref2, index) => { | ||
let { | ||
width, | ||
height | ||
} = _ref2; | ||
if (width > cols) { | ||
if (process.env.NODE_ENV !== "production") { | ||
throw new Error("[Floating UI]: Invalid grid - item width at index " + index + " is greater than grid columns"); | ||
} | ||
} | ||
let itemPlaced = false; | ||
if (dense) { | ||
startIndex = 0; | ||
} | ||
while (!itemPlaced) { | ||
const targetCells = []; | ||
for (let i = 0; i < width; i++) { | ||
for (let j = 0; j < height; j++) { | ||
targetCells.push(startIndex + i + j * cols); | ||
} | ||
} | ||
if (startIndex % cols + width <= cols && targetCells.every(cell => cellMap[cell] == null)) { | ||
targetCells.forEach(cell => { | ||
cellMap[cell] = index; | ||
}); | ||
itemPlaced = true; | ||
} else { | ||
startIndex++; | ||
} | ||
} | ||
}); | ||
// convert into a non-sparse array | ||
return [...cellMap]; | ||
} | ||
/** Gets cell index of an item's corner or -1 when index is -1. */ | ||
function getGridCellIndexOfCorner(index, sizes, cellMap, cols, corner) { | ||
if (index === -1) return -1; | ||
const firstCellIndex = cellMap.indexOf(index); | ||
const sizeItem = sizes[index]; | ||
switch (corner) { | ||
case 'tl': | ||
return firstCellIndex; | ||
case 'tr': | ||
if (!sizeItem) { | ||
return firstCellIndex; | ||
} | ||
return firstCellIndex + sizeItem.width - 1; | ||
case 'bl': | ||
if (!sizeItem) { | ||
return firstCellIndex; | ||
} | ||
return firstCellIndex + (sizeItem.height - 1) * cols; | ||
case 'br': | ||
return cellMap.lastIndexOf(index); | ||
} | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support | ||
// `composedPath()`, but browsers without shadow DOM don't. | ||
return event.target; | ||
/** Gets all cell indices that correspond to the specified indices */ | ||
function getGridCellIndices(indices, cellMap) { | ||
return cellMap.flatMap((index, cellIndex) => indices.includes(index) ? [cellIndex] : []); | ||
} | ||
const TYPEABLE_SELECTOR = "input:not([type='hidden']):not([disabled])," + "[contenteditable]:not([contenteditable='false']),textarea:not([disabled])"; | ||
function isTypeableElement(element) { | ||
return isHTMLElement(element) && element.matches(TYPEABLE_SELECTOR); | ||
function isListIndexDisabled(listRef, index, disabledIndices) { | ||
if (disabledIndices) { | ||
return disabledIndices.includes(index); | ||
} | ||
const element = listRef.current[index]; | ||
return element == null || element.hasAttribute('disabled') || element.getAttribute('aria-disabled') === 'true'; | ||
} | ||
function stopEvent(event) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
const getTabbableOptions = () => ({ | ||
getShadowRoot: true, | ||
displayCheck: | ||
// JSDOM does not support the `tabbable` library. To solve this we can | ||
// check if `ResizeObserver` is a real function (not polyfilled), which | ||
// determines if the current environment is JSDOM-like. | ||
typeof ResizeObserver === 'function' && ResizeObserver.toString().includes('[native code]') ? 'full' : 'none' | ||
}); | ||
function getTabbableIn(container, dir) { | ||
const list = tabbable(container, getTabbableOptions()); | ||
const len = list.length; | ||
if (len === 0) return; | ||
const active = activeElement(getDocument(container)); | ||
const index = list.indexOf(active); | ||
const nextIndex = index === -1 ? dir === 1 ? 0 : len - 1 : index + dir; | ||
return list[nextIndex]; | ||
} | ||
function isTypeableCombobox(element) { | ||
if (!element) return false; | ||
return element.getAttribute('role') === 'combobox' && isTypeableElement(element); | ||
function getNextTabbable(referenceElement) { | ||
return getTabbableIn(getDocument(referenceElement).body, 1) || referenceElement; | ||
} | ||
function getPreviousTabbable(referenceElement) { | ||
return getTabbableIn(getDocument(referenceElement).body, -1) || referenceElement; | ||
} | ||
function isOutsideEvent(event, container) { | ||
const containerElement = container || event.currentTarget; | ||
const relatedTarget = event.relatedTarget; | ||
return !relatedTarget || !contains(containerElement, relatedTarget); | ||
} | ||
function disableFocusInside(container) { | ||
const tabbableElements = tabbable(container, getTabbableOptions()); | ||
tabbableElements.forEach(element => { | ||
element.dataset.tabindex = element.getAttribute('tabindex') || ''; | ||
element.setAttribute('tabindex', '-1'); | ||
}); | ||
} | ||
function enableFocusInside(container) { | ||
const elements = container.querySelectorAll('[data-tabindex]'); | ||
elements.forEach(element => { | ||
const tabindex = element.dataset.tabindex; | ||
delete element.dataset.tabindex; | ||
if (tabindex) { | ||
element.setAttribute('tabindex', tabindex); | ||
} else { | ||
element.removeAttribute('tabindex'); | ||
} | ||
}); | ||
} | ||
export { TYPEABLE_SELECTOR, activeElement, contains, getDocument, getPlatform, getTarget, getUserAgent, isAndroid, isEventTargetWithin, isJSDOM, isMac, isMouseLikePointerType, isReactEvent, isRootElement, isSafari, isTypeableCombobox, isTypeableElement, isVirtualClick, isVirtualPointerEvent, stopEvent }; | ||
export { activeElement, contains, createGridCellMap, disableFocusInside, enableFocusInside, findNonDisabledListIndex, getDeepestNode, getDocument, getFloatingFocusElement, getGridCellIndexOfCorner, getGridCellIndices, getGridNavigatedIndex, getMaxListIndex, getMinListIndex, getNextTabbable, getNodeAncestors, getNodeChildren, getPlatform, getPreviousTabbable, getTabbableOptions, getTarget, getUserAgent, isAndroid, isDifferentGridRow, isEventTargetWithin, isIndexOutOfListBounds, isJSDOM, isListIndexDisabled, isMac, isMouseLikePointerType, isOutsideEvent, isReactEvent, isRootElement, isSafari, isTypeableCombobox, isTypeableElement, isVirtualClick, isVirtualPointerEvent, matchesFocusVisible, stopEvent, useEffectEvent, useLatestRef, index as useModernLayoutEffect }; |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FloatingUIReactUtils = {})); | ||
})(this, (function (exports) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FloatingUIReactUtils = {}, global.React)); | ||
})(this, (function (exports, React) { 'use strict'; | ||
function _interopNamespaceDefault(e) { | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
} | ||
n.default = e; | ||
return Object.freeze(n); | ||
} | ||
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React); | ||
function hasWindow() { | ||
@@ -27,2 +46,45 @@ return typeof window !== 'undefined'; | ||
// Avoid Chrome DevTools blue warning. | ||
function getPlatform() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData != null && uaData.platform) { | ||
return uaData.platform; | ||
} | ||
return navigator.platform; | ||
} | ||
function getUserAgent() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData && Array.isArray(uaData.brands)) { | ||
return uaData.brands.map(_ref => { | ||
let { | ||
brand, | ||
version | ||
} = _ref; | ||
return brand + "/" + version; | ||
}).join(' '); | ||
} | ||
return navigator.userAgent; | ||
} | ||
function isSafari() { | ||
// Chrome DevTools does not complain about navigator.vendor | ||
return /apple/i.test(navigator.vendor); | ||
} | ||
function isAndroid() { | ||
const re = /android/i; | ||
return re.test(getPlatform()) || re.test(getUserAgent()); | ||
} | ||
function isMac() { | ||
return getPlatform().toLowerCase().startsWith('mac') && !navigator.maxTouchPoints; | ||
} | ||
function isJSDOM() { | ||
return getUserAgent().includes('jsdom/'); | ||
} | ||
const FOCUSABLE_ATTRIBUTE = 'data-floating-ui-focusable'; | ||
const TYPEABLE_SELECTOR = "input:not([type='hidden']):not([disabled])," + "[contenteditable]:not([contenteditable='false']),textarea:not([disabled])"; | ||
const ARROW_LEFT = 'ArrowLeft'; | ||
const ARROW_RIGHT = 'ArrowRight'; | ||
const ARROW_UP = 'ArrowUp'; | ||
const ARROW_DOWN = 'ArrowDown'; | ||
function activeElement(doc) { | ||
@@ -62,24 +124,116 @@ let activeElement = doc.activeElement; | ||
} | ||
// Avoid Chrome DevTools blue warning. | ||
function getPlatform() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData != null && uaData.platform) { | ||
return uaData.platform; | ||
function getTarget(event) { | ||
if ('composedPath' in event) { | ||
return event.composedPath()[0]; | ||
} | ||
return navigator.platform; | ||
// TS thinks `event` is of type never as it assumes all browsers support | ||
// `composedPath()`, but browsers without shadow DOM don't. | ||
return event.target; | ||
} | ||
function getUserAgent() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData && Array.isArray(uaData.brands)) { | ||
return uaData.brands.map(_ref => { | ||
let { | ||
brand, | ||
version | ||
} = _ref; | ||
return brand + "/" + version; | ||
}).join(' '); | ||
function isEventTargetWithin(event, node) { | ||
if (node == null) { | ||
return false; | ||
} | ||
return navigator.userAgent; | ||
if ('composedPath' in event) { | ||
return event.composedPath().includes(node); | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support composedPath, but browsers without shadow dom don't | ||
const e = event; | ||
return e.target != null && node.contains(e.target); | ||
} | ||
function isRootElement(element) { | ||
return element.matches('html,body'); | ||
} | ||
function getDocument(node) { | ||
return (node == null ? void 0 : node.ownerDocument) || document; | ||
} | ||
function isTypeableElement(element) { | ||
return isHTMLElement(element) && element.matches(TYPEABLE_SELECTOR); | ||
} | ||
function isTypeableCombobox(element) { | ||
if (!element) return false; | ||
return element.getAttribute('role') === 'combobox' && isTypeableElement(element); | ||
} | ||
function matchesFocusVisible(element) { | ||
// We don't want to block focus from working with `visibleOnly` | ||
// (JSDOM doesn't match `:focus-visible` when the element has `:focus`) | ||
if (!element || isJSDOM()) return true; | ||
try { | ||
return element.matches(':focus-visible'); | ||
} catch (_e) { | ||
return true; | ||
} | ||
} | ||
function getFloatingFocusElement(floatingElement) { | ||
if (!floatingElement) { | ||
return null; | ||
} | ||
// Try to find the element that has `{...getFloatingProps()}` spread on it. | ||
// This indicates the floating element is acting as a positioning wrapper, and | ||
// so focus should be managed on the child element with the event handlers and | ||
// aria props. | ||
return floatingElement.hasAttribute(FOCUSABLE_ATTRIBUTE) ? floatingElement : floatingElement.querySelector("[" + FOCUSABLE_ATTRIBUTE + "]") || floatingElement; | ||
} | ||
function getNodeChildren(nodes, id, onlyOpenChildren) { | ||
if (onlyOpenChildren === void 0) { | ||
onlyOpenChildren = true; | ||
} | ||
let allChildren = nodes.filter(node => { | ||
var _node$context; | ||
return node.parentId === id && ((_node$context = node.context) == null ? void 0 : _node$context.open); | ||
}); | ||
let currentChildren = allChildren; | ||
while (currentChildren.length) { | ||
currentChildren = onlyOpenChildren ? nodes.filter(node => { | ||
var _currentChildren; | ||
return (_currentChildren = currentChildren) == null ? void 0 : _currentChildren.some(n => { | ||
var _node$context2; | ||
return node.parentId === n.id && ((_node$context2 = node.context) == null ? void 0 : _node$context2.open); | ||
}); | ||
}) : nodes; | ||
allChildren = allChildren.concat(currentChildren); | ||
} | ||
return allChildren; | ||
} | ||
function getDeepestNode(nodes, id) { | ||
let deepestNodeId; | ||
let maxDepth = -1; | ||
function findDeepest(nodeId, depth) { | ||
if (depth > maxDepth) { | ||
deepestNodeId = nodeId; | ||
maxDepth = depth; | ||
} | ||
const children = getNodeChildren(nodes, nodeId); | ||
children.forEach(child => { | ||
findDeepest(child.id, depth + 1); | ||
}); | ||
} | ||
findDeepest(id, 0); | ||
return nodes.find(node => node.id === deepestNodeId); | ||
} | ||
function getNodeAncestors(nodes, id) { | ||
var _nodes$find; | ||
let allAncestors = []; | ||
let currentParentId = (_nodes$find = nodes.find(node => node.id === id)) == null ? void 0 : _nodes$find.parentId; | ||
while (currentParentId) { | ||
const currentNode = nodes.find(node => node.id === currentParentId); | ||
currentParentId = currentNode == null ? void 0 : currentNode.parentId; | ||
if (currentNode) { | ||
allAncestors = allAncestors.concat(currentNode); | ||
} | ||
} | ||
return allAncestors; | ||
} | ||
function stopEvent(event) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
} | ||
function isReactEvent(event) { | ||
return 'nativeEvent' in event; | ||
} | ||
// License: https://github.com/adobe/react-spectrum/blob/b35d5c02fe900badccd0cf1a8f23bb593419f238/packages/@react-aria/utils/src/isVirtualEvent.ts | ||
@@ -103,16 +257,2 @@ function isVirtualClick(event) { | ||
} | ||
function isSafari() { | ||
// Chrome DevTools does not complain about navigator.vendor | ||
return /apple/i.test(navigator.vendor); | ||
} | ||
function isAndroid() { | ||
const re = /android/i; | ||
return re.test(getPlatform()) || re.test(getUserAgent()); | ||
} | ||
function isMac() { | ||
return getPlatform().toLowerCase().startsWith('mac') && !navigator.maxTouchPoints; | ||
} | ||
function isJSDOM() { | ||
return getUserAgent().includes('jsdom/'); | ||
} | ||
function isMouseLikePointerType(pointerType, strict) { | ||
@@ -127,57 +267,892 @@ // On some Linux machines with Chromium, mouse inputs return a `pointerType` | ||
} | ||
function isReactEvent(event) { | ||
return 'nativeEvent' in event; | ||
var index = typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect; | ||
// https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379 | ||
const SafeReact = { | ||
...React__namespace | ||
}; | ||
function useLatestRef(value) { | ||
const ref = React__namespace.useRef(value); | ||
index(() => { | ||
ref.current = value; | ||
}); | ||
return ref; | ||
} | ||
function isRootElement(element) { | ||
return element.matches('html,body'); | ||
const useInsertionEffect = SafeReact.useInsertionEffect; | ||
const useSafeInsertionEffect = useInsertionEffect || (fn => fn()); | ||
function useEffectEvent(callback) { | ||
const ref = React__namespace.useRef(() => { | ||
{ | ||
throw new Error('Cannot call an event handler while rendering.'); | ||
} | ||
}); | ||
useSafeInsertionEffect(() => { | ||
ref.current = callback; | ||
}); | ||
return React__namespace.useCallback(function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return ref.current == null ? void 0 : ref.current(...args); | ||
}, []); | ||
} | ||
function getDocument(node) { | ||
return (node == null ? void 0 : node.ownerDocument) || document; | ||
/** | ||
* Custom positioning reference element. | ||
* @see https://floating-ui.com/docs/virtual-elements | ||
*/ | ||
const floor = Math.floor; | ||
function isDifferentGridRow(index, cols, prevRow) { | ||
return Math.floor(index / cols) !== prevRow; | ||
} | ||
function isEventTargetWithin(event, node) { | ||
if (node == null) { | ||
return false; | ||
function isIndexOutOfListBounds(listRef, index) { | ||
return index < 0 || index >= listRef.current.length; | ||
} | ||
function getMinListIndex(listRef, disabledIndices) { | ||
return findNonDisabledListIndex(listRef, { | ||
disabledIndices | ||
}); | ||
} | ||
function getMaxListIndex(listRef, disabledIndices) { | ||
return findNonDisabledListIndex(listRef, { | ||
decrement: true, | ||
startingIndex: listRef.current.length, | ||
disabledIndices | ||
}); | ||
} | ||
function findNonDisabledListIndex(listRef, _temp) { | ||
let { | ||
startingIndex = -1, | ||
decrement = false, | ||
disabledIndices, | ||
amount = 1 | ||
} = _temp === void 0 ? {} : _temp; | ||
let index = startingIndex; | ||
do { | ||
index += decrement ? -amount : amount; | ||
} while (index >= 0 && index <= listRef.current.length - 1 && isListIndexDisabled(listRef, index, disabledIndices)); | ||
return index; | ||
} | ||
function getGridNavigatedIndex(listRef, _ref) { | ||
let { | ||
event, | ||
orientation, | ||
loop, | ||
rtl, | ||
cols, | ||
disabledIndices, | ||
minIndex, | ||
maxIndex, | ||
prevIndex, | ||
stopEvent: stop = false | ||
} = _ref; | ||
let nextIndex = prevIndex; | ||
if (event.key === ARROW_UP) { | ||
stop && stopEvent(event); | ||
if (prevIndex === -1) { | ||
nextIndex = maxIndex; | ||
} else { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: nextIndex, | ||
amount: cols, | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
if (loop && (prevIndex - cols < minIndex || nextIndex < 0)) { | ||
const col = prevIndex % cols; | ||
const maxCol = maxIndex % cols; | ||
const offset = maxIndex - (maxCol - col); | ||
if (maxCol === col) { | ||
nextIndex = maxIndex; | ||
} else { | ||
nextIndex = maxCol > col ? offset : offset - cols; | ||
} | ||
} | ||
} | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
if ('composedPath' in event) { | ||
return event.composedPath().includes(node); | ||
if (event.key === ARROW_DOWN) { | ||
stop && stopEvent(event); | ||
if (prevIndex === -1) { | ||
nextIndex = minIndex; | ||
} else { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
amount: cols, | ||
disabledIndices | ||
}); | ||
if (loop && prevIndex + cols > maxIndex) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex % cols - cols, | ||
amount: cols, | ||
disabledIndices | ||
}); | ||
} | ||
} | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support composedPath, but browsers without shadow dom don't | ||
const e = event; | ||
return e.target != null && node.contains(e.target); | ||
// Remains on the same row/column. | ||
if (orientation === 'both') { | ||
const prevRow = floor(prevIndex / cols); | ||
if (event.key === (rtl ? ARROW_LEFT : ARROW_RIGHT)) { | ||
stop && stopEvent(event); | ||
if (prevIndex % cols !== cols - 1) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
disabledIndices | ||
}); | ||
if (loop && isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} | ||
} else if (loop) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} | ||
if (isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
if (event.key === (rtl ? ARROW_RIGHT : ARROW_LEFT)) { | ||
stop && stopEvent(event); | ||
if (prevIndex % cols !== 0) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
if (loop && isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex + (cols - prevIndex % cols), | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
} | ||
} else if (loop) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex + (cols - prevIndex % cols), | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
} | ||
if (isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
const lastRow = floor(maxIndex / cols) === prevRow; | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
if (loop && lastRow) { | ||
nextIndex = event.key === (rtl ? ARROW_RIGHT : ARROW_LEFT) ? maxIndex : findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} else { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
} | ||
return nextIndex; | ||
} | ||
function getTarget(event) { | ||
if ('composedPath' in event) { | ||
return event.composedPath()[0]; | ||
/** For each cell index, gets the item index that occupies that cell */ | ||
function createGridCellMap(sizes, cols, dense) { | ||
const cellMap = []; | ||
let startIndex = 0; | ||
sizes.forEach((_ref2, index) => { | ||
let { | ||
width, | ||
height | ||
} = _ref2; | ||
if (width > cols) { | ||
{ | ||
throw new Error("[Floating UI]: Invalid grid - item width at index " + index + " is greater than grid columns"); | ||
} | ||
} | ||
let itemPlaced = false; | ||
if (dense) { | ||
startIndex = 0; | ||
} | ||
while (!itemPlaced) { | ||
const targetCells = []; | ||
for (let i = 0; i < width; i++) { | ||
for (let j = 0; j < height; j++) { | ||
targetCells.push(startIndex + i + j * cols); | ||
} | ||
} | ||
if (startIndex % cols + width <= cols && targetCells.every(cell => cellMap[cell] == null)) { | ||
targetCells.forEach(cell => { | ||
cellMap[cell] = index; | ||
}); | ||
itemPlaced = true; | ||
} else { | ||
startIndex++; | ||
} | ||
} | ||
}); | ||
// convert into a non-sparse array | ||
return [...cellMap]; | ||
} | ||
/** Gets cell index of an item's corner or -1 when index is -1. */ | ||
function getGridCellIndexOfCorner(index, sizes, cellMap, cols, corner) { | ||
if (index === -1) return -1; | ||
const firstCellIndex = cellMap.indexOf(index); | ||
const sizeItem = sizes[index]; | ||
switch (corner) { | ||
case 'tl': | ||
return firstCellIndex; | ||
case 'tr': | ||
if (!sizeItem) { | ||
return firstCellIndex; | ||
} | ||
return firstCellIndex + sizeItem.width - 1; | ||
case 'bl': | ||
if (!sizeItem) { | ||
return firstCellIndex; | ||
} | ||
return firstCellIndex + (sizeItem.height - 1) * cols; | ||
case 'br': | ||
return cellMap.lastIndexOf(index); | ||
} | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support | ||
// `composedPath()`, but browsers without shadow DOM don't. | ||
return event.target; | ||
/** Gets all cell indices that correspond to the specified indices */ | ||
function getGridCellIndices(indices, cellMap) { | ||
return cellMap.flatMap((index, cellIndex) => indices.includes(index) ? [cellIndex] : []); | ||
} | ||
const TYPEABLE_SELECTOR = "input:not([type='hidden']):not([disabled])," + "[contenteditable]:not([contenteditable='false']),textarea:not([disabled])"; | ||
function isTypeableElement(element) { | ||
return isHTMLElement(element) && element.matches(TYPEABLE_SELECTOR); | ||
function isListIndexDisabled(listRef, index, disabledIndices) { | ||
if (disabledIndices) { | ||
return disabledIndices.includes(index); | ||
} | ||
const element = listRef.current[index]; | ||
return element == null || element.hasAttribute('disabled') || element.getAttribute('aria-disabled') === 'true'; | ||
} | ||
function stopEvent(event) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
/*! | ||
* tabbable 6.2.0 | ||
* @license MIT, https://github.com/focus-trap/tabbable/blob/master/LICENSE | ||
*/ | ||
// NOTE: separate `:not()` selectors has broader browser support than the newer | ||
// `:not([inert], [inert] *)` (Feb 2023) | ||
// CAREFUL: JSDom does not support `:not([inert] *)` as a selector; using it causes | ||
// the entire query to fail, resulting in no nodes found, which will break a lot | ||
// of things... so we have to rely on JS to identify nodes inside an inert container | ||
var candidateSelectors = ['input:not([inert])', 'select:not([inert])', 'textarea:not([inert])', 'a[href]:not([inert])', 'button:not([inert])', '[tabindex]:not(slot):not([inert])', 'audio[controls]:not([inert])', 'video[controls]:not([inert])', '[contenteditable]:not([contenteditable="false"]):not([inert])', 'details>summary:first-of-type:not([inert])', 'details:not([inert])']; | ||
var candidateSelector = /* #__PURE__ */candidateSelectors.join(','); | ||
var NoElement = typeof Element === 'undefined'; | ||
var matches = NoElement ? function () {} : Element.prototype.matches || Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; | ||
var getRootNode = !NoElement && Element.prototype.getRootNode ? function (element) { | ||
var _element$getRootNode; | ||
return element === null || element === void 0 ? void 0 : (_element$getRootNode = element.getRootNode) === null || _element$getRootNode === void 0 ? void 0 : _element$getRootNode.call(element); | ||
} : function (element) { | ||
return element === null || element === void 0 ? void 0 : element.ownerDocument; | ||
}; | ||
/** | ||
* Determines if a node is inert or in an inert ancestor. | ||
* @param {Element} [node] | ||
* @param {boolean} [lookUp] If true and `node` is not inert, looks up at ancestors to | ||
* see if any of them are inert. If false, only `node` itself is considered. | ||
* @returns {boolean} True if inert itself or by way of being in an inert ancestor. | ||
* False if `node` is falsy. | ||
*/ | ||
var isInert = function isInert(node, lookUp) { | ||
var _node$getAttribute; | ||
if (lookUp === void 0) { | ||
lookUp = true; | ||
} | ||
// CAREFUL: JSDom does not support inert at all, so we can't use the `HTMLElement.inert` | ||
// JS API property; we have to check the attribute, which can either be empty or 'true'; | ||
// if it's `null` (not specified) or 'false', it's an active element | ||
var inertAtt = node === null || node === void 0 ? void 0 : (_node$getAttribute = node.getAttribute) === null || _node$getAttribute === void 0 ? void 0 : _node$getAttribute.call(node, 'inert'); | ||
var inert = inertAtt === '' || inertAtt === 'true'; | ||
// NOTE: this could also be handled with `node.matches('[inert], :is([inert] *)')` | ||
// if it weren't for `matches()` not being a function on shadow roots; the following | ||
// code works for any kind of node | ||
// CAREFUL: JSDom does not appear to support certain selectors like `:not([inert] *)` | ||
// so it likely would not support `:is([inert] *)` either... | ||
var result = inert || lookUp && node && isInert(node.parentNode); // recursive | ||
return result; | ||
}; | ||
/** | ||
* Determines if a node's content is editable. | ||
* @param {Element} [node] | ||
* @returns True if it's content-editable; false if it's not or `node` is falsy. | ||
*/ | ||
var isContentEditable = function isContentEditable(node) { | ||
var _node$getAttribute2; | ||
// CAREFUL: JSDom does not support the `HTMLElement.isContentEditable` API so we have | ||
// to use the attribute directly to check for this, which can either be empty or 'true'; | ||
// if it's `null` (not specified) or 'false', it's a non-editable element | ||
var attValue = node === null || node === void 0 ? void 0 : (_node$getAttribute2 = node.getAttribute) === null || _node$getAttribute2 === void 0 ? void 0 : _node$getAttribute2.call(node, 'contenteditable'); | ||
return attValue === '' || attValue === 'true'; | ||
}; | ||
/** | ||
* @param {Element} el container to check in | ||
* @param {boolean} includeContainer add container to check | ||
* @param {(node: Element) => boolean} filter filter candidates | ||
* @returns {Element[]} | ||
*/ | ||
var getCandidates = function getCandidates(el, includeContainer, filter) { | ||
// even if `includeContainer=false`, we still have to check it for inertness because | ||
// if it's inert, all its children are inert | ||
if (isInert(el)) { | ||
return []; | ||
} | ||
var candidates = Array.prototype.slice.apply(el.querySelectorAll(candidateSelector)); | ||
if (includeContainer && matches.call(el, candidateSelector)) { | ||
candidates.unshift(el); | ||
} | ||
candidates = candidates.filter(filter); | ||
return candidates; | ||
}; | ||
/** | ||
* @callback GetShadowRoot | ||
* @param {Element} element to check for shadow root | ||
* @returns {ShadowRoot|boolean} ShadowRoot if available or boolean indicating if a shadowRoot is attached but not available. | ||
*/ | ||
/** | ||
* @callback ShadowRootFilter | ||
* @param {Element} shadowHostNode the element which contains shadow content | ||
* @returns {boolean} true if a shadow root could potentially contain valid candidates. | ||
*/ | ||
/** | ||
* @typedef {Object} CandidateScope | ||
* @property {Element} scopeParent contains inner candidates | ||
* @property {Element[]} candidates list of candidates found in the scope parent | ||
*/ | ||
/** | ||
* @typedef {Object} IterativeOptions | ||
* @property {GetShadowRoot|boolean} getShadowRoot true if shadow support is enabled; falsy if not; | ||
* if a function, implies shadow support is enabled and either returns the shadow root of an element | ||
* or a boolean stating if it has an undisclosed shadow root | ||
* @property {(node: Element) => boolean} filter filter candidates | ||
* @property {boolean} flatten if true then result will flatten any CandidateScope into the returned list | ||
* @property {ShadowRootFilter} shadowRootFilter filter shadow roots; | ||
*/ | ||
/** | ||
* @param {Element[]} elements list of element containers to match candidates from | ||
* @param {boolean} includeContainer add container list to check | ||
* @param {IterativeOptions} options | ||
* @returns {Array.<Element|CandidateScope>} | ||
*/ | ||
var getCandidatesIteratively = function getCandidatesIteratively(elements, includeContainer, options) { | ||
var candidates = []; | ||
var elementsToCheck = Array.from(elements); | ||
while (elementsToCheck.length) { | ||
var element = elementsToCheck.shift(); | ||
if (isInert(element, false)) { | ||
// no need to look up since we're drilling down | ||
// anything inside this container will also be inert | ||
continue; | ||
} | ||
if (element.tagName === 'SLOT') { | ||
// add shadow dom slot scope (slot itself cannot be focusable) | ||
var assigned = element.assignedElements(); | ||
var content = assigned.length ? assigned : element.children; | ||
var nestedCandidates = getCandidatesIteratively(content, true, options); | ||
if (options.flatten) { | ||
candidates.push.apply(candidates, nestedCandidates); | ||
} else { | ||
candidates.push({ | ||
scopeParent: element, | ||
candidates: nestedCandidates | ||
}); | ||
} | ||
} else { | ||
// check candidate element | ||
var validCandidate = matches.call(element, candidateSelector); | ||
if (validCandidate && options.filter(element) && (includeContainer || !elements.includes(element))) { | ||
candidates.push(element); | ||
} | ||
// iterate over shadow content if possible | ||
var shadowRoot = element.shadowRoot || | ||
// check for an undisclosed shadow | ||
typeof options.getShadowRoot === 'function' && options.getShadowRoot(element); | ||
// no inert look up because we're already drilling down and checking for inertness | ||
// on the way down, so all containers to this root node should have already been | ||
// vetted as non-inert | ||
var validShadowRoot = !isInert(shadowRoot, false) && (!options.shadowRootFilter || options.shadowRootFilter(element)); | ||
if (shadowRoot && validShadowRoot) { | ||
// add shadow dom scope IIF a shadow root node was given; otherwise, an undisclosed | ||
// shadow exists, so look at light dom children as fallback BUT create a scope for any | ||
// child candidates found because they're likely slotted elements (elements that are | ||
// children of the web component element (which has the shadow), in the light dom, but | ||
// slotted somewhere _inside_ the undisclosed shadow) -- the scope is created below, | ||
// _after_ we return from this recursive call | ||
var _nestedCandidates = getCandidatesIteratively(shadowRoot === true ? element.children : shadowRoot.children, true, options); | ||
if (options.flatten) { | ||
candidates.push.apply(candidates, _nestedCandidates); | ||
} else { | ||
candidates.push({ | ||
scopeParent: element, | ||
candidates: _nestedCandidates | ||
}); | ||
} | ||
} else { | ||
// there's not shadow so just dig into the element's (light dom) children | ||
// __without__ giving the element special scope treatment | ||
elementsToCheck.unshift.apply(elementsToCheck, element.children); | ||
} | ||
} | ||
} | ||
return candidates; | ||
}; | ||
/** | ||
* @private | ||
* Determines if the node has an explicitly specified `tabindex` attribute. | ||
* @param {HTMLElement} node | ||
* @returns {boolean} True if so; false if not. | ||
*/ | ||
var hasTabIndex = function hasTabIndex(node) { | ||
return !isNaN(parseInt(node.getAttribute('tabindex'), 10)); | ||
}; | ||
/** | ||
* Determine the tab index of a given node. | ||
* @param {HTMLElement} node | ||
* @returns {number} Tab order (negative, 0, or positive number). | ||
* @throws {Error} If `node` is falsy. | ||
*/ | ||
var getTabIndex = function getTabIndex(node) { | ||
if (!node) { | ||
throw new Error('No node provided'); | ||
} | ||
if (node.tabIndex < 0) { | ||
// in Chrome, <details/>, <audio controls/> and <video controls/> elements get a default | ||
// `tabIndex` of -1 when the 'tabindex' attribute isn't specified in the DOM, | ||
// yet they are still part of the regular tab order; in FF, they get a default | ||
// `tabIndex` of 0; since Chrome still puts those elements in the regular tab | ||
// order, consider their tab index to be 0. | ||
// Also browsers do not return `tabIndex` correctly for contentEditable nodes; | ||
// so if they don't have a tabindex attribute specifically set, assume it's 0. | ||
if ((/^(AUDIO|VIDEO|DETAILS)$/.test(node.tagName) || isContentEditable(node)) && !hasTabIndex(node)) { | ||
return 0; | ||
} | ||
} | ||
return node.tabIndex; | ||
}; | ||
/** | ||
* Determine the tab index of a given node __for sort order purposes__. | ||
* @param {HTMLElement} node | ||
* @param {boolean} [isScope] True for a custom element with shadow root or slot that, by default, | ||
* has tabIndex -1, but needs to be sorted by document order in order for its content to be | ||
* inserted into the correct sort position. | ||
* @returns {number} Tab order (negative, 0, or positive number). | ||
*/ | ||
var getSortOrderTabIndex = function getSortOrderTabIndex(node, isScope) { | ||
var tabIndex = getTabIndex(node); | ||
if (tabIndex < 0 && isScope && !hasTabIndex(node)) { | ||
return 0; | ||
} | ||
return tabIndex; | ||
}; | ||
var sortOrderedTabbables = function sortOrderedTabbables(a, b) { | ||
return a.tabIndex === b.tabIndex ? a.documentOrder - b.documentOrder : a.tabIndex - b.tabIndex; | ||
}; | ||
var isInput = function isInput(node) { | ||
return node.tagName === 'INPUT'; | ||
}; | ||
var isHiddenInput = function isHiddenInput(node) { | ||
return isInput(node) && node.type === 'hidden'; | ||
}; | ||
var isDetailsWithSummary = function isDetailsWithSummary(node) { | ||
var r = node.tagName === 'DETAILS' && Array.prototype.slice.apply(node.children).some(function (child) { | ||
return child.tagName === 'SUMMARY'; | ||
}); | ||
return r; | ||
}; | ||
var getCheckedRadio = function getCheckedRadio(nodes, form) { | ||
for (var i = 0; i < nodes.length; i++) { | ||
if (nodes[i].checked && nodes[i].form === form) { | ||
return nodes[i]; | ||
} | ||
} | ||
}; | ||
var isTabbableRadio = function isTabbableRadio(node) { | ||
if (!node.name) { | ||
return true; | ||
} | ||
var radioScope = node.form || getRootNode(node); | ||
var queryRadios = function queryRadios(name) { | ||
return radioScope.querySelectorAll('input[type="radio"][name="' + name + '"]'); | ||
}; | ||
var radioSet; | ||
if (typeof window !== 'undefined' && typeof window.CSS !== 'undefined' && typeof window.CSS.escape === 'function') { | ||
radioSet = queryRadios(window.CSS.escape(node.name)); | ||
} else { | ||
try { | ||
radioSet = queryRadios(node.name); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error('Looks like you have a radio button with a name attribute containing invalid CSS selector characters and need the CSS.escape polyfill: %s', err.message); | ||
return false; | ||
} | ||
} | ||
var checked = getCheckedRadio(radioSet, node.form); | ||
return !checked || checked === node; | ||
}; | ||
var isRadio = function isRadio(node) { | ||
return isInput(node) && node.type === 'radio'; | ||
}; | ||
var isNonTabbableRadio = function isNonTabbableRadio(node) { | ||
return isRadio(node) && !isTabbableRadio(node); | ||
}; | ||
// determines if a node is ultimately attached to the window's document | ||
var isNodeAttached = function isNodeAttached(node) { | ||
var _nodeRoot; | ||
// The root node is the shadow root if the node is in a shadow DOM; some document otherwise | ||
// (but NOT _the_ document; see second 'If' comment below for more). | ||
// If rootNode is shadow root, it'll have a host, which is the element to which the shadow | ||
// is attached, and the one we need to check if it's in the document or not (because the | ||
// shadow, and all nodes it contains, is never considered in the document since shadows | ||
// behave like self-contained DOMs; but if the shadow's HOST, which is part of the document, | ||
// is hidden, or is not in the document itself but is detached, it will affect the shadow's | ||
// visibility, including all the nodes it contains). The host could be any normal node, | ||
// or a custom element (i.e. web component). Either way, that's the one that is considered | ||
// part of the document, not the shadow root, nor any of its children (i.e. the node being | ||
// tested). | ||
// To further complicate things, we have to look all the way up until we find a shadow HOST | ||
// that is attached (or find none) because the node might be in nested shadows... | ||
// If rootNode is not a shadow root, it won't have a host, and so rootNode should be the | ||
// document (per the docs) and while it's a Document-type object, that document does not | ||
// appear to be the same as the node's `ownerDocument` for some reason, so it's safer | ||
// to ignore the rootNode at this point, and use `node.ownerDocument`. Otherwise, | ||
// using `rootNode.contains(node)` will _always_ be true we'll get false-positives when | ||
// node is actually detached. | ||
// NOTE: If `nodeRootHost` or `node` happens to be the `document` itself (which is possible | ||
// if a tabbable/focusable node was quickly added to the DOM, focused, and then removed | ||
// from the DOM as in https://github.com/focus-trap/focus-trap-react/issues/905), then | ||
// `ownerDocument` will be `null`, hence the optional chaining on it. | ||
var nodeRoot = node && getRootNode(node); | ||
var nodeRootHost = (_nodeRoot = nodeRoot) === null || _nodeRoot === void 0 ? void 0 : _nodeRoot.host; | ||
// in some cases, a detached node will return itself as the root instead of a document or | ||
// shadow root object, in which case, we shouldn't try to look further up the host chain | ||
var attached = false; | ||
if (nodeRoot && nodeRoot !== node) { | ||
var _nodeRootHost, _nodeRootHost$ownerDo, _node$ownerDocument; | ||
attached = !!((_nodeRootHost = nodeRootHost) !== null && _nodeRootHost !== void 0 && (_nodeRootHost$ownerDo = _nodeRootHost.ownerDocument) !== null && _nodeRootHost$ownerDo !== void 0 && _nodeRootHost$ownerDo.contains(nodeRootHost) || node !== null && node !== void 0 && (_node$ownerDocument = node.ownerDocument) !== null && _node$ownerDocument !== void 0 && _node$ownerDocument.contains(node)); | ||
while (!attached && nodeRootHost) { | ||
var _nodeRoot2, _nodeRootHost2, _nodeRootHost2$ownerD; | ||
// since it's not attached and we have a root host, the node MUST be in a nested shadow DOM, | ||
// which means we need to get the host's host and check if that parent host is contained | ||
// in (i.e. attached to) the document | ||
nodeRoot = getRootNode(nodeRootHost); | ||
nodeRootHost = (_nodeRoot2 = nodeRoot) === null || _nodeRoot2 === void 0 ? void 0 : _nodeRoot2.host; | ||
attached = !!((_nodeRootHost2 = nodeRootHost) !== null && _nodeRootHost2 !== void 0 && (_nodeRootHost2$ownerD = _nodeRootHost2.ownerDocument) !== null && _nodeRootHost2$ownerD !== void 0 && _nodeRootHost2$ownerD.contains(nodeRootHost)); | ||
} | ||
} | ||
return attached; | ||
}; | ||
var isZeroArea = function isZeroArea(node) { | ||
var _node$getBoundingClie = node.getBoundingClientRect(), | ||
width = _node$getBoundingClie.width, | ||
height = _node$getBoundingClie.height; | ||
return width === 0 && height === 0; | ||
}; | ||
var isHidden = function isHidden(node, _ref) { | ||
var displayCheck = _ref.displayCheck, | ||
getShadowRoot = _ref.getShadowRoot; | ||
// NOTE: visibility will be `undefined` if node is detached from the document | ||
// (see notes about this further down), which means we will consider it visible | ||
// (this is legacy behavior from a very long way back) | ||
// NOTE: we check this regardless of `displayCheck="none"` because this is a | ||
// _visibility_ check, not a _display_ check | ||
if (getComputedStyle(node).visibility === 'hidden') { | ||
return true; | ||
} | ||
var isDirectSummary = matches.call(node, 'details>summary:first-of-type'); | ||
var nodeUnderDetails = isDirectSummary ? node.parentElement : node; | ||
if (matches.call(nodeUnderDetails, 'details:not([open]) *')) { | ||
return true; | ||
} | ||
if (!displayCheck || displayCheck === 'full' || displayCheck === 'legacy-full') { | ||
if (typeof getShadowRoot === 'function') { | ||
// figure out if we should consider the node to be in an undisclosed shadow and use the | ||
// 'non-zero-area' fallback | ||
var originalNode = node; | ||
while (node) { | ||
var parentElement = node.parentElement; | ||
var rootNode = getRootNode(node); | ||
if (parentElement && !parentElement.shadowRoot && getShadowRoot(parentElement) === true // check if there's an undisclosed shadow | ||
) { | ||
// node has an undisclosed shadow which means we can only treat it as a black box, so we | ||
// fall back to a non-zero-area test | ||
return isZeroArea(node); | ||
} else if (node.assignedSlot) { | ||
// iterate up slot | ||
node = node.assignedSlot; | ||
} else if (!parentElement && rootNode !== node.ownerDocument) { | ||
// cross shadow boundary | ||
node = rootNode.host; | ||
} else { | ||
// iterate up normal dom | ||
node = parentElement; | ||
} | ||
} | ||
node = originalNode; | ||
} | ||
// else, `getShadowRoot` might be true, but all that does is enable shadow DOM support | ||
// (i.e. it does not also presume that all nodes might have undisclosed shadows); or | ||
// it might be a falsy value, which means shadow DOM support is disabled | ||
// Since we didn't find it sitting in an undisclosed shadow (or shadows are disabled) | ||
// now we can just test to see if it would normally be visible or not, provided it's | ||
// attached to the main document. | ||
// NOTE: We must consider case where node is inside a shadow DOM and given directly to | ||
// `isTabbable()` or `isFocusable()` -- regardless of `getShadowRoot` option setting. | ||
if (isNodeAttached(node)) { | ||
// this works wherever the node is: if there's at least one client rect, it's | ||
// somehow displayed; it also covers the CSS 'display: contents' case where the | ||
// node itself is hidden in place of its contents; and there's no need to search | ||
// up the hierarchy either | ||
return !node.getClientRects().length; | ||
} | ||
// Else, the node isn't attached to the document, which means the `getClientRects()` | ||
// API will __always__ return zero rects (this can happen, for example, if React | ||
// is used to render nodes onto a detached tree, as confirmed in this thread: | ||
// https://github.com/facebook/react/issues/9117#issuecomment-284228870) | ||
// | ||
// It also means that even window.getComputedStyle(node).display will return `undefined` | ||
// because styles are only computed for nodes that are in the document. | ||
// | ||
// NOTE: THIS HAS BEEN THE CASE FOR YEARS. It is not new, nor is it caused by tabbable | ||
// somehow. Though it was never stated officially, anyone who has ever used tabbable | ||
// APIs on nodes in detached containers has actually implicitly used tabbable in what | ||
// was later (as of v5.2.0 on Apr 9, 2021) called `displayCheck="none"` mode -- essentially | ||
// considering __everything__ to be visible because of the innability to determine styles. | ||
// | ||
// v6.0.0: As of this major release, the default 'full' option __no longer treats detached | ||
// nodes as visible with the 'none' fallback.__ | ||
if (displayCheck !== 'legacy-full') { | ||
return true; // hidden | ||
} | ||
// else, fallback to 'none' mode and consider the node visible | ||
} else if (displayCheck === 'non-zero-area') { | ||
// NOTE: Even though this tests that the node's client rect is non-zero to determine | ||
// whether it's displayed, and that a detached node will __always__ have a zero-area | ||
// client rect, we don't special-case for whether the node is attached or not. In | ||
// this mode, we do want to consider nodes that have a zero area to be hidden at all | ||
// times, and that includes attached or not. | ||
return isZeroArea(node); | ||
} | ||
// visible, as far as we can tell, or per current `displayCheck=none` mode, we assume | ||
// it's visible | ||
return false; | ||
}; | ||
// form fields (nested) inside a disabled fieldset are not focusable/tabbable | ||
// unless they are in the _first_ <legend> element of the top-most disabled | ||
// fieldset | ||
var isDisabledFromFieldset = function isDisabledFromFieldset(node) { | ||
if (/^(INPUT|BUTTON|SELECT|TEXTAREA)$/.test(node.tagName)) { | ||
var parentNode = node.parentElement; | ||
// check if `node` is contained in a disabled <fieldset> | ||
while (parentNode) { | ||
if (parentNode.tagName === 'FIELDSET' && parentNode.disabled) { | ||
// look for the first <legend> among the children of the disabled <fieldset> | ||
for (var i = 0; i < parentNode.children.length; i++) { | ||
var child = parentNode.children.item(i); | ||
// when the first <legend> (in document order) is found | ||
if (child.tagName === 'LEGEND') { | ||
// if its parent <fieldset> is not nested in another disabled <fieldset>, | ||
// return whether `node` is a descendant of its first <legend> | ||
return matches.call(parentNode, 'fieldset[disabled] *') ? true : !child.contains(node); | ||
} | ||
} | ||
// the disabled <fieldset> containing `node` has no <legend> | ||
return true; | ||
} | ||
parentNode = parentNode.parentElement; | ||
} | ||
} | ||
// else, node's tabbable/focusable state should not be affected by a fieldset's | ||
// enabled/disabled state | ||
return false; | ||
}; | ||
var isNodeMatchingSelectorFocusable = function isNodeMatchingSelectorFocusable(options, node) { | ||
if (node.disabled || | ||
// we must do an inert look up to filter out any elements inside an inert ancestor | ||
// because we're limited in the type of selectors we can use in JSDom (see related | ||
// note related to `candidateSelectors`) | ||
isInert(node) || isHiddenInput(node) || isHidden(node, options) || | ||
// For a details element with a summary, the summary element gets the focus | ||
isDetailsWithSummary(node) || isDisabledFromFieldset(node)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
var isNodeMatchingSelectorTabbable = function isNodeMatchingSelectorTabbable(options, node) { | ||
if (isNonTabbableRadio(node) || getTabIndex(node) < 0 || !isNodeMatchingSelectorFocusable(options, node)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
var isValidShadowRootTabbable = function isValidShadowRootTabbable(shadowHostNode) { | ||
var tabIndex = parseInt(shadowHostNode.getAttribute('tabindex'), 10); | ||
if (isNaN(tabIndex) || tabIndex >= 0) { | ||
return true; | ||
} | ||
// If a custom element has an explicit negative tabindex, | ||
// browsers will not allow tab targeting said element's children. | ||
return false; | ||
}; | ||
/** | ||
* @param {Array.<Element|CandidateScope>} candidates | ||
* @returns Element[] | ||
*/ | ||
var sortByOrder = function sortByOrder(candidates) { | ||
var regularTabbables = []; | ||
var orderedTabbables = []; | ||
candidates.forEach(function (item, i) { | ||
var isScope = !!item.scopeParent; | ||
var element = isScope ? item.scopeParent : item; | ||
var candidateTabindex = getSortOrderTabIndex(element, isScope); | ||
var elements = isScope ? sortByOrder(item.candidates) : element; | ||
if (candidateTabindex === 0) { | ||
isScope ? regularTabbables.push.apply(regularTabbables, elements) : regularTabbables.push(element); | ||
} else { | ||
orderedTabbables.push({ | ||
documentOrder: i, | ||
tabIndex: candidateTabindex, | ||
item: item, | ||
isScope: isScope, | ||
content: elements | ||
}); | ||
} | ||
}); | ||
return orderedTabbables.sort(sortOrderedTabbables).reduce(function (acc, sortable) { | ||
sortable.isScope ? acc.push.apply(acc, sortable.content) : acc.push(sortable.content); | ||
return acc; | ||
}, []).concat(regularTabbables); | ||
}; | ||
var tabbable = function tabbable(container, options) { | ||
options = options || {}; | ||
var candidates; | ||
if (options.getShadowRoot) { | ||
candidates = getCandidatesIteratively([container], options.includeContainer, { | ||
filter: isNodeMatchingSelectorTabbable.bind(null, options), | ||
flatten: false, | ||
getShadowRoot: options.getShadowRoot, | ||
shadowRootFilter: isValidShadowRootTabbable | ||
}); | ||
} else { | ||
candidates = getCandidates(container, options.includeContainer, isNodeMatchingSelectorTabbable.bind(null, options)); | ||
} | ||
return sortByOrder(candidates); | ||
}; | ||
const getTabbableOptions = () => ({ | ||
getShadowRoot: true, | ||
displayCheck: | ||
// JSDOM does not support the `tabbable` library. To solve this we can | ||
// check if `ResizeObserver` is a real function (not polyfilled), which | ||
// determines if the current environment is JSDOM-like. | ||
typeof ResizeObserver === 'function' && ResizeObserver.toString().includes('[native code]') ? 'full' : 'none' | ||
}); | ||
function getTabbableIn(container, dir) { | ||
const list = tabbable(container, getTabbableOptions()); | ||
const len = list.length; | ||
if (len === 0) return; | ||
const active = activeElement(getDocument(container)); | ||
const index = list.indexOf(active); | ||
const nextIndex = index === -1 ? dir === 1 ? 0 : len - 1 : index + dir; | ||
return list[nextIndex]; | ||
} | ||
function isTypeableCombobox(element) { | ||
if (!element) return false; | ||
return element.getAttribute('role') === 'combobox' && isTypeableElement(element); | ||
function getNextTabbable(referenceElement) { | ||
return getTabbableIn(getDocument(referenceElement).body, 1) || referenceElement; | ||
} | ||
function getPreviousTabbable(referenceElement) { | ||
return getTabbableIn(getDocument(referenceElement).body, -1) || referenceElement; | ||
} | ||
function isOutsideEvent(event, container) { | ||
const containerElement = container || event.currentTarget; | ||
const relatedTarget = event.relatedTarget; | ||
return !relatedTarget || !contains(containerElement, relatedTarget); | ||
} | ||
function disableFocusInside(container) { | ||
const tabbableElements = tabbable(container, getTabbableOptions()); | ||
tabbableElements.forEach(element => { | ||
element.dataset.tabindex = element.getAttribute('tabindex') || ''; | ||
element.setAttribute('tabindex', '-1'); | ||
}); | ||
} | ||
function enableFocusInside(container) { | ||
const elements = container.querySelectorAll('[data-tabindex]'); | ||
elements.forEach(element => { | ||
const tabindex = element.dataset.tabindex; | ||
delete element.dataset.tabindex; | ||
if (tabindex) { | ||
element.setAttribute('tabindex', tabindex); | ||
} else { | ||
element.removeAttribute('tabindex'); | ||
} | ||
}); | ||
} | ||
exports.TYPEABLE_SELECTOR = TYPEABLE_SELECTOR; | ||
exports.activeElement = activeElement; | ||
exports.contains = contains; | ||
exports.createGridCellMap = createGridCellMap; | ||
exports.disableFocusInside = disableFocusInside; | ||
exports.enableFocusInside = enableFocusInside; | ||
exports.findNonDisabledListIndex = findNonDisabledListIndex; | ||
exports.getDeepestNode = getDeepestNode; | ||
exports.getDocument = getDocument; | ||
exports.getFloatingFocusElement = getFloatingFocusElement; | ||
exports.getGridCellIndexOfCorner = getGridCellIndexOfCorner; | ||
exports.getGridCellIndices = getGridCellIndices; | ||
exports.getGridNavigatedIndex = getGridNavigatedIndex; | ||
exports.getMaxListIndex = getMaxListIndex; | ||
exports.getMinListIndex = getMinListIndex; | ||
exports.getNextTabbable = getNextTabbable; | ||
exports.getNodeAncestors = getNodeAncestors; | ||
exports.getNodeChildren = getNodeChildren; | ||
exports.getPlatform = getPlatform; | ||
exports.getPreviousTabbable = getPreviousTabbable; | ||
exports.getTabbableOptions = getTabbableOptions; | ||
exports.getTarget = getTarget; | ||
exports.getUserAgent = getUserAgent; | ||
exports.isAndroid = isAndroid; | ||
exports.isDifferentGridRow = isDifferentGridRow; | ||
exports.isEventTargetWithin = isEventTargetWithin; | ||
exports.isIndexOutOfListBounds = isIndexOutOfListBounds; | ||
exports.isJSDOM = isJSDOM; | ||
exports.isListIndexDisabled = isListIndexDisabled; | ||
exports.isMac = isMac; | ||
exports.isMouseLikePointerType = isMouseLikePointerType; | ||
exports.isOutsideEvent = isOutsideEvent; | ||
exports.isReactEvent = isReactEvent; | ||
@@ -190,4 +1165,8 @@ exports.isRootElement = isRootElement; | ||
exports.isVirtualPointerEvent = isVirtualPointerEvent; | ||
exports.matchesFocusVisible = matchesFocusVisible; | ||
exports.stopEvent = stopEvent; | ||
exports.useEffectEvent = useEffectEvent; | ||
exports.useLatestRef = useLatestRef; | ||
exports.useModernLayoutEffect = index; | ||
})); |
@@ -1,1 +0,5 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).FloatingUIReactUtils={})}(this,(function(t){"use strict";function e(){return"undefined"!=typeof window}function n(t){var e;return(null==t||null==(e=t.ownerDocument)?void 0:e.defaultView)||window}function o(){const t=navigator.userAgentData;return null!=t&&t.platform?t.platform:navigator.platform}function i(){const t=navigator.userAgentData;return t&&Array.isArray(t.brands)?t.brands.map((t=>{let{brand:e,version:n}=t;return e+"/"+n})).join(" "):navigator.userAgent}function r(){const t=/android/i;return t.test(o())||t.test(i())}function u(){return i().includes("jsdom/")}const a="input:not([type='hidden']):not([disabled]),[contenteditable]:not([contenteditable='false']),textarea:not([disabled])";function s(t){return o=t,!!e()&&(o instanceof HTMLElement||o instanceof n(o).HTMLElement)&&t.matches(a);var o}t.TYPEABLE_SELECTOR=a,t.activeElement=function(t){let e=t.activeElement;for(;null!=(null==(n=e)||null==(n=n.shadowRoot)?void 0:n.activeElement);){var n;e=e.shadowRoot.activeElement}return e},t.contains=function(t,o){if(!t||!o)return!1;const i=null==o.getRootNode?void 0:o.getRootNode();if(t.contains(o))return!0;if(i&&(r=i,e()&&"undefined"!=typeof ShadowRoot&&(r instanceof ShadowRoot||r instanceof n(r).ShadowRoot))){let e=o;for(;e;){if(t===e)return!0;e=e.parentNode||e.host}}var r;return!1},t.getDocument=function(t){return(null==t?void 0:t.ownerDocument)||document},t.getPlatform=o,t.getTarget=function(t){return"composedPath"in t?t.composedPath()[0]:t.target},t.getUserAgent=i,t.isAndroid=r,t.isEventTargetWithin=function(t,e){if(null==e)return!1;if("composedPath"in t)return t.composedPath().includes(e);const n=t;return null!=n.target&&e.contains(n.target)},t.isJSDOM=u,t.isMac=function(){return o().toLowerCase().startsWith("mac")&&!navigator.maxTouchPoints},t.isMouseLikePointerType=function(t,e){const n=["mouse","pen"];return e||n.push("",void 0),n.includes(t)},t.isReactEvent=function(t){return"nativeEvent"in t},t.isRootElement=function(t){return t.matches("html,body")},t.isSafari=function(){return/apple/i.test(navigator.vendor)},t.isTypeableCombobox=function(t){return!!t&&("combobox"===t.getAttribute("role")&&s(t))},t.isTypeableElement=s,t.isVirtualClick=function(t){return!(0!==t.mozInputSource||!t.isTrusted)||(r()&&t.pointerType?"click"===t.type&&1===t.buttons:0===t.detail&&!t.pointerType)},t.isVirtualPointerEvent=function(t){return!u()&&(!r()&&0===t.width&&0===t.height||r()&&1===t.width&&1===t.height&&0===t.pressure&&0===t.detail&&"mouse"===t.pointerType||t.width<1&&t.height<1&&0===t.pressure&&0===t.detail&&"touch"===t.pointerType)},t.stopEvent=function(t){t.preventDefault(),t.stopPropagation()}})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).FloatingUIReactUtils={},e.React)}(this,(function(e,t){"use strict";function n(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var r=n(t);function o(){return"undefined"!=typeof window}function i(e){var t;return(null==e||null==(t=e.ownerDocument)?void 0:t.defaultView)||window}function a(){const e=navigator.userAgentData;return null!=e&&e.platform?e.platform:navigator.platform}function u(){const e=navigator.userAgentData;return e&&Array.isArray(e.brands)?e.brands.map((e=>{let{brand:t,version:n}=e;return t+"/"+n})).join(" "):navigator.userAgent}function d(){const e=/android/i;return e.test(a())||e.test(u())}function c(){return u().includes("jsdom/")}const l="data-floating-ui-focusable",s="ArrowLeft",f="ArrowRight";function p(e){let t=e.activeElement;for(;null!=(null==(n=t)||null==(n=n.shadowRoot)?void 0:n.activeElement);){var n;t=t.shadowRoot.activeElement}return t}function h(e,t){if(!e||!t)return!1;const n=null==t.getRootNode?void 0:t.getRootNode();if(e.contains(t))return!0;if(n&&(r=n,o()&&"undefined"!=typeof ShadowRoot&&(r instanceof ShadowRoot||r instanceof i(r).ShadowRoot))){let n=t;for(;n;){if(e===n)return!0;n=n.parentNode||n.host}}var r;return!1}function g(e){return(null==e?void 0:e.ownerDocument)||document}function v(e){return t=e,!!o()&&(t instanceof HTMLElement||t instanceof i(t).HTMLElement)&&e.matches("input:not([type='hidden']):not([disabled]),[contenteditable]:not([contenteditable='false']),textarea:not([disabled])");var t}function b(e,t,n){void 0===n&&(n=!0);let r=e.filter((e=>{var n;return e.parentId===t&&(null==(n=e.context)?void 0:n.open)})),o=r;for(;o.length;)o=n?e.filter((e=>{var t;return null==(t=o)?void 0:t.some((t=>{var n;return e.parentId===t.id&&(null==(n=e.context)?void 0:n.open)}))})):e,r=r.concat(o);return r}function m(e){e.preventDefault(),e.stopPropagation()}var y="undefined"!=typeof document?t.useLayoutEffect:t.useEffect;const I={...r}.useInsertionEffect||(e=>e());const w=Math.floor;function x(e,t,n){return Math.floor(e/t)!==n}function E(e,t){return t<0||t>=e.current.length}function S(e,t){let{startingIndex:n=-1,decrement:r=!1,disabledIndices:o,amount:i=1}=void 0===t?{}:t,a=n;do{a+=r?-i:i}while(a>=0&&a<=e.current.length-1&&A(e,a,o));return a}function A(e,t,n){if(n)return n.includes(t);const r=e.current[t];return null==r||r.hasAttribute("disabled")||"true"===r.getAttribute("aria-disabled")} | ||
/*! | ||
* tabbable 6.2.0 | ||
* @license MIT, https://github.com/focus-trap/tabbable/blob/master/LICENSE | ||
*/var R=["input:not([inert])","select:not([inert])","textarea:not([inert])","a[href]:not([inert])","button:not([inert])","[tabindex]:not(slot):not([inert])","audio[controls]:not([inert])","video[controls]:not([inert])",'[contenteditable]:not([contenteditable="false"]):not([inert])',"details>summary:first-of-type:not([inert])","details:not([inert])"].join(","),T="undefined"==typeof Element,N=T?function(){}:Element.prototype.matches||Element.prototype.msMatchesSelector||Element.prototype.webkitMatchesSelector,D=!T&&Element.prototype.getRootNode?function(e){var t;return null==e||null===(t=e.getRootNode)||void 0===t?void 0:t.call(e)}:function(e){return null==e?void 0:e.ownerDocument},O=function e(t,n){var r;void 0===n&&(n=!0);var o=null==t||null===(r=t.getAttribute)||void 0===r?void 0:r.call(t,"inert");return""===o||"true"===o||n&&t&&e(t.parentNode)},C=function e(t,n,r){for(var o=[],i=Array.from(t);i.length;){var a=i.shift();if(!O(a,!1))if("SLOT"===a.tagName){var u=a.assignedElements(),d=e(u.length?u:a.children,!0,r);r.flatten?o.push.apply(o,d):o.push({scopeParent:a,candidates:d})}else{N.call(a,R)&&r.filter(a)&&(n||!t.includes(a))&&o.push(a);var c=a.shadowRoot||"function"==typeof r.getShadowRoot&&r.getShadowRoot(a),l=!O(c,!1)&&(!r.shadowRootFilter||r.shadowRootFilter(a));if(c&&l){var s=e(!0===c?a.children:c.children,!0,r);r.flatten?o.push.apply(o,s):o.push({scopeParent:a,candidates:s})}else i.unshift.apply(i,a.children)}}return o},L=function(e){return!isNaN(parseInt(e.getAttribute("tabindex"),10))},P=function(e){if(!e)throw new Error("No node provided");return e.tabIndex<0&&(/^(AUDIO|VIDEO|DETAILS)$/.test(e.tagName)||function(e){var t,n=null==e||null===(t=e.getAttribute)||void 0===t?void 0:t.call(e,"contenteditable");return""===n||"true"===n}(e))&&!L(e)?0:e.tabIndex},k=function(e,t){return e.tabIndex===t.tabIndex?e.documentOrder-t.documentOrder:e.tabIndex-t.tabIndex},M=function(e){return"INPUT"===e.tagName},F=function(e){return function(e){return M(e)&&"radio"===e.type}(e)&&!function(e){if(!e.name)return!0;var t,n=e.form||D(e),r=function(e){return n.querySelectorAll('input[type="radio"][name="'+e+'"]')};if("undefined"!=typeof window&&void 0!==window.CSS&&"function"==typeof window.CSS.escape)t=r(window.CSS.escape(e.name));else try{t=r(e.name)}catch(e){return console.error("Looks like you have a radio button with a name attribute containing invalid CSS selector characters and need the CSS.escape polyfill: %s",e.message),!1}var o=function(e,t){for(var n=0;n<e.length;n++)if(e[n].checked&&e[n].form===t)return e[n]}(t,e.form);return!o||o===e}(e)},j=function(e){var t=e.getBoundingClientRect(),n=t.width,r=t.height;return 0===n&&0===r},U=function(e,t){var n=t.displayCheck,r=t.getShadowRoot;if("hidden"===getComputedStyle(e).visibility)return!0;var o=N.call(e,"details>summary:first-of-type")?e.parentElement:e;if(N.call(o,"details:not([open]) *"))return!0;if(n&&"full"!==n&&"legacy-full"!==n){if("non-zero-area"===n)return j(e)}else{if("function"==typeof r){for(var i=e;e;){var a=e.parentElement,u=D(e);if(a&&!a.shadowRoot&&!0===r(a))return j(e);e=e.assignedSlot?e.assignedSlot:a||u===e.ownerDocument?a:u.host}e=i}if(function(e){var t,n,r,o,i=e&&D(e),a=null===(t=i)||void 0===t?void 0:t.host,u=!1;if(i&&i!==e)for(u=!!(null!==(n=a)&&void 0!==n&&null!==(r=n.ownerDocument)&&void 0!==r&&r.contains(a)||null!=e&&null!==(o=e.ownerDocument)&&void 0!==o&&o.contains(e));!u&&a;){var d,c,l;u=!(null===(c=a=null===(d=i=D(a))||void 0===d?void 0:d.host)||void 0===c||null===(l=c.ownerDocument)||void 0===l||!l.contains(a))}return u}(e))return!e.getClientRects().length;if("legacy-full"!==n)return!0}return!1},G=function(e,t){return!(t.disabled||O(t)||function(e){return M(e)&&"hidden"===e.type}(t)||U(t,e)||function(e){return"DETAILS"===e.tagName&&Array.prototype.slice.apply(e.children).some((function(e){return"SUMMARY"===e.tagName}))}(t)||function(e){if(/^(INPUT|BUTTON|SELECT|TEXTAREA)$/.test(e.tagName))for(var t=e.parentElement;t;){if("FIELDSET"===t.tagName&&t.disabled){for(var n=0;n<t.children.length;n++){var r=t.children.item(n);if("LEGEND"===r.tagName)return!!N.call(t,"fieldset[disabled] *")||!r.contains(e)}return!0}t=t.parentElement}return!1}(t))},q=function(e,t){return!(F(t)||P(t)<0||!G(e,t))},z=function(e){var t=parseInt(e.getAttribute("tabindex"),10);return!!(isNaN(t)||t>=0)},V=function e(t){var n=[],r=[];return t.forEach((function(t,o){var i=!!t.scopeParent,a=i?t.scopeParent:t,u=function(e,t){var n=P(e);return n<0&&t&&!L(e)?0:n}(a,i),d=i?e(t.candidates):a;0===u?i?n.push.apply(n,d):n.push(a):r.push({documentOrder:o,tabIndex:u,item:t,isScope:i,content:d})})),r.sort(k).reduce((function(e,t){return t.isScope?e.push.apply(e,t.content):e.push(t.content),e}),[]).concat(n)},B=function(e,t){var n;return n=(t=t||{}).getShadowRoot?C([e],t.includeContainer,{filter:q.bind(null,t),flatten:!1,getShadowRoot:t.getShadowRoot,shadowRootFilter:z}):function(e,t,n){if(O(e))return[];var r=Array.prototype.slice.apply(e.querySelectorAll(R));return t&&N.call(e,R)&&r.unshift(e),r.filter(n)}(e,t.includeContainer,q.bind(null,t)),V(n)};const H=()=>({getShadowRoot:!0,displayCheck:"function"==typeof ResizeObserver&&ResizeObserver.toString().includes("[native code]")?"full":"none"});function W(e,t){const n=B(e,H()),r=n.length;if(0===r)return;const o=p(g(e)),i=n.indexOf(o);return n[-1===i?1===t?0:r-1:i+t]}e.activeElement=p,e.contains=h,e.createGridCellMap=function(e,t,n){const r=[];let o=0;return e.forEach(((e,i)=>{let{width:a,height:u}=e,d=!1;for(n&&(o=0);!d;){const e=[];for(let n=0;n<a;n++)for(let r=0;r<u;r++)e.push(o+n+r*t);o%t+a<=t&&e.every((e=>null==r[e]))?(e.forEach((e=>{r[e]=i})),d=!0):o++}})),[...r]},e.disableFocusInside=function(e){B(e,H()).forEach((e=>{e.dataset.tabindex=e.getAttribute("tabindex")||"",e.setAttribute("tabindex","-1")}))},e.enableFocusInside=function(e){e.querySelectorAll("[data-tabindex]").forEach((e=>{const t=e.dataset.tabindex;delete e.dataset.tabindex,t?e.setAttribute("tabindex",t):e.removeAttribute("tabindex")}))},e.findNonDisabledListIndex=S,e.getDeepestNode=function(e,t){let n,r=-1;return function t(o,i){i>r&&(n=o,r=i),b(e,o).forEach((e=>{t(e.id,i+1)}))}(t,0),e.find((e=>e.id===n))},e.getDocument=g,e.getFloatingFocusElement=function(e){return e?e.hasAttribute(l)?e:e.querySelector("["+l+"]")||e:null},e.getGridCellIndexOfCorner=function(e,t,n,r,o){if(-1===e)return-1;const i=n.indexOf(e),a=t[e];switch(o){case"tl":return i;case"tr":return a?i+a.width-1:i;case"bl":return a?i+(a.height-1)*r:i;case"br":return n.lastIndexOf(e)}},e.getGridCellIndices=function(e,t){return t.flatMap(((t,n)=>e.includes(t)?[n]:[]))},e.getGridNavigatedIndex=function(e,t){let{event:n,orientation:r,loop:o,rtl:i,cols:a,disabledIndices:u,minIndex:d,maxIndex:c,prevIndex:l,stopEvent:p=!1}=t,h=l;if("ArrowUp"===n.key){if(p&&m(n),-1===l)h=c;else if(h=S(e,{startingIndex:h,amount:a,decrement:!0,disabledIndices:u}),o&&(l-a<d||h<0)){const e=l%a,t=c%a,n=c-(t-e);h=t===e?c:t>e?n:n-a}E(e,h)&&(h=l)}if("ArrowDown"===n.key&&(p&&m(n),-1===l?h=d:(h=S(e,{startingIndex:l,amount:a,disabledIndices:u}),o&&l+a>c&&(h=S(e,{startingIndex:l%a-a,amount:a,disabledIndices:u}))),E(e,h)&&(h=l)),"both"===r){const t=w(l/a);n.key===(i?s:f)&&(p&&m(n),l%a!=a-1?(h=S(e,{startingIndex:l,disabledIndices:u}),o&&x(h,a,t)&&(h=S(e,{startingIndex:l-l%a-1,disabledIndices:u}))):o&&(h=S(e,{startingIndex:l-l%a-1,disabledIndices:u})),x(h,a,t)&&(h=l)),n.key===(i?f:s)&&(p&&m(n),l%a!=0?(h=S(e,{startingIndex:l,decrement:!0,disabledIndices:u}),o&&x(h,a,t)&&(h=S(e,{startingIndex:l+(a-l%a),decrement:!0,disabledIndices:u}))):o&&(h=S(e,{startingIndex:l+(a-l%a),decrement:!0,disabledIndices:u})),x(h,a,t)&&(h=l));const r=w(c/a)===t;E(e,h)&&(h=o&&r?n.key===(i?f:s)?c:S(e,{startingIndex:l-l%a-1,disabledIndices:u}):l)}return h},e.getMaxListIndex=function(e,t){return S(e,{decrement:!0,startingIndex:e.current.length,disabledIndices:t})},e.getMinListIndex=function(e,t){return S(e,{disabledIndices:t})},e.getNextTabbable=function(e){return W(g(e).body,1)||e},e.getNodeAncestors=function(e,t){var n;let r=[],o=null==(n=e.find((e=>e.id===t)))?void 0:n.parentId;for(;o;){const t=e.find((e=>e.id===o));o=null==t?void 0:t.parentId,t&&(r=r.concat(t))}return r},e.getNodeChildren=b,e.getPlatform=a,e.getPreviousTabbable=function(e){return W(g(e).body,-1)||e},e.getTabbableOptions=H,e.getTarget=function(e){return"composedPath"in e?e.composedPath()[0]:e.target},e.getUserAgent=u,e.isAndroid=d,e.isDifferentGridRow=x,e.isEventTargetWithin=function(e,t){if(null==t)return!1;if("composedPath"in e)return e.composedPath().includes(t);const n=e;return null!=n.target&&t.contains(n.target)},e.isIndexOutOfListBounds=E,e.isJSDOM=c,e.isListIndexDisabled=A,e.isMac=function(){return a().toLowerCase().startsWith("mac")&&!navigator.maxTouchPoints},e.isMouseLikePointerType=function(e,t){const n=["mouse","pen"];return t||n.push("",void 0),n.includes(e)},e.isOutsideEvent=function(e,t){const n=t||e.currentTarget,r=e.relatedTarget;return!r||!h(n,r)},e.isReactEvent=function(e){return"nativeEvent"in e},e.isRootElement=function(e){return e.matches("html,body")},e.isSafari=function(){return/apple/i.test(navigator.vendor)},e.isTypeableCombobox=function(e){return!!e&&("combobox"===e.getAttribute("role")&&v(e))},e.isTypeableElement=v,e.isVirtualClick=function(e){return!(0!==e.mozInputSource||!e.isTrusted)||(d()&&e.pointerType?"click"===e.type&&1===e.buttons:0===e.detail&&!e.pointerType)},e.isVirtualPointerEvent=function(e){return!c()&&(!d()&&0===e.width&&0===e.height||d()&&1===e.width&&1===e.height&&0===e.pressure&&0===e.detail&&"mouse"===e.pointerType||e.width<1&&e.height<1&&0===e.pressure&&0===e.detail&&"touch"===e.pointerType)},e.matchesFocusVisible=function(e){if(!e||c())return!0;try{return e.matches(":focus-visible")}catch(e){return!0}},e.stopEvent=m,e.useEffectEvent=function(e){const t=r.useRef((()=>{}));return I((()=>{t.current=e})),r.useCallback((function(){for(var e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];return null==t.current?void 0:t.current(...n)}),[])},e.useLatestRef=function(e){const t=r.useRef(e);return y((()=>{t.current=e})),t},e.useModernLayoutEffect=y})); |
{ | ||
"name": "@floating-ui/react", | ||
"version": "0.27.7", | ||
"version": "0.27.8", | ||
"description": "Floating UI for React", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -1,11 +0,120 @@ | ||
import type * as React from 'react'; | ||
/// <reference types="react" /> | ||
import { Dimensions } from '@floating-ui/react-dom'; | ||
import { FocusableElement } from 'tabbable'; | ||
import * as React from 'react'; | ||
import type { UseFloatingReturn } from '@floating-ui/react-dom'; | ||
import useModernLayoutEffect from 'use-isomorphic-layout-effect'; | ||
import type { VirtualElement } from '@floating-ui/react-dom'; | ||
export declare function activeElement(doc: Document): Element | null; | ||
declare type AnyFunction = (...args: any[]) => any; | ||
export declare function contains(parent?: Element | null, child?: Element | null): boolean; | ||
declare interface ContextData { | ||
openEvent?: Event; | ||
floatingContext?: FloatingContext; | ||
/** @deprecated use `onTypingChange` prop in `useTypeahead` */ | ||
typing?: boolean; | ||
[key: string]: any; | ||
} | ||
/** For each cell index, gets the item index that occupies that cell */ | ||
export declare function createGridCellMap(sizes: Dimensions[], cols: number, dense: boolean): (number | undefined)[]; | ||
export declare function disableFocusInside(container: HTMLElement): void; | ||
export declare function enableFocusInside(container: HTMLElement): void; | ||
declare interface ExtendedElements<RT> { | ||
reference: ReferenceType | null; | ||
floating: HTMLElement | null; | ||
domReference: NarrowedElement<RT> | null; | ||
} | ||
declare interface ExtendedRefs<RT> { | ||
reference: React.MutableRefObject<ReferenceType | null>; | ||
floating: React.MutableRefObject<HTMLElement | null>; | ||
domReference: React.MutableRefObject<NarrowedElement<RT> | null>; | ||
setReference(node: RT | null): void; | ||
setFloating(node: HTMLElement | null): void; | ||
setPositionReference(node: ReferenceType | null): void; | ||
} | ||
export declare function findNonDisabledListIndex(listRef: React.MutableRefObject<Array<HTMLElement | null>>, { startingIndex, decrement, disabledIndices, amount, }?: { | ||
startingIndex?: number; | ||
decrement?: boolean; | ||
disabledIndices?: Array<number>; | ||
amount?: number; | ||
}): number; | ||
declare type FloatingContext<RT extends ReferenceType = ReferenceType> = Omit<UseFloatingReturn<RT>, 'refs' | 'elements'> & { | ||
open: boolean; | ||
onOpenChange(open: boolean, event?: Event, reason?: OpenChangeReason): void; | ||
events: FloatingEvents; | ||
dataRef: React.MutableRefObject<ContextData>; | ||
nodeId: string | undefined; | ||
floatingId: string | undefined; | ||
refs: ExtendedRefs<RT>; | ||
elements: ExtendedElements<RT>; | ||
}; | ||
declare interface FloatingEvents { | ||
emit<T extends string>(event: T, data?: any): void; | ||
on(event: string, handler: (data: any) => void): void; | ||
off(event: string, handler: (data: any) => void): void; | ||
} | ||
declare interface FloatingNodeType<RT extends ReferenceType = ReferenceType> { | ||
id: string | undefined; | ||
parentId: string | null; | ||
context?: FloatingContext<RT>; | ||
} | ||
export declare function getDeepestNode<RT extends ReferenceType = ReferenceType>(nodes: Array<FloatingNodeType<RT>>, id: string | undefined): FloatingNodeType<RT> | undefined; | ||
export declare function getDocument(node: Element | null): Document; | ||
export declare function getFloatingFocusElement(floatingElement: HTMLElement | null | undefined): HTMLElement | null; | ||
/** Gets cell index of an item's corner or -1 when index is -1. */ | ||
export declare function getGridCellIndexOfCorner(index: number, sizes: Dimensions[], cellMap: (number | undefined)[], cols: number, corner: 'tl' | 'tr' | 'bl' | 'br'): number; | ||
/** Gets all cell indices that correspond to the specified indices */ | ||
export declare function getGridCellIndices(indices: (number | undefined)[], cellMap: (number | undefined)[]): number[]; | ||
export declare function getGridNavigatedIndex(listRef: React.MutableRefObject<Array<HTMLElement | null>>, { event, orientation, loop, rtl, cols, disabledIndices, minIndex, maxIndex, prevIndex, stopEvent: stop, }: { | ||
event: React.KeyboardEvent; | ||
orientation: 'horizontal' | 'vertical' | 'both'; | ||
loop: boolean; | ||
rtl: boolean; | ||
cols: number; | ||
disabledIndices: Array<number> | undefined; | ||
minIndex: number; | ||
maxIndex: number; | ||
prevIndex: number; | ||
stopEvent?: boolean; | ||
}): number; | ||
export declare function getMaxListIndex(listRef: React.MutableRefObject<Array<HTMLElement | null>>, disabledIndices: Array<number> | undefined): number; | ||
export declare function getMinListIndex(listRef: React.MutableRefObject<Array<HTMLElement | null>>, disabledIndices: Array<number> | undefined): number; | ||
export declare function getNextTabbable(referenceElement: Element | null): FocusableElement | null; | ||
export declare function getNodeAncestors<RT extends ReferenceType = ReferenceType>(nodes: Array<FloatingNodeType<RT>>, id: string | undefined): FloatingNodeType<RT>[]; | ||
export declare function getNodeChildren<RT extends ReferenceType = ReferenceType>(nodes: Array<FloatingNodeType<RT>>, id: string | undefined, onlyOpenChildren?: boolean): FloatingNodeType<RT>[]; | ||
export declare function getPlatform(): string; | ||
export declare function getPreviousTabbable(referenceElement: Element | null): FocusableElement | null; | ||
export declare const getTabbableOptions: () => { | ||
readonly getShadowRoot: true; | ||
readonly displayCheck: "none" | "full"; | ||
}; | ||
export declare function getTarget(event: Event): EventTarget | null; | ||
@@ -17,6 +126,12 @@ | ||
export declare function isDifferentGridRow(index: number, cols: number, prevRow: number): boolean; | ||
export declare function isEventTargetWithin(event: Event, node: Node | null | undefined): boolean; | ||
export declare function isIndexOutOfListBounds(listRef: React.MutableRefObject<Array<HTMLElement | null>>, index: number): boolean; | ||
export declare function isJSDOM(): boolean; | ||
export declare function isListIndexDisabled(listRef: React.MutableRefObject<Array<HTMLElement | null>>, index: number, disabledIndices?: Array<number>): boolean; | ||
export declare function isMac(): boolean; | ||
@@ -26,2 +141,4 @@ | ||
export declare function isOutsideEvent(event: FocusEvent | React.FocusEvent, container?: Element): boolean; | ||
export declare function isReactEvent(event: any): event is React.SyntheticEvent; | ||
@@ -41,6 +158,18 @@ | ||
export declare function matchesFocusVisible(element: Element | null): boolean; | ||
declare type NarrowedElement<T> = T extends Element ? T : Element; | ||
declare type OpenChangeReason = 'outside-press' | 'escape-key' | 'ancestor-scroll' | 'reference-press' | 'click' | 'hover' | 'focus' | 'focus-out' | 'list-navigation' | 'safe-polygon'; | ||
declare type ReferenceType = Element | VirtualElement; | ||
export declare function stopEvent(event: Event | React.SyntheticEvent): void; | ||
export declare const TYPEABLE_SELECTOR: string; | ||
export declare function useEffectEvent<T extends AnyFunction>(callback?: T): T; | ||
export declare function useLatestRef<T>(value: T): React.MutableRefObject<T>; | ||
export { useModernLayoutEffect } | ||
export { } |
import { isShadowRoot, isHTMLElement } from '@floating-ui/utils/dom'; | ||
import * as React from 'react'; | ||
import { useLayoutEffect, useEffect } from 'react'; | ||
import { floor } from '@floating-ui/utils'; | ||
import { tabbable } from 'tabbable'; | ||
// Avoid Chrome DevTools blue warning. | ||
function getPlatform() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData != null && uaData.platform) { | ||
return uaData.platform; | ||
} | ||
return navigator.platform; | ||
} | ||
function getUserAgent() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData && Array.isArray(uaData.brands)) { | ||
return uaData.brands.map(_ref => { | ||
let { | ||
brand, | ||
version | ||
} = _ref; | ||
return brand + "/" + version; | ||
}).join(' '); | ||
} | ||
return navigator.userAgent; | ||
} | ||
function isSafari() { | ||
// Chrome DevTools does not complain about navigator.vendor | ||
return /apple/i.test(navigator.vendor); | ||
} | ||
function isAndroid() { | ||
const re = /android/i; | ||
return re.test(getPlatform()) || re.test(getUserAgent()); | ||
} | ||
function isMac() { | ||
return getPlatform().toLowerCase().startsWith('mac') && !navigator.maxTouchPoints; | ||
} | ||
function isJSDOM() { | ||
return getUserAgent().includes('jsdom/'); | ||
} | ||
const FOCUSABLE_ATTRIBUTE = 'data-floating-ui-focusable'; | ||
const TYPEABLE_SELECTOR = "input:not([type='hidden']):not([disabled])," + "[contenteditable]:not([contenteditable='false']),textarea:not([disabled])"; | ||
const ARROW_LEFT = 'ArrowLeft'; | ||
const ARROW_RIGHT = 'ArrowRight'; | ||
const ARROW_UP = 'ArrowUp'; | ||
const ARROW_DOWN = 'ArrowDown'; | ||
function activeElement(doc) { | ||
@@ -37,24 +84,116 @@ let activeElement = doc.activeElement; | ||
} | ||
// Avoid Chrome DevTools blue warning. | ||
function getPlatform() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData != null && uaData.platform) { | ||
return uaData.platform; | ||
function getTarget(event) { | ||
if ('composedPath' in event) { | ||
return event.composedPath()[0]; | ||
} | ||
return navigator.platform; | ||
// TS thinks `event` is of type never as it assumes all browsers support | ||
// `composedPath()`, but browsers without shadow DOM don't. | ||
return event.target; | ||
} | ||
function getUserAgent() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData && Array.isArray(uaData.brands)) { | ||
return uaData.brands.map(_ref => { | ||
let { | ||
brand, | ||
version | ||
} = _ref; | ||
return brand + "/" + version; | ||
}).join(' '); | ||
function isEventTargetWithin(event, node) { | ||
if (node == null) { | ||
return false; | ||
} | ||
return navigator.userAgent; | ||
if ('composedPath' in event) { | ||
return event.composedPath().includes(node); | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support composedPath, but browsers without shadow dom don't | ||
const e = event; | ||
return e.target != null && node.contains(e.target); | ||
} | ||
function isRootElement(element) { | ||
return element.matches('html,body'); | ||
} | ||
function getDocument(node) { | ||
return (node == null ? void 0 : node.ownerDocument) || document; | ||
} | ||
function isTypeableElement(element) { | ||
return isHTMLElement(element) && element.matches(TYPEABLE_SELECTOR); | ||
} | ||
function isTypeableCombobox(element) { | ||
if (!element) return false; | ||
return element.getAttribute('role') === 'combobox' && isTypeableElement(element); | ||
} | ||
function matchesFocusVisible(element) { | ||
// We don't want to block focus from working with `visibleOnly` | ||
// (JSDOM doesn't match `:focus-visible` when the element has `:focus`) | ||
if (!element || isJSDOM()) return true; | ||
try { | ||
return element.matches(':focus-visible'); | ||
} catch (_e) { | ||
return true; | ||
} | ||
} | ||
function getFloatingFocusElement(floatingElement) { | ||
if (!floatingElement) { | ||
return null; | ||
} | ||
// Try to find the element that has `{...getFloatingProps()}` spread on it. | ||
// This indicates the floating element is acting as a positioning wrapper, and | ||
// so focus should be managed on the child element with the event handlers and | ||
// aria props. | ||
return floatingElement.hasAttribute(FOCUSABLE_ATTRIBUTE) ? floatingElement : floatingElement.querySelector("[" + FOCUSABLE_ATTRIBUTE + "]") || floatingElement; | ||
} | ||
function getNodeChildren(nodes, id, onlyOpenChildren) { | ||
if (onlyOpenChildren === void 0) { | ||
onlyOpenChildren = true; | ||
} | ||
let allChildren = nodes.filter(node => { | ||
var _node$context; | ||
return node.parentId === id && ((_node$context = node.context) == null ? void 0 : _node$context.open); | ||
}); | ||
let currentChildren = allChildren; | ||
while (currentChildren.length) { | ||
currentChildren = onlyOpenChildren ? nodes.filter(node => { | ||
var _currentChildren; | ||
return (_currentChildren = currentChildren) == null ? void 0 : _currentChildren.some(n => { | ||
var _node$context2; | ||
return node.parentId === n.id && ((_node$context2 = node.context) == null ? void 0 : _node$context2.open); | ||
}); | ||
}) : nodes; | ||
allChildren = allChildren.concat(currentChildren); | ||
} | ||
return allChildren; | ||
} | ||
function getDeepestNode(nodes, id) { | ||
let deepestNodeId; | ||
let maxDepth = -1; | ||
function findDeepest(nodeId, depth) { | ||
if (depth > maxDepth) { | ||
deepestNodeId = nodeId; | ||
maxDepth = depth; | ||
} | ||
const children = getNodeChildren(nodes, nodeId); | ||
children.forEach(child => { | ||
findDeepest(child.id, depth + 1); | ||
}); | ||
} | ||
findDeepest(id, 0); | ||
return nodes.find(node => node.id === deepestNodeId); | ||
} | ||
function getNodeAncestors(nodes, id) { | ||
var _nodes$find; | ||
let allAncestors = []; | ||
let currentParentId = (_nodes$find = nodes.find(node => node.id === id)) == null ? void 0 : _nodes$find.parentId; | ||
while (currentParentId) { | ||
const currentNode = nodes.find(node => node.id === currentParentId); | ||
currentParentId = currentNode == null ? void 0 : currentNode.parentId; | ||
if (currentNode) { | ||
allAncestors = allAncestors.concat(currentNode); | ||
} | ||
} | ||
return allAncestors; | ||
} | ||
function stopEvent(event) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
} | ||
function isReactEvent(event) { | ||
return 'nativeEvent' in event; | ||
} | ||
// License: https://github.com/adobe/react-spectrum/blob/b35d5c02fe900badccd0cf1a8f23bb593419f238/packages/@react-aria/utils/src/isVirtualEvent.ts | ||
@@ -78,16 +217,2 @@ function isVirtualClick(event) { | ||
} | ||
function isSafari() { | ||
// Chrome DevTools does not complain about navigator.vendor | ||
return /apple/i.test(navigator.vendor); | ||
} | ||
function isAndroid() { | ||
const re = /android/i; | ||
return re.test(getPlatform()) || re.test(getUserAgent()); | ||
} | ||
function isMac() { | ||
return getPlatform().toLowerCase().startsWith('mac') && !navigator.maxTouchPoints; | ||
} | ||
function isJSDOM() { | ||
return getUserAgent().includes('jsdom/'); | ||
} | ||
function isMouseLikePointerType(pointerType, strict) { | ||
@@ -102,45 +227,320 @@ // On some Linux machines with Chromium, mouse inputs return a `pointerType` | ||
} | ||
function isReactEvent(event) { | ||
return 'nativeEvent' in event; | ||
var index = typeof document !== 'undefined' ? useLayoutEffect : useEffect; | ||
// https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379 | ||
const SafeReact = { | ||
...React | ||
}; | ||
function useLatestRef(value) { | ||
const ref = React.useRef(value); | ||
index(() => { | ||
ref.current = value; | ||
}); | ||
return ref; | ||
} | ||
function isRootElement(element) { | ||
return element.matches('html,body'); | ||
const useInsertionEffect = SafeReact.useInsertionEffect; | ||
const useSafeInsertionEffect = useInsertionEffect || (fn => fn()); | ||
function useEffectEvent(callback) { | ||
const ref = React.useRef(() => { | ||
if (process.env.NODE_ENV !== "production") { | ||
throw new Error('Cannot call an event handler while rendering.'); | ||
} | ||
}); | ||
useSafeInsertionEffect(() => { | ||
ref.current = callback; | ||
}); | ||
return React.useCallback(function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return ref.current == null ? void 0 : ref.current(...args); | ||
}, []); | ||
} | ||
function getDocument(node) { | ||
return (node == null ? void 0 : node.ownerDocument) || document; | ||
function isDifferentGridRow(index, cols, prevRow) { | ||
return Math.floor(index / cols) !== prevRow; | ||
} | ||
function isEventTargetWithin(event, node) { | ||
if (node == null) { | ||
return false; | ||
function isIndexOutOfListBounds(listRef, index) { | ||
return index < 0 || index >= listRef.current.length; | ||
} | ||
function getMinListIndex(listRef, disabledIndices) { | ||
return findNonDisabledListIndex(listRef, { | ||
disabledIndices | ||
}); | ||
} | ||
function getMaxListIndex(listRef, disabledIndices) { | ||
return findNonDisabledListIndex(listRef, { | ||
decrement: true, | ||
startingIndex: listRef.current.length, | ||
disabledIndices | ||
}); | ||
} | ||
function findNonDisabledListIndex(listRef, _temp) { | ||
let { | ||
startingIndex = -1, | ||
decrement = false, | ||
disabledIndices, | ||
amount = 1 | ||
} = _temp === void 0 ? {} : _temp; | ||
let index = startingIndex; | ||
do { | ||
index += decrement ? -amount : amount; | ||
} while (index >= 0 && index <= listRef.current.length - 1 && isListIndexDisabled(listRef, index, disabledIndices)); | ||
return index; | ||
} | ||
function getGridNavigatedIndex(listRef, _ref) { | ||
let { | ||
event, | ||
orientation, | ||
loop, | ||
rtl, | ||
cols, | ||
disabledIndices, | ||
minIndex, | ||
maxIndex, | ||
prevIndex, | ||
stopEvent: stop = false | ||
} = _ref; | ||
let nextIndex = prevIndex; | ||
if (event.key === ARROW_UP) { | ||
stop && stopEvent(event); | ||
if (prevIndex === -1) { | ||
nextIndex = maxIndex; | ||
} else { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: nextIndex, | ||
amount: cols, | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
if (loop && (prevIndex - cols < minIndex || nextIndex < 0)) { | ||
const col = prevIndex % cols; | ||
const maxCol = maxIndex % cols; | ||
const offset = maxIndex - (maxCol - col); | ||
if (maxCol === col) { | ||
nextIndex = maxIndex; | ||
} else { | ||
nextIndex = maxCol > col ? offset : offset - cols; | ||
} | ||
} | ||
} | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
if ('composedPath' in event) { | ||
return event.composedPath().includes(node); | ||
if (event.key === ARROW_DOWN) { | ||
stop && stopEvent(event); | ||
if (prevIndex === -1) { | ||
nextIndex = minIndex; | ||
} else { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
amount: cols, | ||
disabledIndices | ||
}); | ||
if (loop && prevIndex + cols > maxIndex) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex % cols - cols, | ||
amount: cols, | ||
disabledIndices | ||
}); | ||
} | ||
} | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support composedPath, but browsers without shadow dom don't | ||
const e = event; | ||
return e.target != null && node.contains(e.target); | ||
// Remains on the same row/column. | ||
if (orientation === 'both') { | ||
const prevRow = floor(prevIndex / cols); | ||
if (event.key === (rtl ? ARROW_LEFT : ARROW_RIGHT)) { | ||
stop && stopEvent(event); | ||
if (prevIndex % cols !== cols - 1) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
disabledIndices | ||
}); | ||
if (loop && isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} | ||
} else if (loop) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} | ||
if (isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
if (event.key === (rtl ? ARROW_RIGHT : ARROW_LEFT)) { | ||
stop && stopEvent(event); | ||
if (prevIndex % cols !== 0) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
if (loop && isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex + (cols - prevIndex % cols), | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
} | ||
} else if (loop) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex + (cols - prevIndex % cols), | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
} | ||
if (isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
const lastRow = floor(maxIndex / cols) === prevRow; | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
if (loop && lastRow) { | ||
nextIndex = event.key === (rtl ? ARROW_RIGHT : ARROW_LEFT) ? maxIndex : findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} else { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
} | ||
return nextIndex; | ||
} | ||
function getTarget(event) { | ||
if ('composedPath' in event) { | ||
return event.composedPath()[0]; | ||
/** For each cell index, gets the item index that occupies that cell */ | ||
function createGridCellMap(sizes, cols, dense) { | ||
const cellMap = []; | ||
let startIndex = 0; | ||
sizes.forEach((_ref2, index) => { | ||
let { | ||
width, | ||
height | ||
} = _ref2; | ||
if (width > cols) { | ||
if (process.env.NODE_ENV !== "production") { | ||
throw new Error("[Floating UI]: Invalid grid - item width at index " + index + " is greater than grid columns"); | ||
} | ||
} | ||
let itemPlaced = false; | ||
if (dense) { | ||
startIndex = 0; | ||
} | ||
while (!itemPlaced) { | ||
const targetCells = []; | ||
for (let i = 0; i < width; i++) { | ||
for (let j = 0; j < height; j++) { | ||
targetCells.push(startIndex + i + j * cols); | ||
} | ||
} | ||
if (startIndex % cols + width <= cols && targetCells.every(cell => cellMap[cell] == null)) { | ||
targetCells.forEach(cell => { | ||
cellMap[cell] = index; | ||
}); | ||
itemPlaced = true; | ||
} else { | ||
startIndex++; | ||
} | ||
} | ||
}); | ||
// convert into a non-sparse array | ||
return [...cellMap]; | ||
} | ||
/** Gets cell index of an item's corner or -1 when index is -1. */ | ||
function getGridCellIndexOfCorner(index, sizes, cellMap, cols, corner) { | ||
if (index === -1) return -1; | ||
const firstCellIndex = cellMap.indexOf(index); | ||
const sizeItem = sizes[index]; | ||
switch (corner) { | ||
case 'tl': | ||
return firstCellIndex; | ||
case 'tr': | ||
if (!sizeItem) { | ||
return firstCellIndex; | ||
} | ||
return firstCellIndex + sizeItem.width - 1; | ||
case 'bl': | ||
if (!sizeItem) { | ||
return firstCellIndex; | ||
} | ||
return firstCellIndex + (sizeItem.height - 1) * cols; | ||
case 'br': | ||
return cellMap.lastIndexOf(index); | ||
} | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support | ||
// `composedPath()`, but browsers without shadow DOM don't. | ||
return event.target; | ||
/** Gets all cell indices that correspond to the specified indices */ | ||
function getGridCellIndices(indices, cellMap) { | ||
return cellMap.flatMap((index, cellIndex) => indices.includes(index) ? [cellIndex] : []); | ||
} | ||
const TYPEABLE_SELECTOR = "input:not([type='hidden']):not([disabled])," + "[contenteditable]:not([contenteditable='false']),textarea:not([disabled])"; | ||
function isTypeableElement(element) { | ||
return isHTMLElement(element) && element.matches(TYPEABLE_SELECTOR); | ||
function isListIndexDisabled(listRef, index, disabledIndices) { | ||
if (disabledIndices) { | ||
return disabledIndices.includes(index); | ||
} | ||
const element = listRef.current[index]; | ||
return element == null || element.hasAttribute('disabled') || element.getAttribute('aria-disabled') === 'true'; | ||
} | ||
function stopEvent(event) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
const getTabbableOptions = () => ({ | ||
getShadowRoot: true, | ||
displayCheck: | ||
// JSDOM does not support the `tabbable` library. To solve this we can | ||
// check if `ResizeObserver` is a real function (not polyfilled), which | ||
// determines if the current environment is JSDOM-like. | ||
typeof ResizeObserver === 'function' && ResizeObserver.toString().includes('[native code]') ? 'full' : 'none' | ||
}); | ||
function getTabbableIn(container, dir) { | ||
const list = tabbable(container, getTabbableOptions()); | ||
const len = list.length; | ||
if (len === 0) return; | ||
const active = activeElement(getDocument(container)); | ||
const index = list.indexOf(active); | ||
const nextIndex = index === -1 ? dir === 1 ? 0 : len - 1 : index + dir; | ||
return list[nextIndex]; | ||
} | ||
function isTypeableCombobox(element) { | ||
if (!element) return false; | ||
return element.getAttribute('role') === 'combobox' && isTypeableElement(element); | ||
function getNextTabbable(referenceElement) { | ||
return getTabbableIn(getDocument(referenceElement).body, 1) || referenceElement; | ||
} | ||
function getPreviousTabbable(referenceElement) { | ||
return getTabbableIn(getDocument(referenceElement).body, -1) || referenceElement; | ||
} | ||
function isOutsideEvent(event, container) { | ||
const containerElement = container || event.currentTarget; | ||
const relatedTarget = event.relatedTarget; | ||
return !relatedTarget || !contains(containerElement, relatedTarget); | ||
} | ||
function disableFocusInside(container) { | ||
const tabbableElements = tabbable(container, getTabbableOptions()); | ||
tabbableElements.forEach(element => { | ||
element.dataset.tabindex = element.getAttribute('tabindex') || ''; | ||
element.setAttribute('tabindex', '-1'); | ||
}); | ||
} | ||
function enableFocusInside(container) { | ||
const elements = container.querySelectorAll('[data-tabindex]'); | ||
elements.forEach(element => { | ||
const tabindex = element.dataset.tabindex; | ||
delete element.dataset.tabindex; | ||
if (tabindex) { | ||
element.setAttribute('tabindex', tabindex); | ||
} else { | ||
element.removeAttribute('tabindex'); | ||
} | ||
}); | ||
} | ||
export { TYPEABLE_SELECTOR, activeElement, contains, getDocument, getPlatform, getTarget, getUserAgent, isAndroid, isEventTargetWithin, isJSDOM, isMac, isMouseLikePointerType, isReactEvent, isRootElement, isSafari, isTypeableCombobox, isTypeableElement, isVirtualClick, isVirtualPointerEvent, stopEvent }; | ||
export { activeElement, contains, createGridCellMap, disableFocusInside, enableFocusInside, findNonDisabledListIndex, getDeepestNode, getDocument, getFloatingFocusElement, getGridCellIndexOfCorner, getGridCellIndices, getGridNavigatedIndex, getMaxListIndex, getMinListIndex, getNextTabbable, getNodeAncestors, getNodeChildren, getPlatform, getPreviousTabbable, getTabbableOptions, getTarget, getUserAgent, isAndroid, isDifferentGridRow, isEventTargetWithin, isIndexOutOfListBounds, isJSDOM, isListIndexDisabled, isMac, isMouseLikePointerType, isOutsideEvent, isReactEvent, isRootElement, isSafari, isTypeableCombobox, isTypeableElement, isVirtualClick, isVirtualPointerEvent, matchesFocusVisible, stopEvent, useEffectEvent, useLatestRef, index as useModernLayoutEffect }; |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FloatingUIReactUtils = {})); | ||
})(this, (function (exports) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FloatingUIReactUtils = {}, global.React)); | ||
})(this, (function (exports, React) { 'use strict'; | ||
function _interopNamespaceDefault(e) { | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
} | ||
n.default = e; | ||
return Object.freeze(n); | ||
} | ||
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React); | ||
function hasWindow() { | ||
@@ -27,2 +46,45 @@ return typeof window !== 'undefined'; | ||
// Avoid Chrome DevTools blue warning. | ||
function getPlatform() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData != null && uaData.platform) { | ||
return uaData.platform; | ||
} | ||
return navigator.platform; | ||
} | ||
function getUserAgent() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData && Array.isArray(uaData.brands)) { | ||
return uaData.brands.map(_ref => { | ||
let { | ||
brand, | ||
version | ||
} = _ref; | ||
return brand + "/" + version; | ||
}).join(' '); | ||
} | ||
return navigator.userAgent; | ||
} | ||
function isSafari() { | ||
// Chrome DevTools does not complain about navigator.vendor | ||
return /apple/i.test(navigator.vendor); | ||
} | ||
function isAndroid() { | ||
const re = /android/i; | ||
return re.test(getPlatform()) || re.test(getUserAgent()); | ||
} | ||
function isMac() { | ||
return getPlatform().toLowerCase().startsWith('mac') && !navigator.maxTouchPoints; | ||
} | ||
function isJSDOM() { | ||
return getUserAgent().includes('jsdom/'); | ||
} | ||
const FOCUSABLE_ATTRIBUTE = 'data-floating-ui-focusable'; | ||
const TYPEABLE_SELECTOR = "input:not([type='hidden']):not([disabled])," + "[contenteditable]:not([contenteditable='false']),textarea:not([disabled])"; | ||
const ARROW_LEFT = 'ArrowLeft'; | ||
const ARROW_RIGHT = 'ArrowRight'; | ||
const ARROW_UP = 'ArrowUp'; | ||
const ARROW_DOWN = 'ArrowDown'; | ||
function activeElement(doc) { | ||
@@ -62,24 +124,116 @@ let activeElement = doc.activeElement; | ||
} | ||
// Avoid Chrome DevTools blue warning. | ||
function getPlatform() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData != null && uaData.platform) { | ||
return uaData.platform; | ||
function getTarget(event) { | ||
if ('composedPath' in event) { | ||
return event.composedPath()[0]; | ||
} | ||
return navigator.platform; | ||
// TS thinks `event` is of type never as it assumes all browsers support | ||
// `composedPath()`, but browsers without shadow DOM don't. | ||
return event.target; | ||
} | ||
function getUserAgent() { | ||
const uaData = navigator.userAgentData; | ||
if (uaData && Array.isArray(uaData.brands)) { | ||
return uaData.brands.map(_ref => { | ||
let { | ||
brand, | ||
version | ||
} = _ref; | ||
return brand + "/" + version; | ||
}).join(' '); | ||
function isEventTargetWithin(event, node) { | ||
if (node == null) { | ||
return false; | ||
} | ||
return navigator.userAgent; | ||
if ('composedPath' in event) { | ||
return event.composedPath().includes(node); | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support composedPath, but browsers without shadow dom don't | ||
const e = event; | ||
return e.target != null && node.contains(e.target); | ||
} | ||
function isRootElement(element) { | ||
return element.matches('html,body'); | ||
} | ||
function getDocument(node) { | ||
return (node == null ? void 0 : node.ownerDocument) || document; | ||
} | ||
function isTypeableElement(element) { | ||
return isHTMLElement(element) && element.matches(TYPEABLE_SELECTOR); | ||
} | ||
function isTypeableCombobox(element) { | ||
if (!element) return false; | ||
return element.getAttribute('role') === 'combobox' && isTypeableElement(element); | ||
} | ||
function matchesFocusVisible(element) { | ||
// We don't want to block focus from working with `visibleOnly` | ||
// (JSDOM doesn't match `:focus-visible` when the element has `:focus`) | ||
if (!element || isJSDOM()) return true; | ||
try { | ||
return element.matches(':focus-visible'); | ||
} catch (_e) { | ||
return true; | ||
} | ||
} | ||
function getFloatingFocusElement(floatingElement) { | ||
if (!floatingElement) { | ||
return null; | ||
} | ||
// Try to find the element that has `{...getFloatingProps()}` spread on it. | ||
// This indicates the floating element is acting as a positioning wrapper, and | ||
// so focus should be managed on the child element with the event handlers and | ||
// aria props. | ||
return floatingElement.hasAttribute(FOCUSABLE_ATTRIBUTE) ? floatingElement : floatingElement.querySelector("[" + FOCUSABLE_ATTRIBUTE + "]") || floatingElement; | ||
} | ||
function getNodeChildren(nodes, id, onlyOpenChildren) { | ||
if (onlyOpenChildren === void 0) { | ||
onlyOpenChildren = true; | ||
} | ||
let allChildren = nodes.filter(node => { | ||
var _node$context; | ||
return node.parentId === id && ((_node$context = node.context) == null ? void 0 : _node$context.open); | ||
}); | ||
let currentChildren = allChildren; | ||
while (currentChildren.length) { | ||
currentChildren = onlyOpenChildren ? nodes.filter(node => { | ||
var _currentChildren; | ||
return (_currentChildren = currentChildren) == null ? void 0 : _currentChildren.some(n => { | ||
var _node$context2; | ||
return node.parentId === n.id && ((_node$context2 = node.context) == null ? void 0 : _node$context2.open); | ||
}); | ||
}) : nodes; | ||
allChildren = allChildren.concat(currentChildren); | ||
} | ||
return allChildren; | ||
} | ||
function getDeepestNode(nodes, id) { | ||
let deepestNodeId; | ||
let maxDepth = -1; | ||
function findDeepest(nodeId, depth) { | ||
if (depth > maxDepth) { | ||
deepestNodeId = nodeId; | ||
maxDepth = depth; | ||
} | ||
const children = getNodeChildren(nodes, nodeId); | ||
children.forEach(child => { | ||
findDeepest(child.id, depth + 1); | ||
}); | ||
} | ||
findDeepest(id, 0); | ||
return nodes.find(node => node.id === deepestNodeId); | ||
} | ||
function getNodeAncestors(nodes, id) { | ||
var _nodes$find; | ||
let allAncestors = []; | ||
let currentParentId = (_nodes$find = nodes.find(node => node.id === id)) == null ? void 0 : _nodes$find.parentId; | ||
while (currentParentId) { | ||
const currentNode = nodes.find(node => node.id === currentParentId); | ||
currentParentId = currentNode == null ? void 0 : currentNode.parentId; | ||
if (currentNode) { | ||
allAncestors = allAncestors.concat(currentNode); | ||
} | ||
} | ||
return allAncestors; | ||
} | ||
function stopEvent(event) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
} | ||
function isReactEvent(event) { | ||
return 'nativeEvent' in event; | ||
} | ||
// License: https://github.com/adobe/react-spectrum/blob/b35d5c02fe900badccd0cf1a8f23bb593419f238/packages/@react-aria/utils/src/isVirtualEvent.ts | ||
@@ -103,16 +257,2 @@ function isVirtualClick(event) { | ||
} | ||
function isSafari() { | ||
// Chrome DevTools does not complain about navigator.vendor | ||
return /apple/i.test(navigator.vendor); | ||
} | ||
function isAndroid() { | ||
const re = /android/i; | ||
return re.test(getPlatform()) || re.test(getUserAgent()); | ||
} | ||
function isMac() { | ||
return getPlatform().toLowerCase().startsWith('mac') && !navigator.maxTouchPoints; | ||
} | ||
function isJSDOM() { | ||
return getUserAgent().includes('jsdom/'); | ||
} | ||
function isMouseLikePointerType(pointerType, strict) { | ||
@@ -127,57 +267,892 @@ // On some Linux machines with Chromium, mouse inputs return a `pointerType` | ||
} | ||
function isReactEvent(event) { | ||
return 'nativeEvent' in event; | ||
var index = typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect; | ||
// https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379 | ||
const SafeReact = { | ||
...React__namespace | ||
}; | ||
function useLatestRef(value) { | ||
const ref = React__namespace.useRef(value); | ||
index(() => { | ||
ref.current = value; | ||
}); | ||
return ref; | ||
} | ||
function isRootElement(element) { | ||
return element.matches('html,body'); | ||
const useInsertionEffect = SafeReact.useInsertionEffect; | ||
const useSafeInsertionEffect = useInsertionEffect || (fn => fn()); | ||
function useEffectEvent(callback) { | ||
const ref = React__namespace.useRef(() => { | ||
{ | ||
throw new Error('Cannot call an event handler while rendering.'); | ||
} | ||
}); | ||
useSafeInsertionEffect(() => { | ||
ref.current = callback; | ||
}); | ||
return React__namespace.useCallback(function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return ref.current == null ? void 0 : ref.current(...args); | ||
}, []); | ||
} | ||
function getDocument(node) { | ||
return (node == null ? void 0 : node.ownerDocument) || document; | ||
/** | ||
* Custom positioning reference element. | ||
* @see https://floating-ui.com/docs/virtual-elements | ||
*/ | ||
const floor = Math.floor; | ||
function isDifferentGridRow(index, cols, prevRow) { | ||
return Math.floor(index / cols) !== prevRow; | ||
} | ||
function isEventTargetWithin(event, node) { | ||
if (node == null) { | ||
return false; | ||
function isIndexOutOfListBounds(listRef, index) { | ||
return index < 0 || index >= listRef.current.length; | ||
} | ||
function getMinListIndex(listRef, disabledIndices) { | ||
return findNonDisabledListIndex(listRef, { | ||
disabledIndices | ||
}); | ||
} | ||
function getMaxListIndex(listRef, disabledIndices) { | ||
return findNonDisabledListIndex(listRef, { | ||
decrement: true, | ||
startingIndex: listRef.current.length, | ||
disabledIndices | ||
}); | ||
} | ||
function findNonDisabledListIndex(listRef, _temp) { | ||
let { | ||
startingIndex = -1, | ||
decrement = false, | ||
disabledIndices, | ||
amount = 1 | ||
} = _temp === void 0 ? {} : _temp; | ||
let index = startingIndex; | ||
do { | ||
index += decrement ? -amount : amount; | ||
} while (index >= 0 && index <= listRef.current.length - 1 && isListIndexDisabled(listRef, index, disabledIndices)); | ||
return index; | ||
} | ||
function getGridNavigatedIndex(listRef, _ref) { | ||
let { | ||
event, | ||
orientation, | ||
loop, | ||
rtl, | ||
cols, | ||
disabledIndices, | ||
minIndex, | ||
maxIndex, | ||
prevIndex, | ||
stopEvent: stop = false | ||
} = _ref; | ||
let nextIndex = prevIndex; | ||
if (event.key === ARROW_UP) { | ||
stop && stopEvent(event); | ||
if (prevIndex === -1) { | ||
nextIndex = maxIndex; | ||
} else { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: nextIndex, | ||
amount: cols, | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
if (loop && (prevIndex - cols < minIndex || nextIndex < 0)) { | ||
const col = prevIndex % cols; | ||
const maxCol = maxIndex % cols; | ||
const offset = maxIndex - (maxCol - col); | ||
if (maxCol === col) { | ||
nextIndex = maxIndex; | ||
} else { | ||
nextIndex = maxCol > col ? offset : offset - cols; | ||
} | ||
} | ||
} | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
if ('composedPath' in event) { | ||
return event.composedPath().includes(node); | ||
if (event.key === ARROW_DOWN) { | ||
stop && stopEvent(event); | ||
if (prevIndex === -1) { | ||
nextIndex = minIndex; | ||
} else { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
amount: cols, | ||
disabledIndices | ||
}); | ||
if (loop && prevIndex + cols > maxIndex) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex % cols - cols, | ||
amount: cols, | ||
disabledIndices | ||
}); | ||
} | ||
} | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support composedPath, but browsers without shadow dom don't | ||
const e = event; | ||
return e.target != null && node.contains(e.target); | ||
// Remains on the same row/column. | ||
if (orientation === 'both') { | ||
const prevRow = floor(prevIndex / cols); | ||
if (event.key === (rtl ? ARROW_LEFT : ARROW_RIGHT)) { | ||
stop && stopEvent(event); | ||
if (prevIndex % cols !== cols - 1) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
disabledIndices | ||
}); | ||
if (loop && isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} | ||
} else if (loop) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} | ||
if (isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
if (event.key === (rtl ? ARROW_RIGHT : ARROW_LEFT)) { | ||
stop && stopEvent(event); | ||
if (prevIndex % cols !== 0) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex, | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
if (loop && isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex + (cols - prevIndex % cols), | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
} | ||
} else if (loop) { | ||
nextIndex = findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex + (cols - prevIndex % cols), | ||
decrement: true, | ||
disabledIndices | ||
}); | ||
} | ||
if (isDifferentGridRow(nextIndex, cols, prevRow)) { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
const lastRow = floor(maxIndex / cols) === prevRow; | ||
if (isIndexOutOfListBounds(listRef, nextIndex)) { | ||
if (loop && lastRow) { | ||
nextIndex = event.key === (rtl ? ARROW_RIGHT : ARROW_LEFT) ? maxIndex : findNonDisabledListIndex(listRef, { | ||
startingIndex: prevIndex - prevIndex % cols - 1, | ||
disabledIndices | ||
}); | ||
} else { | ||
nextIndex = prevIndex; | ||
} | ||
} | ||
} | ||
return nextIndex; | ||
} | ||
function getTarget(event) { | ||
if ('composedPath' in event) { | ||
return event.composedPath()[0]; | ||
/** For each cell index, gets the item index that occupies that cell */ | ||
function createGridCellMap(sizes, cols, dense) { | ||
const cellMap = []; | ||
let startIndex = 0; | ||
sizes.forEach((_ref2, index) => { | ||
let { | ||
width, | ||
height | ||
} = _ref2; | ||
if (width > cols) { | ||
{ | ||
throw new Error("[Floating UI]: Invalid grid - item width at index " + index + " is greater than grid columns"); | ||
} | ||
} | ||
let itemPlaced = false; | ||
if (dense) { | ||
startIndex = 0; | ||
} | ||
while (!itemPlaced) { | ||
const targetCells = []; | ||
for (let i = 0; i < width; i++) { | ||
for (let j = 0; j < height; j++) { | ||
targetCells.push(startIndex + i + j * cols); | ||
} | ||
} | ||
if (startIndex % cols + width <= cols && targetCells.every(cell => cellMap[cell] == null)) { | ||
targetCells.forEach(cell => { | ||
cellMap[cell] = index; | ||
}); | ||
itemPlaced = true; | ||
} else { | ||
startIndex++; | ||
} | ||
} | ||
}); | ||
// convert into a non-sparse array | ||
return [...cellMap]; | ||
} | ||
/** Gets cell index of an item's corner or -1 when index is -1. */ | ||
function getGridCellIndexOfCorner(index, sizes, cellMap, cols, corner) { | ||
if (index === -1) return -1; | ||
const firstCellIndex = cellMap.indexOf(index); | ||
const sizeItem = sizes[index]; | ||
switch (corner) { | ||
case 'tl': | ||
return firstCellIndex; | ||
case 'tr': | ||
if (!sizeItem) { | ||
return firstCellIndex; | ||
} | ||
return firstCellIndex + sizeItem.width - 1; | ||
case 'bl': | ||
if (!sizeItem) { | ||
return firstCellIndex; | ||
} | ||
return firstCellIndex + (sizeItem.height - 1) * cols; | ||
case 'br': | ||
return cellMap.lastIndexOf(index); | ||
} | ||
} | ||
// TS thinks `event` is of type never as it assumes all browsers support | ||
// `composedPath()`, but browsers without shadow DOM don't. | ||
return event.target; | ||
/** Gets all cell indices that correspond to the specified indices */ | ||
function getGridCellIndices(indices, cellMap) { | ||
return cellMap.flatMap((index, cellIndex) => indices.includes(index) ? [cellIndex] : []); | ||
} | ||
const TYPEABLE_SELECTOR = "input:not([type='hidden']):not([disabled])," + "[contenteditable]:not([contenteditable='false']),textarea:not([disabled])"; | ||
function isTypeableElement(element) { | ||
return isHTMLElement(element) && element.matches(TYPEABLE_SELECTOR); | ||
function isListIndexDisabled(listRef, index, disabledIndices) { | ||
if (disabledIndices) { | ||
return disabledIndices.includes(index); | ||
} | ||
const element = listRef.current[index]; | ||
return element == null || element.hasAttribute('disabled') || element.getAttribute('aria-disabled') === 'true'; | ||
} | ||
function stopEvent(event) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
/*! | ||
* tabbable 6.2.0 | ||
* @license MIT, https://github.com/focus-trap/tabbable/blob/master/LICENSE | ||
*/ | ||
// NOTE: separate `:not()` selectors has broader browser support than the newer | ||
// `:not([inert], [inert] *)` (Feb 2023) | ||
// CAREFUL: JSDom does not support `:not([inert] *)` as a selector; using it causes | ||
// the entire query to fail, resulting in no nodes found, which will break a lot | ||
// of things... so we have to rely on JS to identify nodes inside an inert container | ||
var candidateSelectors = ['input:not([inert])', 'select:not([inert])', 'textarea:not([inert])', 'a[href]:not([inert])', 'button:not([inert])', '[tabindex]:not(slot):not([inert])', 'audio[controls]:not([inert])', 'video[controls]:not([inert])', '[contenteditable]:not([contenteditable="false"]):not([inert])', 'details>summary:first-of-type:not([inert])', 'details:not([inert])']; | ||
var candidateSelector = /* #__PURE__ */candidateSelectors.join(','); | ||
var NoElement = typeof Element === 'undefined'; | ||
var matches = NoElement ? function () {} : Element.prototype.matches || Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; | ||
var getRootNode = !NoElement && Element.prototype.getRootNode ? function (element) { | ||
var _element$getRootNode; | ||
return element === null || element === void 0 ? void 0 : (_element$getRootNode = element.getRootNode) === null || _element$getRootNode === void 0 ? void 0 : _element$getRootNode.call(element); | ||
} : function (element) { | ||
return element === null || element === void 0 ? void 0 : element.ownerDocument; | ||
}; | ||
/** | ||
* Determines if a node is inert or in an inert ancestor. | ||
* @param {Element} [node] | ||
* @param {boolean} [lookUp] If true and `node` is not inert, looks up at ancestors to | ||
* see if any of them are inert. If false, only `node` itself is considered. | ||
* @returns {boolean} True if inert itself or by way of being in an inert ancestor. | ||
* False if `node` is falsy. | ||
*/ | ||
var isInert = function isInert(node, lookUp) { | ||
var _node$getAttribute; | ||
if (lookUp === void 0) { | ||
lookUp = true; | ||
} | ||
// CAREFUL: JSDom does not support inert at all, so we can't use the `HTMLElement.inert` | ||
// JS API property; we have to check the attribute, which can either be empty or 'true'; | ||
// if it's `null` (not specified) or 'false', it's an active element | ||
var inertAtt = node === null || node === void 0 ? void 0 : (_node$getAttribute = node.getAttribute) === null || _node$getAttribute === void 0 ? void 0 : _node$getAttribute.call(node, 'inert'); | ||
var inert = inertAtt === '' || inertAtt === 'true'; | ||
// NOTE: this could also be handled with `node.matches('[inert], :is([inert] *)')` | ||
// if it weren't for `matches()` not being a function on shadow roots; the following | ||
// code works for any kind of node | ||
// CAREFUL: JSDom does not appear to support certain selectors like `:not([inert] *)` | ||
// so it likely would not support `:is([inert] *)` either... | ||
var result = inert || lookUp && node && isInert(node.parentNode); // recursive | ||
return result; | ||
}; | ||
/** | ||
* Determines if a node's content is editable. | ||
* @param {Element} [node] | ||
* @returns True if it's content-editable; false if it's not or `node` is falsy. | ||
*/ | ||
var isContentEditable = function isContentEditable(node) { | ||
var _node$getAttribute2; | ||
// CAREFUL: JSDom does not support the `HTMLElement.isContentEditable` API so we have | ||
// to use the attribute directly to check for this, which can either be empty or 'true'; | ||
// if it's `null` (not specified) or 'false', it's a non-editable element | ||
var attValue = node === null || node === void 0 ? void 0 : (_node$getAttribute2 = node.getAttribute) === null || _node$getAttribute2 === void 0 ? void 0 : _node$getAttribute2.call(node, 'contenteditable'); | ||
return attValue === '' || attValue === 'true'; | ||
}; | ||
/** | ||
* @param {Element} el container to check in | ||
* @param {boolean} includeContainer add container to check | ||
* @param {(node: Element) => boolean} filter filter candidates | ||
* @returns {Element[]} | ||
*/ | ||
var getCandidates = function getCandidates(el, includeContainer, filter) { | ||
// even if `includeContainer=false`, we still have to check it for inertness because | ||
// if it's inert, all its children are inert | ||
if (isInert(el)) { | ||
return []; | ||
} | ||
var candidates = Array.prototype.slice.apply(el.querySelectorAll(candidateSelector)); | ||
if (includeContainer && matches.call(el, candidateSelector)) { | ||
candidates.unshift(el); | ||
} | ||
candidates = candidates.filter(filter); | ||
return candidates; | ||
}; | ||
/** | ||
* @callback GetShadowRoot | ||
* @param {Element} element to check for shadow root | ||
* @returns {ShadowRoot|boolean} ShadowRoot if available or boolean indicating if a shadowRoot is attached but not available. | ||
*/ | ||
/** | ||
* @callback ShadowRootFilter | ||
* @param {Element} shadowHostNode the element which contains shadow content | ||
* @returns {boolean} true if a shadow root could potentially contain valid candidates. | ||
*/ | ||
/** | ||
* @typedef {Object} CandidateScope | ||
* @property {Element} scopeParent contains inner candidates | ||
* @property {Element[]} candidates list of candidates found in the scope parent | ||
*/ | ||
/** | ||
* @typedef {Object} IterativeOptions | ||
* @property {GetShadowRoot|boolean} getShadowRoot true if shadow support is enabled; falsy if not; | ||
* if a function, implies shadow support is enabled and either returns the shadow root of an element | ||
* or a boolean stating if it has an undisclosed shadow root | ||
* @property {(node: Element) => boolean} filter filter candidates | ||
* @property {boolean} flatten if true then result will flatten any CandidateScope into the returned list | ||
* @property {ShadowRootFilter} shadowRootFilter filter shadow roots; | ||
*/ | ||
/** | ||
* @param {Element[]} elements list of element containers to match candidates from | ||
* @param {boolean} includeContainer add container list to check | ||
* @param {IterativeOptions} options | ||
* @returns {Array.<Element|CandidateScope>} | ||
*/ | ||
var getCandidatesIteratively = function getCandidatesIteratively(elements, includeContainer, options) { | ||
var candidates = []; | ||
var elementsToCheck = Array.from(elements); | ||
while (elementsToCheck.length) { | ||
var element = elementsToCheck.shift(); | ||
if (isInert(element, false)) { | ||
// no need to look up since we're drilling down | ||
// anything inside this container will also be inert | ||
continue; | ||
} | ||
if (element.tagName === 'SLOT') { | ||
// add shadow dom slot scope (slot itself cannot be focusable) | ||
var assigned = element.assignedElements(); | ||
var content = assigned.length ? assigned : element.children; | ||
var nestedCandidates = getCandidatesIteratively(content, true, options); | ||
if (options.flatten) { | ||
candidates.push.apply(candidates, nestedCandidates); | ||
} else { | ||
candidates.push({ | ||
scopeParent: element, | ||
candidates: nestedCandidates | ||
}); | ||
} | ||
} else { | ||
// check candidate element | ||
var validCandidate = matches.call(element, candidateSelector); | ||
if (validCandidate && options.filter(element) && (includeContainer || !elements.includes(element))) { | ||
candidates.push(element); | ||
} | ||
// iterate over shadow content if possible | ||
var shadowRoot = element.shadowRoot || | ||
// check for an undisclosed shadow | ||
typeof options.getShadowRoot === 'function' && options.getShadowRoot(element); | ||
// no inert look up because we're already drilling down and checking for inertness | ||
// on the way down, so all containers to this root node should have already been | ||
// vetted as non-inert | ||
var validShadowRoot = !isInert(shadowRoot, false) && (!options.shadowRootFilter || options.shadowRootFilter(element)); | ||
if (shadowRoot && validShadowRoot) { | ||
// add shadow dom scope IIF a shadow root node was given; otherwise, an undisclosed | ||
// shadow exists, so look at light dom children as fallback BUT create a scope for any | ||
// child candidates found because they're likely slotted elements (elements that are | ||
// children of the web component element (which has the shadow), in the light dom, but | ||
// slotted somewhere _inside_ the undisclosed shadow) -- the scope is created below, | ||
// _after_ we return from this recursive call | ||
var _nestedCandidates = getCandidatesIteratively(shadowRoot === true ? element.children : shadowRoot.children, true, options); | ||
if (options.flatten) { | ||
candidates.push.apply(candidates, _nestedCandidates); | ||
} else { | ||
candidates.push({ | ||
scopeParent: element, | ||
candidates: _nestedCandidates | ||
}); | ||
} | ||
} else { | ||
// there's not shadow so just dig into the element's (light dom) children | ||
// __without__ giving the element special scope treatment | ||
elementsToCheck.unshift.apply(elementsToCheck, element.children); | ||
} | ||
} | ||
} | ||
return candidates; | ||
}; | ||
/** | ||
* @private | ||
* Determines if the node has an explicitly specified `tabindex` attribute. | ||
* @param {HTMLElement} node | ||
* @returns {boolean} True if so; false if not. | ||
*/ | ||
var hasTabIndex = function hasTabIndex(node) { | ||
return !isNaN(parseInt(node.getAttribute('tabindex'), 10)); | ||
}; | ||
/** | ||
* Determine the tab index of a given node. | ||
* @param {HTMLElement} node | ||
* @returns {number} Tab order (negative, 0, or positive number). | ||
* @throws {Error} If `node` is falsy. | ||
*/ | ||
var getTabIndex = function getTabIndex(node) { | ||
if (!node) { | ||
throw new Error('No node provided'); | ||
} | ||
if (node.tabIndex < 0) { | ||
// in Chrome, <details/>, <audio controls/> and <video controls/> elements get a default | ||
// `tabIndex` of -1 when the 'tabindex' attribute isn't specified in the DOM, | ||
// yet they are still part of the regular tab order; in FF, they get a default | ||
// `tabIndex` of 0; since Chrome still puts those elements in the regular tab | ||
// order, consider their tab index to be 0. | ||
// Also browsers do not return `tabIndex` correctly for contentEditable nodes; | ||
// so if they don't have a tabindex attribute specifically set, assume it's 0. | ||
if ((/^(AUDIO|VIDEO|DETAILS)$/.test(node.tagName) || isContentEditable(node)) && !hasTabIndex(node)) { | ||
return 0; | ||
} | ||
} | ||
return node.tabIndex; | ||
}; | ||
/** | ||
* Determine the tab index of a given node __for sort order purposes__. | ||
* @param {HTMLElement} node | ||
* @param {boolean} [isScope] True for a custom element with shadow root or slot that, by default, | ||
* has tabIndex -1, but needs to be sorted by document order in order for its content to be | ||
* inserted into the correct sort position. | ||
* @returns {number} Tab order (negative, 0, or positive number). | ||
*/ | ||
var getSortOrderTabIndex = function getSortOrderTabIndex(node, isScope) { | ||
var tabIndex = getTabIndex(node); | ||
if (tabIndex < 0 && isScope && !hasTabIndex(node)) { | ||
return 0; | ||
} | ||
return tabIndex; | ||
}; | ||
var sortOrderedTabbables = function sortOrderedTabbables(a, b) { | ||
return a.tabIndex === b.tabIndex ? a.documentOrder - b.documentOrder : a.tabIndex - b.tabIndex; | ||
}; | ||
var isInput = function isInput(node) { | ||
return node.tagName === 'INPUT'; | ||
}; | ||
var isHiddenInput = function isHiddenInput(node) { | ||
return isInput(node) && node.type === 'hidden'; | ||
}; | ||
var isDetailsWithSummary = function isDetailsWithSummary(node) { | ||
var r = node.tagName === 'DETAILS' && Array.prototype.slice.apply(node.children).some(function (child) { | ||
return child.tagName === 'SUMMARY'; | ||
}); | ||
return r; | ||
}; | ||
var getCheckedRadio = function getCheckedRadio(nodes, form) { | ||
for (var i = 0; i < nodes.length; i++) { | ||
if (nodes[i].checked && nodes[i].form === form) { | ||
return nodes[i]; | ||
} | ||
} | ||
}; | ||
var isTabbableRadio = function isTabbableRadio(node) { | ||
if (!node.name) { | ||
return true; | ||
} | ||
var radioScope = node.form || getRootNode(node); | ||
var queryRadios = function queryRadios(name) { | ||
return radioScope.querySelectorAll('input[type="radio"][name="' + name + '"]'); | ||
}; | ||
var radioSet; | ||
if (typeof window !== 'undefined' && typeof window.CSS !== 'undefined' && typeof window.CSS.escape === 'function') { | ||
radioSet = queryRadios(window.CSS.escape(node.name)); | ||
} else { | ||
try { | ||
radioSet = queryRadios(node.name); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error('Looks like you have a radio button with a name attribute containing invalid CSS selector characters and need the CSS.escape polyfill: %s', err.message); | ||
return false; | ||
} | ||
} | ||
var checked = getCheckedRadio(radioSet, node.form); | ||
return !checked || checked === node; | ||
}; | ||
var isRadio = function isRadio(node) { | ||
return isInput(node) && node.type === 'radio'; | ||
}; | ||
var isNonTabbableRadio = function isNonTabbableRadio(node) { | ||
return isRadio(node) && !isTabbableRadio(node); | ||
}; | ||
// determines if a node is ultimately attached to the window's document | ||
var isNodeAttached = function isNodeAttached(node) { | ||
var _nodeRoot; | ||
// The root node is the shadow root if the node is in a shadow DOM; some document otherwise | ||
// (but NOT _the_ document; see second 'If' comment below for more). | ||
// If rootNode is shadow root, it'll have a host, which is the element to which the shadow | ||
// is attached, and the one we need to check if it's in the document or not (because the | ||
// shadow, and all nodes it contains, is never considered in the document since shadows | ||
// behave like self-contained DOMs; but if the shadow's HOST, which is part of the document, | ||
// is hidden, or is not in the document itself but is detached, it will affect the shadow's | ||
// visibility, including all the nodes it contains). The host could be any normal node, | ||
// or a custom element (i.e. web component). Either way, that's the one that is considered | ||
// part of the document, not the shadow root, nor any of its children (i.e. the node being | ||
// tested). | ||
// To further complicate things, we have to look all the way up until we find a shadow HOST | ||
// that is attached (or find none) because the node might be in nested shadows... | ||
// If rootNode is not a shadow root, it won't have a host, and so rootNode should be the | ||
// document (per the docs) and while it's a Document-type object, that document does not | ||
// appear to be the same as the node's `ownerDocument` for some reason, so it's safer | ||
// to ignore the rootNode at this point, and use `node.ownerDocument`. Otherwise, | ||
// using `rootNode.contains(node)` will _always_ be true we'll get false-positives when | ||
// node is actually detached. | ||
// NOTE: If `nodeRootHost` or `node` happens to be the `document` itself (which is possible | ||
// if a tabbable/focusable node was quickly added to the DOM, focused, and then removed | ||
// from the DOM as in https://github.com/focus-trap/focus-trap-react/issues/905), then | ||
// `ownerDocument` will be `null`, hence the optional chaining on it. | ||
var nodeRoot = node && getRootNode(node); | ||
var nodeRootHost = (_nodeRoot = nodeRoot) === null || _nodeRoot === void 0 ? void 0 : _nodeRoot.host; | ||
// in some cases, a detached node will return itself as the root instead of a document or | ||
// shadow root object, in which case, we shouldn't try to look further up the host chain | ||
var attached = false; | ||
if (nodeRoot && nodeRoot !== node) { | ||
var _nodeRootHost, _nodeRootHost$ownerDo, _node$ownerDocument; | ||
attached = !!((_nodeRootHost = nodeRootHost) !== null && _nodeRootHost !== void 0 && (_nodeRootHost$ownerDo = _nodeRootHost.ownerDocument) !== null && _nodeRootHost$ownerDo !== void 0 && _nodeRootHost$ownerDo.contains(nodeRootHost) || node !== null && node !== void 0 && (_node$ownerDocument = node.ownerDocument) !== null && _node$ownerDocument !== void 0 && _node$ownerDocument.contains(node)); | ||
while (!attached && nodeRootHost) { | ||
var _nodeRoot2, _nodeRootHost2, _nodeRootHost2$ownerD; | ||
// since it's not attached and we have a root host, the node MUST be in a nested shadow DOM, | ||
// which means we need to get the host's host and check if that parent host is contained | ||
// in (i.e. attached to) the document | ||
nodeRoot = getRootNode(nodeRootHost); | ||
nodeRootHost = (_nodeRoot2 = nodeRoot) === null || _nodeRoot2 === void 0 ? void 0 : _nodeRoot2.host; | ||
attached = !!((_nodeRootHost2 = nodeRootHost) !== null && _nodeRootHost2 !== void 0 && (_nodeRootHost2$ownerD = _nodeRootHost2.ownerDocument) !== null && _nodeRootHost2$ownerD !== void 0 && _nodeRootHost2$ownerD.contains(nodeRootHost)); | ||
} | ||
} | ||
return attached; | ||
}; | ||
var isZeroArea = function isZeroArea(node) { | ||
var _node$getBoundingClie = node.getBoundingClientRect(), | ||
width = _node$getBoundingClie.width, | ||
height = _node$getBoundingClie.height; | ||
return width === 0 && height === 0; | ||
}; | ||
var isHidden = function isHidden(node, _ref) { | ||
var displayCheck = _ref.displayCheck, | ||
getShadowRoot = _ref.getShadowRoot; | ||
// NOTE: visibility will be `undefined` if node is detached from the document | ||
// (see notes about this further down), which means we will consider it visible | ||
// (this is legacy behavior from a very long way back) | ||
// NOTE: we check this regardless of `displayCheck="none"` because this is a | ||
// _visibility_ check, not a _display_ check | ||
if (getComputedStyle(node).visibility === 'hidden') { | ||
return true; | ||
} | ||
var isDirectSummary = matches.call(node, 'details>summary:first-of-type'); | ||
var nodeUnderDetails = isDirectSummary ? node.parentElement : node; | ||
if (matches.call(nodeUnderDetails, 'details:not([open]) *')) { | ||
return true; | ||
} | ||
if (!displayCheck || displayCheck === 'full' || displayCheck === 'legacy-full') { | ||
if (typeof getShadowRoot === 'function') { | ||
// figure out if we should consider the node to be in an undisclosed shadow and use the | ||
// 'non-zero-area' fallback | ||
var originalNode = node; | ||
while (node) { | ||
var parentElement = node.parentElement; | ||
var rootNode = getRootNode(node); | ||
if (parentElement && !parentElement.shadowRoot && getShadowRoot(parentElement) === true // check if there's an undisclosed shadow | ||
) { | ||
// node has an undisclosed shadow which means we can only treat it as a black box, so we | ||
// fall back to a non-zero-area test | ||
return isZeroArea(node); | ||
} else if (node.assignedSlot) { | ||
// iterate up slot | ||
node = node.assignedSlot; | ||
} else if (!parentElement && rootNode !== node.ownerDocument) { | ||
// cross shadow boundary | ||
node = rootNode.host; | ||
} else { | ||
// iterate up normal dom | ||
node = parentElement; | ||
} | ||
} | ||
node = originalNode; | ||
} | ||
// else, `getShadowRoot` might be true, but all that does is enable shadow DOM support | ||
// (i.e. it does not also presume that all nodes might have undisclosed shadows); or | ||
// it might be a falsy value, which means shadow DOM support is disabled | ||
// Since we didn't find it sitting in an undisclosed shadow (or shadows are disabled) | ||
// now we can just test to see if it would normally be visible or not, provided it's | ||
// attached to the main document. | ||
// NOTE: We must consider case where node is inside a shadow DOM and given directly to | ||
// `isTabbable()` or `isFocusable()` -- regardless of `getShadowRoot` option setting. | ||
if (isNodeAttached(node)) { | ||
// this works wherever the node is: if there's at least one client rect, it's | ||
// somehow displayed; it also covers the CSS 'display: contents' case where the | ||
// node itself is hidden in place of its contents; and there's no need to search | ||
// up the hierarchy either | ||
return !node.getClientRects().length; | ||
} | ||
// Else, the node isn't attached to the document, which means the `getClientRects()` | ||
// API will __always__ return zero rects (this can happen, for example, if React | ||
// is used to render nodes onto a detached tree, as confirmed in this thread: | ||
// https://github.com/facebook/react/issues/9117#issuecomment-284228870) | ||
// | ||
// It also means that even window.getComputedStyle(node).display will return `undefined` | ||
// because styles are only computed for nodes that are in the document. | ||
// | ||
// NOTE: THIS HAS BEEN THE CASE FOR YEARS. It is not new, nor is it caused by tabbable | ||
// somehow. Though it was never stated officially, anyone who has ever used tabbable | ||
// APIs on nodes in detached containers has actually implicitly used tabbable in what | ||
// was later (as of v5.2.0 on Apr 9, 2021) called `displayCheck="none"` mode -- essentially | ||
// considering __everything__ to be visible because of the innability to determine styles. | ||
// | ||
// v6.0.0: As of this major release, the default 'full' option __no longer treats detached | ||
// nodes as visible with the 'none' fallback.__ | ||
if (displayCheck !== 'legacy-full') { | ||
return true; // hidden | ||
} | ||
// else, fallback to 'none' mode and consider the node visible | ||
} else if (displayCheck === 'non-zero-area') { | ||
// NOTE: Even though this tests that the node's client rect is non-zero to determine | ||
// whether it's displayed, and that a detached node will __always__ have a zero-area | ||
// client rect, we don't special-case for whether the node is attached or not. In | ||
// this mode, we do want to consider nodes that have a zero area to be hidden at all | ||
// times, and that includes attached or not. | ||
return isZeroArea(node); | ||
} | ||
// visible, as far as we can tell, or per current `displayCheck=none` mode, we assume | ||
// it's visible | ||
return false; | ||
}; | ||
// form fields (nested) inside a disabled fieldset are not focusable/tabbable | ||
// unless they are in the _first_ <legend> element of the top-most disabled | ||
// fieldset | ||
var isDisabledFromFieldset = function isDisabledFromFieldset(node) { | ||
if (/^(INPUT|BUTTON|SELECT|TEXTAREA)$/.test(node.tagName)) { | ||
var parentNode = node.parentElement; | ||
// check if `node` is contained in a disabled <fieldset> | ||
while (parentNode) { | ||
if (parentNode.tagName === 'FIELDSET' && parentNode.disabled) { | ||
// look for the first <legend> among the children of the disabled <fieldset> | ||
for (var i = 0; i < parentNode.children.length; i++) { | ||
var child = parentNode.children.item(i); | ||
// when the first <legend> (in document order) is found | ||
if (child.tagName === 'LEGEND') { | ||
// if its parent <fieldset> is not nested in another disabled <fieldset>, | ||
// return whether `node` is a descendant of its first <legend> | ||
return matches.call(parentNode, 'fieldset[disabled] *') ? true : !child.contains(node); | ||
} | ||
} | ||
// the disabled <fieldset> containing `node` has no <legend> | ||
return true; | ||
} | ||
parentNode = parentNode.parentElement; | ||
} | ||
} | ||
// else, node's tabbable/focusable state should not be affected by a fieldset's | ||
// enabled/disabled state | ||
return false; | ||
}; | ||
var isNodeMatchingSelectorFocusable = function isNodeMatchingSelectorFocusable(options, node) { | ||
if (node.disabled || | ||
// we must do an inert look up to filter out any elements inside an inert ancestor | ||
// because we're limited in the type of selectors we can use in JSDom (see related | ||
// note related to `candidateSelectors`) | ||
isInert(node) || isHiddenInput(node) || isHidden(node, options) || | ||
// For a details element with a summary, the summary element gets the focus | ||
isDetailsWithSummary(node) || isDisabledFromFieldset(node)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
var isNodeMatchingSelectorTabbable = function isNodeMatchingSelectorTabbable(options, node) { | ||
if (isNonTabbableRadio(node) || getTabIndex(node) < 0 || !isNodeMatchingSelectorFocusable(options, node)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
var isValidShadowRootTabbable = function isValidShadowRootTabbable(shadowHostNode) { | ||
var tabIndex = parseInt(shadowHostNode.getAttribute('tabindex'), 10); | ||
if (isNaN(tabIndex) || tabIndex >= 0) { | ||
return true; | ||
} | ||
// If a custom element has an explicit negative tabindex, | ||
// browsers will not allow tab targeting said element's children. | ||
return false; | ||
}; | ||
/** | ||
* @param {Array.<Element|CandidateScope>} candidates | ||
* @returns Element[] | ||
*/ | ||
var sortByOrder = function sortByOrder(candidates) { | ||
var regularTabbables = []; | ||
var orderedTabbables = []; | ||
candidates.forEach(function (item, i) { | ||
var isScope = !!item.scopeParent; | ||
var element = isScope ? item.scopeParent : item; | ||
var candidateTabindex = getSortOrderTabIndex(element, isScope); | ||
var elements = isScope ? sortByOrder(item.candidates) : element; | ||
if (candidateTabindex === 0) { | ||
isScope ? regularTabbables.push.apply(regularTabbables, elements) : regularTabbables.push(element); | ||
} else { | ||
orderedTabbables.push({ | ||
documentOrder: i, | ||
tabIndex: candidateTabindex, | ||
item: item, | ||
isScope: isScope, | ||
content: elements | ||
}); | ||
} | ||
}); | ||
return orderedTabbables.sort(sortOrderedTabbables).reduce(function (acc, sortable) { | ||
sortable.isScope ? acc.push.apply(acc, sortable.content) : acc.push(sortable.content); | ||
return acc; | ||
}, []).concat(regularTabbables); | ||
}; | ||
var tabbable = function tabbable(container, options) { | ||
options = options || {}; | ||
var candidates; | ||
if (options.getShadowRoot) { | ||
candidates = getCandidatesIteratively([container], options.includeContainer, { | ||
filter: isNodeMatchingSelectorTabbable.bind(null, options), | ||
flatten: false, | ||
getShadowRoot: options.getShadowRoot, | ||
shadowRootFilter: isValidShadowRootTabbable | ||
}); | ||
} else { | ||
candidates = getCandidates(container, options.includeContainer, isNodeMatchingSelectorTabbable.bind(null, options)); | ||
} | ||
return sortByOrder(candidates); | ||
}; | ||
const getTabbableOptions = () => ({ | ||
getShadowRoot: true, | ||
displayCheck: | ||
// JSDOM does not support the `tabbable` library. To solve this we can | ||
// check if `ResizeObserver` is a real function (not polyfilled), which | ||
// determines if the current environment is JSDOM-like. | ||
typeof ResizeObserver === 'function' && ResizeObserver.toString().includes('[native code]') ? 'full' : 'none' | ||
}); | ||
function getTabbableIn(container, dir) { | ||
const list = tabbable(container, getTabbableOptions()); | ||
const len = list.length; | ||
if (len === 0) return; | ||
const active = activeElement(getDocument(container)); | ||
const index = list.indexOf(active); | ||
const nextIndex = index === -1 ? dir === 1 ? 0 : len - 1 : index + dir; | ||
return list[nextIndex]; | ||
} | ||
function isTypeableCombobox(element) { | ||
if (!element) return false; | ||
return element.getAttribute('role') === 'combobox' && isTypeableElement(element); | ||
function getNextTabbable(referenceElement) { | ||
return getTabbableIn(getDocument(referenceElement).body, 1) || referenceElement; | ||
} | ||
function getPreviousTabbable(referenceElement) { | ||
return getTabbableIn(getDocument(referenceElement).body, -1) || referenceElement; | ||
} | ||
function isOutsideEvent(event, container) { | ||
const containerElement = container || event.currentTarget; | ||
const relatedTarget = event.relatedTarget; | ||
return !relatedTarget || !contains(containerElement, relatedTarget); | ||
} | ||
function disableFocusInside(container) { | ||
const tabbableElements = tabbable(container, getTabbableOptions()); | ||
tabbableElements.forEach(element => { | ||
element.dataset.tabindex = element.getAttribute('tabindex') || ''; | ||
element.setAttribute('tabindex', '-1'); | ||
}); | ||
} | ||
function enableFocusInside(container) { | ||
const elements = container.querySelectorAll('[data-tabindex]'); | ||
elements.forEach(element => { | ||
const tabindex = element.dataset.tabindex; | ||
delete element.dataset.tabindex; | ||
if (tabindex) { | ||
element.setAttribute('tabindex', tabindex); | ||
} else { | ||
element.removeAttribute('tabindex'); | ||
} | ||
}); | ||
} | ||
exports.TYPEABLE_SELECTOR = TYPEABLE_SELECTOR; | ||
exports.activeElement = activeElement; | ||
exports.contains = contains; | ||
exports.createGridCellMap = createGridCellMap; | ||
exports.disableFocusInside = disableFocusInside; | ||
exports.enableFocusInside = enableFocusInside; | ||
exports.findNonDisabledListIndex = findNonDisabledListIndex; | ||
exports.getDeepestNode = getDeepestNode; | ||
exports.getDocument = getDocument; | ||
exports.getFloatingFocusElement = getFloatingFocusElement; | ||
exports.getGridCellIndexOfCorner = getGridCellIndexOfCorner; | ||
exports.getGridCellIndices = getGridCellIndices; | ||
exports.getGridNavigatedIndex = getGridNavigatedIndex; | ||
exports.getMaxListIndex = getMaxListIndex; | ||
exports.getMinListIndex = getMinListIndex; | ||
exports.getNextTabbable = getNextTabbable; | ||
exports.getNodeAncestors = getNodeAncestors; | ||
exports.getNodeChildren = getNodeChildren; | ||
exports.getPlatform = getPlatform; | ||
exports.getPreviousTabbable = getPreviousTabbable; | ||
exports.getTabbableOptions = getTabbableOptions; | ||
exports.getTarget = getTarget; | ||
exports.getUserAgent = getUserAgent; | ||
exports.isAndroid = isAndroid; | ||
exports.isDifferentGridRow = isDifferentGridRow; | ||
exports.isEventTargetWithin = isEventTargetWithin; | ||
exports.isIndexOutOfListBounds = isIndexOutOfListBounds; | ||
exports.isJSDOM = isJSDOM; | ||
exports.isListIndexDisabled = isListIndexDisabled; | ||
exports.isMac = isMac; | ||
exports.isMouseLikePointerType = isMouseLikePointerType; | ||
exports.isOutsideEvent = isOutsideEvent; | ||
exports.isReactEvent = isReactEvent; | ||
@@ -190,4 +1165,8 @@ exports.isRootElement = isRootElement; | ||
exports.isVirtualPointerEvent = isVirtualPointerEvent; | ||
exports.matchesFocusVisible = matchesFocusVisible; | ||
exports.stopEvent = stopEvent; | ||
exports.useEffectEvent = useEffectEvent; | ||
exports.useLatestRef = useLatestRef; | ||
exports.useModernLayoutEffect = index; | ||
})); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
923471
17.35%20292
14.92%3
50%20
42.86%