Skip to content

Commit

Permalink
bugfix: appContext.unmount() no longer removes portal roots
Browse files Browse the repository at this point in the history
  • Loading branch information
LankyMoose committed May 29, 2024
1 parent fa61d0f commit 3487885
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/lib/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { node, nodeToCtxMap } from "./globals.js"
export { Component }

abstract class Component<T = Record<string, unknown>> {
rootDom?: HTMLElement
doNotModifyDom = false
static [componentSymbol] = true
state = {} as Record<string, unknown>
props: T
Expand Down
10 changes: 6 additions & 4 deletions packages/lib/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function setStyleProp(
}

function updateDom(node: VNode, dom: HTMLElement | SVGElement | Text) {
if (node.instance?.rootDom) return node.instance.rootDom
if (node.instance?.doNotModifyDom) return node.dom
const prevProps: Record<string, any> = node.prev?.props ?? {}
const nextProps: Record<string, any> = node.props ?? {}

Expand Down Expand Up @@ -164,10 +164,10 @@ type DomParentSearchResult = {
}
function getDomParent(node: VNode): DomParentSearchResult {
let domParentNode: VNode | undefined = node.parent ?? node.prev?.parent
let domParent = domParentNode?.instance?.rootDom ?? domParentNode?.dom
let domParent = domParentNode?.dom
while (domParentNode && !domParent) {
domParentNode = domParentNode.parent
domParent = domParentNode?.instance?.rootDom ?? domParentNode?.dom
domParent = domParentNode?.dom
}

if (!domParent || !domParentNode) {
Expand Down Expand Up @@ -297,12 +297,14 @@ function commitDeletion(vNode: VNode, deleteSibling = false) {
const stack: VNode[] = [vNode]
while (stack.length) {
const n = stack.pop()!
let skipDomRemoval = false
if (Component.isCtor(n.type) && n.instance) {
n.instance.componentWillUnmount?.()
if (n.instance.doNotModifyDom) skipDomRemoval = true
}
while (n.hooks?.length) cleanupHook(n.hooks.pop()!)
while (n.subs?.length) Signal.unsubscribeNode(n, n.subs.pop()!)
if (n.dom?.isConnected) n.dom.remove()
if (n.dom?.isConnected && !skipDomRemoval) n.dom.remove()
delete n.dom
if (deleteSibling && n.sibling) stack.push(n.sibling)
if (n.child) stack.push(n.child)
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ type PortalProps = {
}

class Portal extends Component<PortalProps> {
doNotModifyDom = true
constructor(props: PortalProps) {
super(props)
this.rootDom = props.container
}
componentDidMount(): void {
this.vNode.dom = this.props.container
Expand Down
14 changes: 12 additions & 2 deletions sandbox/csr/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import "./index.css"
import { App } from "./App"
import { mount, renderToString } from "kaioken"
import { AppContext, mount, renderToString } from "kaioken"

const root = document.getElementById("app")!

mount(App, { root, maxFrameMs: 16, name: "CSR app" })
declare global {
interface Window {
kaiokenInstance: AppContext
}
}

window.kaiokenInstance = await mount(App, {
root,
maxFrameMs: 16,
name: "CSR app",
})

let testRenderToString = false
if (testRenderToString) {
Expand Down

0 comments on commit 3487885

Please sign in to comment.