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.

129 lines (127 loc) 4.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSelectItem = useSelectItem; var React = _interopRequireWildcard(require("react")); var _useButton = require("../../use-button"); var _mergeReactProps = require("../../utils/mergeReactProps"); var _useEventCallback = require("../../utils/useEventCallback"); var _useForkRef = require("../../utils/useForkRef"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } 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 = (0, _useForkRef.useForkRef)(externalRef, ref); const { getButtonProps, buttonRef } = (0, _useButton.useButton)({ disabled, focusableWhenDisabled: true, buttonRef: mergedRef }); const commitSelection = (0, _useEventCallback.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((0, _mergeReactProps.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]); }