mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
perf(ui): avoid per-frame DOM queries and retarget allocations in motion primitives
This commit is contained in:
parent
c8b31fc371
commit
4492fe5e07
2 changed files with 78 additions and 41 deletions
|
|
@ -60,7 +60,7 @@ export function steppedSlider(options: {
|
|||
onCommit?: (value: number) => void
|
||||
}) {
|
||||
let spec = options.spec
|
||||
validate(spec)
|
||||
let continuous = sliderStops(spec) > 10
|
||||
let target = sliderValue(spec, options.value)
|
||||
const spring = new Spring(target, { omega: 34, zeta: 0.86 })
|
||||
let pointer: number | undefined
|
||||
|
|
@ -68,6 +68,7 @@ export function steppedSlider(options: {
|
|||
let width = 1
|
||||
let remove: (() => void) | undefined
|
||||
let disposed = false
|
||||
let reduced = prefersReducedMotion()
|
||||
const el = options.el
|
||||
const originalTouchAction = el.style.touchAction
|
||||
el.style.touchAction = "pan-y"
|
||||
|
|
@ -76,10 +77,10 @@ export function steppedSlider(options: {
|
|||
if (!el.hasAttribute("tabindex")) el.tabIndex = 0
|
||||
|
||||
const paint = (dt: number) => {
|
||||
if (prefersReducedMotion()) spring.snap(target)
|
||||
if (reduced) spring.snap(target)
|
||||
const value = spring.step(dt)
|
||||
options.paint(value, sliderFraction(spec, value))
|
||||
const moving = !spring.resting
|
||||
const moving = spring.value !== spring.target || spring.velocity !== 0
|
||||
|
||||
if (!moving) remove = undefined
|
||||
|
||||
|
|
@ -90,7 +91,10 @@ export function steppedSlider(options: {
|
|||
if (!remove && !disposed) remove = frameLoop.add(paint)
|
||||
}
|
||||
|
||||
const unsubscribe = observeReducedMotion(wake)
|
||||
const unsubscribe = observeReducedMotion(() => {
|
||||
reduced = prefersReducedMotion()
|
||||
wake()
|
||||
})
|
||||
|
||||
const aria = () => {
|
||||
el.setAttribute("aria-valuemin", String(spec.min))
|
||||
|
|
@ -104,7 +108,7 @@ export function steppedSlider(options: {
|
|||
|
||||
spring.set(target)
|
||||
|
||||
if (snap || prefersReducedMotion()) spring.snap(target)
|
||||
if (snap || reduced) spring.snap(target)
|
||||
aria()
|
||||
|
||||
if (notify) options.onChange(target)
|
||||
|
|
@ -113,7 +117,7 @@ export function steppedSlider(options: {
|
|||
|
||||
const point = (event: PointerEvent) => {
|
||||
const value = spec.min + clamp((event.clientX - left) / width, 0, 1) * (spec.max - spec.min)
|
||||
set(value, sliderStops(spec) > 10, true)
|
||||
set(value, continuous, true)
|
||||
}
|
||||
|
||||
const down = (event: PointerEvent) => {
|
||||
|
|
@ -201,7 +205,7 @@ export function steppedSlider(options: {
|
|||
return {
|
||||
set: (value: number, options: { snap?: boolean } = {}) => set(value, options.snap),
|
||||
configure: (next: SliderSpec) => {
|
||||
validate(next)
|
||||
continuous = sliderStops(next) > 10
|
||||
spec = next
|
||||
set(target)
|
||||
},
|
||||
|
|
|
|||
|
|
@ -10,12 +10,13 @@ export function springStyle<K extends string>(
|
|||
apply: (el: HTMLElement, values: Record<K, number>) => void,
|
||||
options: SpringOptions & { snap?: () => boolean } = {},
|
||||
): void {
|
||||
const springs = new Map<K, Spring>()
|
||||
let entries: { key: K; spring: Spring }[] = []
|
||||
const keys: K[] = []
|
||||
const springs: Spring[] = []
|
||||
// SAFETY: every key is written by the target effect before the first paint reads values.
|
||||
const values = {} as Record<K, number>
|
||||
let element: HTMLElement | undefined
|
||||
let remove: (() => void) | undefined
|
||||
let reduced = prefersReducedMotion()
|
||||
|
||||
const paint = (dt: number) => {
|
||||
if (!element) {
|
||||
|
|
@ -25,12 +26,12 @@ export function springStyle<K extends string>(
|
|||
}
|
||||
|
||||
let moving = false
|
||||
const reduced = prefersReducedMotion() || Boolean(options.snap?.())
|
||||
const snap = reduced || Boolean(options.snap?.())
|
||||
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
const { key, spring } = entries[i]!
|
||||
values[key] = reduced ? spring.snap(spring.target).value : spring.step(dt)
|
||||
moving = moving || !spring.resting
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const spring = springs[i]!
|
||||
values[keys[i]!] = snap ? spring.snap(spring.target).value : spring.step(dt)
|
||||
moving = moving || spring.value !== spring.target || spring.velocity !== 0
|
||||
}
|
||||
|
||||
apply(element, values)
|
||||
|
|
@ -44,35 +45,45 @@ export function springStyle<K extends string>(
|
|||
if (!remove && element) remove = frameLoop.add(paint)
|
||||
}
|
||||
|
||||
const unsubscribe = observeReducedMotion(wake)
|
||||
const unsubscribe = observeReducedMotion(() => {
|
||||
reduced = prefersReducedMotion()
|
||||
wake()
|
||||
})
|
||||
|
||||
createEffect(() => {
|
||||
const el = ref()
|
||||
const next = targets()
|
||||
const snap = options.snap?.() || prefersReducedMotion() || el !== element
|
||||
const snap = options.snap?.() || reduced || el !== element
|
||||
element = el
|
||||
|
||||
for (const key of springs.keys()) {
|
||||
for (let i = keys.length - 1; i >= 0; i--) {
|
||||
const key = keys[i]!
|
||||
|
||||
if (!(key in next)) {
|
||||
springs.delete(key)
|
||||
keys.splice(i, 1)
|
||||
springs.splice(i, 1)
|
||||
delete values[key]
|
||||
}
|
||||
}
|
||||
|
||||
// SAFETY: next is the Record<K, number> returned by targets().
|
||||
for (const key of Object.keys(next) as K[]) {
|
||||
let spring = springs.get(key)
|
||||
|
||||
if (!spring) {
|
||||
spring = new Spring(next[key], options)
|
||||
springs.set(key, spring)
|
||||
continue
|
||||
}
|
||||
|
||||
spring.set(next[key])
|
||||
const target = next[key]
|
||||
const spring = springs[i]!
|
||||
spring.set(target)
|
||||
|
||||
if (snap) spring.snap(next[key])
|
||||
if (snap) spring.snap(target)
|
||||
}
|
||||
|
||||
for (const key in next) {
|
||||
if (!Object.hasOwn(next, key) || keys.includes(key)) continue
|
||||
const target = next[key]
|
||||
const spring = new Spring(target, options)
|
||||
|
||||
if (snap) spring.snap(target)
|
||||
keys.push(key)
|
||||
springs.push(spring)
|
||||
values[key] = target
|
||||
}
|
||||
|
||||
entries = Array.from(springs, ([key, spring]) => ({ key, spring }))
|
||||
// The write phase is shared with all measuring primitives, even on first run.
|
||||
wake()
|
||||
})
|
||||
|
|
@ -98,6 +109,7 @@ export function slidingHighlight(options: {
|
|||
const y = new Spring(0, { omega: 30, zeta: 0.88 })
|
||||
const w = new Spring(0, { omega: 30, zeta: 0.88 })
|
||||
const h = new Spring(0, { omega: 30, zeta: 0.88 })
|
||||
const box = { x: 0, y: 0, width: 0, height: 0 }
|
||||
let first = true
|
||||
createEffect(() => {
|
||||
const container = options.container()
|
||||
|
|
@ -108,6 +120,7 @@ export function slidingHighlight(options: {
|
|||
if (!container || !highlight || !selected) return
|
||||
let until = performance.now() + 420
|
||||
let remove: (() => void) | undefined
|
||||
let reduced = prefersReducedMotion()
|
||||
|
||||
const read = () => {
|
||||
if (performance.now() > until && !first) return
|
||||
|
|
@ -115,11 +128,10 @@ export function slidingHighlight(options: {
|
|||
// Children must share the container as offset parent (position: relative).
|
||||
if (options.spring && (selected.offsetLeft !== x.target || selected.offsetTop !== y.target)) {
|
||||
const curve = options.spring(Math.hypot(selected.offsetLeft - x.value, selected.offsetTop - y.value))
|
||||
|
||||
for (const spring of [x, y, w, h]) {
|
||||
spring.omega = (2 * Math.PI) / curve.duration
|
||||
spring.zeta = 1 - curve.bounce
|
||||
}
|
||||
const omega = (2 * Math.PI) / curve.duration
|
||||
const zeta = 1 - curve.bounce
|
||||
x.omega = y.omega = w.omega = h.omega = omega
|
||||
x.zeta = y.zeta = w.zeta = h.zeta = zeta
|
||||
}
|
||||
|
||||
x.set(selected.offsetLeft)
|
||||
|
|
@ -129,7 +141,7 @@ export function slidingHighlight(options: {
|
|||
}
|
||||
|
||||
const write = (dt: number) => {
|
||||
const snap = first || prefersReducedMotion() || Boolean(options.snap?.())
|
||||
const snap = first || reduced || Boolean(options.snap?.())
|
||||
|
||||
if (snap) {
|
||||
x.snap(x.target)
|
||||
|
|
@ -148,7 +160,15 @@ export function slidingHighlight(options: {
|
|||
if (axis !== "x") highlight.style.height = `${h.value}px`
|
||||
|
||||
if (axis !== "y") highlight.style.width = `${w.value}px`
|
||||
options.paint?.({ x: x.value, y: y.value, width: w.value, height: h.value })
|
||||
|
||||
if (options.paint) {
|
||||
box.x = x.value
|
||||
box.y = y.value
|
||||
box.width = w.value
|
||||
box.height = h.value
|
||||
options.paint(box)
|
||||
}
|
||||
|
||||
const moving = !snap && (performance.now() < until || !x.resting || !y.resting || !w.resting || !h.resting)
|
||||
|
||||
if (!moving) remove = undefined
|
||||
|
|
@ -166,7 +186,12 @@ export function slidingHighlight(options: {
|
|||
const observer = typeof ResizeObserver === "undefined" ? undefined : new ResizeObserver(wake)
|
||||
observer?.observe(container)
|
||||
observer?.observe(selected)
|
||||
const unsubscribe = observeReducedMotion(wake)
|
||||
|
||||
const unsubscribe = observeReducedMotion(() => {
|
||||
reduced = prefersReducedMotion()
|
||||
wake()
|
||||
})
|
||||
|
||||
onCleanup(() => {
|
||||
remove?.()
|
||||
observer?.disconnect()
|
||||
|
|
@ -221,6 +246,7 @@ export function widthMorph(options: {
|
|||
el.append(clone)
|
||||
let measured = false
|
||||
let remove: (() => void) | undefined
|
||||
let reduced = prefersReducedMotion()
|
||||
|
||||
const read = () => {
|
||||
if (measured) return
|
||||
|
|
@ -229,7 +255,7 @@ export function widthMorph(options: {
|
|||
}
|
||||
|
||||
const write = (dt: number) => {
|
||||
if (first || prefersReducedMotion()) spring.snap(spring.target)
|
||||
if (first || reduced) spring.snap(spring.target)
|
||||
first = false
|
||||
el.style.width = `${spring.step(dt)}px`
|
||||
clone.remove()
|
||||
|
|
@ -243,6 +269,8 @@ export function widthMorph(options: {
|
|||
remove = frameLoop.addMeasured(read, write)
|
||||
|
||||
const unsubscribe = observeReducedMotion(() => {
|
||||
reduced = prefersReducedMotion()
|
||||
|
||||
if (!remove) remove = frameLoop.addMeasured(read, write)
|
||||
})
|
||||
|
||||
|
|
@ -263,9 +291,14 @@ export function numberTween(options: {
|
|||
zeta?: number
|
||||
snap?: () => boolean
|
||||
}): void {
|
||||
const target = { value: 0 }
|
||||
springStyle(
|
||||
options.ref,
|
||||
() => ({ value: options.value() }),
|
||||
() => {
|
||||
target.value = options.value()
|
||||
|
||||
return target
|
||||
},
|
||||
(el, numbers) => {
|
||||
const text = options.format?.(numbers.value) ?? String(Math.round(numbers.value))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue