mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
refactor(ui): use one animation library (#59025)
This commit is contained in:
committed by
GitHub
Unverified
parent
3e4463a8a3
commit
a4eb2f89a5
@@ -657,6 +657,7 @@
|
||||
"name": "@opencode/session-ui",
|
||||
"version": "2.1.4",
|
||||
"dependencies": {
|
||||
"@kitlangton/solid-motion": "0.2.2",
|
||||
"@kobalte/core": "catalog:",
|
||||
"@opencode/client": "workspace:*",
|
||||
"@opencode/ui": "workspace:*",
|
||||
@@ -671,7 +672,6 @@
|
||||
"dompurify": "3.4.14",
|
||||
"effect": "catalog:",
|
||||
"marked": "catalog:",
|
||||
"motion": "12.34.5",
|
||||
"pele": "1.7.0",
|
||||
"remend": "catalog:",
|
||||
"solid-js": "catalog:",
|
||||
@@ -856,7 +856,6 @@
|
||||
"fuzzysort": "catalog:",
|
||||
"katex": "0.16.47",
|
||||
"marked": "catalog:",
|
||||
"motion": "12.34.5",
|
||||
"solid-list": "catalog:",
|
||||
"solid-sonner": "catalog:",
|
||||
},
|
||||
@@ -3648,8 +3647,6 @@
|
||||
|
||||
"formdata-polyfill": ["formdata-polyfill@4.0.10", "", { "dependencies": { "fetch-blob": "^3.1.2" } }, "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g=="],
|
||||
|
||||
"framer-motion": ["framer-motion@12.43.0", "", { "dependencies": { "motion-dom": "^12.43.0", "motion-utils": "^12.39.0", "tslib": "^2.4.0" }, "peerDependencies": { "@emotion/is-prop-valid": "*", "react": "^18.0.0 || ^19.0.0", "react-dom": "^18.0.0 || ^19.0.0" }, "optionalPeers": ["@emotion/is-prop-valid", "react", "react-dom"] }, "sha512-1eaL3RvR/kAlbG7UYcpMptEyzPoENO0c6w7ZnB3/hh2vSAz/6uGAFn6fdoqTBguNstf3MsFhJHsD/0DHiclG+g=="],
|
||||
|
||||
"fs-extra": ["fs-extra@10.1.0", "", { "dependencies": { "graceful-fs": "^4.2.0", "jsonfile": "^6.0.1", "universalify": "^2.0.0" } }, "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ=="],
|
||||
|
||||
"fs-minipass": ["fs-minipass@3.0.3", "", { "dependencies": { "minipass": "^7.0.3" } }, "sha512-XUBA9XClHbnJWSfBzjkm6RvPsyg3sryZt06BEQoXcF7EK/xpGaQYJgQKDJSUH5SGZ76Y7pFx1QBnXz09rU5Fbw=="],
|
||||
@@ -4274,8 +4271,6 @@
|
||||
|
||||
"module-details-from-path": ["module-details-from-path@1.0.4", "", {}, "sha512-EGWKgxALGMgzvxYF1UyGTy0HXX/2vHLkw6+NvDKW2jypWbHpjQuj4UMcqQWXHERJhVGKikolT06G3bcKe4fi7w=="],
|
||||
|
||||
"motion": ["motion@12.34.5", "", { "dependencies": { "framer-motion": "^12.34.5", "tslib": "^2.4.0" }, "peerDependencies": { "@emotion/is-prop-valid": "*", "react": "^18.0.0 || ^19.0.0", "react-dom": "^18.0.0 || ^19.0.0" }, "optionalPeers": ["@emotion/is-prop-valid", "react", "react-dom"] }, "sha512-N06NLJ9IeBHeielRqIvYvjPfXuRdyTxa+9++BgpGa+hY2D7TcMkI6QzV3jaRuv0aZRXgMa7cPy9YcBUBisPzAQ=="],
|
||||
|
||||
"motion-dom": ["motion-dom@14.0.0", "", { "dependencies": { "motion-utils": "14.0.0" } }, "sha512-aU3ApXo1yTpeUi3JU+jbDk2TELjxPE3MbBMUP7nZHlARb6RweJK+ojmLjpuB0YsY6zhbBl1YWGo8LMsI1/Llqw=="],
|
||||
|
||||
"motion-utils": ["motion-utils@14.0.0", "", {}, "sha512-jBsyjVxTTRZNeuvd/v+btVXr90LQkYl4HhRu9T2ARc+PMnvkmjsjRVeAg4Qm10b6TlTw+kykWI8gu/6LuvpLSQ=="],
|
||||
@@ -5760,10 +5755,6 @@
|
||||
|
||||
"form-data/mime-types": ["mime-types@2.1.35", "", { "dependencies": { "mime-db": "1.52.0" } }, "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw=="],
|
||||
|
||||
"framer-motion/motion-dom": ["motion-dom@12.43.0", "", { "dependencies": { "motion-utils": "^12.39.0" } }, "sha512-azKON4d9S65PEoFUiQTMTgPheEmzf2QngdRc50AKfJp9Q9mmcBVw22c8eMq9k8kxOFHdL7+WZY7N/5F/lwiDag=="],
|
||||
|
||||
"framer-motion/motion-utils": ["motion-utils@12.39.0", "", {}, "sha512-8nadJAJjTtqRkmRF36FoJTrywK9nnFmnPwnSMyxaOCU7GDjN9RTMJIxx9De8ErM+vpPhMccr/6fo5WciyQLnMQ=="],
|
||||
|
||||
"gaxios/node-fetch": ["node-fetch@3.3.2", "", { "dependencies": { "data-uri-to-buffer": "^4.0.0", "fetch-blob": "^3.1.4", "formdata-polyfill": "^4.0.10" } }, "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA=="],
|
||||
|
||||
"gcp-metadata/gaxios": ["gaxios@7.1.3", "", { "dependencies": { "extend": "^3.0.2", "https-proxy-agent": "^7.0.1", "node-fetch": "^3.3.2", "rimraf": "^5.0.1" } }, "sha512-YGGyuEdVIjqxkxVH1pUTMY/XtmmsApXrCVv5EU25iX6inEPbV+VakJfLealkBtJN69AQmh1eGOdCl9Sm1UP6XQ=="],
|
||||
|
||||
@@ -1,6 +1,60 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { useSpring } from "@opencode/ui/motion-spring"
|
||||
import { createRoot, createSignal } from "solid-js"
|
||||
import { createEffect, createRoot, createSignal } from "solid-js"
|
||||
|
||||
// Records every value the spring emits and resolves once it reaches `settled`.
|
||||
function watch(progress: () => number, settled: number) {
|
||||
const seen: number[] = []
|
||||
const { promise, resolve } = Promise.withResolvers<void>()
|
||||
|
||||
createEffect(() => {
|
||||
const value = progress()
|
||||
|
||||
seen.push(value)
|
||||
|
||||
if (value === settled) resolve()
|
||||
})
|
||||
|
||||
return { seen, settled: promise }
|
||||
}
|
||||
|
||||
test("springs progress to its target", async () => {
|
||||
const state = createRoot((dispose) => {
|
||||
const [target, setTarget] = createSignal(0)
|
||||
const progress = useSpring(target, { visualDuration: 0.3, bounce: 0 })
|
||||
|
||||
return { dispose, progress, setTarget, ...watch(progress, 1) }
|
||||
})
|
||||
|
||||
state.setTarget(1)
|
||||
await Promise.race([state.settled, Bun.sleep(2000)])
|
||||
|
||||
expect(state.progress()).toBe(1)
|
||||
expect(state.seen.some((value) => value > 0 && value < 1)).toBe(true)
|
||||
expect(state.seen.every((value) => value <= 1)).toBe(true)
|
||||
state.dispose()
|
||||
})
|
||||
|
||||
test("new spring options replace the running spring", async () => {
|
||||
const state = createRoot((dispose) => {
|
||||
const [target, setTarget] = createSignal(0)
|
||||
const [duration, setDuration] = createSignal(4)
|
||||
const progress = useSpring(target, () => ({ visualDuration: duration(), bounce: 0 }))
|
||||
|
||||
return { dispose, progress, setTarget, setDuration, ...watch(progress, 1) }
|
||||
})
|
||||
|
||||
state.setTarget(1)
|
||||
await Bun.sleep(100)
|
||||
expect(state.progress()).toBeLessThan(0.5)
|
||||
|
||||
// The old four-second spring would still be far from its target after a second.
|
||||
state.setDuration(0.1)
|
||||
await Promise.race([state.settled, Bun.sleep(1000)])
|
||||
|
||||
expect(state.progress()).toBe(1)
|
||||
state.dispose()
|
||||
})
|
||||
|
||||
test("snaps spring progress when the session changes", async () => {
|
||||
const state = createRoot((dispose) => {
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
"vite-plugin-solid": "catalog:"
|
||||
},
|
||||
"dependencies": {
|
||||
"@kitlangton/solid-motion": "0.2.2",
|
||||
"@kobalte/core": "catalog:",
|
||||
"@opencode/client": "workspace:*",
|
||||
"@opencode/ui": "workspace:*",
|
||||
@@ -77,7 +78,6 @@
|
||||
"effect": "catalog:",
|
||||
"dompurify": "3.4.14",
|
||||
"marked": "catalog:",
|
||||
"motion": "12.34.5",
|
||||
"pele": "1.7.0",
|
||||
"remend": "catalog:",
|
||||
"solid-js": "catalog:"
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
type Accessor,
|
||||
type JSX,
|
||||
} from "solid-js"
|
||||
import { animate, type AnimationPlaybackControls } from "motion"
|
||||
import { Option, Schema } from "effect"
|
||||
import { useI18n } from "@opencode/ui/context/i18n"
|
||||
import { createStore } from "solid-js/store"
|
||||
import { Collapsible } from "@opencode/ui/collapsible"
|
||||
@@ -28,13 +28,8 @@ export type TriggerTitle = {
|
||||
action?: JSX.Element
|
||||
}
|
||||
|
||||
const isTriggerTitle = (val: unknown): val is TriggerTitle => {
|
||||
if (typeof val !== "object" || val === null) return false
|
||||
|
||||
if (typeof Node !== "undefined" && val instanceof Node) return false
|
||||
|
||||
return "title" in val && typeof val.title === "string"
|
||||
}
|
||||
const isTriggerTitle = (value: TriggerTitle | JSX.Element): value is TriggerTitle =>
|
||||
value instanceof Object && !("nodeType" in value) && "title" in value
|
||||
|
||||
export interface BasicToolProps {
|
||||
icon: IconProps["name"]
|
||||
@@ -51,7 +46,6 @@ export interface BasicToolProps {
|
||||
allowOpenWhilePending?: boolean
|
||||
defer?: boolean
|
||||
locked?: boolean
|
||||
animated?: boolean
|
||||
rail?: boolean
|
||||
onSubtitleClick?: () => void
|
||||
onTriggerClick?: JSX.EventHandlerUnion<HTMLElement, MouseEvent>
|
||||
@@ -62,8 +56,6 @@ export interface BasicToolProps {
|
||||
compact?: boolean
|
||||
}
|
||||
|
||||
const SPRING = { type: "spring" as const, visualDuration: 0.35, bounce: 0 }
|
||||
|
||||
const deferredMounts: Array<{ active: boolean; fn: () => void }> = []
|
||||
|
||||
let deferredFrame: number | undefined
|
||||
@@ -122,7 +114,7 @@ export function BasicTool(props: BasicToolProps) {
|
||||
const triggerContent = createMemo(() => {
|
||||
const value = props.trigger
|
||||
|
||||
return typeof value === "function" ? value(open) : value
|
||||
return value instanceof Function ? value(open) : value
|
||||
})
|
||||
|
||||
const triggerTitle = createMemo(() => {
|
||||
@@ -131,6 +123,12 @@ export function BasicTool(props: BasicToolProps) {
|
||||
return isTriggerTitle(value) ? value : undefined
|
||||
})
|
||||
|
||||
const triggerElement = () => {
|
||||
const value = triggerContent()
|
||||
|
||||
return isTriggerTitle(value) ? undefined : value
|
||||
}
|
||||
|
||||
let cancelReady: (() => void) | undefined
|
||||
|
||||
const cancel = () => {
|
||||
@@ -185,39 +183,6 @@ export function BasicTool(props: BasicToolProps) {
|
||||
),
|
||||
)
|
||||
|
||||
// Animated height for collapsible open/close
|
||||
let contentRef: HTMLDivElement | undefined
|
||||
let heightAnim: AnimationPlaybackControls | undefined
|
||||
const initialOpen = open()
|
||||
|
||||
createEffect(
|
||||
on(
|
||||
open,
|
||||
(isOpen) => {
|
||||
if (!props.animated || !contentRef) return
|
||||
heightAnim?.stop()
|
||||
|
||||
if (isOpen) {
|
||||
contentRef.style.overflow = "hidden"
|
||||
heightAnim = animate(contentRef, { height: "auto" }, SPRING)
|
||||
void heightAnim.finished.then(() => {
|
||||
if (!contentRef || !open()) return
|
||||
contentRef.style.overflow = "visible"
|
||||
contentRef.style.height = "auto"
|
||||
})
|
||||
} else {
|
||||
contentRef.style.overflow = "hidden"
|
||||
heightAnim = animate(contentRef, { height: "0px" }, SPRING)
|
||||
}
|
||||
},
|
||||
{ defer: true },
|
||||
),
|
||||
)
|
||||
|
||||
onCleanup(() => {
|
||||
heightAnim?.stop()
|
||||
})
|
||||
|
||||
const handleOpenChange = (value: boolean) => {
|
||||
if (pending() && !props.allowOpenWhilePending) return
|
||||
|
||||
@@ -286,7 +251,7 @@ export function BasicTool(props: BasicToolProps) {
|
||||
</div>
|
||||
)}
|
||||
</Match>
|
||||
<Match when={true}>{triggerContent() as JSX.Element}</Match>
|
||||
<Match when={true}>{triggerElement()}</Match>
|
||||
</Switch>
|
||||
</div>
|
||||
</div>
|
||||
@@ -340,20 +305,7 @@ export function BasicTool(props: BasicToolProps) {
|
||||
{trigger()}
|
||||
</Collapsible.Trigger>
|
||||
</Show>
|
||||
<Show when={props.animated && hasChildren() && !props.hideDetails}>
|
||||
<div
|
||||
ref={contentRef}
|
||||
data-slot="collapsible-content"
|
||||
data-animated
|
||||
style={{
|
||||
height: initialOpen ? "auto" : "0px",
|
||||
overflow: initialOpen ? "visible" : "hidden",
|
||||
}}
|
||||
>
|
||||
<Show when={!props.defer || ready()}>{props.children}</Show>
|
||||
</div>
|
||||
</Show>
|
||||
<Show when={!props.animated && hasChildren() && !props.hideDetails}>
|
||||
<Show when={hasChildren() && !props.hideDetails}>
|
||||
<Show
|
||||
when={props.locked && open() && !props.defer}
|
||||
fallback={
|
||||
@@ -371,36 +323,30 @@ export function BasicTool(props: BasicToolProps) {
|
||||
)
|
||||
}
|
||||
|
||||
function label(input: Record<string, unknown> | undefined) {
|
||||
const keys = ["description", "query", "url", "path", "pattern", "name"]
|
||||
// SAFETY: tool input is an open per-tool record. GenericTool shows arbitrary keys and decodes each value it reads.
|
||||
// oxlint-disable-next-line anti-slop/no-unsafe-dictionary-type -- see SAFETY above
|
||||
type GenericToolInput = Record<string, unknown>
|
||||
|
||||
return keys.map((key) => input?.[key]).find((value): value is string => typeof value === "string" && value.length > 0)
|
||||
const labelKeys = ["description", "query", "url", "path", "pattern", "name"]
|
||||
|
||||
const labelValue = Schema.decodeUnknownOption(Schema.NonEmptyString)
|
||||
|
||||
const argValue = Schema.decodeUnknownOption(Schema.Union([Schema.String, Schema.Number, Schema.Boolean]))
|
||||
|
||||
function label(input: GenericToolInput | undefined) {
|
||||
return labelKeys.flatMap((key) => Option.toArray(labelValue(input?.[key])))[0]
|
||||
}
|
||||
|
||||
function args(input: Record<string, unknown> | undefined) {
|
||||
function args(input: GenericToolInput | undefined) {
|
||||
if (!input) return []
|
||||
const skip = new Set(["description", "query", "url", "path", "pattern", "name"])
|
||||
|
||||
return Object.entries(input)
|
||||
.filter(([key]) => !skip.has(key))
|
||||
.flatMap(([key, value]) => {
|
||||
if (typeof value === "string") return [`${key}=${value}`]
|
||||
|
||||
if (typeof value === "number") return [`${key}=${value}`]
|
||||
|
||||
if (typeof value === "boolean") return [`${key}=${value}`]
|
||||
|
||||
return []
|
||||
})
|
||||
.filter(([key]) => !labelKeys.includes(key))
|
||||
.flatMap(([key, value]) => Option.toArray(argValue(value)).map((arg) => `${key}=${arg}`))
|
||||
.slice(0, 3)
|
||||
}
|
||||
|
||||
export function GenericTool(props: {
|
||||
tool: string
|
||||
status?: string
|
||||
hideDetails?: boolean
|
||||
input?: Record<string, unknown>
|
||||
}) {
|
||||
export function GenericTool(props: { tool: string; status?: string; hideDetails?: boolean; input?: GenericToolInput }) {
|
||||
const i18n = useI18n()
|
||||
|
||||
return (
|
||||
|
||||
@@ -40,7 +40,7 @@ import { IconButton } from "@opencode/ui/icon-button"
|
||||
import { TextShimmer } from "@opencode/ui/text-shimmer"
|
||||
import { AnimatedNumber } from "@opencode/ui/animated-number"
|
||||
import { changedFileDiff, patchFileGroups } from "../components/apply-patch-file"
|
||||
import { animate } from "motion"
|
||||
import { animate } from "@kitlangton/solid-motion"
|
||||
import { SessionProgressIndicatorV2 } from "../v2/components/session-progress-indicator-v2"
|
||||
import type {
|
||||
FileDiffInfo,
|
||||
@@ -73,7 +73,12 @@ function ShellSubmessage(props: { text: string; animate?: boolean }) {
|
||||
}
|
||||
|
||||
if (valueRef) {
|
||||
animate(valueRef, { opacity: 1, filter: "blur(0px)" }, { duration: 0.32, ease: [0.16, 1, 0.3, 1] })
|
||||
valueRef.animate?.(
|
||||
{ opacity: [0, 1], filter: ["blur(2px)", "blur(0px)"] },
|
||||
{ duration: 320, easing: "cubic-bezier(0.16, 1, 0.3, 1)" },
|
||||
)
|
||||
valueRef.style.opacity = "1"
|
||||
valueRef.style.filter = "blur(0px)"
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -120,7 +120,6 @@
|
||||
"fuzzysort": "catalog:",
|
||||
"katex": "0.16.47",
|
||||
"marked": "catalog:",
|
||||
"motion": "12.34.5",
|
||||
"solid-list": "catalog:",
|
||||
"solid-sonner": "catalog:"
|
||||
},
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { attachSpring, motionValue } from "motion"
|
||||
import type { SpringOptions } from "motion"
|
||||
import { attachSpring, motionValue, type SpringOptions } from "@kitlangton/solid-motion"
|
||||
import { createComputed, createEffect, createSignal, onCleanup } from "solid-js"
|
||||
|
||||
type Opt = Partial<Pick<SpringOptions, "visualDuration" | "bounce" | "stiffness" | "damping" | "mass" | "velocity">>
|
||||
@@ -12,8 +11,8 @@ const eq = (a: Opt | undefined, b: Opt | undefined) =>
|
||||
a?.mass === b?.mass &&
|
||||
a?.velocity === b?.velocity
|
||||
|
||||
export function useSpring(target: () => number, options?: Opt | (() => Opt), snapKey?: () => unknown) {
|
||||
const read = () => (typeof options === "function" ? options() : options)
|
||||
export function useSpring<K>(target: () => number, options?: Opt | (() => Opt), snapKey?: () => K) {
|
||||
const read = () => (options instanceof Function ? options() : options)
|
||||
const [value, setValue] = createSignal(target())
|
||||
const source = motionValue(value())
|
||||
const spring = motionValue(value())
|
||||
@@ -48,7 +47,10 @@ export function useSpring(target: () => number, options?: Opt | (() => Opt), sna
|
||||
if (eq(config, next)) return
|
||||
config = next
|
||||
stop()
|
||||
// attachSpring's cleanup only unsubscribes from the source; the running animation keeps ticking until stopped.
|
||||
spring.stop()
|
||||
stop = attachSpring(spring, source, next)
|
||||
spring.set(source.get())
|
||||
setValue(spring.get())
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user