Repository navigation
Expand file tree
/
Copy pathlogo-orbit.tsx
More file actions
250 lines (233 loc) · 8.18 KB
/
Copy pathlogo-orbit.tsx
File metadata and controls
250 lines (233 loc) · 8.18 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
"use client";
import { useEffect, useRef, useState } from "react";
import {
siArc,
siBun,
siCloudflare,
siFigma,
siFramer,
siGithub,
siLinear,
siNeovim,
siNextdotjs,
siNotion,
siRaycast,
siReact,
siStripe,
siSupabase,
siTailwindcss,
siTypescript,
siVercel,
} from "simple-icons";
import { useReducedMotion } from "@/lib/use-reduced-motion";
import { usePreviewPlay } from "@/lab/preview-play";
import { cn } from "@/lib/cn";
export type OrbitLogo = { title: string; path: string; hex: string };
type Ring = {
logos: OrbitLogo[];
// Radius across, as a share of the stage's width.
radius: number;
// Seconds per lap; negative turns the other way.
lap: number;
};
// How far the ring is tipped away from you: the vertical radius is this
// share of the horizontal one, so the circle reads as a tilted plane. Tipped
// enough that the rings pass above and below the heading, not through it.
const TILT = 0.42;
// A hovered orbit brakes to a stop rather than freezing, and picks back up
// as smoothly when you leave.
const BRAKE = 4;
// Brand colours only show where they'd read: near-black marks (Vercel,
// Notion, GitHub) would vanish on a dark page, so those stay in the text
// colour.
function readable(hex: string) {
const n = parseInt(hex, 16);
const lum = 0.2126 * (n >> 16) + 0.7152 * ((n >> 8) & 255) + 0.0722 * (n & 255);
return lum > 40 && lum < 225;
}
export function LogoOrbit({
rings,
paused = false,
className,
children,
}: {
rings: Ring[];
// Holds a still frame and runs nothing, for idle previews.
paused?: boolean;
className?: string;
// What the logos circle: a heading, a stat, a line of copy.
children: React.ReactNode;
}) {
const reduceMotion = useReducedMotion();
const stageRef = useRef<HTMLDivElement>(null);
const itemRefs = useRef<(HTMLDivElement | null)[]>([]);
const hovering = useRef(false);
const [active, setActive] = useState<string | null>(null);
const flat = rings.flatMap((ring, r) =>
ring.logos.map((logo, i) => ({ logo, r, i, n: ring.logos.length })),
);
useEffect(() => {
const stage = stageRef.current;
if (!stage) return;
const turns = rings.map(() => 0);
let speed = 1;
let frame = 0;
let visible = true;
let last = performance.now();
const place = () => {
const w = stage.offsetWidth;
const h = stage.offsetHeight;
flat.forEach(({ r, i, n }, k) => {
const el = itemRefs.current[k];
if (!el) return;
const ring = rings[r];
const angle = turns[r] + (i / n) * Math.PI * 2;
const rx = (ring.radius * w) / 2;
const x = Math.cos(angle) * rx;
const y = Math.sin(angle) * rx * TILT;
// -1 at the back of the ring, 1 at the front.
const depth = Math.sin(angle);
const near = (depth + 1) / 2;
el.style.transform = `translate(${(w / 2 + x).toFixed(1)}px, ${(h / 2 + y).toFixed(1)}px) translate(-50%, -50%) scale(${(0.7 + near * 0.35).toFixed(3)})`;
el.style.opacity = (0.28 + near * 0.72).toFixed(3);
// The front half passes over the text, the back half behind it.
el.style.zIndex = depth > 0 ? "3" : "1";
});
};
const step = (now: number) => {
const dt = Math.min((now - last) / 1000, 1 / 30);
last = now;
const target = hovering.current ? 0 : 1;
speed += (target - speed) * (1 - Math.exp(-BRAKE * dt));
rings.forEach((ring, r) => {
turns[r] += ((Math.PI * 2) / ring.lap) * dt * speed;
});
place();
frame = visible ? requestAnimationFrame(step) : 0;
};
place();
if (paused || reduceMotion) return;
// Sleeps offscreen, so a long page never pays for it.
const io = new IntersectionObserver(([entry]) => {
visible = entry.isIntersecting;
if (visible && !frame) {
last = performance.now();
frame = requestAnimationFrame(step);
}
});
io.observe(stage);
const ro = new ResizeObserver(place);
ro.observe(stage);
frame = requestAnimationFrame(step);
return () => {
cancelAnimationFrame(frame);
io.disconnect();
ro.disconnect();
};
// `flat` is derived from `rings` on every render; the rings are the
// real input.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [rings, paused, reduceMotion]);
return (
<div
ref={stageRef}
className={cn("relative isolate aspect-[16/10] w-full", className)}
onPointerEnter={(e) => {
if (e.pointerType !== "touch") hovering.current = true;
}}
onPointerLeave={() => {
hovering.current = false;
setActive(null);
}}
>
{/* Centre layer: sits between the back and front of the orbit. It
spans the stage only to centre its content, so the pointer passes
through to the logos behind it everywhere but the text itself. */}
<div className="pointer-events-none absolute inset-0 z-[2] grid place-items-center px-[18%] text-center [&>*]:pointer-events-auto">
{children}
</div>
{flat.map(({ logo }, k) => {
const on = active === logo.title;
return (
<div
key={logo.title}
ref={(el) => void (itemRefs.current[k] = el)}
data-on={on}
// The one you point at comes forward at full strength, even from
// the far side of the ring. !important beats the per-frame depth.
className="absolute top-0 left-0 will-change-transform data-[on=true]:!z-[4] data-[on=true]:!opacity-100"
>
<button
type="button"
aria-label={logo.title}
onPointerEnter={(e) => e.pointerType !== "touch" && setActive(logo.title)}
onPointerLeave={() => setActive(null)}
onFocus={() => {
hovering.current = true;
setActive(logo.title);
}}
onBlur={() => {
hovering.current = false;
setActive(null);
}}
className="relative grid size-11 touch-manipulation place-items-center rounded-full text-foreground outline-hidden focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground"
>
<svg
viewBox="0 0 24 24"
aria-hidden
className="size-7 transition-[color,scale] duration-200 ease-out"
style={{
fill: on && readable(logo.hex) ? `#${logo.hex}` : "currentColor",
scale: on ? "1.12" : "1",
}}
>
<path d={logo.path} />
</svg>
{/* The name surfaces under the mark you're pointing at. */}
<span
aria-hidden
className={cn(
"pointer-events-none absolute top-full left-1/2 mt-1 -translate-x-1/2 rounded-full bg-foreground px-2 py-0.5 text-xs font-medium whitespace-nowrap text-background transition-[opacity,translate] duration-150 ease-out",
on ? "translate-y-0 opacity-100" : "-translate-y-1 opacity-0",
)}
>
{logo.title}
</span>
</button>
</div>
);
})}
</div>
);
}
const pick = (i: { title: string; path: string; hex: string }) => ({
title: i.title,
path: i.path,
hex: i.hex,
});
// The lab's own stack, so the section says something true.
const RINGS: Ring[] = [
{
logos: [siReact, siNextdotjs, siTypescript, siTailwindcss, siBun, siVercel, siFramer].map(pick),
radius: 0.72,
lap: 42,
},
{
logos: [siFigma, siLinear, siGithub, siCloudflare, siSupabase, siStripe, siNotion, siRaycast, siArc, siNeovim].map(pick),
radius: 0.98,
lap: -64,
},
];
export default function LogoOrbitDemo() {
const play = usePreviewPlay();
return (
<LogoOrbit rings={RINGS} paused={play === false} className="w-[min(600px,100%)]">
<div className="flex flex-col items-center gap-2">
<p className="text-[13px] font-medium text-muted">The stack</p>
<h2 className="text-2xl leading-tight font-semibold tracking-tight text-balance sm:text-[28px]">
The tools behind the lab
</h2>
</div>
</LogoOrbit>
);
}