Dashboard motion: round dashboards page up and down, the ring eases to a notch or a reading, the slider tile to a value set elsewhere; reduced motion honoured by every tween; motion spec in the docs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Melvin Strobl
2026-10-14 16:34:10 +02:00
co-authored by Claude Opus 5.5
parent 5b2769c141
commit b500af84ec
11 changed files with 245 additions and 91 deletions
@@ -8,6 +8,7 @@
import { useCallback, useEffect, useId, useRef, useState } from "react"
import { fractionOf } from "./config"
import { useEased } from "./values"
// ---------------------------------------------------------------------------
// Press
@@ -173,7 +174,8 @@ export function fitMarks<T extends { label: string }>(
* A drag would otherwise send a value per pixel and flood whatever is
* listening, so the draft follows the finger and only the release publishes.
* While there is no draft the handle follows the engine, which is what makes a
* value set elsewhere show up here.
* value set elsewhere show up here — easing there, as a reading does, where a
* drag or a key moves it at once.
*/
export function useSliderDrag({
value,
@@ -242,13 +244,16 @@ export function useSliderDrag({
}))
: []
const fraction = useEased(fractionOf(current, min, max), draft !== null)
const markAt = useEased(mark === null ? 0 : fractionOf(mark, min, max))
return {
current,
/** Goes on the slider, whose width is the scale's width. */
trackRef,
fraction: fractionOf(current, min, max),
fraction,
/** Where the mark sits along the track, 0..1 and held to it; null for none. */
markAt: mark === null ? null : fractionOf(mark, min, max),
markAt: mark === null ? null : markAt,
inputProps: {
type: "range" as const,
min,
@@ -694,11 +694,15 @@
stroke: var(--foreground);
}
/* Page dots, one per face, in the ring's gap. */
/* Page dots, one per face, in the ring's gap. The current one changes over
as the face slides in: a colour, so reduced motion keeps it. */
.dui-dot-mark {
border-radius: 50%;
background: var(--muted-foreground);
opacity: 0.5;
transition:
background-color var(--duration-base) var(--ease-standard),
opacity var(--duration-base) var(--ease-standard);
}
.dui-dot[aria-current="page"] .dui-dot-mark {
@@ -132,7 +132,7 @@ export function fractionAt(dx: number, dy: number): number {
* a draft the ring follows the engine, through the caller's hold-until-echo.
*/
export function useRingDraft<T>(value: T, send: (value: T) => void) {
const [draft, setDraft] = useState<{ value: T } | null>(null)
const [draft, setDraft] = useState<{ value: T; settle: boolean } | null>(null)
// Read between renders: notches arrive faster than React draws, and each
// one steps from the last rather than from what was last drawn.
const pending = useRef<{ value: T } | null>(null)
@@ -155,12 +155,14 @@ export function useRingDraft<T>(value: T, send: (value: T) => void) {
return {
current: draft ? draft.value : value,
/** A drag is moving it, which the drawing follows without easing. */
held: draft !== null && !draft.settle,
/** The value a further notch steps from. */
latest: () => (pending.current ? pending.current.value : value),
/** Move the draft; `settle` publishes once nothing has moved it for a while. */
move: (next: T, settle: boolean) => {
pending.current = { value: next }
setDraft({ value: next })
setDraft({ value: next, settle })
if (timer.current) clearTimeout(timer.current)
timer.current = settle ? setTimeout(commit, RING_SETTLE_MS) : null
},
@@ -3,16 +3,20 @@
*
* A number that jumps is hard to read and a number that crawls is a distraction
* — both sets tween theirs, at their own look's stiffness, through the motion
* values here. `<MotionConfig reducedMotion="user">` at the app root switches
* every one of them off when the device asks.
* values here. These are motion values rather than animated props, which
* `<MotionConfig reducedMotion="user">` never reaches, so `useEased` asks the
* device itself and moves at once when it wants less motion.
*/
import {
animate,
cancelFrame,
frame,
type MotionValue,
useMotionValue,
useSpring,
useReducedMotion,
useTransform,
} from "motion/react"
import { useEffect } from "react"
import { useLayoutEffect, useRef } from "react"
import type { WidgetDef } from "@/client"
import { CHART_SLOTS } from "@/components/Common/UplotChart"
@@ -43,14 +47,12 @@ export function useAnimatedNumber(
value: unknown,
precision: number | null,
): { label: MotionValue<string>; numeric: boolean } {
const { look } = useLook()
const numeric = typeof value === "number"
const raw = useMotionValue(numeric ? value : 0)
const settled = useSpring(raw, LOOK[look].spring)
// A reading that goes away and comes back tweens from where it last was.
const last = useRef(numeric ? value : 0)
if (numeric) last.current = value
const settled = useEased(last.current)
const written = precision ?? (numeric ? decimalsOf(value as number) : null)
useEffect(() => {
if (numeric) raw.set(value as number)
}, [numeric, value, raw])
return {
label: useTransform(settled, (at) => format(at, written)),
numeric,
@@ -59,12 +61,42 @@ export function useAnimatedNumber(
/** Where a reading sits on its scale, tweened, for a fill or an arc. */
export function useAnimatedFraction(fraction: number): MotionValue<number> {
return useEased(fraction)
}
/**
* A number on its way to `value`, at the look's spring.
*
* A new value retargets a move still running, from where it is and as fast as
* it was going. It moves at once instead while a hand is on it (`held`: a drag
* stays under the finger), when the device asks for reduced motion, and for a
* value that `wraps`, a hue, when it crosses the seam — more than half the
* range, which would otherwise sweep back along the whole of it.
*/
export function useEased(
value: number,
held = false,
wraps = false,
): MotionValue<number> {
const { look } = useLook()
const raw = useMotionValue(fraction)
useEffect(() => {
raw.set(fraction)
}, [fraction, raw])
return useSpring(raw, LOOK[look].spring)
const still = useReducedMotion()
const eased = useMotionValue(value)
// Before paint, so a drag is drawn in the frame it moved in.
useLayoutEffect(() => {
if (held || still || (wraps && Math.abs(value - eased.get()) > 0.5)) {
// In the frame about to be drawn rather than now: what the reader
// derives from this value re-subscribes after this effect, and would
// miss a change made in it.
const jump = () => eased.jump(value)
frame.preRender(jump)
return () => cancelFrame(jump)
}
const move = animate(eased, value, LOOK[look].spring)
// Stopped rather than left running when the reader goes away; a value
// that changes again retargets instead, from inside `animate`.
return () => move.stop()
}, [value, held, wraps, still, look, eased])
return eased
}
/**
@@ -12,7 +12,7 @@
import * as SelectPrimitive from "@radix-ui/react-select"
import { Check, ChevronDown } from "lucide-react"
import { motion } from "motion/react"
import { type MotionStyle, motion } from "motion/react"
import { cn } from "@/lib/utils"
import { asOriginal, text } from "../core/config"
@@ -68,14 +68,14 @@ export function Slider(props: SliderProps) {
const { fraction, inputProps, marks, markAt, trackRef } = useSliderDrag(props)
const orientation = props.orientation ?? "horizontal"
return (
<div
<motion.div
className={cn(
"dui-slider fx-slider",
orientation === "horizontal" && "w-full",
)}
data-orientation={orientation}
ref={trackRef}
style={{ "--dui-fraction": fraction } as React.CSSProperties}
style={{ "--dui-fraction": fraction } as MotionStyle}
>
<div className="dui-slider-body">
<span className="dui-slider-rail" aria-hidden>
@@ -88,10 +88,10 @@ export function Slider(props: SliderProps) {
{/* Under the handle, so where the two meet the value being set wins;
after the input only for its `:disabled`, as it takes no pointer. */}
{markAt === null ? null : (
<span
<motion.span
className="dui-slider-mark"
aria-hidden
style={{ "--dui-mark": markAt } as React.CSSProperties}
style={{ "--dui-mark": markAt } as MotionStyle}
/>
)}
<span className="dui-slider-thumb" aria-hidden />
@@ -115,7 +115,7 @@ export function Slider(props: SliderProps) {
))}
</div>
) : null}
</div>
</motion.div>
)
}
@@ -7,7 +7,7 @@
*/
import * as SelectPrimitive from "@radix-ui/react-select"
import { Check, ChevronDown } from "lucide-react"
import { motion } from "motion/react"
import { type MotionStyle, motion } from "motion/react"
import { cn } from "@/lib/utils"
import { asOriginal, text } from "../core/config"
@@ -79,14 +79,14 @@ export function Slider(props: SliderProps) {
const { fraction, inputProps, marks, markAt, trackRef } = useSliderDrag(props)
const orientation = props.orientation ?? "horizontal"
return (
<div
<motion.div
className={cn(
"dui-slider gl-slider",
orientation === "horizontal" && "w-full",
)}
data-orientation={orientation}
ref={trackRef}
style={{ "--dui-fraction": fraction } as React.CSSProperties}
style={{ "--dui-fraction": fraction } as MotionStyle}
>
<div className="dui-slider-body">
<span className="dui-slider-rail" aria-hidden>
@@ -99,10 +99,10 @@ export function Slider(props: SliderProps) {
{/* Under the handle, so where the two meet the value being set wins;
after the input only for its `:disabled`, as it takes no pointer. */}
{markAt === null ? null : (
<span
<motion.span
className="dui-slider-mark"
aria-hidden
style={{ "--dui-mark": markAt } as React.CSSProperties}
style={{ "--dui-mark": markAt } as MotionStyle}
/>
)}
<span className="dui-slider-thumb" aria-hidden />
@@ -126,7 +126,7 @@ export function Slider(props: SliderProps) {
))}
</div>
) : null}
</div>
</motion.div>
)
}
@@ -8,7 +8,7 @@
import * as SelectPrimitive from "@radix-ui/react-select"
import { Check, ChevronDown } from "lucide-react"
import { AnimatePresence, motion } from "motion/react"
import { AnimatePresence, type MotionStyle, motion } from "motion/react"
import { cn } from "@/lib/utils"
import { asOriginal, text } from "../core/config"
@@ -108,14 +108,14 @@ export function Slider(props: SliderProps) {
const { fraction, inputProps, marks, markAt, trackRef } = useSliderDrag(props)
const orientation = props.orientation ?? "horizontal"
return (
<div
<motion.div
className={cn(
"dui-slider m3-slider",
orientation === "horizontal" && "w-full",
)}
data-orientation={orientation}
ref={trackRef}
style={{ "--dui-fraction": fraction } as React.CSSProperties}
style={{ "--dui-fraction": fraction } as MotionStyle}
>
<div className="dui-slider-body">
<span className="dui-slider-rail" aria-hidden>
@@ -128,10 +128,10 @@ export function Slider(props: SliderProps) {
{/* Under the handle, so where the two meet the value being set wins;
after the input only for its `:disabled`, as it takes no pointer. */}
{markAt === null ? null : (
<span
<motion.span
className="dui-slider-mark"
aria-hidden
style={{ "--dui-mark": markAt } as React.CSSProperties}
style={{ "--dui-mark": markAt } as MotionStyle}
/>
)}
<span className="dui-slider-thumb" aria-hidden />
@@ -155,7 +155,7 @@ export function Slider(props: SliderProps) {
))}
</div>
) : null}
</div>
</motion.div>
)
}