About
Reasoning effort on a knob: it follows the pointer, settles into the nearest detent on a spring, lights an arc and rolls the level's name like a drum.
Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Effort dial to your project and fits it to your theme.
Code
How Effort dial is used: the demo's code. The component's own source comes with a free account.
Usage.tsx
"use client"
import * as React from "react"
import { forwardRef, createElement, type CSSProperties, type SVGAttributes, type ForwardRefExoticComponent, type RefAttributes } from "react"
import { EffortDial } from "@/components/ui/effort-dial"
const FIT_CSS = "html,body{overflow:hidden}[data-fit-small] [data-fit-hide]{display:none!important}"
/**
* 21st shows a demo in a small frame: about 674 by 421 on a component page, 358 by 224 on a
* phone and 245 by 139 on a profile card. A demo taller than its frame gets scrollbars, which
* render white on dark pages. FitFrame fills the frame exactly instead.
*
* - "scale": the content shrinks until it fits, centered. For visuals such as orbs.
* - "crop": the content stays at full size from the top and fades out at the bottom. For text
* that streams in, where a changing scale would make the page breathe.
*
* `minWidth` lays out a wide demo (a table, a two column panel) at that width and scales it down
* to the frame, instead of letting it collapse to a narrow layout. The frame centers it, so it
* shrinks evenly on both sides.
*
* Anything marked `data-fit-hide` (state buttons, the chatcn.dev line) drops out in frames
* shorter than `hideBelow`, so a profile card shows the visual alone.
*/
function FitFrame({
children,
mode = "scale",
hideBelow = 360,
minWidth,
maxScale = 1,
}: {
children: React.ReactNode
mode?: "scale" | "crop"
hideBelow?: number
minWidth?: string
/** Above 1, a small demo grows until its first child fills most of the frame. */
maxScale?: number
}) {
const content = React.useRef<HTMLDivElement>(null)
const [cropped, setCropped] = React.useState(false)
React.useLayoutEffect(() => {
const el = content.current
if (!el) return
const measure = () => {
const vw = window.innerWidth
const vh = window.innerHeight
el.toggleAttribute("data-fit-small", vh < hideBelow)
// Layout sizes ignore the transform, so measuring never feeds back.
const w = Math.max(el.offsetWidth, el.scrollWidth)
const h = Math.max(el.offsetHeight, el.scrollHeight)
let scale = mode === "scale" ? Math.min(1, vw / w, vh / h) : 1
const child = el.firstElementChild as HTMLElement | null
if (mode === "scale" && maxScale > 1 && scale === 1 && child) {
const grow = Math.min(maxScale, (vw * 0.9) / child.offsetWidth, (vh * 0.9) / child.offsetHeight)
if (grow > 1) scale = grow
}
el.style.transform = scale !== 1 ? `scale(${scale})` : ""
setCropped(mode === "crop" && h > vh + 1)
}
measure()
const observer = new ResizeObserver(measure)
observer.observe(el)
// Content that changes inside a fixed width box (a headline that cycles words) never resizes
// the box itself, so changes below it measure again on the next frame.
let frame = 0
const mutations = new MutationObserver(() => {
cancelAnimationFrame(frame)
frame = requestAnimationFrame(measure)
})
mutations.observe(el, { subtree: true, childList: true, characterData: true })
window.addEventListener("resize", measure)
return () => {
observer.disconnect()
mutations.disconnect()
cancelAnimationFrame(frame)
window.removeEventListener("resize", measure)
}
}, [mode, hideBelow, maxScale])
const fade = "linear-gradient(to bottom, #000 calc(100% - 40px), transparent)"
return (
<div
className="flex w-full justify-center overflow-hidden"
style={{
height: "100dvh",
alignItems: mode === "crop" ? "flex-start" : "center",
maskImage: cropped ? fade : undefined,
WebkitMaskImage: cropped ? fade : undefined,
}}
>
<style>{FIT_CSS}</style>
<div
ref={content}
className="w-full shrink-0"
style={{ transformOrigin: mode === "crop" ? "top center" : "center", minWidth }}
>
{children}
</div>
</div>
)
}
function centerOf(el: Element): [number, number] {
const r = el.getBoundingClientRect()
return [r.left + r.width / 2, r.top + r.height / 2]
}
/**
* Scripted gestures for demos that play themselves. They dispatch untrusted pointer events, so a
* demo can tell them from a person (`event.isTrusted`) and stop its loop the moment someone
* touches it. Every function returns a cancel.
*/
const POINTER = 47
function fire(el: Element, type: string, x: number, y: number) {
el.dispatchEvent(
new PointerEvent(type, {
bubbles: true,
cancelable: true,
composed: true,
pointerId: POINTER,
pointerType: "mouse",
isPrimary: true,
button: 0,
buttons: type === "pointerup" ? 0 : 1,
clientX: x,
clientY: y,
}),
)
}
/**
* Presses `el`, moves along `path(t)` (t from 0 to 1, eased in and out) for `ms`, lets go. The
* path gives client coordinates.
*/
function dragAlong(
el: Element,
path: (t: number) => [number, number],
ms: number,
): () => void {
let raf = 0
let done = false
const [x0, y0] = path(0)
fire(el, "pointerdown", x0, y0)
const t0 = performance.now()
const step = (now: number) => {
if (done) return
const t = Math.min(1, (now - t0) / ms)
const k = t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2
const [x, y] = path(k)
fire(el, "pointermove", x, y)
if (t < 1) raf = requestAnimationFrame(step)
else {
done = true
fire(el, "pointerup", x, y)
}
}
raf = requestAnimationFrame(step)
return () => {
done = true
cancelAnimationFrame(raf)
}
}
/**
* Turns a knob: drags round the center of `el` from angle `a0` to `a1` (degrees, 0 up, clockwise)
* at `radius` px.
*/
function turn(el: Element, a0: number, a1: number, ms: number, radius = 24): () => void {
const [cx, cy] = centerOf(el)
return dragAlong(
el,
(k) => {
const a = ((a0 + (a1 - a0) * k) * Math.PI) / 180
return [cx + Math.sin(a) * radius, cy - Math.cos(a) * radius]
},
ms,
)
}
/** When an icon draws itself. */
type IconDraw = "hover" | "parent" | "mount"
interface IconProps extends SVGAttributes<SVGSVGElement> {
/** Width and height. Number means px. Default 24. */
size?: number | string
/** Stroke width in viewBox units (24 grid). Default 1.5. */
strokeWidth?: number | string
/** Keep the rendered stroke at `strokeWidth` px at any size. */
absoluteStrokeWidth?: boolean
/** Stroke color. Default currentColor. */
color?: string
/** Accessible name. Renders <title> and sets role="img"; without it the icon is aria-hidden. */
title?: string
/**
* Draw the icon stroke by stroke (needs `chatcn-icons/draw.css` imported once, or the chatcn
* theme, which carries the same rules). `"hover"`: when the icon is hovered or focused.
* `"parent"` (or `true`): when the nearest `button`, `a`, `[role=button]` or
* `[data-icon-trigger]` is. `"mount"`: once, when it appears. Reduced motion keeps the icon
* still. Without the prop nothing is added.
*/
draw?: boolean | IconDraw
}
type Icon = ForwardRefExoticComponent<IconProps & RefAttributes<SVGSVGElement>>
/** Shape elements an icon may contain. */
type IconElement = "path" | "circle" | "rect" | "ellipse" | "line" | "polyline" | "polygon"
/** An icon's drawing: a flat list of [element, attributes] pairs, lucide compatible. */
type IconNode = ReadonlyArray<readonly [IconElement, Readonly<Record<string, string>>]>
const toPascal = (name: string): string =>
name
.split("-")
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
.join("")
/**
* Builds a React icon component from an icon node.
* `name` is the kebab-case icon name, used for the class `chatcn-icon-<name>` and displayName.
*/
function createIcon(name: string, iconNode: IconNode): Icon {
// biome-ignore lint/correctness/noComponentHookFactories: runs once per icon at module load
const Component = forwardRef<SVGSVGElement, IconProps>(function ChatcnIcon(
{
size = 24,
strokeWidth = 1.5,
absoluteStrokeWidth = false,
color = "currentColor",
title,
draw,
className,
children,
...rest
},
ref,
) {
const stroke = absoluteStrokeWidth
? (Number(strokeWidth) * 24) / Number.parseFloat(String(size))
: strokeWidth
const labelled = Boolean(title || rest["aria-label"] || rest["aria-labelledby"])
const drawMode = draw === true ? "parent" : draw || undefined
return createElement(
"svg",
{
ref,
xmlns: "http://www.w3.org/2000/svg",
width: size,
height: size,
viewBox: "0 0 24 24",
fill: "none",
stroke: color,
strokeWidth: stroke,
strokeLinecap: "round",
strokeLinejoin: "round",
"data-slot": "icon",
className: className
? `chatcn-icon chatcn-icon-${name} ${className}`
: `chatcn-icon chatcn-icon-${name}`,
...(labelled ? { role: "img" } : { "aria-hidden": true }),
...(drawMode ? { "data-draw": drawMode } : null),
...rest,
},
title ? createElement("title", null, title) : null,
...iconNode.map(([tag, attrs], i) =>
createElement(
tag,
drawMode
? // Path order is draw order: each path's index staggers it (draw.css caps it at 3).
{ key: i, ...attrs, pathLength: 1, style: { "--ci-i": i } as CSSProperties }
: { key: i, ...attrs },
),
),
children,
)
})
Component.displayName = `${toPascal(name)}Icon`
return Component
}
const iconNode: IconNode = [
["path", {"d":"m14.81 7.92-6.36 6.36a1.75 1.75 0 0 0 0 2.47 1.75 1.75 0 0 0 2.47 0l7.07-7.07a3.5 3.5 0 0 0 0-4.95 3.5 3.5 0 0 0-4.95 0L5.97 11.8a5.25 5.25 0 0 0 0 7.43 5.25 5.25 0 0 0 7.43 0l4.95-4.95"}],
]
const PaperclipIcon: Icon = /* @__PURE__ */ createIcon("paperclip", iconNode)
const ESTIMATE: Record<string, string> = {
low: "Answers in a few seconds",
medium: "Usually under 20 s",
high: "Can think for a minute or two",
max: "Can think for several minutes",
}
const iconNode2: IconNode = [
["path", {"d":"m18.5 11.5-4.73-4.73a2.5 2.5 0 0 0-3.54 0L5.5 11.5"}],
["path", {"d":"M12 19v-9"}],
]
const ArrowUpIcon: Icon = /* @__PURE__ */ createIcon("arrow-up", iconNode2)
function EffortDialDemo() {
const [effort, setEffort] = React.useState("medium")
// Plays itself: turns the knob up to max, back down to low, then home to medium, until someone
// touches it. Detents sit 90 degrees apart, 0 at the top.
const ref = React.useRef<HTMLDivElement>(null)
const [touched, setTouched] = React.useState(false)
const [cycle, setCycle] = React.useState(0)
React.useEffect(() => {
if (touched) return
const knob = ref.current?.querySelector('[data-slot="effort-dial"] [role="slider"]')
if (!knob) return
const stops: (() => void)[] = []
const ids = [
window.setTimeout(() => stops.push(turn(knob, -45, 45, 650)), 700),
window.setTimeout(() => stops.push(turn(knob, 45, 135, 600)), 2000),
window.setTimeout(() => stops.push(turn(knob, 135, -135, 1100)), 3400),
window.setTimeout(() => stops.push(turn(knob, -135, -45, 650)), 5300),
window.setTimeout(() => setCycle((c) => c + 1), 7400),
]
return () => {
for (const id of ids) window.clearTimeout(id)
for (const stop of stops) stop()
}
}, [touched, cycle])
return (
<div
ref={ref}
className="flex w-full justify-center px-4 py-12"
onPointerDownCapture={(e) => e.isTrusted && setTouched(true)}
onKeyDownCapture={() => setTouched(true)}
>
<div className="border-(color:--chatcn-border) bg-(color:--chatcn-card) shadow-(--chatcn-shadow-xs) rounded-(--chatcn-radius-xl) flex w-full max-w-lg flex-col gap-3 border p-3">
<p className="text-(color:--chatcn-fg) text-[0.9375rem]/[1.5rem] tracking-[-0.006em] px-1 pt-1">
Why does the session refresh twice when two tabs are open?
</p>
<div className="flex items-center justify-between gap-3">
<div className="flex items-center gap-3">
<button
type="button"
aria-label="Attach files"
className="chatcn-focus text-(color:--chatcn-fg-muted) hover:bg-(color:--chatcn-fill) rounded-(--chatcn-radius-md) grid size-8 place-items-center transition-colors duration-150"
>
<PaperclipIcon aria-hidden="true" className="size-4" />
</button>
<EffortDial value={effort} onValueChange={setEffort} />
<span className="text-(color:--chatcn-fg-subtle) text-[0.75rem]/[1rem] hidden sm:block">
{ESTIMATE[effort]}
</span>
</div>
<button
type="button"
aria-label="Send message"
className="chatcn-focus bg-(color:--chatcn-primary) text-(color:--chatcn-primary-fg) grid size-8 place-items-center rounded-full"
>
<ArrowUpIcon aria-hidden="true" className="size-4" />
</button>
</div>
</div>
</div>
)
}
/** The line under each 21st demo that points back to the kit. */
function MoreOn({ slug, children }: { slug: string; children: React.ReactNode }) {
return (
<p data-fit-hide className="text-[0.75rem]/[1rem] text-(color:--chatcn-fg-faint) text-center text-balance">
{children}{" "}
<a
href={`https://chatcn.dev/docs/components/${slug}`}
target="_blank"
rel="noopener"
className="text-(color:--chatcn-fg-subtle) hover:text-(color:--chatcn-fg) underline underline-offset-2"
>
chatcn.dev
</a>
</p>
)
}
/** Effort dial, playing on its own, with the line back to its chatcn page. */
export default function EffortDialPreview() {
return (
<FitFrame maxScale={1.6}>
<div className="mx-auto flex w-[34rem] max-w-full flex-col items-center gap-2 py-4">
<EffortDialDemo />
<MoreOn slug="effort-dial">One of 30 free microinteractions on</MoreOn>
</div>
</FitFrame>
)
}















