{"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 {\n    toasterId,\n    position,\n    timeout,\n    pauseOnWindowBlur,\n    pauseOnHover,\n    priority,\n    offset,\n    shortcuts,\n    limit,\n    announce: announceProp,\n    ...rest\n  } = props;\n\n  const playAllToastsRef = React.useRef<() => void>(() => undefined);\n  const toasterShortcuts = React.useMemo(\n    () =>\n      shortcuts\n        ? {\n            focus: (e: KeyboardEvent) => {\n              const isFocusShortcut = shortcuts.focus(e);\n              if (isFocusShortcut) {\n                Promise.resolve().then(() => playAllToastsRef.current());\n              }\n              return isFocusShortcut;\n            },\n          }\n        : undefined,\n    [shortcuts],\n  );\n  const { toastsToRender, isToastVisible, playAllToasts, tryRestoreFocus, closeAllToasts } =\n    useToasterState<HTMLDivElement>({\n      toasterId,\n      position,\n      timeout,\n      pauseOnWindowBlur,\n      pauseOnHover,\n      priority,\n      shortcuts: toasterShortcuts,\n      limit,\n    });\n  useIsomorphicLayoutEffect(() => {\n    playAllToastsRef.current = playAllToasts;\n  }, [playAllToasts]);\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        focusgroup: 'toolbar block itemcontrols',\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","position","timeout","pauseOnWindowBlur","pauseOnHover","priority","offset","shortcuts","limit","announce","announceProp","rest","playAllToastsRef","React","useRef","undefined","toasterShortcuts","useMemo","focus","e","isFocusShortcut","Promise","resolve","then","current","toastsToRender","isToastVisible","playAllToasts","tryRestoreFocus","closeAllToasts","useToasterState","useIsomorphicLayoutEffect","announceRef","useCallback","message","options","dir","useFluent","onKeyDown","onKeyDownProp","rootProps","slot","always","getIntrinsicElementProps","elementType","useEventCallback","key","Escape","preventDefault","usePositionSlot","toastPosition","announceToast","toasterRef","useToastAnnounce","popoverRef","positionHasToasts","has","el","showPopover","isOpen","matches","optional","defaultProps","ref","useMergedRefs","children","get","map","toast","ToastContainer","intent","toastId","visible","content","focusgroup","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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