{"version":3,"sources":["../../../../src/hooks/internal/usePresence.ts"],"sourcesContent":["import { whenExitAnimationEnds } from '@novu/js/ui-core';\nimport { useRef, useState } from 'react';\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';\n\ntype PresenceState = 'open' | 'closed';\n\n/**\n * Keeps an element mounted while its exit animation plays: the React twin of the engine's `createPresence`, so a\n * block animates the same way in both renderers.\n *\n * Render the element while `isMounted`, with `ref` and `data-state={state}`; the core motion recipes animate `open`\n * in and `closed` out. With `appear: false` an element that is present from the start renders no state until\n * `present` first changes, so it does not animate on mount.\n */\nexport const usePresence = <T extends Element>(present: boolean, { appear = true }: { appear?: boolean } = {}) => {\n  const ref = useRef<T>(null);\n  const [isMounted, setIsMounted] = useState(present);\n  const [hasChanged, setHasChanged] = useState(appear || !present);\n  const [previous, setPrevious] = useState(present);\n\n  // Adjusted while rendering, so the render that follows the change already carries `closed` for the exit to start.\n  if (present !== previous) {\n    setPrevious(present);\n    setHasChanged(true);\n    if (present) {\n      setIsMounted(true);\n    }\n  }\n\n  useIsomorphicLayoutEffect(() => {\n    if (present) {\n      return undefined;\n    }\n\n    return whenExitAnimationEnds(ref.current, () => setIsMounted(false));\n  }, [present]);\n\n  const state: PresenceState | undefined = hasChanged ? (present ? 'open' : 'closed') : undefined;\n\n  return { ref, isMounted, state };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAAsC;AACtC,mBAAiC;AACjC,uCAA0C;AAYnC,IAAM,cAAc,CAAoB,SAAkB,EAAE,SAAS,KAAK,IAA0B,CAAC,MAAM;AAChH,QAAM,UAAM,qBAAU,IAAI;AAC1B,QAAM,CAAC,WAAW,YAAY,QAAI,uBAAS,OAAO;AAClD,QAAM,CAAC,YAAY,aAAa,QAAI,uBAAS,UAAU,CAAC,OAAO;AAC/D,QAAM,CAAC,UAAU,WAAW,QAAI,uBAAS,OAAO;AAGhD,MAAI,YAAY,UAAU;AACxB,gBAAY,OAAO;AACnB,kBAAc,IAAI;AAClB,QAAI,SAAS;AACX,mBAAa,IAAI;AAAA,IACnB;AAAA,EACF;AAEA,kEAA0B,MAAM;AAC9B,QAAI,SAAS;AACX,aAAO;AAAA,IACT;AAEA,eAAO,sCAAsB,IAAI,SAAS,MAAM,aAAa,KAAK,CAAC;AAAA,EACrE,GAAG,CAAC,OAAO,CAAC;AAEZ,QAAM,QAAmC,aAAc,UAAU,SAAS,WAAY;AAEtF,SAAO,EAAE,KAAK,WAAW,MAAM;AACjC;","names":[]}