Repository navigation
Expand file tree
/
Copy pathbar-chart.tsx
More file actions
352 lines (334 loc) · 10.7 KB
/
Copy pathbar-chart.tsx
File metadata and controls
352 lines (334 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"use client";
import { useEffect, useRef, useState } from "react";
import {
animate,
motion,
useInView,
useMotionValue,
useTransform,
type AnimationPlaybackControls,
type MotionValue,
} from "motion/react";
import { useReducedMotion } from "@/lib/use-reduced-motion";
import { cn } from "@/lib/cn";
type Datum = { label: string; value: number };
const PLOT_H = 190;
// The 6px rounded cap rides on top of a square body. Scaling one rounded
// rect would squash its corners, so only the body scales.
const CAP = 6;
// Mount: a quick cascade reads as one gesture across the week.
const STAGGER = 0.04;
// No bounce: an overshooting bar briefly claims a value that isn't true.
const GROW = { type: "spring", visualDuration: 0.45, bounce: 0 } as const;
const EASE = "ease-[cubic-bezier(0.23,1,0.32,1)]";
export function BarChart({
data,
max,
ticks,
unit,
suffix,
label,
className,
}: {
data: Datum[];
max: number;
ticks: number[];
/** Spoken after each value, e.g. "hours". */
unit: string;
/** Shown after each value in the tooltip, e.g. "h". */
suffix: string;
label: string;
className?: string;
}) {
const plotRef = useRef<HTMLDivElement>(null);
const barRefs = useRef<(HTMLDivElement | null)[]>([]);
const inView = useInView(plotRef, { once: true });
const [active, setActive] = useState<number | null>(null);
// Roving tabindex: one stop for the whole chart, arrows move within it.
const [focusIndex, setFocusIndex] = useState(0);
const move = (i: number) => {
const next = Math.min(Math.max(i, 0), data.length - 1);
setFocusIndex(next);
barRefs.current[next]?.focus();
};
return (
<div className={cn("flex w-[520px] max-w-full", className)}>
{/* Tick labels sit in their own gutter so gridlines can span the plot. */}
<div
aria-hidden
className="relative mr-3 w-6 shrink-0 text-right text-xs text-muted tabular-nums"
style={{ height: PLOT_H }}
>
{ticks.map((t) => (
<span
key={t}
className="absolute right-0 -translate-y-1/2 leading-none"
style={{ top: PLOT_H - (t / max) * PLOT_H }}
>
{t}
</span>
))}
</div>
<div className="flex-1">
<div
ref={plotRef}
role="group"
aria-label={label}
className="relative flex"
style={{ height: PLOT_H }}
onPointerLeave={(e) => {
if (e.pointerType !== "touch") setActive(null);
}}
>
{ticks.map((t) => (
<div
key={t}
aria-hidden
className="absolute inset-x-0 h-px bg-border"
style={{ top: PLOT_H - (t / max) * PLOT_H }}
/>
))}
{data.map((d, i) => (
<Bar
key={d.label}
ref={(el) => {
barRefs.current[i] = el;
}}
datum={d}
fraction={d.value / max}
index={i}
started={inView}
unit={unit}
suffix={suffix}
active={active === i}
dimmed={active !== null && active !== i}
tabIndex={i === focusIndex ? 0 : -1}
onActivate={() => setActive(i)}
onFocus={() => {
setFocusIndex(i);
setActive(i);
}}
onBlur={(e) => {
if (!plotRef.current?.contains(e.relatedTarget as Node)) setActive(null);
}}
onKeyDown={(e) => {
const next = {
ArrowLeft: i - 1,
ArrowRight: i + 1,
Home: 0,
End: data.length - 1,
}[e.key];
if (next === undefined) return;
e.preventDefault();
move(next);
}}
/>
))}
</div>
<div aria-hidden className="mt-3 flex">
{data.map((d, i) => (
<span
key={d.label}
className={cn(
"flex-1 text-center text-sm transition-colors duration-150 ease-out",
active === i ? "text-foreground" : "text-muted",
)}
>
{d.label}
</span>
))}
</div>
</div>
</div>
);
}
function Bar({
ref,
datum,
fraction,
index,
started,
unit,
suffix,
active,
dimmed,
tabIndex,
onActivate,
onFocus,
onBlur,
onKeyDown,
}: {
ref: (el: HTMLDivElement | null) => void;
datum: Datum;
fraction: number;
index: number;
started: boolean;
unit: string;
suffix: string;
active: boolean;
dimmed: boolean;
tabIndex: number;
onActivate: () => void;
onFocus: () => void;
onBlur: (e: React.FocusEvent) => void;
onKeyDown: (e: React.KeyboardEvent) => void;
}) {
const reduceMotion = useReducedMotion();
const height = useMotionValue(0);
const grown = useRef(false);
const controls = useRef<AnimationPlaybackControls>(undefined);
useEffect(() => {
if (!started) return;
const target = fraction * PLOT_H;
if (reduceMotion) {
height.jump(target);
} else {
// Only the first growth staggers; a dataset switch moves every bar
// at once so the comparison lands together. animate() retargets from
// the current value and velocity, so rapid toggles stay smooth.
controls.current = animate(height, target, {
...GROW,
delay: grown.current ? 0 : index * STAGGER,
});
}
grown.current = true;
}, [started, fraction, index, reduceMotion, height]);
useEffect(() => () => controls.current?.stop(), []);
return (
<div
ref={ref}
role="img"
tabIndex={tabIndex}
aria-label={`${datum.label}, ${datum.value} ${unit}`}
// The whole column is the hit target, not just the 40px bar.
className="relative flex-1 rounded-sm outline-hidden focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground"
onPointerEnter={(e) => {
if (e.pointerType !== "touch") onActivate();
}}
onPointerDown={(e) => {
if (e.pointerType === "touch") onActivate();
}}
onFocus={onFocus}
onBlur={onBlur}
onKeyDown={onKeyDown}
>
<BarShape height={height} dimmed={dimmed} />
<Tooltip height={height} visible={active}>
<span className="font-semibold tabular-nums">{datum.value}</span> {suffix}
</Tooltip>
</div>
);
}
function BarShape({ height, dimmed }: { height: MotionValue<number>; dimmed: boolean }) {
const body = useTransform(height, (h) => `scaleY(${Math.max(h - CAP, 0) / (PLOT_H - CAP)})`);
// Overlaps the body by 1px so no hairline seam shows at fractional heights.
const cap = useTransform(height, (h) => `translateY(${-Math.max(h - CAP - 1, 0)}px)`);
// Keeps an empty bar from showing a stub before it grows.
const capOpacity = useTransform(height, (h) => (h < 0.5 ? 0 : 1));
return (
<div
aria-hidden
className={cn(
"absolute bottom-0 left-1/2 w-10 -translate-x-1/2 transition-opacity duration-150 ease-out",
dimmed ? "opacity-40" : "opacity-100",
)}
style={{ height: PLOT_H }}
>
<motion.div
className="absolute inset-x-0 bottom-0 origin-bottom bg-foreground"
style={{ height: PLOT_H - CAP, transform: body }}
/>
<motion.div
className="absolute inset-x-0 bottom-0 rounded-t-[6px] bg-foreground"
style={{ height: CAP + 1, transform: cap, opacity: capOpacity }}
/>
</div>
);
}
function Tooltip({
height,
visible,
children,
}: {
height: MotionValue<number>;
visible: boolean;
children: React.ReactNode;
}) {
// Rides 8px above the bar's top, following it while the bar animates.
const transform = useTransform(height, (h) => `translate(-50%, ${-h - 8}px)`);
return (
<div aria-hidden className="pointer-events-none absolute bottom-0 left-1/2">
<motion.div style={{ transform }}>
<div
className={cn(
"rounded-full bg-foreground px-3 py-1.5 text-sm whitespace-nowrap text-background transition-[opacity,translate] ease-out motion-reduce:transition-opacity",
visible
? "translate-y-0 opacity-100 duration-100"
: "translate-y-1 opacity-0 duration-75 motion-reduce:translate-y-0",
)}
>
{children}
</div>
</motion.div>
</div>
);
}
const DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
const DATASETS = {
"This week": [6.5, 7.2, 5.1, 8.4, 6, 2.3, 1.2],
"Last week": [5.2, 6.1, 7.4, 4.8, 5.5, 3.1, 0.8],
};
type Week = keyof typeof DATASETS;
const WEEKS = Object.keys(DATASETS) as Week[];
export default function BarChartDemo() {
const [week, setWeek] = useState<Week>("This week");
const values = DATASETS[week];
const data = DAYS.map((label, i) => ({ label, value: values[i] }));
const total = values.reduce((a, b) => a + b, 0);
return (
<div className="flex w-[520px] max-w-full flex-col gap-6">
<div className="flex items-end justify-between">
<div>
<p className="text-[15px] text-muted">Focus hours</p>
<p className="text-3xl font-semibold tracking-tight text-foreground tabular-nums">
{total.toFixed(1)} h
</p>
</div>
<div className="relative flex rounded-full bg-surface p-1">
{/* One indicator slides between the options instead of two
backgrounds fading, so the switch reads as a single motion. */}
<span
aria-hidden
className={cn(
"absolute top-1 bottom-1 left-1 w-[96px] rounded-full bg-background shadow-raised transition-transform duration-200 motion-reduce:transition-none",
EASE,
week === WEEKS[1] && "translate-x-full",
)}
/>
{WEEKS.map((w) => (
<button
key={w}
type="button"
aria-pressed={week === w}
onClick={() => setWeek(w)}
className={cn(
"relative h-9 w-[96px] touch-manipulation rounded-full text-sm font-medium outline-hidden transition-[scale,color] duration-150 ease-out select-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground active:scale-[0.96] motion-reduce:transition-[color]",
week === w ? "text-foreground" : "text-muted hover:text-foreground",
)}
>
{w}
</button>
))}
</div>
</div>
<BarChart
data={data}
max={10}
ticks={[0, 5, 10]}
unit="hours"
suffix="h"
label={`Focus hours per day, ${week.toLowerCase()}`}
/>
</div>
);
}