UNPKG

@base-ui/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

39 lines (37 loc) 1.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.gridNavigation = gridNavigation; var _composite = require("../utils/composite"); /** * Positional arguments are deliberate: property names of an options object * don't minify, and the signature is locked to the caller via `typeof` on the * `grid` option of `useListNavigation`. * * The injected grid navigator only ever operates on a uniform 1x1 grid (sizes are * always `1x1` and packing is never dense), so the cell-map machinery that supports * multi-cell items collapses to an identity transform over the item list. Calling * `getGridNavigatedIndex` directly keeps the cell-map helpers * (`createGridCellMap`/`getGridCellIndexOfCorner`/`getGridCellIndices`) out of * grid-combobox bundles. */ function gridNavigation(event, prevIndex, listRef, orientation, loopFocus, rtl, disabledIndices, minIndex, maxIndex, cols = 2) { const nextIndex = (0, _composite.getGridNavigatedIndex)(listRef.current, { event, orientation, loopFocus, rtl, cols, disabledIndices, minIndex, maxIndex, // An out-of-range previous index falls back to the first enabled item. prevIndex: prevIndex > maxIndex ? minIndex : prevIndex, stopEvent: true }); // `getGridNavigatedIndex` can return an out-of-bounds sentinel (e.g. `-1` when there is no // previous item to move from); surface that as `undefined` so the caller treats it as // "no navigation" rather than highlighting index `-1`. return (0, _composite.isIndexOutOfListBounds)(listRef.current, nextIndex) ? undefined : nextIndex; }