diff --git a/.changeset/ui-mount-preserve-children.md b/.changeset/ui-mount-preserve-children.md new file mode 100644 index 00000000000..30ba462256a --- /dev/null +++ b/.changeset/ui-mount-preserve-children.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Remove only rendered children when emptying or replacing children of a UI element with a Mount, so DOM nodes managed by the Mount are preserved. diff --git a/packages/effect/src/ui/internal/kernel.ts b/packages/effect/src/ui/internal/kernel.ts index ba686b851e4..4cc83ab2ae2 100644 --- a/packages/effect/src/ui/internal/kernel.ts +++ b/packages/effect/src/ui/internal/kernel.ts @@ -1396,15 +1396,21 @@ const remove = (inst: Instance, parent: unknown, rec: Rec): void => { for (const node of nodesOf(rec, [])) inst.tree.host.remove(parent, node) } -// A bound child's old placement may hold only a placeholder while its nodes await a move. -// Clearing the container would detach those nodes and lose keyboard focus. -const removeAll = (inst: Instance, parent: unknown, olds: ReadonlyArray): void => { +// A bound child's old placement may hold only a placeholder while its nodes await a move, +// and an element with a Mount may hold DOM nodes the Mount placed itself. +// Clearing the container wholesale would detach those nodes. +const removeAll = ( + inst: Instance, + parent: unknown, + olds: ReadonlyArray, + canClear = true +): void => { const host = inst.tree.host const from = boundReleases for (let index = 0; index < olds.length; index++) { release(inst, olds[index]!) } - if (host.clear !== undefined && boundReleases === from) { + if (canClear && host.clear !== undefined && boundReleases === from) { host.clear(parent) } else { for (let index = 0; index < olds.length; index++) { @@ -1918,6 +1924,7 @@ const patchElement = ( const views = view.children const olds = rec.kids if (dev && views.length > 1) checkKeys(inst.path, views) + const canClear = prev.mount === undefined && view.mount === undefined if (olds === undefined) { if (views.length > 0) { const kids: Array = new Array(views.length) @@ -1927,7 +1934,7 @@ const patchElement = ( rec.kids = kids } } else if (views.length === 0) { - removeAll(inst, node, olds) + removeAll(inst, node, olds, canClear) rec.kids = undefined } else if (views.length === 1 && olds.length === 1) { const old = olds[0]! @@ -1940,7 +1947,7 @@ const patchElement = ( ) { patchElement(inst, old, old.v as View.Element, next) } else olds[0] = patch(inst, old, next) - } else rec.kids = patchList(inst, node, olds, views, null, true) + } else rec.kids = patchList(inst, node, olds, views, null, canClear) // After the children, so a select's value wins over its options'. if ( prev.props !== view.props || diff --git a/packages/effect/test/ui/html/BrowserHost.test.ts b/packages/effect/test/ui/html/BrowserHost.test.ts index 9f7fa6b5faf..a2de823c7f6 100644 --- a/packages/effect/test/ui/html/BrowserHost.test.ts +++ b/packages/effect/test/ui/html/BrowserHost.test.ts @@ -4263,6 +4263,70 @@ describe("BrowserHost", { concurrent: false }, () => { assert.deepStrictEqual(root.model()?.seen, [1, 2]) }).pipe(Effect.scoped)) + it.effect("preserves Mount-managed DOM nodes when emptying or replacing rendered children", () => + Effect.gen(function*() { + let adopted: HTMLElement | undefined + const AttachAside = Mount.make( + "AttachAside", + { args: {} }, + (host: HTMLElement) => + Effect.acquireRelease( + Effect.sync(() => { + adopted = document.createElement("aside") + adopted.textContent = "external" + host.prepend(adopted) + return Component.noop + }), + () => Effect.sync(() => adopted?.remove()) + ) + ) + const Mode = Schema.Literals(["two", "empty", "replaced"]) + const Box = Html.component({ + name: "Box", + model: Mode, + message: Mode + }).make({ + init: () => "two" as const, + update: (_, next) => next, + view: (h, mode) => + h.div.onMount(AttachAside()).children( + mode === "two" + ? [h.span.key("a").children("a"), h.span.key("b").children("b")] + : mode === "replaced" + ? [h.p.key("c").children("c"), h.p.key("d").children("d")] + : [] + ) + }) + const app = yield* container + const root = yield* BrowserHost.mountEffect(Box, {}, app) + yield* tick + const host = app.querySelector("div")! + assert.isDefined(adopted) + assert.strictEqual(adopted!.parentNode, host) + assert.strictEqual(host.querySelectorAll("span").length, 2) + + // Emptying rendered children (N -> 0) removes only rendered children. + root.send("empty") + yield* tick + assert.strictEqual(adopted!.parentNode, host) + assert.isTrue(adopted!.isConnected) + assert.strictEqual(host.querySelectorAll("span").length, 0) + assert.deepStrictEqual(Array.from(host.children), [adopted!]) + + // Repopulating then replacing all rendered children (N -> M) keeps the Mount node connected. + root.send("two") + yield* tick + assert.strictEqual(adopted!.parentNode, host) + assert.strictEqual(host.querySelectorAll("span").length, 2) + + root.send("replaced") + yield* tick + assert.strictEqual(adopted!.parentNode, host) + assert.isTrue(adopted!.isConnected) + assert.strictEqual(host.querySelectorAll("span").length, 0) + assert.strictEqual(host.querySelectorAll("p").length, 2) + }).pipe(Effect.scoped)) + const Field = Html.ref("Field", "input") const Missing = Html.ref("Missing", "input") const Message = UiSchema.TaggedEnum({