{"version":3,"sources":["../src/components/InfoLabel/useInfoLabel.ts"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { useId, slot, useEventCallback, mergeCallbacks } from '@fluentui/react-utilities';\n\nimport { Label } from '../Label';\nimport { InfoButton } from './InfoButton/InfoButton';\nimport type { InfoLabelProps, InfoLabelState } from './InfoLabel.types';\n\n/**\n * Create the state required to render InfoLabel.\n *\n * The returned state can be modified with hooks such as useInfoLabel,\n * before being passed to renderInfoLabel.\n *\n * @param props - props from this instance of InfoLabel\n * @param ref - reference to label element of InfoLabel\n */\nexport const useInfoLabel = (props: InfoLabelProps, ref: React.Ref<HTMLLabelElement>): InfoLabelState => {\n  const {\n    root: rootShorthand,\n    label: labelShorthand,\n    infoButton: infoButtonShorthand,\n    info,\n    className,\n    style,\n    ...labelProps\n  } = props;\n  const baseId = useId('infolabel-');\n  const [open, setOpen] = React.useState(false);\n\n  const root = slot.always(rootShorthand, {\n    defaultProps: {\n      className,\n      style,\n    },\n    elementType: 'span',\n  });\n\n  const label = slot.always(labelShorthand, {\n    defaultProps: {\n      id: baseId + '__label',\n      ref,\n      ...labelProps,\n    },\n    elementType: Label,\n  });\n\n  const infoButton = slot.optional(infoButtonShorthand, {\n    renderByDefault: !!info,\n    defaultProps: {\n      id: baseId + '__infoButton',\n      info,\n    },\n    elementType: InfoButton,\n  });\n\n  const infoButtonPopover = slot.always(infoButton?.popover, {\n    elementType: 'div',\n  });\n\n  infoButtonPopover.onOpenChange = useEventCallback(\n    mergeCallbacks(infoButtonPopover.onOpenChange, (_, data) => {\n      setOpen(data.open);\n    }),\n  );\n\n  if (infoButton) {\n    const infoPopupId = baseId + '__info'; // used as a self-referencing aria-labelledby to name the popup\n    infoButtonPopover.id ??= infoPopupId;\n    infoButton.popover = infoButtonPopover;\n    infoButton.info = slot.optional(infoButton?.info, {\n      defaultProps: {\n        'aria-labelledby': infoButtonPopover.id,\n      },\n      elementType: 'dialog',\n    });\n\n    infoButton['aria-labelledby'] ??= `${label.id} ${infoButton.id}`;\n\n    if (open) {\n      root['aria-owns'] ??= infoButtonPopover.id;\n    }\n  }\n\n  return {\n    components: {\n      root: 'span',\n      label: Label,\n      infoButton: InfoButton,\n    },\n    root,\n    label,\n    infoButton,\n  };\n};\n"],"names":["useInfoLabel","props","ref","root","rootShorthand","label","labelShorthand","infoButton","infoButtonShorthand","info","className","style","labelProps","baseId","useId","open","setOpen","React","useState","slot","always","defaultProps","elementType","id","Label","optional","renderByDefault","InfoButton","infoButtonPopover","popover","onOpenChange","useEventCallback","mergeCallbacks","_","data","infoPopupId","components"],"mappings":"AAAA;;;;;+BAkBaA;;;eAAAA;;;;iEAhBU;gCACuC;uBAExC;4BACK;AAYpB,MAAMA,eAAe,CAACC,OAAuBC;IAClD,MAAM,EACJC,MAAMC,aAAa,EACnBC,OAAOC,cAAc,EACrBC,YAAYC,mBAAmB,EAC/BC,IAAI,EACJC,SAAS,EACTC,KAAK,EACL,GAAGC,YACJ,GAAGX;IACJ,MAAMY,SAASC,IAAAA,qBAAK,EAAC;IACrB,MAAM,CAACC,MAAMC,QAAQ,GAAGC,OAAMC,QAAQ,CAAC;IAEvC,MAAMf,OAAOgB,oBAAI,CAACC,MAAM,CAAChB,eAAe;QACtCiB,cAAc;YACZX;YACAC;QACF;QACAW,aAAa;IACf;IAEA,MAAMjB,QAAQc,oBAAI,CAACC,MAAM,CAACd,gBAAgB;QACxCe,cAAc;YACZE,IAAIV,SAAS;YACbX;YACA,GAAGU,UAAU;QACf;QACAU,aAAaE,YAAK;IACpB;IAEA,MAAMjB,aAAaY,oBAAI,CAACM,QAAQ,CAACjB,qBAAqB;QACpDkB,iBAAiB,CAAC,CAACjB;QACnBY,cAAc;YACZE,IAAIV,SAAS;YACbJ;QACF;QACAa,aAAaK,sBAAU;IACzB;IAEA,MAAMC,oBAAoBT,oBAAI,CAACC,MAAM,CAACb,YAAYsB,SAAS;QACzDP,aAAa;IACf;IAEAM,kBAAkBE,YAAY,GAAGC,IAAAA,gCAAgB,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