{"version":3,"sources":["../src/components/Toast/Toaster/useToaster.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { useToaster as useToasterState, useToastAnnounce } from '@fluentui/react-toast';\nimport type { ToastAnnounce, ToastPosition } from '@fluentui/react-toast';\nimport type { ToasterProps, ToasterState } from './Toaster.types';\nimport type { ExtractSlotProps, Slot } from '@fluentui/react-utilities';\nimport {\n  getIntrinsicElementProps,\n  slot,\n  useEventCallback,\n  useIsomorphicLayoutEffect,\n  useMergedRefs,\n} from '@fluentui/react-utilities';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\nimport { Escape } from '@fluentui/keyboard-keys';\nimport { ToastContainer } from '../ToastContainer';\n\nconst SUPPORTS_POPOVER_OPEN_SELECTOR =\n  typeof CSS !== 'undefined' && typeof CSS.supports === 'function' && CSS.supports('selector(:popover-open)');\n\n/**\n * Create the state required to render the Toaster.\n */\nexport const useToaster = (props: ToasterProps): ToasterState => {\n  const { toasterId, offset, shortcuts, announce: announceProp, ...rest } = props;\n\n  const { toastsToRender, isToastVisible, tryRestoreFocus, closeAllToasts } = useToasterState<HTMLDivElement>({\n    toasterId,\n    offset,\n    shortcuts,\n    pauseOnHover: true,\n  });\n\n  const announceRef = React.useRef<ToastAnnounce>(() => null);\n  const announce = React.useCallback<ToastAnnounce>((message, options) => announceRef.current(message, options), []);\n  const { dir } = useFluent();\n\n  const { onKeyDown: onKeyDownProp, ...rootProps } = slot.always(\n    getIntrinsicElementProps<ExtractSlotProps<Slot<'div'>>>('div', rest),\n    {\n      elementType: 'div',\n    },\n  );\n  const onKeyDown = useEventCallback((e: React.KeyboardEvent<HTMLDivElement>) => {\n    if (e.key === Escape) {\n      e.preventDefault();\n      closeAllToasts();\n    }\n    onKeyDownProp?.(e);\n  });\n\n  const usePositionSlot = (toastPosition: ToastPosition) => {\n    const { announceToast, toasterRef } = useToastAnnounce(announceProp ?? announce);\n    const popoverRef = React.useRef<HTMLDivElement>(null);\n    const positionHasToasts = toastsToRender.has(toastPosition);\n\n    // Each rendered position container is its own native popover. We open it on\n    // mount so the position is promoted to the browser top layer. When the\n    // position has no more toasts, the slot unmounts and the browser removes\n    // it from the top layer automatically — no explicit hidePopover needed.\n    useIsomorphicLayoutEffect(() => {\n      if (!positionHasToasts) {\n        return;\n      }\n      const el = popoverRef.current;\n      if (!el || typeof el.showPopover !== 'function') {\n        return;\n      }\n      const isOpen = SUPPORTS_POPOVER_OPEN_SELECTOR && el.matches(':popover-open');\n      if (!isOpen) {\n        el.showPopover();\n      }\n    }, [positionHasToasts]);\n\n    return slot.optional<ExtractSlotProps<Slot<'div'>>>(positionHasToasts ? {} : null, {\n      defaultProps: {\n        ref: useMergedRefs(toasterRef, popoverRef),\n        children: toastsToRender.get(toastPosition)?.map(toast => (\n          <ToastContainer\n            {...toast}\n            tryRestoreFocus={tryRestoreFocus}\n            intent={toast.intent}\n            announce={announceToast}\n            key={toast.toastId}\n            visible={isToastVisible(toast.toastId)}\n          >\n            {toast.content as React.ReactNode}\n          </ToastContainer>\n        )),\n        onKeyDown,\n        popover: 'manual' as const,\n        'data-toaster-position': toastPosition,\n        role: 'list',\n        ...rootProps,\n      } as ExtractSlotProps<Slot<'div'>>,\n      elementType: 'div',\n    });\n  };\n\n  const bottomStart = usePositionSlot('bottom-start');\n  const bottomEnd = usePositionSlot('bottom-end');\n  const topStart = usePositionSlot('top-start');\n  const topEnd = usePositionSlot('top-end');\n  const top = usePositionSlot('top');\n  const bottom = usePositionSlot('bottom');\n\n  return {\n    dir,\n    components: {\n      root: 'div',\n      bottomStart: 'div',\n      bottomEnd: 'div',\n      topStart: 'div',\n      topEnd: 'div',\n      top: 'div',\n      bottom: 'div',\n    },\n    root: slot.always(rootProps, { elementType: 'div' }),\n    bottomStart,\n    bottomEnd,\n    topStart,\n    topEnd,\n    top,\n    bottom,\n    announceRef,\n    offset,\n    announce: announceProp ?? announce,\n    renderAriaLive: !announceProp,\n  };\n};\n"],"names":["useToaster","SUPPORTS_POPOVER_OPEN_SELECTOR","CSS","supports","props","toasterId","offset","shortcuts","announce","announceProp","rest","toastsToRender","isToastVisible","tryRestoreFocus","closeAllToasts","useToasterState","pauseOnHover","announceRef","React","useRef","useCallback","message","options","current","dir","useFluent","onKeyDown","onKeyDownProp","rootProps","slot","always","getIntrinsicElementProps","elementType","useEventCallback","e","key","Escape","preventDefault","usePositionSlot","toastPosition","announceToast","toasterRef","useToastAnnounce","popoverRef","positionHasToasts","has","useIsomorphicLayoutEffect","el","showPopover","isOpen","matches","optional","defaultProps","ref","useMergedRefs","children","get","map","toast","ToastContainer","intent","toastId","visible","content","popover","role","bottomStart","bottomEnd","topStart","topEnd","top","bottom","components","root","renderAriaLive"],"mappings":"AAAA;;;;;+BAwBaA;;;eAAAA;;;;iEAtBU;4BACyC;gCAUzD;qCACyC;8BACzB;gCACQ;AAE/B,MAAMC,iCACJ,OAAOC,QAAQ,eAAe,OAAOA,IAAIC,QAAQ,KAAK,cAAcD,IAAIC,QAAQ,CAAC;AAK5E,MAAMH,aAAa,CAACI;IACzB,MAAM,EAAEC,SAAS,EAAEC,MAAM,EAAEC,SAAS,EAAEC,UAAUC,YAAY,EAAE,GAAGC,MAAM,GAAGN;IAE1E,MAAM,EAAEO,cAAc,EAAEC,cAAc,EAAEC,eAAe,EAAEC,cAAc,EAAE,GAAGC,IAAAA,sBAAe,EAAiB;QAC1GV;QACAC;QACAC;QACAS,cAAc;IAChB;IAEA,MAAMC,cAAcC,OAAMC,MAAM,CAAgB,IAAM;IACtD,MAAMX,WAAWU,OAAME,WAAW,CAAgB,CAACC,SAASC,UAAYL,YAAYM,OAAO,CAACF,SAASC,UAAU,EAAE;IACjH,MAAM,EAAEE,GAAG,EAAE,GAAGC,IAAAA,uCAAS;IAEzB,MAAM,EAAEC,WAAWC,aAAa,EAAE,GAAGC,WAAW,GAAGC,oBAAI,CAACC,MAAM,CAC5DC,IAAAA,wCAAwB,EAAgC,OAAOrB,OAC/D;QACEsB,aAAa;IACf;IAEF,MAA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