Skip to content

Commit 50ddde9

Browse files
authored
refactor(app): replace solid-presence with a local createPresence (#59019)
1 parent 5d8ebf3 commit 50ddde9

7 files changed

Lines changed: 237 additions & 6 deletions

File tree

‎bun.lock‎

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/app/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
"qr-scanner": "1.4.2",
8989
"remeda": "catalog:",
9090
"solid-js": "catalog:",
91-
"solid-presence": "0.2.0",
9291
"tailwindcss": "4.3.3"
9392
}
9493
}

‎packages/app/src/new-session/view.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Tooltip } from "@opencode/ui/tooltip"
33
import { Icon } from "@opencode/ui/icon"
44
import { Show, createMemo, createSignal } from "solid-js"
55
import { Schema } from "effect"
6-
import createPresence from "solid-presence"
6+
import { createPresence } from "@/runtime/presence"
77
import { Composer } from "@/composer/composer"
88
import { ComposerDropzone } from "@/composer/dropzone"
99
import type { ComposerModel } from "@/composer/model"

‎packages/app/src/runtime/animated-presence.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createMemo, createSignal, onCleanup, type Accessor } from "solid-js"
2-
import createPresence from "solid-presence"
2+
import { createPresence } from "./presence"
33

44
export function createAnimatedPresence<T>(
55
value: Accessor<T | undefined>,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { createEffect, createMemo, createSignal, onCleanup, untrack, type Accessor } from "solid-js"
2+
3+
// Keeps an element mounted while its CSS exit animation runs. Reduced motion needs no special case: when the styles
4+
// drop the animation, the computed name is "none" and the element hides at once.
5+
export function createPresence(props: { show: Accessor<boolean>; element: Accessor<HTMLElement | null> }) {
6+
const styles = createMemo(() => {
7+
const element = props.element()
8+
9+
if (!element) return
10+
11+
return getComputedStyle(element)
12+
})
13+
14+
const animationName = () => styles()?.animationName ?? "none"
15+
const [state, setState] = createSignal<"present" | "hiding" | "hidden">(props.show() ? "present" : "hidden")
16+
// The last animation the element started. A different name when hiding means an exit animation is running.
17+
let started = "none"
18+
19+
createEffect((previous) => {
20+
const show = props.show()
21+
22+
untrack(() => {
23+
if (previous === show) return
24+
25+
if (show) {
26+
// Forget the previous exit animation so the next hide runs it again.
27+
started = "none"
28+
29+
return setState("present")
30+
}
31+
32+
const current = animationName()
33+
34+
if (current === "none" || styles()?.display === "none") return setState("hidden")
35+
36+
setState(started === current ? "hidden" : "hiding")
37+
})
38+
39+
return show
40+
}, props.show())
41+
42+
createEffect(() => {
43+
const element = props.element()
44+
45+
if (!element) return
46+
47+
const start = (event: AnimationEvent) => {
48+
if (event.target === element) started = animationName()
49+
}
50+
51+
const end = (event: AnimationEvent) => {
52+
if (event.target === element && animationName().includes(event.animationName) && state() === "hiding")
53+
setState("hidden")
54+
}
55+
56+
element.addEventListener("animationstart", start)
57+
element.addEventListener("animationcancel", end)
58+
element.addEventListener("animationend", end)
59+
60+
onCleanup(() => {
61+
element.removeEventListener("animationstart", start)
62+
element.removeEventListener("animationcancel", end)
63+
element.removeEventListener("animationend", end)
64+
})
65+
})
66+
67+
return { present: () => state() !== "hidden", state }
68+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import { afterEach, describe, expect, test } from "bun:test"
2+
import { createRoot, createSignal } from "solid-js"
3+
import { createPresence } from "../src/runtime/presence"
4+
5+
const cleanups: Array<() => void> = []
6+
7+
afterEach(() => {
8+
cleanups.splice(0).forEach((cleanup) => cleanup())
9+
})
10+
11+
function setup(input: { show: boolean; element?: boolean }) {
12+
const element = document.createElement("div")
13+
// Browsers compute "none" without an animation; happy-dom computes an empty string.
14+
element.style.animationName = "none"
15+
document.body.append(element)
16+
17+
return createRoot((dispose) => {
18+
const [show, setShow] = createSignal(input.show)
19+
const [target, setTarget] = createSignal<HTMLElement | null>(input.element === false ? null : element)
20+
const presence = createPresence({ show, element: target })
21+
22+
cleanups.push(() => {
23+
dispose()
24+
element.remove()
25+
})
26+
27+
return { element, presence, setShow, setTarget }
28+
})
29+
}
30+
31+
function animate(element: HTMLElement, name: string) {
32+
element.style.animationName = name
33+
element.dispatchEvent(new AnimationEvent("animationstart", { animationName: name }))
34+
}
35+
36+
describe("createPresence", () => {
37+
test("starts from the initial show value", () => {
38+
expect(setup({ show: true }).presence.state()).toBe("present")
39+
expect(setup({ show: false }).presence.state()).toBe("hidden")
40+
expect(setup({ show: false }).presence.present()).toBe(false)
41+
})
42+
43+
test("enters at once, with or without an enter animation", () => {
44+
const view = setup({ show: false })
45+
46+
view.setShow(true)
47+
expect(view.presence.state()).toBe("present")
48+
expect(view.presence.present()).toBe(true)
49+
})
50+
51+
test("hides at once without an exit animation, as under reduced motion", () => {
52+
const view = setup({ show: true })
53+
54+
view.setShow(false)
55+
expect(view.presence.state()).toBe("hidden")
56+
expect(view.presence.present()).toBe(false)
57+
})
58+
59+
test("stays present while the exit animation runs and hides when it ends", () => {
60+
const view = setup({ show: true })
61+
animate(view.element, "enter")
62+
63+
view.element.style.animationName = "exit"
64+
view.setShow(false)
65+
expect(view.presence.state()).toBe("hiding")
66+
expect(view.presence.present()).toBe(true)
67+
68+
view.element.dispatchEvent(new AnimationEvent("animationstart", { animationName: "exit" }))
69+
view.element.dispatchEvent(new AnimationEvent("animationend", { animationName: "enter" }))
70+
expect(view.presence.state()).toBe("hiding")
71+
72+
view.element.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit" }))
73+
expect(view.presence.state()).toBe("hidden")
74+
})
75+
76+
test("hides when the exit animation is cancelled", () => {
77+
const view = setup({ show: true })
78+
view.element.style.animationName = "exit"
79+
view.setShow(false)
80+
81+
view.element.dispatchEvent(new AnimationEvent("animationcancel", { animationName: "exit" }))
82+
expect(view.presence.state()).toBe("hidden")
83+
})
84+
85+
test("ignores animation events from children", () => {
86+
const view = setup({ show: true })
87+
const child = document.createElement("span")
88+
view.element.append(child)
89+
view.element.style.animationName = "exit"
90+
view.setShow(false)
91+
92+
child.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit", bubbles: true }))
93+
expect(view.presence.state()).toBe("hiding")
94+
})
95+
96+
test("hides at once when the running animation did not change", () => {
97+
const view = setup({ show: true })
98+
animate(view.element, "pulse")
99+
100+
view.setShow(false)
101+
expect(view.presence.state()).toBe("hidden")
102+
})
103+
104+
test("hides at once when the element is not displayed", () => {
105+
const view = setup({ show: true })
106+
view.element.style.animationName = "exit"
107+
view.element.style.display = "none"
108+
109+
view.setShow(false)
110+
expect(view.presence.state()).toBe("hidden")
111+
})
112+
113+
test("hides at once without an element", () => {
114+
const view = setup({ show: true, element: false })
115+
116+
view.setShow(false)
117+
expect(view.presence.state()).toBe("hidden")
118+
})
119+
120+
test("comes back when shown again during the exit animation", () => {
121+
const view = setup({ show: true })
122+
view.element.style.animationName = "exit"
123+
view.setShow(false)
124+
125+
view.element.style.animationName = "enter"
126+
view.setShow(true)
127+
expect(view.presence.state()).toBe("present")
128+
129+
view.element.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit" }))
130+
expect(view.presence.state()).toBe("present")
131+
})
132+
133+
test("runs an exit-only animation again on every hide", () => {
134+
const view = setup({ show: true })
135+
136+
const hide = () => {
137+
view.element.style.animationName = "exit"
138+
view.setShow(false)
139+
expect(view.presence.state()).toBe("hiding")
140+
view.element.dispatchEvent(new AnimationEvent("animationstart", { animationName: "exit" }))
141+
view.element.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit" }))
142+
expect(view.presence.state()).toBe("hidden")
143+
}
144+
145+
hide()
146+
view.element.style.animationName = "none"
147+
view.setShow(true)
148+
hide()
149+
})
150+
151+
test("stops listening to a replaced element", () => {
152+
const view = setup({ show: true })
153+
const previous = view.element
154+
const next = document.createElement("div")
155+
document.body.append(next)
156+
view.setTarget(next)
157+
next.style.animationName = "exit"
158+
view.setShow(false)
159+
160+
previous.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit" }))
161+
expect(view.presence.state()).toBe("hiding")
162+
163+
next.dispatchEvent(new AnimationEvent("animationend", { animationName: "exit" }))
164+
expect(view.presence.state()).toBe("hidden")
165+
next.remove()
166+
})
167+
})

‎packages/gui-extensions/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"ghostty-web": "github:anomalyco/ghostty-web#83c0a07b8628b748aed073b232cb4b52a6ca11c1",
4848
"remeda": "catalog:",
4949
"solid-js": "catalog:",
50-
"solid-presence": "0.2.0",
5150
"uqr": "0.1.3"
5251
},
5352
"devDependencies": {

0 commit comments

Comments
 (0)