Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unmount react root #893

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading