{"version":3,"sources":["../../../../src/components/InfoLabel/InfoButton/useInfoButton.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport {\n  getIntrinsicElementProps,\n  mergeCallbacks,\n  useControllableState,\n  slot,\n  useMergedRefs,\n  elementContains,\n  useEventCallback,\n  useId,\n} from '@fluentui/react-utilities';\n\nimport type { InfoButtonProps, InfoButtonState } from './InfoButton.types';\nimport { Popover, PopoverSurface } from '../../Popover';\nimport type { PopoverProps } from '../../Popover';\nimport { toDataAttributeValue } from '../../../utils';\n\n/**\n * Create the state required to render InfoButton.\n *\n * @param props - props from this instance of InfoButton\n * @param ref - reference to root HTMLButtonElement of InfoButton\n */\nexport const useInfoButton = (props: InfoButtonProps, ref: React.Ref<HTMLButtonElement>): InfoButtonState => {\n  const { popover, info, ...rest } = props;\n\n  const rootRef = useMergedRefs(ref);\n  const buttonId = useId('info-button-', rest.id);\n  const popoverSlot = slot.always(popover, {\n    defaultProps: {\n      positioning: 'above-start',\n      withArrow: true,\n    },\n    elementType: Popover as React.ComponentType<Partial<Omit<PopoverProps, 'openOnHover'>>>,\n  });\n\n  const state: InfoButtonState = {\n    components: {\n      root: 'button',\n      popover: Popover as React.ComponentType<Partial<PopoverProps>>,\n      info: PopoverSurface,\n    },\n\n    root: slot.always(\n      // eslint-disable-next-line react-hooks/refs\n      getIntrinsicElementProps('button', {\n        type: 'button',\n        'aria-label': 'information',\n        ...rest,\n        ref: rootRef,\n      }),\n      { elementType: 'button' },\n    ),\n    popover: popoverSlot,\n    info: slot.always(info, {\n      defaultProps: {\n        as: popoverSlot.inertTrapFocus ? 'dialog' : 'div',\n        role: popoverSlot.inertTrapFocus ? 'dialog' : 'note',\n        tabIndex: -1,\n      },\n      elementType: PopoverSurface,\n    }),\n  };\n\n  if (state.popover.inertTrapFocus) {\n    state.root.id ??= buttonId;\n    if (state.info['aria-label'] === undefined && state.info['aria-labelledby'] === undefined) {\n      state.info['aria-labelledby'] = state.root.id;\n    }\n  }\n\n  const [popoverOpen, setPopoverOpen] = useControllableState({\n    state: state.popover.open,\n    defaultState: state.popover.defaultOpen,\n    initialState: false,\n  });\n\n  state.popover.open = popoverOpen;\n  // InfoButton consumes defaultOpen; the nested Popover receives only the resolved open state.\n  state.popover.defaultOpen = undefined;\n  state.root['data-open'] = toDataAttributeValue(popoverOpen);\n  state.popover.onOpenChange = mergeCallbacks(state.popover.onOpenChange, (_e, data) => setPopoverOpen(data.open));\n\n  const infoRef = useMergedRefs(state.info.ref);\n  state.info.ref = infoRef;\n\n  React.useEffect(() => {\n    if (!popoverOpen) {\n      return;\n    }\n\n    // InfoButton pattern requires focusing the surface first so content is read in order.\n    infoRef.current?.focus();\n  }, [popoverOpen, infoRef]);\n\n  // Hide the popover when focus moves out of the button and popover\n  const onBlurButtonOrInfo: React.FocusEventHandler<HTMLElement> = e => {\n    const nextFocused = e.relatedTarget;\n\n    if (!nextFocused || (rootRef.current !== nextFocused && !elementContains(infoRef.current, nextFocused))) {\n      setPopoverOpen(false);\n    }\n  };\n\n  // eslint-disable-next-line react-hooks/refs\n  state.root.onBlur = useEventCallback(mergeCallbacks(state.root.onBlur, onBlurButtonOrInfo));\n  const onInfoBlurCapture = state.info.onBlurCapture as React.FocusEventHandler<HTMLElement> | undefined;\n  // eslint-disable-next-line react-hooks/refs\n  state.info.onBlurCapture = useEventCallback(mergeCallbacks(onInfoBlurCapture, onBlurButtonOrInfo));\n  return state;\n};\n"],"names":["useInfoButton","props","ref","popover","info","rest","rootRef","useMergedRefs","buttonId","useId","id","popoverSlot","slot","always","defaultProps","positioning","withArrow","elementType","Popover","state","components","root","PopoverSurface","getIntrinsicElementProps","type","as","inertTrapFocus","role","tabIndex","undefined","popoverOpen","setPopoverOpen","useControllableState","open","defaultState","defaultOpen","initialState","toDataAttributeValue","onOpenChange","mergeCallbacks","_e","data","infoRef","React","useEffect","current","focus","onBlurButtonOrInfo","e","nextFocused","relatedTarget","elementContains","onBlur","useEventCallback","onInfoBlurCapture","onBlurCapture"],"mapp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