Skip to content

Commit

Permalink
unmount react root
Browse files Browse the repository at this point in the history
  • Loading branch information
veedeo committed Feb 18, 2024
1 parent e48ae16 commit c3680f2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cli/plasmo/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ManifestContentScript } from "@plasmo/constants/manifest/content-script"
import type { Root } from "react-dom/client"

// See https://www.plasmo.com/engineering/log/2022.04#update-2022.04.23
export type PlasmoCSConfig = Omit<Partial<ManifestContentScript>, "js">
Expand All @@ -16,12 +17,14 @@ type GetElement = Getter<Element>

type PlasmoCSUIOverlayAnchor = {
element: Element
root?: Root
type: "overlay"
}

type PlasmoCSUIInlineAnchor = {
element: Element
type: "inline"
root?: Root
insertPosition?: "beforebegin" | "afterbegin" | "beforeend" | "afterend"
}

Expand Down
4 changes: 3 additions & 1 deletion cli/plasmo/templates/static/common/csui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function createAnchorObserver<T>(Mount: PlasmoCSUI<T>) {

// Go through mounted sets and check if they are still mounted
for (const el of mountState.hostSet) {
if (isMounted(el)) {
if (isMounted(el)) {
const anchor = mountState.hostMap.get(el)
if (!!anchor) {
if (anchor.type === "inline") {
Expand All @@ -168,6 +168,8 @@ export function createAnchorObserver<T>(Mount: PlasmoCSUI<T>) {
}
}
} else {
const anchor = mountState.hostMap.get(el)
anchor.root?.unmount();
mountState.hostSet.delete(el)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const render = createRender(
observer?.mountState,
async (anchor, rootContainer) => {
const root = createRoot(rootContainer)
anchor.root = root

const Layout = getLayout(RawMount)

Expand Down

0 comments on commit c3680f2

Please sign in to comment.