{"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} from '@fluentui/react-utilities';\n\nimport type { InfoButtonProps, InfoButtonState } from './InfoButton.types';\nimport { Popover, PopoverSurface } from '../../Popover';\nimport type { PopoverProps } from '../../Popover';\nimport { stringifyDataAttribute } 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\n  const state: InfoButtonState = {\n    components: {\n      root: 'button',\n      popover: Popover as React.FC<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: slot.always(popover, {\n      defaultProps: {\n        positioning: 'above-start',\n        withArrow: true,\n      },\n      elementType: Popover as React.FC<Partial<Omit<PopoverProps, 'openOnHover'>>>,\n    }),\n    info: slot.always(info, {\n      defaultProps: {\n        role: 'note',\n        tabIndex: -1,\n      },\n      elementType: PopoverSurface,\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  state.root['data-open'] = stringifyDataAttribute(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 = (e: React.FocusEvent) => {\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  // eslint-disable-next-line react-hooks/refs\n  state.info.onBlurCapture = useEventCallback(mergeCallbacks(state.info.onBlurCapture, onBlurButtonOrInfo));\n  return state;\n};\n"],"names":["useInfoButton","props","ref","popover","info","rest","rootRef","useMergedRefs","state","components","root","Popover","PopoverSurface","slot","always","getIntrinsicElementProps","type","elementType","defaultProps","positioning","withArrow","role","tabIndex","popoverOpen","setPopoverOpen","useControllableState","open","defaultState","defaultOpen","initialState","stringifyDataAttribute","onOpenChange","mergeCallbacks","_e","data","infoRef","React","useEffect","current","focus","onBlurButtonOrInfo","e","nextFocused","relatedTarget","elementContains","onBlur","useEventCallback","onBlurCapture"],"mappings":"AAAA;;;;;+BAwBaA;;;eAAAA;;;;iEAtBU;gCAShB;yBAGiC;uBAED;AAQhC,MAAMA,gBAAgB,CAACC,OAAwBC;IACpD,MAAM,EAAEC,OAAO,EAAEC,IAAI,EAAE,GAAGC,MAAM,GAAGJ;IAEnC,MAAMK,UAAUC,IAAAA,6BAAa,EAACL;IAE9B,MAAMM,QAAyB;QAC7BC,YAAY;YACVC,MAAM;YACNP,SAASQ,gBAAO;YAChBP,MAAMQ,uBAAc;QACtB;QAEAF,MAAMG,oBAAI,CAACC,MAAM,CACf,4CAA4C;QAC5CC,IAAAA,wCAAwB,EAAC,UAAU;YACjCC,MAAM;YACN,cAAc;YACd,GAAGX,IAAI;YACPH,KAA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