Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/ui-mount-preserve-children.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 13 additions & 6 deletions packages/effect/src/ui/internal/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rec>): 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<Rec>,
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++) {
Expand Down Expand Up @@ -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<Rec> = new Array(views.length)
Expand All @@ -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]!
Expand All @@ -1940,7 +1947,7 @@ const patchElement = (
) {
patchElement(inst, old, old.v as View.Element<any, any>, 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 ||
Expand Down
64 changes: 64 additions & 0 deletions packages/effect/test/ui/html/BrowserHost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading