perf(ui): defer tool status width measurement (#26282)
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Show, createEffect, createMemo, on, onCleanup, onMount } from "solid-js"
|
import { Show, createEffect, createMemo, on, onCleanup } from "solid-js"
|
||||||
import { createStore } from "solid-js/store"
|
import { createStore } from "solid-js/store"
|
||||||
import { TextShimmer } from "./text-shimmer"
|
import { TextShimmer } from "./text-shimmer"
|
||||||
|
|
||||||
@@ -15,10 +15,8 @@ function common(active: string, done: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function contentWidth(el: HTMLSpanElement | undefined) {
|
function contentWidth(el: HTMLSpanElement | undefined) {
|
||||||
if (!el) return 0
|
if (!el) return
|
||||||
const range = document.createRange()
|
return `${Math.ceil(el.getBoundingClientRect().width)}px`
|
||||||
range.selectNodeContents(el)
|
|
||||||
return Math.ceil(range.getBoundingClientRect().width)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ToolStatusTitle(props: {
|
export function ToolStatusTitle(props: {
|
||||||
@@ -37,99 +35,99 @@ export function ToolStatusTitle(props: {
|
|||||||
const doneTail = createMemo(() => (suffix() ? split().done : props.doneText))
|
const doneTail = createMemo(() => (suffix() ? split().done : props.doneText))
|
||||||
|
|
||||||
const [state, setState] = createStore({
|
const [state, setState] = createStore({
|
||||||
width: "auto",
|
active: props.active,
|
||||||
ready: false,
|
animating: false,
|
||||||
|
width: undefined as string | undefined,
|
||||||
})
|
})
|
||||||
const width = () => state.width
|
const width = () => state.width
|
||||||
const ready = () => state.ready
|
const active = () => state.active
|
||||||
|
const animating = () => state.animating
|
||||||
let activeRef: HTMLSpanElement | undefined
|
let activeRef: HTMLSpanElement | undefined
|
||||||
let doneRef: HTMLSpanElement | undefined
|
let doneRef: HTMLSpanElement | undefined
|
||||||
|
let widthRef: HTMLSpanElement | undefined
|
||||||
let frame: number | undefined
|
let frame: number | undefined
|
||||||
let readyFrame: number | undefined
|
let finishTimer: ReturnType<typeof setTimeout> | undefined
|
||||||
|
|
||||||
const measure = () => {
|
|
||||||
const target = props.active ? activeRef : doneRef
|
|
||||||
const px = contentWidth(target)
|
|
||||||
if (px > 0) setState("width", `${px}px`)
|
|
||||||
}
|
|
||||||
|
|
||||||
const schedule = () => {
|
|
||||||
if (typeof requestAnimationFrame !== "function") {
|
|
||||||
measure()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (frame !== undefined) cancelAnimationFrame(frame)
|
|
||||||
frame = requestAnimationFrame(() => {
|
|
||||||
frame = undefined
|
|
||||||
measure()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const finish = () => {
|
const finish = () => {
|
||||||
if (typeof requestAnimationFrame !== "function") {
|
if (frame !== undefined) cancelAnimationFrame(frame)
|
||||||
setState("ready", true)
|
if (finishTimer !== undefined) clearTimeout(finishTimer)
|
||||||
|
frame = undefined
|
||||||
|
finishTimer = undefined
|
||||||
|
setState("animating", false)
|
||||||
|
setState("width", undefined)
|
||||||
|
}
|
||||||
|
|
||||||
|
const animate = () => {
|
||||||
|
const first = contentWidth(widthRef)
|
||||||
|
finish()
|
||||||
|
setState("animating", true)
|
||||||
|
setState("active", props.active)
|
||||||
|
const last = contentWidth(props.active ? activeRef : doneRef)
|
||||||
|
if (!first || !last) {
|
||||||
|
finish()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (readyFrame !== undefined) cancelAnimationFrame(readyFrame)
|
|
||||||
readyFrame = requestAnimationFrame(() => {
|
setState("width", first)
|
||||||
readyFrame = undefined
|
if (first === last) {
|
||||||
setState("ready", true)
|
finishTimer = setTimeout(finish, 600)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
frame = requestAnimationFrame(() => {
|
||||||
|
frame = undefined
|
||||||
|
setState("width", last)
|
||||||
|
finishTimer = setTimeout(finish, 600)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
createEffect(on([() => props.active, activeTail, doneTail, suffix], () => schedule()))
|
createEffect(on([() => props.active, activeTail, doneTail], () => animate(), { defer: true }))
|
||||||
|
|
||||||
onMount(() => {
|
|
||||||
measure()
|
|
||||||
const fonts = typeof document !== "undefined" ? document.fonts : undefined
|
|
||||||
if (!fonts) {
|
|
||||||
finish()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
void fonts.ready.finally(() => {
|
|
||||||
measure()
|
|
||||||
finish()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
onCleanup(() => {
|
onCleanup(() => {
|
||||||
if (frame !== undefined) cancelAnimationFrame(frame)
|
finish()
|
||||||
if (readyFrame !== undefined) cancelAnimationFrame(readyFrame)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<span
|
<span
|
||||||
data-component="tool-status-title"
|
data-component="tool-status-title"
|
||||||
data-active={props.active ? "true" : "false"}
|
data-active={active() ? "true" : "false"}
|
||||||
data-ready={ready() ? "true" : "false"}
|
data-ready={animating() ? "true" : "false"}
|
||||||
data-mode={suffix() ? "suffix" : "swap"}
|
data-mode={suffix() ? "suffix" : "swap"}
|
||||||
class={props.class}
|
class={props.class}
|
||||||
aria-label={props.active ? props.activeText : props.doneText}
|
aria-label={active() ? props.activeText : props.doneText}
|
||||||
>
|
>
|
||||||
<Show
|
<Show
|
||||||
when={suffix()}
|
when={suffix()}
|
||||||
fallback={
|
fallback={
|
||||||
<span data-slot="tool-status-swap" style={{ width: width() }}>
|
<span data-slot="tool-status-swap" ref={widthRef} style={{ width: width() }}>
|
||||||
<span data-slot="tool-status-active" ref={activeRef}>
|
<Show when={animating() || active()}>
|
||||||
<TextShimmer text={activeTail()} active={props.active} offset={0} />
|
<span data-slot="tool-status-active" ref={activeRef}>
|
||||||
</span>
|
<TextShimmer text={activeTail()} active={active()} offset={0} />
|
||||||
<span data-slot="tool-status-done" ref={doneRef}>
|
</span>
|
||||||
<TextShimmer text={doneTail()} active={false} offset={0} />
|
</Show>
|
||||||
</span>
|
<Show when={animating() || !active()}>
|
||||||
|
<span data-slot="tool-status-done" ref={doneRef}>
|
||||||
|
<TextShimmer text={doneTail()} active={false} offset={0} />
|
||||||
|
</span>
|
||||||
|
</Show>
|
||||||
</span>
|
</span>
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<span data-slot="tool-status-suffix">
|
<span data-slot="tool-status-suffix">
|
||||||
<span data-slot="tool-status-prefix">
|
<span data-slot="tool-status-prefix">
|
||||||
<TextShimmer text={split().prefix} active={props.active} offset={0} />
|
<TextShimmer text={split().prefix} active={active()} offset={0} />
|
||||||
</span>
|
</span>
|
||||||
<span data-slot="tool-status-tail" style={{ width: width() }}>
|
<span data-slot="tool-status-tail" ref={widthRef} style={{ width: width() }}>
|
||||||
<span data-slot="tool-status-active" ref={activeRef}>
|
<Show when={animating() || active()}>
|
||||||
<TextShimmer text={activeTail()} active={props.active} offset={prefixLen()} />
|
<span data-slot="tool-status-active" ref={activeRef}>
|
||||||
</span>
|
<TextShimmer text={activeTail()} active={active()} offset={prefixLen()} />
|
||||||
<span data-slot="tool-status-done" ref={doneRef}>
|
</span>
|
||||||
<TextShimmer text={doneTail()} active={false} offset={prefixLen()} />
|
</Show>
|
||||||
</span>
|
<Show when={animating() || !active()}>
|
||||||
|
<span data-slot="tool-status-done" ref={doneRef}>
|
||||||
|
<TextShimmer text={doneTail()} active={false} offset={prefixLen()} />
|
||||||
|
</span>
|
||||||
|
</Show>
|
||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
</Show>
|
</Show>
|
||||||
|
|||||||
Reference in New Issue
Block a user