{"version":3,"sources":["../src/components/Popover/PopoverTrigger/usePopoverTrigger.ts"],"sourcesContent":["'use client';\n\nimport type * as React from 'react';\nimport {\n  applyTriggerPropsToChildren,\n  getTriggerChild,\n  getReactElementRef,\n  mergeCallbacks,\n  useMergedRefs,\n  useEventCallback,\n} from '@fluentui/react-utilities';\nimport { useARIAButtonProps } from '@fluentui/react-aria';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\nimport { Escape } from '@fluentui/keyboard-keys';\nimport { usePopoverContext } from '../popoverContext';\nimport { stringifyDataAttribute } from '../../../utils';\nimport type { PopoverTriggerChildProps, PopoverTriggerProps, PopoverTriggerState } from './PopoverTrigger.types';\n\n/**\n * Returns the state for a PopoverTrigger component.\n * Applies event handlers and ARIA attributes to the child element.\n */\nexport const usePopoverTrigger = (props: PopoverTriggerProps): PopoverTriggerState => {\n  const { children, disableButtonEnhancement = false } = props;\n  const child = getTriggerChild<PopoverTriggerChildProps>(children);\n\n  const open = usePopoverContext(context => context.open);\n  const setOpen = usePopoverContext(context => context.setOpen);\n  const toggleOpen = usePopoverContext(context => context.toggleOpen);\n  const triggerRef = usePopoverContext(context => context.triggerRef);\n  const openOnHover = usePopoverContext(context => context.openOnHover);\n  const openOnContext = usePopoverContext(context => context.openOnContext);\n  const positioningCtx = usePopoverContext(context => context.positioning);\n  const surfaceId = usePopoverContext(context => context.surfaceId);\n\n  const { targetDocument } = useFluent();\n\n  const onContextMenu = (e: React.MouseEvent<HTMLElement>) => {\n    if (!openOnContext || !targetDocument) {\n      return;\n    }\n\n    e.preventDefault();\n    const nativeEvent = e.nativeEvent;\n    // Defer to trailing `pointerup` so popover=\"auto\" light-dismiss doesn't treat it as outside-click.\n    targetDocument.addEventListener('pointerup', () => setOpen(nativeEvent, true), {\n      once: true,\n      capture: true,\n    });\n  };\n\n  const onClick = (e: React.MouseEvent<HTMLElement>) => {\n    if (!openOnContext) {\n      toggleOpen(e);\n    }\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent<HTMLElement>) => {\n    if (e.key === Escape && open && !e.isDefaultPrevented()) {\n      setOpen(e, false);\n      e.preventDefault();\n    }\n  };\n\n  const onMouseEnter = (e: React.MouseEvent<HTMLElement>) => {\n    if (openOnHover) {\n      setOpen(e, true);\n    }\n  };\n\n  const onMouseLeave = (e: React.MouseEvent<HTMLElement>) => {\n    if (openOnHover) {\n      setOpen(e, false);\n    }\n  };\n\n  const triggerChildProps = {\n    'aria-expanded': `${open}` as 'true' | 'false',\n    'aria-haspopup': 'true' as const,\n    'aria-details': open ? surfaceId : undefined,\n    'data-open': stringifyDataAttribute(open),\n    ...child?.props,\n    onMouseEnter: useEventCallback(mergeCallbacks(child?.props.onMouseEnter, onMouseEnter)),\n    onMouseLeave: useEventCallback(mergeCallbacks(child?.props.onMouseLeave, onMouseLeave)),\n    onContextMenu: useEventCallback(mergeCallbacks(child?.props.onContextMenu, onContextMenu)),\n    onClick: useEventCallback(mergeCallbacks(child?.props.onClick, onClick)),\n    onKeyDown: useEventCallback(mergeCallbacks(child?.props.onKeyDown, onKeyDown)),\n    ref: useMergedRefs(triggerRef, positioningCtx.targetRef, getReactElementRef(child)),\n  };\n\n  const ariaButtonProps = useARIAButtonProps(\n    child?.type === 'button' || child?.type === 'a' ? child.type : 'div',\n    triggerChildProps,\n  );\n\n  return {\n    children: applyTriggerPropsToChildren(\n      children,\n      openOnContext ? triggerChildProps : disableButtonEnhancement ? triggerChildProps : ariaButtonProps,\n    ),\n  };\n};\n"],"names":["usePopoverTrigger","props","children","disableButtonEnhancement","child","getTriggerChild","open","usePopoverContext","context","setOpen","toggleOpen","triggerRef","openOnHover","openOnContext","positioningCtx","positioning","surfaceId","targetDocument","useFluent","onContextMenu","e","preventDefault","nativeEvent","addEventListener","once","capture","onClick","onKeyDown","key","Escape","isDefaultPrevented","onMouseEnter","onMouseLeave","triggerChildProps","undefined","stringifyDataAttribute","useEventCallback","mergeCallbacks","ref","useMergedRefs","targetRef","getReactElementRef","ariaButtonProps","useAR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