{"version":3,"file":"useMutationObserver.mjs","names":[],"sources":["../../../../../src/components/UtilityComponents/hooks/useMutationObserver.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\nimport type { RefObject } from 'react';\n\nimport { useStableCallback } from '../../../utils/useStableCallback';\n\nconst DEFAULT_INIT: MutationObserverInit = { childList: true, subtree: true };\nconst NOOP = () => undefined;\n\nexport type UseMutationObserverOptions = {\n  /** Whether to observe; `false` disconnects and runs `onDisconnect`. Defaults to `true`. */\n  enabled?: boolean;\n  /** `MutationObserver` init, read on connect. Defaults to `{ childList: true, subtree: true }`. */\n  init?: MutationObserverInit;\n  /**\n   * Runs whenever observation stops, including on unmount, to clean up side effects of\n   * `onMutation`.\n   */\n  onDisconnect?: () => void;\n  /** Also calls `onMutation` once right after connecting. Defaults to `true`. */\n  runOnConnect?: boolean;\n};\n\n/**\n * Runs `onMutation` when the observed subtree of `ref` changes, and once on connect unless\n * `runOnConnect` is `false`. The latest `onMutation` and `onDisconnect` are used without\n * reconnecting the observer.\n *\n * It reconnects on an `enabled` change or a remount, not when `ref.current` is swapped silently;\n * use a `key` or toggle `enabled` when the target changes.\n */\nexport const useMutationObserver = (\n  ref: RefObject<HTMLElement | null>,\n  onMutation: () => void,\n  {\n    enabled = true,\n    init = DEFAULT_INIT,\n    onDisconnect,\n    runOnConnect = true,\n  }: UseMutationObserverOptions = {},\n) => {\n  // Stable identities that always call the latest callback, so updating either does not reconnect\n  // the observer.\n  const handleMutation = useStableCallback(onMutation);\n  const handleDisconnect = useStableCallback(onDisconnect ?? NOOP);\n  // `init` is read only when (re)connecting; keep it off the effect deps so a fresh object literal\n  // each render does not reconnect.\n  const initRef = useRef(init);\n  initRef.current = init;\n\n  useEffect(() => {\n    const target = ref.current;\n    if (!enabled || !target) return;\n\n    if (runOnConnect) handleMutation();\n\n    // Without `MutationObserver` (e.g. a non-DOM test environment) only the initial call and the\n    // disconnect cleanup run.\n    if (typeof MutationObserver === 'undefined') return handleDisconnect;\n\n    const observer = new MutationObserver(() => handleMutation());\n    observer.observe(target, initRef.current);\n\n    return () => {\n      observer.disconnect();\n      handleDisconnect();\n    };\n  }, [enabled, handleDisconnect, handleMutation, ref, runOnConnect]);\n};\n"],"mappings":";;;AAKA,IAAM,eAAqC;CAAE,WAAW;CAAM,SAAS;AAAK;AAC5E,IAAM,aAAa,KAAA;;;;;;;;;AAwBnB,IAAa,uBACX,KACA,YACA,EACE,UAAU,MACV,OAAO,cACP,cACA,eAAe,SACe,CAAC,MAC9B;CAGH,MAAM,iBAAiB,kBAAkB,UAAU;CACnD,MAAM,mBAAmB,kBAAkB,gBAAgB,IAAI;CAG/D,MAAM,UAAU,OAAO,IAAI;CAC3B,QAAQ,UAAU;CAElB,gBAAgB;EACd,MAAM,SAAS,IAAI;EACnB,IAAI,CAAC,WAAW,CAAC,QAAQ;EAEzB,IAAI,cAAc,eAAe;EAIjC,IAAI,OAAO,qBAAqB,aAAa,OAAO;EAEpD,MAAM,WAAW,IAAI,uBAAuB,eAAe,CAAC;EAC5D,SAAS,QAAQ,QAAQ,QAAQ,OAAO;EAExC,aAAa;GACX,SAAS,WAAW;GACpB,iBAAiB;EACnB;CACF,GAAG;EAAC;EAAS;EAAkB;EAAgB;EAAK;CAAY,CAAC;AACnE"}