Marks as dots inside the track: ticks, a selector's options and a slider's reading on the track's midline and the ring's centre line, coloured by the fill or the handle over them; the selector ring fills to its option

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Melvin Strobl
2026-10-14 16:42:56 +02:00
co-authored by Claude Opus 5.5
parent b500af84ec
commit f5412d2f7d
12 changed files with 387 additions and 112 deletions
+112 -41
View File
@@ -1,5 +1,5 @@
import { motion, useTransform } from "motion/react"
import { useEffect, useRef } from "react"
import { type MotionValue, motion, useTransform } from "motion/react"
import { useEffect, useId, useRef } from "react"
import type { WidgetDef } from "@/client"
import { cn } from "@/lib/utils"
@@ -21,6 +21,7 @@ import {
showTitle,
text,
} from "./ui/core/config"
import { tickIntervals } from "./ui/core/controls"
import {
CENTRE_LABEL,
CENTRE_SWATCH,
@@ -34,7 +35,6 @@ import {
RING_INSET,
RING_MARK,
RING_READING,
RING_READING_STROKE,
RING_STROKE,
ringArc,
ringPoint,
@@ -78,10 +78,13 @@ type Dial = {
/** Written out; empty for a colour, whose handle is the reading. */
reading: (value: unknown) => string
unit?: string
/** A value arc from the start to the reading: a slider and a gauge. */
/** A value arc from the start to the reading: a slider, a selector and a
* gauge. */
fill?: boolean
handle?: boolean
marks?: number[]
/** Dots along the sweep, as fractions of it: a selector's options, a
* slider's ticks. */
stops?: number[]
hue?: boolean
}
@@ -109,8 +112,9 @@ function dialOf(widget: WidgetDef, live: unknown): Dial {
const option = options[index as number]
return option ? (option.label ?? text(option.value)) : "—"
},
fill: true,
handle: true,
marks: options.map((_, index) => at(index) ?? 0.5),
stops: options.map((_, index) => at(index) ?? 0.5),
}
}
@@ -159,6 +163,8 @@ function dialOf(widget: WidgetDef, live: unknown): Dial {
const digits = (String(step).split(".")[1] ?? "").length
const fit = (value: number) =>
Number(Math.min(max, Math.max(min, value)).toFixed(digits))
// The tile's scale, as dots: the same intervals its labels are cut into.
const intervals = tickIntervals((max - min) / step)
return {
value: typeof live === "number" ? live : min,
at: (value) => fractionOf(value as number, min, max),
@@ -170,6 +176,10 @@ function dialOf(widget: WidgetDef, live: unknown): Dial {
unit,
fill: true,
handle: true,
stops:
cfg.ticks !== false && max > min
? Array.from({ length: intervals + 1 }, (_, index) => index / intervals)
: [],
}
}
@@ -333,14 +343,16 @@ export function Ring({
// Written to the slider's own decimals and in its unit: the two are read
// against each other.
const other = beside.message ? dial.reading(read?.value ?? null) : null
// The same reading, placed: a bar across the band where it sits on the
// slider's scale, held to the sweep at either end.
// The same reading, placed: a dot where it sits on the slider's scale,
// held to the sweep at either end.
const actual = typeof read?.value === "number" ? dial.at(read.value) : null
const across = useTransform(useEased(actual ?? 0), (fraction) => {
const from = ringPoint(size, fraction, -RING_READING / 2)
const to = ringPoint(size, fraction, RING_READING / 2)
return `M${from.x} ${from.y}L${to.x} ${to.y}`
})
const readAt = useEased(actual ?? 0)
const readX = useTransform(readAt, (fraction) => ringPoint(size, fraction).x)
const readY = useTransform(readAt, (fraction) => ringPoint(size, fraction).y)
const readDot = actual === null ? null : { x: readX, y: readY }
// What the dots are drawn under: the value arc and the handle, which
// repaint whatever part of a dot they cover (`RingDots`).
const id = useId().replace(/[^\w-]/g, "")
const whole = ringArc(size, 0, 1)
const name = widget.title || text(cfg.target, widget.id)
@@ -397,38 +409,23 @@ export function Ring({
className="dui-ring-track"
/>
)}
<RingDots size={size} stops={dial.stops} reading={readDot} />
{/* Revealed rather than re-pathed, as the gauge tile's arc is: `d`
does not ease, and `pathLength` makes the reveal the fraction. */}
{dial.fill ? (
<motion.path
d={whole}
fill="none"
strokeWidth={RING_STROKE}
strokeLinecap="round"
className="dui-ring-fill"
style={{ pathLength: along, opacity: shown }}
/>
) : null}
{dial.marks?.map((mark, index) => {
const point = ringPoint(size, mark)
return (
<circle
// Two options may share a label; position is the identity.
key={`mark-${index}`}
cx={point.x}
cy={point.y}
r={RING_MARK / 2}
className="dui-ring-mark"
<>
<motion.path
d={whole}
fill="none"
strokeWidth={RING_STROKE}
strokeLinecap="round"
className="dui-ring-fill"
style={{ pathLength: along, opacity: shown }}
/>
)
})}
{/* Under the handle, so where the two meet the value being set wins. */}
{actual !== null ? (
<motion.path
d={across}
strokeWidth={RING_READING_STROKE}
className="dui-ring-reading"
/>
<g className="dui-ring-reached" mask={`url(#${id}-fill)`}>
<RingDots size={size} stops={dial.stops} reading={readDot} />
</g>
</>
) : null}
{dial.handle && at !== null ? (
<motion.circle
@@ -439,7 +436,36 @@ export function Ring({
style={dial.hue ? { fill: cssOf(current as Triple) } : undefined}
/>
) : null}
{readDot && dial.handle && at !== null ? (
<g className="dui-ring-on-handle" clipPath={`url(#${id}-handle)`}>
<RingDots size={size} reading={readDot} />
</g>
) : null}
</g>
<defs>
{/* The value arc again, as a stencil: a dot it has reached is
drawn in the colour that reads on it, to the pixel it covers. */}
<mask
id={`${id}-fill`}
maskUnits="userSpaceOnUse"
x={0}
y={0}
width={size}
height={size}
>
<motion.path
d={whole}
fill="none"
stroke="#fff"
strokeWidth={RING_STROKE}
strokeLinecap="round"
style={{ pathLength: along, opacity: shown }}
/>
</mask>
<clipPath id={`${id}-handle`}>
<motion.circle cx={handleX} cy={handleY} r={RING_HANDLE / 2} />
</clipPath>
</defs>
{turns || editing ? (
<path
d={whole}
@@ -527,6 +553,51 @@ export function Ring({
)
}
/**
* The ring's dots: its stops, and what a slider reads beside its value.
*
* Drawn on the ring's centre line, inside the band, and drawn up to three
* times: on the track, again through the value arc as a stencil, and the
* reading once more through the handle. Each copy is in the colour that reads
* on what it sits on, so a dot changes colour exactly where the arc or the
* handle passes over it — and the reading still shows on the handle.
*/
function RingDots({
size,
stops = [],
reading,
}: {
size: number
stops?: number[]
reading: { x: MotionValue<number>; y: MotionValue<number> } | null
}) {
return (
<>
{stops.map((stop, index) => {
const point = ringPoint(size, stop)
return (
<circle
// Two options may share a label; position is the identity.
key={`stop-${index}`}
cx={point.x}
cy={point.y}
r={RING_MARK / 2}
className="dui-ring-stop"
/>
)
})}
{reading ? (
<motion.circle
cx={reading.x}
cy={reading.y}
r={RING_READING / 2}
className="dui-ring-reading"
/>
) : null}
</>
)
}
/**
* The colour ring's track: every hue in order along the sweep, red at both
* ends. Drawn as short strokes — an SVG stroke takes no conic gradient — each