1"use client";
2
3import { useId, type CSSProperties, type ReactNode } from "react";
4import { useReducedMotion } from "framer-motion";
5
6export type IconMaterialName = "line" | "spark" | "neon" | "aurora" | "chrome" | "mercury" | "liquid" | "glass";
7
8/**
9 * Looks for the icons: wrap one icon, or a whole grid of them, and they all take on the same finish.
10 *
11 * line plain strokes in the current colour (the default; nothing is added)
12 * spark strokes in the current colour, with each icon's small accent details in colour:
13 * give the icons an `accent`, e.g. <BellIcon accent="#ff3b30" />
14 * neon a glow around every stroke, in the current colour
15 * aurora every stroke painted with a blue-to-orange spectrum
16 * chrome every stroke painted as polished metal (looks best on a dark background)
17 * mercury liquid metal: the strokes round and thicken into a lit tube, and a reflected room of light and
18 * dark bands flows slowly along them (best on a dark background)
19 * liquid the strokes in the current colour as a wet droplet: where they touch they melt together, with a
20 * glossy highlight, and the whole icon wobbles like jelly while the pointer is over it
21 * glass clear glass: a translucent body with a bright edge, a fringe of red and blue where the lens bends
22 * the light, and a sheen that drifts across (best on a dark or colourful background)
23 *
24 * It only restyles, so it works with any icon that draws in currentColor and has a 24-unit viewBox. The three
25 * liquid looks are pure SVG (gradients and filters): no canvas, no WebGL, nothing to load, and they hold still
26 * for visitors who have reduced motion switched on.
27 */
28
29const AURORA: [string, string][] = [["0", "#0090f7"], ["0.3", "#6e5bff"], ["0.55", "#ba62fc"], ["0.78", "#f2416b"], ["1", "#f55600"]];
30/** The rainbow of the site's theme (the dot beside "Icons" in the nav, the rainbow word in every heading): blue, violet, pink, orange. */
31const RAINBOW: [string, string][] = [["0", "#0a84ff"], ["0.34", "#bf5af2"], ["0.66", "#ff375f"], ["1", "#ff9f0a"]];
32const CHROME: [string, string][] = [["0", "#f4f5fb"], ["0.22", "#9ea3b6"], ["0.38", "#ffffff"], ["0.55", "#6d7183"], ["0.72", "#e8e9f2"], ["0.84", "#ffe3b8"], ["1", "#bfc3d4"]];
33// The room mercury reflects, as one stripe of light: bright sky, soft grey, two hard dark bands and a warm window.
34// It starts and ends on the same colour so it can repeat and flow without a seam.
35const MERCURY: [string, string][] = [
36 ["0", "#f6f7fc"], ["0.12", "#d6d9e8"], ["0.26", "#8e93aa"], ["0.32", "#262833"], ["0.4", "#a4a9bf"], ["0.52", "#ffffff"],
37 ["0.6", "#646a80"], ["0.65", "#1c1e27"], ["0.73", "#cdd1e3"], ["0.85", "#ffe8c0"], ["1", "#f6f7fc"],
38];
39const GLASS: [string, string, number][] = [["0", "#ffffff", 0.95], ["0.35", "#e6f0ff", 0.55], ["0.7", "#cfe0ff", 0.3], ["1", "#ffffff", 0.7]];
40
41export function IconMaterial({
42 material = "line",
43 color,
44 rainbow = false,
45 className,
46 style,
47 children,
48}: {
49 material?: IconMaterialName;
50 /** The current colour for everything inside. Left out, it inherits. */
51 color?: string;
52 /** Paint every stroke with the site's blue, violet, pink and orange rainbow. Works with the line, spark and neon looks; the other looks paint themselves. */
53 rainbow?: boolean;
54 className?: string;
55 style?: CSSProperties;
56 children: ReactNode;
57}) {
58 const uid = useId().replace(/[^a-zA-Z0-9_-]/g, "");
59 const scope = `nx-mat-${uid}`;
60 const reduce = useReducedMotion();
61 const rainbowOn = rainbow && (material === "line" || material === "spark" || material === "neon");
62 const flat = material === "aurora" || material === "chrome" || rainbowOn;
63 const stops = rainbowOn ? RAINBOW : material === "aurora" ? AURORA : CHROME;
64 const liquidish = material === "mercury" || material === "liquid" || material === "glass";
65
66 // An inline <style> (not a class in a stylesheet), so that url(#…) points into this page's own document.
67 // Filters go on each top-level shape of an icon (not the <svg>), so their units are the icon's own 24-unit grid
68 // and a look is the same size-for-size at 16px or 160px.
69 let css = "";
70 if (material === "neon" && !rainbowOn) {
71 css = `.${scope} svg{filter:drop-shadow(0 0 1.5px currentColor) drop-shadow(0 0 6px currentColor)}`;
72 } else if (flat) {
73 css =
74 `.${scope} svg *:not([stroke="none"]){stroke:url(#${scope}) !important}` +
75 `.${scope} svg *[fill]:not([fill="none"]){fill:url(#${scope}) !important}` +
76 (material === "chrome" ? `.${scope} svg{filter:drop-shadow(0 1px 1px rgba(0,0,0,.35))}` : "") +
77 (material === "neon" ? `.${scope} svg{filter:drop-shadow(0 0 1.5px #bf5af2) drop-shadow(0 0 6px #0a84ff)}` : "");
78 } else if (liquidish) {
79 css =
80 `.${scope} svg *:not([stroke="none"]){stroke:url(#${scope}) !important}` +
81 `.${scope} svg *[fill]:not([fill="none"]){fill:url(#${scope}) !important}` +
82 `.${scope} svg>*:not(defs){filter:url(#${scope}-f)}` +
83 (material === "liquid" && !reduce ? `.${scope} svg:hover>*:not(defs){filter:url(#${scope}-w)}` : "");
84 }
85
86 const flow = !reduce && (material === "mercury" || material === "glass");
87
88 return (
89 <div className={[scope, className].filter(Boolean).join(" ")} style={{ color, ...style }} data-material={material}>
90 {css ? <style>{css}</style> : null}
91 {flat ? (
92 <svg width="0" height="0" aria-hidden="true" style={{ position: "absolute" }} focusable="false">
93 <defs>
94 <linearGradient id={scope} gradientUnits="userSpaceOnUse" x1="3" y1="21" x2="21" y2="3">
95 {stops.map(([offset, c]) => (
96 <stop key={offset} offset={offset} stopColor={c} />
97 ))}
98 </linearGradient>
99 </defs>
100 </svg>
101 ) : null}
102
103 {material === "mercury" ? (
104 <svg width="0" height="0" aria-hidden="true" style={{ position: "absolute" }} focusable="false">
105 <defs>
106 {/* one stripe of reflected room, repeated, so sliding it by one period loops without a seam */}
107 <linearGradient id={scope} gradientUnits="userSpaceOnUse" x1="0" y1="24" x2="24" y2="0" spreadMethod="repeat">
108 {MERCURY.map(([offset, c]) => (
109 <stop key={offset} offset={offset} stopColor={c} />
110 ))}
111 {flow ? <animateTransform attributeName="gradientTransform" type="translate" from="0 0" to="24 -24" dur="7s" repeatCount="indefinite" /> : null}
112 </linearGradient>
113 <filter id={`${scope}-f`} filterUnits="userSpaceOnUse" x="-4" y="-4" width="32" height="32" colorInterpolationFilters="sRGB">
114 {/* round the strokes off and let touching ones melt together */}
115 <feGaussianBlur in="SourceAlpha" stdDeviation="0.5" result="soft" />
116 <feColorMatrix in="soft" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 -6" result="shape" />
117 {/* a smooth height map of that shape, lit from the top left like a tube of metal */}
118 <feGaussianBlur in="shape" stdDeviation="0.65" result="relief" />
119 <feSpecularLighting in="relief" surfaceScale="2.6" specularConstant="1.2" specularExponent="28" lightingColor="#ffffff" result="shine">
120 <fePointLight x="3" y="0" z="13" />
121 </feSpecularLighting>
122 <feComposite in="shine" in2="shape" operator="in" result="glint" />
123 <feComposite in="SourceGraphic" in2="shape" operator="in" result="body" />
124 <feComposite in="body" in2="glint" operator="arithmetic" k1="0" k2="1" k3="0.9" k4="0" result="lit" />
125 <feDropShadow in="lit" dx="0" dy="0.5" stdDeviation="0.45" floodColor="#000000" floodOpacity="0.42" />
126 </filter>
127 </defs>
128 </svg>
129 ) : null}
130
131 {material === "liquid" ? (
132 <svg width="0" height="0" aria-hidden="true" style={{ position: "absolute" }} focusable="false">
133 <defs>
134 <linearGradient id={scope} gradientUnits="userSpaceOnUse" x1="3" y1="3" x2="21" y2="21">
135 <stop offset="0" stopColor="currentColor" style={{ stopColor: "color-mix(in srgb, currentColor 55%, white)" }} />
136 <stop offset="0.5" stopColor="currentColor" />
137 <stop offset="1" stopColor="currentColor" style={{ stopColor: "color-mix(in srgb, currentColor 70%, black)" }} />
138 </linearGradient>
139 <filter id={`${scope}-f`} filterUnits="userSpaceOnUse" x="-4" y="-4" width="32" height="32" colorInterpolationFilters="sRGB">
140 <LiquidCore wobble={false} />
141 </filter>
142 <filter id={`${scope}-w`} filterUnits="userSpaceOnUse" x="-4" y="-4" width="32" height="32" colorInterpolationFilters="sRGB">
143 <LiquidCore wobble />
144 </filter>
145 </defs>
146 </svg>
147 ) : null}
148
149 {material === "glass" ? (
150 <svg width="0" height="0" aria-hidden="true" style={{ position: "absolute" }} focusable="false">
151 <defs>
152 <linearGradient id={scope} gradientUnits="userSpaceOnUse" x1="0" y1="24" x2="24" y2="0" spreadMethod="repeat">
153 {GLASS.map(([offset, c, o]) => (
154 <stop key={offset} offset={offset} stopColor={c} stopOpacity={o} />
155 ))}
156 {flow ? <animateTransform attributeName="gradientTransform" type="translate" from="0 0" to="24 -24" dur="9s" repeatCount="indefinite" /> : null}
157 </linearGradient>
158 <filter id={`${scope}-f`} filterUnits="userSpaceOnUse" x="-4" y="-4" width="32" height="32" colorInterpolationFilters="sRGB">
159 {/* a bright rim where the glass catches the light */}
160 <feGaussianBlur in="SourceAlpha" stdDeviation="0.3" result="soft" />
161 <feSpecularLighting in="soft" surfaceScale="2" specularConstant="1" specularExponent="13" lightingColor="#ffffff" result="rim">
162 <fePointLight x="2" y="0" z="9" />
163 </feSpecularLighting>
164 <feComposite in="rim" in2="SourceAlpha" operator="in" result="edge" />
165 {/* the lens bends red and blue light apart: a fringe on each side of every stroke */}
166 <feColorMatrix in="SourceGraphic" type="matrix" values="1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0" result="red" />
167 <feOffset in="red" dx="-0.32" dy="0" result="redShift" />
168 <feColorMatrix in="SourceGraphic" type="matrix" values="0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0" result="green" />
169 <feColorMatrix in="SourceGraphic" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0" result="blue" />
170 <feOffset in="blue" dx="0.32" dy="0" result="blueShift" />
171 <feBlend in="redShift" in2="green" mode="screen" result="rg" />
172 <feBlend in="rg" in2="blueShift" mode="screen" result="split" />
173 <feComposite in="edge" in2="split" operator="arithmetic" k1="0" k2="0.9" k3="1" k4="0" result="glass" />
174 <feDropShadow in="glass" dx="0" dy="0.6" stdDeviation="0.55" floodColor="#0a1030" floodOpacity="0.5" />
175 </filter>
176 </defs>
177 </svg>
178 ) : null}
179
180 {children}
181 </div>
182 );
183}
184
185/** The shared part of the liquid filter: melt, light, and (on hover) a slow jelly wobble. */
186function LiquidCore({ wobble }: { wobble: boolean }) {
187 return (
188 <>
189 {wobble ? (
190 <>
191 <feTurbulence type="fractalNoise" baseFrequency="0.05 0.07" numOctaves="1" seed="3" result="noise">
192 <animate attributeName="baseFrequency" values="0.05 0.07;0.08 0.05;0.05 0.07" dur="2.6s" repeatCount="indefinite" />
193 </feTurbulence>
194 <feDisplacementMap in="SourceGraphic" in2="noise" scale="1.1" xChannelSelector="R" yChannelSelector="G" result="wobbled" />
195 </>
196 ) : null}
197 <feGaussianBlur in={wobble ? "wobbled" : "SourceGraphic"} stdDeviation="0.7" result="soft" />
198 <feColorMatrix in="soft" type="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 22 -9" result="melted" />
199 <feGaussianBlur in="melted" stdDeviation="0.7" result="relief" />
200 <feSpecularLighting in="relief" surfaceScale="3" specularConstant="1.3" specularExponent="18" lightingColor="#ffffff" result="wet">
201 <fePointLight x="4" y="0" z="10" />
202 </feSpecularLighting>
203 <feComposite in="wet" in2="melted" operator="in" result="gloss" />
204 <feComposite in="melted" in2="gloss" operator="arithmetic" k1="0" k2="1" k3="0.95" k4="0" result="lit" />
205 <feDropShadow in="lit" dx="0" dy="0.55" stdDeviation="0.5" floodColor="currentColor" floodOpacity="0.45" />
206 </>
207 );
208}
209