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.

121 lines (120 loc) 4 kB
import * as React from 'react'; import { useButton } from '../../use-button/index.js'; import { mergeReactProps } from '../../utils/mergeReactProps.js'; import { useEventCallback } from '../../utils/useEventCallback.js'; import { useForkRef } from '../../utils/useForkRef.js'; export function useSelectItem(params) { const { open, disabled = false, highlighted, selected, ref: externalRef, setOpen, typingRef, handleSelect, selectionRef, indexRef, setActiveIndex, popupRef } = params; const ref = React.useRef(null); const mergedRef = useForkRef(externalRef, ref); const { getButtonProps, buttonRef } = useButton({ disabled, focusableWhenDisabled: true, buttonRef: mergedRef }); const commitSelection = useEventCallback(event => { handleSelect(); setOpen(false, event); }); const lastKeyRef = React.useRef(null); const pointerTypeRef = React.useRef('mouse'); const prevPopupHeightRef = React.useRef(0); const allowFocusSyncRef = React.useRef(true); const getItemProps = React.useCallback(externalProps => { return getButtonProps(mergeReactProps(externalProps, { 'aria-disabled': disabled || undefined, tabIndex: highlighted ? 0 : -1, style: { pointerEvents: disabled ? 'none' : undefined }, onFocus() { if (allowFocusSyncRef.current) { setActiveIndex(indexRef.current); } }, onMouseMove() { setActiveIndex(indexRef.current); if (popupRef.current) { prevPopupHeightRef.current = popupRef.current.offsetHeight; } }, onMouseLeave() { const popup = popupRef.current; if (!popup || !open) { return; } // With `alignItemToTrigger`, avoid re-rendering the root due to `onMouseLeave` // firing and causing a performance issue when expanding the popup. if (popup.offsetHeight === prevPopupHeightRef.current) { // Prevent `onFocus` from causing the highlight to be stuck when quickly moving // the mouse out of the popup. allowFocusSyncRef.current = false; setActiveIndex(null); requestAnimationFrame(() => { popup.focus({ preventScroll: true }); allowFocusSyncRef.current = true; }); } }, onTouchStart() { selectionRef.current = { allowSelectedMouseUp: false, allowUnselectedMouseUp: false, allowSelect: true }; }, onKeyDown(event) { selectionRef.current.allowSelect = true; lastKeyRef.current = event.key; }, onClick(event) { if (lastKeyRef.current === ' ' && typingRef.current || pointerTypeRef.current !== 'touch' && !highlighted) { return; } if (selectionRef.current.allowSelect) { lastKeyRef.current = null; commitSelection(event.nativeEvent); } }, onPointerEnter(event) { pointerTypeRef.current = event.pointerType; }, onPointerDown(event) { pointerTypeRef.current = event.pointerType; }, onMouseUp(event) { const disallowSelectedMouseUp = !selectionRef.current.allowSelectedMouseUp && selected; const disallowUnselectedMouseUp = !selectionRef.current.allowUnselectedMouseUp && !selected; if (disallowSelectedMouseUp || disallowUnselectedMouseUp || pointerTypeRef.current !== 'touch' && !highlighted) { return; } if (selectionRef.current.allowSelect || !selected) { commitSelection(event.nativeEvent); } selectionRef.current.allowSelect = true; } })); }, [commitSelection, disabled, getButtonProps, highlighted, indexRef, open, popupRef, selected, selectionRef, setActiveIndex, typingRef]); return React.useMemo(() => ({ getItemProps, rootRef: buttonRef }), [getItemProps, buttonRef]); }