UNPKG

@base-ui-components/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.

151 lines 5.39 kB
import * as React from 'react'; import { FloatingTreeStore } from "../components/FloatingTreeStore.js"; import type { Dimensions, ElementProps, FloatingContext, FloatingRootContext } from "../types.js"; export declare const ESCAPE = "Escape"; export interface UseListNavigationProps { /** * A ref that holds an array of list items. * @default empty list */ listRef: React.MutableRefObject<Array<HTMLElement | null>>; /** * The index of the currently active (focused or highlighted) item, which may * or may not be selected. * @default null */ activeIndex: number | null; /** * A callback that is called when the user navigates to a new active item, * passed in a new `activeIndex`. */ onNavigate?: (activeIndex: number | null, event: React.SyntheticEvent | undefined) => void; /** * Whether the Hook is enabled, including all internal Effects and event * handlers. * @default true */ enabled?: boolean; /** * The currently selected item index, which may or may not be active. * @default null */ selectedIndex?: number | null; /** * Whether to focus the item upon opening the floating element. 'auto' infers * what to do based on the input type (keyboard vs. pointer), while a boolean * value will force the value. * @default 'auto' */ focusItemOnOpen?: boolean | 'auto'; /** * Whether hovering an item synchronizes the focus. * @default true */ focusItemOnHover?: boolean; /** * Whether pressing an arrow key on the navigation’s main axis opens the * floating element. * @default true */ openOnArrowKeyDown?: boolean; /** * By default elements with either a `disabled` or `aria-disabled` attribute * are skipped in the list navigation — however, this requires the items to * be rendered. * This prop allows you to manually specify indices which should be disabled, * overriding the default logic. * For Windows-style select popups, where the menu does not open when * navigating via arrow keys, specify an empty array. * @default undefined */ disabledIndices?: ReadonlyArray<number> | ((index: number) => boolean); /** * Determines whether focus can escape the list, such that nothing is selected * after navigating beyond the boundary of the list. In some * autocomplete/combobox components, this may be desired, as screen * readers will return to the input. * `loopFocus` must be `true`. * @default false */ allowEscape?: boolean; /** * Determines whether focus should loop around when navigating past the first * or last item. * @default false */ loopFocus?: boolean; /** * If the list is nested within another one (e.g. a nested submenu), the * navigation semantics change. * @default false */ nested?: boolean; /** * 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 * layout. * @default false */ rtl?: boolean; /** * Whether the focus is virtual (using `aria-activedescendant`). * Use this if you need focus to remain on the reference element * (such as an input), but allow arrow keys to navigate list items. * This is common in autocomplete listbox components. * Your virtually-focused list items must have a unique `id` set on them. * If you’re using a component role with the `useRole()` Hook, then an `id` is * generated automatically. * @default false */ virtual?: boolean; /** * The orientation in which navigation occurs. * @default 'vertical' */ orientation?: 'vertical' | 'horizontal' | 'both'; /** * Specifies how many columns the list has (i.e., it’s a grid). Use an * orientation of 'horizontal' (e.g. for an emoji picker/date picker, where * pressing ArrowRight or ArrowLeft can change rows), or 'both' (where the * current row cannot be escaped with ArrowRight or ArrowLeft, only ArrowUp * and ArrowDown). * @default 1 */ cols?: number; /** * Whether to scroll the active item into view when navigating. The default * value uses nearest options. */ scrollItemIntoView?: boolean | ScrollIntoViewOptions; /** * Only for `cols > 1`, specify sizes for grid items. * `{ width: 2, height: 2 }` means an item is 2 columns wide and 2 rows tall. */ itemSizes?: Dimensions[]; /** * Only relevant for `cols > 1` and items with different sizes, specify if * the grid is dense (as defined in the CSS spec for `grid-auto-flow`). * @default false */ dense?: boolean; /** * The id of the root component. */ id?: string | undefined; /** * External FlatingTree to use when the one provided by context can't be used. */ externalTree?: FloatingTreeStore; } /** * Adds arrow key-based navigation of a list of items, either using real DOM * focus or virtual focus. * @see https://floating-ui.com/docs/useListNavigation */ export declare function useListNavigation(context: FloatingRootContext | FloatingContext, props: UseListNavigationProps): ElementProps;