{"version":3,"file":"watchElementDisconnection.cjs","names":["getRootNodes","node","rootNode","getRootNode","ShadowRoot","Array","of","concat","host","watchElementDisconnection","element","onDisconnected","isConnected","observeRootNodes","forEach","mutationObserver","observe","childList","subtree","MutationObserver","disconnect","exports"],"sources":["../../../../src/message-bus/fullscreen/watchElementDisconnection.ts"],"sourcesContent":["/*!\n * Copyright (c) 2026-present, Okta, Inc. and/or its affiliates. All rights reserved.\n * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the \"License.\")\n *\n * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n *\n * See the License for the specific language governing permissions and limitations under the License.\n */\n\nexport type WatchElementDisconnectionArgs = {\n  element: Element;\n  /**\n   * Called once, the first time `element` is no longer in the document.\n   */\n  onDisconnected: () => void;\n};\n\n/**\n * Stops watching. Safe to call more than once.\n */\nexport type StopWatchingElementDisconnection = () => void;\n\n/**\n * Every root `node` sits under, innermost first: its shadow root, that shadow root's host's root, and so on up to the document. A removal inside a shadow root is never reported to an observer on the document, so each root needs its own observer.\n */\nconst getRootNodes = (node: Node): Node[] => {\n  const rootNode = node.getRootNode();\n\n  return rootNode instanceof ShadowRoot\n    ? Array.of<Node>(rootNode).concat(getRootNodes(rootNode.host))\n    : [rootNode];\n};\n\n/**\n * Calls `onDisconnected` once `element` leaves the document, even when nothing unmounts it.\n *\n * A host page can remove the container a React app renders into without calling `root.unmount()`, such as a Backbone view's `.remove()` or a router that caches a route's DOM. The app's cleanup then never runs, so watching the element is the only way to notice.\n *\n * Moving an element is not leaving: removing and re-adding it in the same task reads as still connected, because observers report after the task ends. The roots are read again on every report, so an element moved into a different shadow root is still watched.\n */\nexport const watchElementDisconnection = ({\n  element,\n  onDisconnected,\n}: WatchElementDisconnectionArgs): StopWatchingElementDisconnection => {\n  if (!element.isConnected) {\n    onDisconnected();\n\n    return () => {};\n  }\n\n  const observeRootNodes = () => {\n    getRootNodes(element).forEach((rootNode) => {\n      mutationObserver.observe(rootNode, { childList: true, subtree: true });\n    });\n  };\n\n  // Only runs while something is in fullscreen, and each report is one `isConnected` read, so watching the whole document stays cheap.\n  const mutationObserver = new MutationObserver(() => {\n    mutationObserver.disconnect();\n\n    if (element.isConnected) {\n      observeRootNodes();\n    } else {\n      onDisconnected();\n    }\n  });\n\n  observeRootNodes();\n\n  return () => {\n    mutationObserver.disconnect();\n  };\n};\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAkBA,MAAMA,YAAY,GAAIC,IAAU,IAAa;EAC3C,MAAMC,QAAQ,GAAGD,IAAI,CAACE,WAAW,CAAC,CAAC;EAEnC,OAAOD,QAAQ,YAAYE,UAAU,GACjCC,KAAK,CAACC,EAAE,CAAOJ,QAAQ,CAAC,CAACK,MAAM,CAACP,YAAY,CAACE,QAAQ,CAACM,IAAI,CAAC,CAAC,GAC5D,CAACN,QAAQ,CAAC;AAChB,CAAC;AASM,MAAMO,yBAAyB,GAAGA,CAAC;EACxCC,OAAO;EACPC;AAC6B,CAAC,KAAuC;EACrE,IAAI,CAACD,OAAO,CAACE,WAAW,EAAE;IACxBD,cAAc,CAAC,CAAC;IAEhB,OAAO,MAAM,CAAC,CAAC;EACjB;EAEA,MAAME,gBAAgB,GAAGA,CAAA,KAAM;IAC7Bb,YAAY,CAACU,OAAO,CAAC,CAACI,OAAO,CAAEZ,QAAQ,IAAK;MAC1Ca,gBAAgB,CAACC,OAAO,CAACd,QAAQ,EAAE;QAAEe,SAAS,EAAE,IAAI;QAAEC,OAAO,EAAE;MAAK,CAAC,CAAC;IACxE,CAAC,CAAC;EACJ,CAAC;EAGD,MAAMH,gBAAgB,GAAG,IAAII,gBAAgB,CAAC,MAAM;IAClDJ,gBAAgB,CAACK,UAAU,CAAC,CAAC;IAE7B,IAAIV,OAAO,CAACE,WAAW,EAAE;MACvBC,gBAAgB,CAAC,CAAC;IACpB,CAAC,MAAM;MACLF,cAAc,CAAC,CAAC;IAClB;EACF,CAAC,CAAC;EAEFE,gBAAgB,CAAC,CAAC;EAElB,OAAO,MAAM;IACXE,gBAAgB,CAACK,UAAU,CAAC,CAAC;EAC/B,CAAC;AACH,CAAC;AAACC,OAAA,CAAAZ,yBAAA,GAAAA,yBAAA","ignoreList":[]}