UNPKG

@primer/react

Version:

An implementation of GitHub's Primer Design System using React

133 lines (132 loc) 5.04 kB
import { useMergedRefs } from "../hooks/useMergedRefs.js"; import { useAnchoredPosition } from "../hooks/useAnchoredPosition.js"; import VisuallyHidden from "../_VisuallyHidden.js"; import Overlay from "../Overlay/Overlay.js"; import { AutocompleteContext } from "./AutocompleteContext.js"; import AutocompleteOverlay_module_css_default from "./AutocompleteOverlay.module.css.js"; import { c } from "react-compiler-runtime"; import { clsx } from "clsx"; import { jsx } from "react/jsx-runtime"; import { useContext, useEffect, useRef } from "react"; //#region src/Autocomplete/AutocompleteOverlay.tsx function AutocompleteOverlay(t0) { const $ = c(29); let children; let className; let menuAnchorRef; let newOverlayProps; let oldOverlayProps; if ($[0] !== t0) { ({menuAnchorRef, overlayProps: oldOverlayProps, children, className, ...newOverlayProps} = t0); $[0] = t0; $[1] = children; $[2] = className; $[3] = menuAnchorRef; $[4] = newOverlayProps; $[5] = oldOverlayProps; } else { children = $[1]; className = $[2]; menuAnchorRef = $[3]; newOverlayProps = $[4]; oldOverlayProps = $[5]; } const autocompleteContext = useContext(AutocompleteContext); if (autocompleteContext === null) throw new Error("AutocompleteContext returned null values"); let t1; if ($[6] !== newOverlayProps || $[7] !== oldOverlayProps) { t1 = { ...oldOverlayProps, ...newOverlayProps }; $[6] = newOverlayProps; $[7] = oldOverlayProps; $[8] = t1; } else t1 = $[8]; const overlayProps = t1; const { inputRef, scrollContainerRef, selectedItemLength, setShowMenu, showMenu: t2 } = autocompleteContext; const showMenu = t2 === void 0 ? false : t2; const computedAnchorRef = useRef(null); let t3; let t4; if ($[9] !== inputRef || $[10] !== menuAnchorRef) { t3 = () => { var _menuAnchorRef$curren, _menuAnchorRef, _tokensContainer$pare, _ref; const explicit = (_menuAnchorRef$curren = (_menuAnchorRef = menuAnchorRef) === null || _menuAnchorRef === void 0 ? void 0 : _menuAnchorRef.current) !== null && _menuAnchorRef$curren !== void 0 ? _menuAnchorRef$curren : null; const tokensContainer = inputRef.current ? inputRef.current.closest("[data-prevent-token-wrapping]") : null; const tokensRoot = (_tokensContainer$pare = tokensContainer === null || tokensContainer === void 0 ? void 0 : tokensContainer.parentElement) !== null && _tokensContainer$pare !== void 0 ? _tokensContainer$pare : null; computedAnchorRef.current = (_ref = explicit !== null && explicit !== void 0 ? explicit : tokensRoot) !== null && _ref !== void 0 ? _ref : inputRef.current; }; t4 = [menuAnchorRef, inputRef]; $[9] = inputRef; $[10] = menuAnchorRef; $[11] = t3; $[12] = t4; } else { t3 = $[11]; t4 = $[12]; } useEffect(t3, t4); let t5; if ($[13] === Symbol.for("react.memo_cache_sentinel")) { t5 = { side: "outside-bottom", align: "start", anchorElementRef: computedAnchorRef }; $[13] = t5; } else t5 = $[13]; let t6; if ($[14] !== selectedItemLength || $[15] !== showMenu) { t6 = [showMenu, selectedItemLength]; $[14] = selectedItemLength; $[15] = showMenu; $[16] = t6; } else t6 = $[16]; const { floatingElementRef, position } = useAnchoredPosition(t5, t6); const mergedScrollContainerRef = useMergedRefs(scrollContainerRef, floatingElementRef); let t7; if ($[17] !== setShowMenu) { t7 = () => { setShowMenu(false); }; $[17] = setShowMenu; $[18] = t7; } else t7 = $[18]; const closeOptionList = t7; if (typeof window === "undefined") return null; let t8; if ($[19] !== children || $[20] !== className || $[21] !== closeOptionList || $[22] !== inputRef || $[23] !== mergedScrollContainerRef || $[24] !== overlayProps || $[25] !== (position === null || position === void 0 ? void 0 : position.left) || $[26] !== (position === null || position === void 0 ? void 0 : position.top) || $[27] !== showMenu) { t8 = showMenu ? /*#__PURE__*/ jsx(Overlay, { returnFocusRef: inputRef, preventFocusOnOpen: true, onClickOutside: closeOptionList, onEscape: closeOptionList, ref: mergedScrollContainerRef, top: position === null || position === void 0 ? void 0 : position.top, left: position === null || position === void 0 ? void 0 : position.left, className: clsx(AutocompleteOverlay_module_css_default.Overlay, className), ...overlayProps, "data-component": "Autocomplete.Overlay", children }) : /*#__PURE__*/ jsx(VisuallyHidden, { "aria-hidden": "true", children }); $[19] = children; $[20] = className; $[21] = closeOptionList; $[22] = inputRef; $[23] = mergedScrollContainerRef; $[24] = overlayProps; $[25] = position === null || position === void 0 ? void 0 : position.left; $[26] = position === null || position === void 0 ? void 0 : position.top; $[27] = showMenu; $[28] = t8; } else t8 = $[28]; return t8; } AutocompleteOverlay.displayName = "AutocompleteOverlay"; AutocompleteOverlay.__SLOT__ = Symbol("Autocomplete.Overlay"); //#endregion export { AutocompleteOverlay as default };