{"version":3,"file":"DropdownContentInner.cjs","sources":["../../../src/components/Dropdown/DropdownContentInner.tsx"],"sourcesContent":["import {\n  type ComponentProps,\n  type FC,\n  type PropsWithChildren,\n  createContext,\n  useEffect,\n  useMemo,\n  useRef,\n  useState,\n} from 'react'\nimport { tv } from 'tailwind-variants'\n\nimport { useTheme } from '../../hooks/useTheme'\n\nimport { DropdownCloser } from './DropdownCloser'\nimport { type ContentBoxStyle, type Rect, getContentBoxStyle } from './dropdownHelper'\nimport { useKeyboardNavigation } from './useKeyboardNavigation'\n\nconst classNameGenerator = tv({\n  base: 'smarthr-ui-Dropdown-content shr-absolute shr-z-overlap-base shr-overflow-y-auto shr-break-words shr-rounded-m shr-bg-white shr-shadow-layer-3',\n  variants: {\n    isActive: {\n      true: 'shr-visible',\n      false: 'shr-invisible',\n    },\n  },\n})\n\ntype AbstractProps = PropsWithChildren<{\n  triggerRect: Rect\n  controllable: boolean\n}>\n\nexport type ElementProps = Omit<ComponentProps<'div'>, keyof AbstractProps>\ntype Props = AbstractProps & ElementProps\n\ntype DropdownContentInnerContextType = {\n  maxHeight: string\n}\n\nexport const DropdownContentInnerContext = createContext<DropdownContentInnerContextType>({\n  maxHeight: '',\n})\n\nexport const DropdownContentInner: FC<Props> = ({\n  triggerRect,\n  children,\n  className,\n  controllable,\n  ...rest\n}) => {\n  const theme = useTheme()\n  const [isActive, setIsActive] = useState(false)\n  const [contentBox, setContentBox] = useState<ContentBoxStyle>({\n    top: 'auto',\n    maxHeight: '',\n  })\n  const wrapperRef = useRef<HTMLDivElement>(null)\n  const focusTargetRef = useRef<HTMLDivElement>(null)\n\n  const actualClassName = useMemo(\n    () => classNameGenerator({ isActive, className }),\n    [isActive, className],\n  )\n\n  const style = useMemo(() => {\n    const defaultMargin = theme.spacingByChar(0.5)\n    const leftMargin =\n      contentBox.left === undefined ? defaultMargin : `max(${contentBox.left}, 0px)`\n    const rightMargin =\n      contentBox.right === undefined ? defaultMargin : `max(${contentBox.right}, 0px)`\n    const maxWidthStyle = `calc(100% - ${leftMargin} - ${rightMargin})`\n\n    return {\n      insetBlockStart: contentBox.top,\n      insetInlineStart: contentBox.left || undefined,\n      insetInlineEnd: contentBox.right || undefined,\n      maxWidth: maxWidthStyle,\n    }\n  }, [contentBox.left, contentBox.right, contentBox.top, theme])\n  const controllableWrapperStyleProps = useMemo(\n    () => ({\n      maxHeight: contentBox.maxHeight || undefined,\n    }),\n    [contentBox.maxHeight],\n  )\n\n  useEffect(() => {\n    if (wrapperRef.current) {\n      setContentBox(\n        getContentBoxStyle(\n          triggerRect,\n          {\n            width: wrapperRef.current.offsetWidth,\n            height: wrapperRef.current.offsetHeight,\n          },\n          {\n            width: document.body.clientWidth,\n            height: innerHeight,\n          },\n          {\n            top: scrollY,\n            left: scrollX,\n          },\n        ),\n      )\n      setIsActive(true)\n    }\n  }, [triggerRect])\n\n  useEffect(() => {\n    if (isActive) {\n      focusTargetRef.current?.focus()\n    }\n  }, [isActive])\n\n  useKeyboardNavigation(wrapperRef, focusTargetRef)\n\n  return (\n    <div {...rest} ref={wrapperRef} className={actualClassName} style={style}>\n      {/* dummy element for focus management. */}\n      <div ref={focusTargetRef} tabIndex={-1} />\n      {controllable ? (\n        <div style={controllableWrapperStyleProps}>{children}</div>\n      ) : (\n        <DropdownContentInnerContext.Provider value={{ maxHeight: contentBox.maxHeight }}>\n          <DropdownCloser>{children}</DropdownCloser>\n        </DropdownContentInnerContext.Provider>\n      )}\n    </div>\n  )\n}\n"],"names":["tv","createContext","useTheme","useState","useRef","useMemo","useEffect","getContentBoxStyle","useKeyboardNavigation","_jsxs","_jsx","DropdownCloser"],"mappings":";;;;;;;;;;AAkBA,MAAM,kBAAkB,GAAGA,QAAE,CAAC;AAC5B,IAAA,IAAI,EAAE,+IAA+I;AACrJ,IAAA,QAAQ,EAAE;AACR,QAAA,QAAQ,EAAE;AACR,YAAA,IAAI,EAAE,aAAa;AACnB,YAAA,KAAK,EAAE,eAAe;AACvB,SAAA;AACF,KAAA;AAC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