Repository navigation
Expand file tree
/
Copy pathsidebar-highlight.tsx
More file actions
289 lines (276 loc) · 9.98 KB
/
Copy pathsidebar-highlight.tsx
File metadata and controls
289 lines (276 loc) · 9.98 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
"use client";
import { useEffect, useLayoutEffect, useRef, useState } from "react";
import { animate, motion, useMotionValue } from "motion/react";
import { usePreviewPlay } from "@/lab/preview-play";
import { useReducedMotion } from "@/lib/use-reduced-motion";
import { cn } from "@/lib/cn";
// After Gustavo's sidebar active state (https://x.com/heyimgustavo): one dot
// travels to whatever you pick, taking on its section's colour on the way,
// the label steps aside for it, and the colour wipes across the label.
export type SidebarSection = {
title: string;
// The section's hue. Any CSS colour, light-dark() included.
tone: string;
icon: React.ReactNode;
items: { label: string; isNew?: boolean }[];
};
const EASE_OUT = "cubic-bezier(0.23, 1, 0.32, 1)";
// Quick enough to keep up with clicking down the list, soft enough that the
// dot is seen travelling rather than teleporting.
const TRAVEL = { type: "spring", visualDuration: 0.35, bounce: 0.15 } as const;
const DOT = 6;
export function SidebarHighlight({
sections,
defaultActive,
onSelect,
className,
}: {
sections: SidebarSection[];
defaultActive?: string;
onSelect?: (label: string) => void;
className?: string;
}) {
const reduceMotion = useReducedMotion();
const play = usePreviewPlay();
const [own, setOwn] = useState(
defaultActive ?? sections[0]?.items[0]?.label,
);
// The index card clicks around on its own state.
const [show, setShow] = useState(own);
const active = play ? show : own;
const listRef = useRef<HTMLDivElement>(null);
const items = useRef(new Map<string, HTMLButtonElement>());
const y = useMotionValue(0);
const placed = useRef(false);
const tone = sections.find((s) => s.items.some((i) => i.label === active))
?.tone;
// The dot lives in the list, not in an item, so it can travel between
// them. It lines up with the middle of the active row.
useLayoutEffect(() => {
const place = (instant: boolean) => {
const item = active ? items.current.get(active) : undefined;
if (!item) return;
const target = item.offsetTop + item.offsetHeight / 2 - DOT / 2;
if (instant || reduceMotion) y.jump(target);
else animate(y, target, TRAVEL);
};
place(!placed.current);
placed.current = true;
// Fonts landing or the width changing moves the rows; follow without
// animating. An observer reports once as soon as it starts, which would
// snap the dot mid-travel, so only a real change in size counts.
const list = listRef.current;
if (!list) return;
let size = `${list.offsetWidth}x${list.offsetHeight}`;
const observer = new ResizeObserver(() => {
const next = `${list.offsetWidth}x${list.offsetHeight}`;
if (next === size) return;
size = next;
place(true);
});
observer.observe(list);
return () => observer.disconnect();
}, [active, reduceMotion, y]);
// The card's show: someone reading down the list, picking a note now and
// then.
useEffect(() => {
if (!play) return;
const picks = ["Letter spacing", "Novelty budget", "Tabular numbers"];
let i = 0;
const timer = setInterval(() => {
setShow(picks[i % picks.length]);
i += 1;
}, 1100);
return () => {
clearInterval(timer);
setShow(own);
};
}, [play, own]);
return (
<nav
aria-label="Notes"
// Rows fade at the top and bottom edges, so the list reads as going on
// rather than being cut off.
className={cn(
"h-[440px] w-[300px] max-w-full overflow-y-auto overscroll-contain rounded-3xl bg-background shadow-raised [mask-image:linear-gradient(to_bottom,transparent,black_36px,black_calc(100%-36px),transparent)] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
className,
)}
>
<div ref={listRef} className="relative px-7 py-8">
<motion.span
aria-hidden
className="absolute top-0 left-7 size-1.5 rounded-full transition-[background-color] duration-300 ease-out motion-reduce:transition-none"
style={{ y, backgroundColor: tone }}
/>
{sections.map((section) => (
<section key={section.title} className="mt-7 first:mt-0">
<h3
className="mb-1.5 flex items-center gap-2.5 text-[15px] font-medium"
style={{ "--tone": section.tone } as React.CSSProperties}
>
<span className="size-4 text-(--tone)">{section.icon}</span>
{section.title}
</h3>
<ul>
{section.items.map(({ label, isNew }) => {
const on = label === active;
return (
<li key={label}>
<button
ref={(el) => {
if (el) items.current.set(label, el);
else items.current.delete(label);
}}
type="button"
aria-current={on ? "true" : undefined}
onClick={() => {
setOwn(label);
onSelect?.(label);
}}
className="group/item flex h-8 w-full items-center rounded-md text-left text-[15px] outline-hidden focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground"
style={{ "--tone": section.tone } as React.CSSProperties}
>
{/* Steps right to make room for the dot. */}
<span
className={cn(
"flex items-center gap-2 transition-[translate] duration-300 motion-reduce:transition-none",
on && "translate-x-3.5",
)}
style={{ transitionTimingFunction: EASE_OUT }}
>
<span className="relative whitespace-nowrap">
<span className="text-muted transition-[color] duration-150 ease-out group-hover/item:text-foreground">
{label}
</span>
{/* The section colour wipes in left to right on
a copy of the label laid over it, so the
hues never mix mid-way. */}
<span
aria-hidden
className="absolute inset-0 text-(--tone) transition-[clip-path] motion-reduce:transition-none"
style={{
clipPath: on ? "inset(0 0 0 0)" : "inset(0 100% 0 0)",
transitionDuration: on ? "450ms" : "200ms",
transitionTimingFunction: EASE_OUT,
}}
>
{label}
</span>
</span>
{isNew && (
<span className="rounded-full bg-green-500/12 px-1.5 py-px text-[11px] font-medium text-green-700 ring-1 ring-green-500/25 ring-inset dark:text-green-400">
New
</span>
)}
</span>
</button>
</li>
);
})}
</ul>
</section>
))}
</div>
</nav>
);
}
const ICON = {
viewBox: "0 0 16 16",
className: "size-4",
fill: "none",
stroke: "currentColor",
strokeWidth: 1.5,
strokeLinecap: "round",
strokeLinejoin: "round",
"aria-hidden": true,
} as const;
// Section hues are data: each section keeps its own, a step lighter in dark
// mode so it holds the same contrast.
const SECTIONS: SidebarSection[] = [
{
title: "Craft",
tone: "light-dark(oklch(0.62 0.17 45), oklch(0.74 0.15 50))",
icon: (
<svg {...ICON}>
<path d="M3 13l1.5-4.5L11 2l3 3-6.5 6.5L3 13Z" />
<path d="M9.5 3.5l3 3" />
</svg>
),
items: [
{ label: "Taste is trained" },
{ label: "Novelty budget" },
{ label: "Details compound" },
],
},
{
title: "Typography",
tone: "light-dark(oklch(0.55 0.18 260), oklch(0.72 0.14 255))",
icon: (
<svg {...ICON}>
<path d="M1.75 12.5 5 3.5l3.25 9M2.9 9.5h4.2" />
<circle cx="11.75" cy="10.25" r="2.25" />
<path d="M14 8v4.5" />
</svg>
),
items: [
{ label: "Letter spacing" },
{ label: "Text wrapping" },
{ label: "Tabular numbers", isNew: true },
{ label: "Optical sizes" },
],
},
{
title: "Color",
tone: "light-dark(oklch(0.58 0.2 10), oklch(0.72 0.16 10))",
icon: (
<svg {...ICON}>
<circle cx="8" cy="5.25" r="2.5" />
<circle cx="5" cy="10.5" r="2.5" />
<circle cx="11" cy="10.5" r="2.5" />
</svg>
),
items: [
{ label: "OKLCH" },
{ label: "Grain", isNew: true },
{ label: "Shadows over borders" },
{ label: "Image outlines" },
],
},
{
title: "Layout",
tone: "light-dark(oklch(0.66 0.16 70), oklch(0.78 0.14 75))",
icon: (
<svg {...ICON}>
<rect x="2.25" y="3" width="11.5" height="10" rx="1.75" />
<path d="M6.5 3v10M6.5 7h7.25" />
</svg>
),
items: [
{ label: "Concentric radii", isNew: true },
{ label: "Hit areas" },
{ label: "Clip paths" },
{ label: "Scroll fades" },
],
},
{
title: "Motion",
tone: "light-dark(oklch(0.55 0.2 300), oklch(0.72 0.15 300))",
icon: (
<svg {...ICON}>
<path d="M3 12.5C7 12.5 8 3.5 13 3.5" />
<circle cx="3" cy="12.5" r="1.25" />
<circle cx="13" cy="3.5" r="1.25" />
</svg>
),
items: [
{ label: "Icon morphs" },
{ label: "Button press" },
{ label: "Easing curves" },
{ label: "Stagger" },
{ label: "Interruptibility" },
],
},
];
export default function SidebarHighlightDemo() {
return <SidebarHighlight sections={SECTIONS} defaultActive="Tabular numbers" />;
}