Liquid Glass for the web: the rim refraction, light and motion of iOS 27 glass on any rounded element. React bindings and pill tabs. MIT.
| import { DEFAULT_GEOMETRY, type GlassAppearance, type GlassVariant } from "@colineapp/liquid-glass"; |
| 5 | import { LiquidGlass, useLiquidGlassTabs, type LiquidGlassHandle } from "@colineapp/liquid-glass/react"; |
| 6 | import "@colineapp/ui/styles.css"; |
| 7 | import "@colineapp/liquid-glass/styles.css"; |
| 8 | import "./app.css"; |
| 9 |
| 10 | const SOURCE_URL = "https://upstream.codes/coline/liquid-glass"; |
| 11 | const GITHUB_URL = "https://github.com/colineapp/liquid-glass"; |
| 12 | const INSTALL = "npm install @colineapp/liquid-glass"; |
| 13 |
| 14 | type Shape = "capsule" | "card" | "circle"; |
| 15 |
| 16 | const SHAPES: Record<Shape, { width: number; height: number; radius: number }> = { |
| 17 | capsule: { width: 300, height: 84, radius: 42 }, |
| 18 | card: { width: 280, height: 184, radius: 32 }, |
| 19 | circle: { width: 168, height: 168, radius: 84 }, |
| 20 | }; |
| 21 |
| 22 | const HUES = [ |
| 23 | { label: "None", value: null }, |
| 24 | { label: "Blue", value: "#3d6bff" }, |
| 25 | { label: "Pink", value: "#ff4fa3" }, |
| 26 | { label: "Green", value: "#2fb36d" }, |
| 27 | { label: "Amber", value: "#f0a020" }, |
| 28 | ] as const; |
| 29 |
| 30 | function cx(...classes: (string | false | null | undefined)[]) { |
| 31 | return classes.filter(Boolean).join(" "); |
| 32 | } |
| 33 |
| 34 | // ── Pieces ────────────────────────────────────────────── |
| 35 |
| 36 | /** A busy, moving page for the glass to bend: colour, fine lines and large type. */ |
| 37 | function Scene({ appearance, words = true }: { appearance: GlassAppearance; words?: boolean }) { |
| 38 | return ( |
| 39 | <div aria-hidden="true" className={cx("scene", appearance === "dark" ? "scene-dark" : "scene-light")}> |
| 40 | <div className="scene-blob scene-blob-a" /> |
| 41 | <div className="scene-blob scene-blob-b" /> |
| 42 | <div className="scene-blob scene-blob-c" /> |
| 43 | <div className="scene-grid" /> |
| 44 | {words ? ( |
| 45 | <div className="scene-words"> |
| 46 | <span>Bends</span> |
| 47 | <span>the page</span> |
| 48 | <span>behind it.</span> |
| 49 | </div> |
| 50 | ) : null} |
| 51 | </div> |
| 52 | ); |
| 53 | } |
| 54 |
| 55 | /** A segmented control on Liquid Glass tabs: drag the selected one, or press it to lift it. */ |
| 56 | function GlassTabs<T extends string>({ |
| 57 | label, |
| 58 | value, |
| 59 | options, |
| 60 | onChange, |
| 61 | appearance, |
| 62 | className, |
| 63 | }: { |
| 64 | label: string; |
| 65 | value: T; |
| 66 | options: { value: T; label: string }[]; |
| 67 | onChange: (value: T) => void; |
| 68 | appearance: GlassAppearance; |
| 69 | className?: string; |
| 70 | }) { |
| 71 | const track = React.useRef<HTMLDivElement>(null); |
| 72 | useLiquidGlassTabs(track, { appearance, tabs: '[role="radio"]' }); |
| 73 | const index = options.findIndex((option) => option.value === value); |
| 74 | return ( |
| 75 | <div |
| 76 | ref={track} |
| 77 | role="radiogroup" |
| 78 | aria-label={label} |
| 79 | className={cx("flex rounded-full p-1", className)} |
| 80 | onKeyDown={(event) => { |
| 81 | const step = event.key === "ArrowRight" || event.key === "ArrowDown" ? 1 : event.key === "ArrowLeft" || event.key === "ArrowUp" ? -1 : 0; |
| 82 | if (!step) return; |
| 83 | event.preventDefault(); |
| 84 | const next = options[(index + step + options.length) % options.length]; |
| 85 | if (!next) return; |
| 86 | onChange(next.value); |
| 87 | track.current?.querySelector<HTMLElement>(`[data-value="${next.value}"]`)?.focus(); |
| 88 | }} |
| 89 | > |
| 90 | {options.map((option) => { |
| 91 | const selected = option.value === value; |
| 92 | return ( |
| 93 | <button |
| 94 | key={option.value} |
| 95 | type="button" |
| 96 | role="radio" |
| 97 | aria-checked={selected} |
| 98 | tabIndex={selected ? 0 : -1} |
| 99 | data-value={option.value} |
| 100 | onClick={() => onChange(option.value)} |
| 101 | className={cx( |
| 102 | "flex-1 rounded-full px-3 py-1.5 text-sm font-medium whitespace-nowrap transition-opacity duration-150", |
| 103 | !selected && "opacity-60 hover:opacity-90", |
| 104 | )} |
| 105 | > |
| 106 | {option.label} |
| 107 | </button> |
| 108 | ); |
| 109 | })} |
| 110 | </div> |
| 111 | ); |
| 112 | } |
| 113 |
| 114 | function Segmented<T extends string>(props: { |
| 115 | label: string; |
| 116 | value: T; |
| 117 | options: { value: T; label: string }[]; |
| 118 | onChange: (value: T) => void; |
| 119 | appearance: GlassAppearance; |
| 120 | }) { |
| 121 | return ( |
| 122 | <div className="grid gap-2"> |
| 123 | <span className="text-sm font-medium opacity-70">{props.label}</span> |
| 124 | <GlassTabs {...props} /> |
| 125 | </div> |
| 126 | ); |
| 127 | } |
| 128 |
| 129 | function Range({ |
| 130 | label, |
| 131 | value, |
| 132 | min, |
| 133 | max, |
| 134 | step, |
| 135 | onChange, |
| 136 | }: { |
| 137 | label: string; |
| 138 | value: number; |
| 139 | min: number; |
| 140 | max: number; |
| 141 | step: number; |
| 142 | onChange: (value: number) => void; |
| 143 | }) { |
| 144 | return ( |
| 145 | <label className="grid gap-2"> |
| 146 | <span className="flex justify-between text-sm font-medium"> |
| 147 | <span className="opacity-70">{label}</span> |
| 148 | <span className="tabular-nums">{value}</span> |
| 149 | </span> |
| 150 | <input |
| 151 | type="range" |
| 152 | min={min} |
| 153 | max={max} |
| 154 | step={step} |
| 155 | value={value} |
| 156 | onChange={(event) => onChange(Number(event.target.value))} |
| 157 | className="range" |
| 158 | /> |
| 159 | </label> |
| 160 | ); |
| 161 | } |
| 162 |
| 163 | async function copyText(text: string) { |
| 164 | try { |
| 165 | await navigator.clipboard.writeText(text); |
| 166 | return true; |
| 167 | } catch { |
| 168 | // The sandbox may withhold the clipboard API; a selected textarea still copies. |
| 169 | const area = document.createElement("textarea"); |
| 170 | area.value = text; |
| 171 | area.style.position = "fixed"; |
| 172 | area.style.opacity = "0"; |
| 173 | document.body.append(area); |
| 174 | area.select(); |
| 175 | const copied = document.execCommand("copy"); |
| 176 | area.remove(); |
| 177 | return copied; |
| 178 | } |
| 179 | } |
| 180 |
| 181 | function CopyButton({ text, label = "Copy" }: { text: string; label?: string }) { |
| 182 | const [copied, setCopied] = React.useState(false); |
| 183 | return ( |
| 184 | <button |
| 185 | type="button" |
| 186 | onClick={async () => { |
| 187 | if (!(await copyText(text))) return; |
| 188 | setCopied(true); |
| 189 | window.setTimeout(() => setCopied(false), 1400); |
| 190 | }} |
| 191 | className="rounded-full px-3 py-1 text-sm font-medium opacity-70 transition-opacity hover:opacity-100" |
| 192 | > |
| 193 | {copied ? "Copied" : label} |
| 194 | </button> |
| 195 | ); |
| 196 | } |
| 197 |
| 198 | function Code({ code }: { code: string }) { |
| 199 | return ( |
| 200 | <div className="code relative min-w-0"> |
| 201 | <div className="absolute top-2.5 right-2.5"> |
| 202 | <CopyButton text={code} /> |
| 203 | </div> |
| 204 | <pre className="overflow-x-auto py-4 pr-20 pl-5 text-sm leading-6"> |
| 205 | <code>{code}</code> |
| 206 | </pre> |
| 207 | </div> |
| 208 | ); |
| 209 | } |
| 210 |
| 211 | /** In Coline's sandbox, links open through the host; on the site, they are plain links. */ |
| 212 | const IN_SANDBOX = typeof window !== "undefined" && window.parent !== window; |
| 213 |
| 214 | function ExternalLink(props: { href: string; children: React.ReactNode; className?: string }) { |
| 215 | return IN_SANDBOX ? ( |
| 216 | <SandboxLink {...props} /> |
| 217 | ) : ( |
| 218 | <a href={props.href} className={props.className} target="_blank" rel="noreferrer"> |
| 219 | {props.children} |
| 220 | </a> |
| 221 | ); |
| 222 | } |
| 223 |
| 224 | function SandboxLink({ href, children, className }: { href: string; children: React.ReactNode; className?: string }) { |
| 225 | const coline = useColine(); |
| 226 | return ( |
| 227 | <a |
| 228 | href={href} |
| 229 | className={className} |
| 230 | onClick={(event) => { |
| 231 | // The page runs in a sandbox without popups; the host opens the tab. |
| 232 | event.preventDefault(); |
| 233 | void coline.navigation.navigate(href); |
| 234 | }} |
| 235 | > |
| 236 | {children} |
| 237 | </a> |
| 238 | ); |
| 239 | } |
| 240 |
| 241 | const icon = "size-5 shrink-0"; |
| 242 |
| 243 | function IconPlay() { |
| 244 | return <svg viewBox="0 0 24 24" className={icon} fill="currentColor"><path d="M8 5.5v13a1 1 0 0 0 1.52.85l10.4-6.5a1 1 0 0 0 0-1.7L9.52 4.65A1 1 0 0 0 8 5.5Z" /></svg>; |
| 245 | } |
| 246 | function IconPause() { |
| 247 | return <svg viewBox="0 0 24 24" className={icon} fill="currentColor"><rect x="6" y="5" width="4" height="14" rx="1.2" /><rect x="14" y="5" width="4" height="14" rx="1.2" /></svg>; |
| 248 | } |
| 249 | function IconSkip({ back = false }: { back?: boolean }) { |
| 250 | return ( |
| 251 | <svg viewBox="0 0 24 24" className={cx(icon, back && "-scale-x-100")} fill="currentColor"> |
| 252 | <path d="M5 6.2v11.6a.9.9 0 0 0 1.4.75l8.3-5.8a.9.9 0 0 0 0-1.5L6.4 5.45A.9.9 0 0 0 5 6.2Z" /> |
| 253 | <rect x="16.5" y="5.5" width="2.5" height="13" rx="1" /> |
| 254 | </svg> |
| 255 | ); |
| 256 | } |
| 257 | function IconMore() { |
| 258 | return <svg viewBox="0 0 24 24" className={icon} fill="currentColor"><circle cx="5.5" cy="12" r="1.8" /><circle cx="12" cy="12" r="1.8" /><circle cx="18.5" cy="12" r="1.8" /></svg>; |
| 259 | } |
| 260 | function IconSun() { |
| 261 | return ( |
| 262 | <svg viewBox="0 0 24 24" className={icon} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"> |
| 263 | <circle cx="12" cy="12" r="4" /> |
| 264 | <path d="M12 2.5v2M12 19.5v2M4.6 4.6 6 6M18 18l1.4 1.4M2.5 12h2M19.5 12h2M4.6 19.4 6 18M18 6l1.4-1.4" /> |
| 265 | </svg> |
| 266 | ); |
| 267 | } |
| 268 | function IconMoon() { |
| 269 | return <svg viewBox="0 0 24 24" className={icon} fill="currentColor"><path d="M20.5 14.6A8.5 8.5 0 0 1 9.4 3.5a8.5 8.5 0 1 0 11.1 11.1Z" /></svg>; |
| 270 | } |
| 271 |
| 272 | // ── Sections ──────────────────────────────────────────── |
| 273 |
| 274 | function Nav({ appearance, onToggle }: { appearance: GlassAppearance; onToggle: () => void }) { |
| 275 | return ( |
| 276 | <div className="sticky top-0 z-20 px-4 pt-4"> |
| 277 | <nav aria-label="Main" className="relative mx-auto flex max-w-xl items-center justify-between gap-3 rounded-full py-2 pr-2 pl-5"> |
| 278 | <LiquidGlass appearance={appearance} /> |
| 279 | <a href="#top" className="text-sm font-semibold tracking-tight">liquid-glass</a> |
| 280 | <div className="flex items-center gap-1 text-sm font-medium"> |
| 281 | <a href="#playground" className="hidden rounded-full px-3 py-1.5 opacity-75 transition-opacity hover:opacity-100 sm:block">Playground</a> |
| 282 | <a href="#install" className="hidden rounded-full px-3 py-1.5 opacity-75 transition-opacity hover:opacity-100 sm:block">Install</a> |
| 283 | <ExternalLink href={SOURCE_URL} className="rounded-full px-3 py-1.5 opacity-75 transition-opacity hover:opacity-100">Source</ExternalLink> |
| 284 | <button |
| 285 | type="button" |
| 286 | onClick={onToggle} |
| 287 | aria-label={appearance === "dark" ? "Use light glass" : "Use dark glass"} |
| 288 | className="liquid-glass-well flex size-9 items-center justify-center rounded-full" |
| 289 | > |
| 290 | {appearance === "dark" ? <IconSun /> : <IconMoon />} |
| 291 | </button> |
| 292 | </div> |
| 293 | </nav> |
| 294 | </div> |
| 295 | ); |
| 296 | } |
| 297 |
| 298 | function Hero() { |
| 299 | return ( |
| 300 | <header id="top" className="mx-auto grid max-w-3xl gap-6 px-6 pt-20 pb-14 text-center"> |
| 301 | <h1 className="text-5xl font-semibold tracking-tight text-balance sm:text-6xl">Liquid Glass for the web</h1> |
| 302 | <p className="mx-auto max-w-xl text-lg text-pretty opacity-70"> |
| 303 | Rim refraction, lighting and materials after iOS 27, computed from a lens profile instead of drawn by hand. One |
| 304 | function for any element, and a component for React. |
| 305 | </p> |
| 306 | <div className="flex flex-wrap items-center justify-center gap-3"> |
| 307 | <div className="code flex items-center gap-1 rounded-full py-1 pr-1 pl-5 text-sm font-medium"> |
| 308 | <span>{INSTALL}</span> |
| 309 | <CopyButton text={INSTALL} /> |
| 310 | </div> |
| 311 | <ExternalLink href={SOURCE_URL} className="rounded-full px-4 py-2 text-sm font-medium opacity-75 transition-opacity hover:opacity-100"> |
| 312 | View source |
| 313 | </ExternalLink> |
| 314 | </div> |
| 315 | </header> |
| 316 | ); |
| 317 | } |
| 318 |
| 319 | type Shaping = { bezel: number; thickness: number; refractiveIndex: number; edge: number; glint: number }; |
| 320 |
| 321 | function snippet(options: { variant: GlassVariant; hue: string | null } & Shaping) { |
| 322 | const lines: string[] = []; |
| 323 | if (options.variant !== "regular") lines.push(`variant="${options.variant}"`); |
| 324 | if (options.hue) lines.push(`hue="${options.hue}"`); |
| 325 | const geometry = (["bezel", "thickness", "refractiveIndex", "edge", "glint"] as const) |
| 326 | .filter((key) => options[key] !== DEFAULT_GEOMETRY[key]) |
| 327 | .map((key) => `${key}: ${options[key]}`); |
| 328 | if (geometry.length) lines.push(`geometry={{ ${geometry.join(", ")} }}`); |
| 329 | const glass = lines.length ? `<LiquidGlass\n ${lines.join("\n ")}\n />` : "<LiquidGlass />"; |
| 330 | return `<div className="relative rounded-full">\n ${glass}\n …\n</div>`; |
| 331 | } |
| 332 |
| 333 | function Playground({ appearance }: { appearance: GlassAppearance }) { |
| 334 | const stage = React.useRef<HTMLDivElement>(null); |
| 335 | const [shape, setShape] = React.useState<Shape>("capsule"); |
| 336 | const [variant, setVariant] = React.useState<GlassVariant>("regular"); |
| 337 | const [hue, setHue] = React.useState<string | null>(null); |
| 338 | const [bezel, setBezel] = React.useState(DEFAULT_GEOMETRY.bezel); |
| 339 | const [thickness, setThickness] = React.useState(DEFAULT_GEOMETRY.thickness); |
| 340 | const [refractiveIndex, setRefractiveIndex] = React.useState(DEFAULT_GEOMETRY.refractiveIndex); |
| 341 | const [edge, setEdge] = React.useState(DEFAULT_GEOMETRY.edge); |
| 342 | const [glint, setGlint] = React.useState(DEFAULT_GEOMETRY.glint); |
| 343 | const [position, setPosition] = React.useState({ x: 0.36, y: 0.5 }); |
| 344 | const grab = React.useRef<{ dx: number; dy: number } | null>(null); |
| 345 | const size = SHAPES[shape]; |
| 346 |
| 347 | const moveTo = (clientX: number, clientY: number) => { |
| 348 | const rect = stage.current?.getBoundingClientRect(); |
| 349 | if (!rect || !grab.current) return; |
| 350 | const halfX = size.width / 2 / rect.width; |
| 351 | const halfY = size.height / 2 / rect.height; |
| 352 | const clamp = (value: number, half: number) => Math.min(1 - half, Math.max(half, value)); |
| 353 | setPosition({ |
| 354 | x: clamp((clientX - rect.left - grab.current.dx) / rect.width, halfX), |
| 355 | y: clamp((clientY - rect.top - grab.current.dy) / rect.height, halfY), |
| 356 | }); |
| 357 | }; |
| 358 |
| 359 | const reset = () => { |
| 360 | setBezel(DEFAULT_GEOMETRY.bezel); |
| 361 | setThickness(DEFAULT_GEOMETRY.thickness); |
| 362 | setRefractiveIndex(DEFAULT_GEOMETRY.refractiveIndex); |
| 363 | setEdge(DEFAULT_GEOMETRY.edge); |
| 364 | setGlint(DEFAULT_GEOMETRY.glint); |
| 365 | }; |
| 366 |
| 367 | // Rendered over the stage on wide screens and under it on narrow ones. |
| 368 | const controls = ( |
| 369 | <> |
| 370 | <Segmented |
| 371 | label="Shape" |
| 372 | value={shape} |
| 373 | onChange={setShape} |
| 374 | appearance={appearance} |
| 375 | options={[ |
| 376 | { value: "capsule", label: "Capsule" }, |
| 377 | { value: "card", label: "Card" }, |
| 378 | { value: "circle", label: "Circle" }, |
| 379 | ]} |
| 380 | /> |
| 381 | <Segmented |
| 382 | label="Material" |
| 383 | value={variant} |
| 384 | onChange={setVariant} |
| 385 | appearance={appearance} |
| 386 | options={[ |
| 387 | { value: "regular", label: "Regular" }, |
| 388 | { value: "clear", label: "Clear" }, |
| 389 | ]} |
| 390 | /> |
| 391 | <div className="grid gap-2"> |
| 392 | <span className="text-sm font-medium opacity-70">Hue</span> |
| 393 | <div role="radiogroup" aria-label="Hue" className="flex gap-2"> |
| 394 | {HUES.map((option) => ( |
| 395 | <button |
| 396 | key={option.label} |
| 397 | type="button" |
| 398 | role="radio" |
| 399 | aria-checked={hue === option.value} |
| 400 | aria-label={option.label} |
| 401 | onClick={() => setHue(option.value)} |
| 402 | className={cx( |
| 403 | "size-7 rounded-full ring-offset-2 ring-offset-transparent transition-shadow", |
| 404 | hue === option.value ? "ring-2 ring-current" : "ring-1 ring-current/25", |
| 405 | )} |
| 406 | style={{ background: option.value ?? "transparent" }} |
| 407 | /> |
| 408 | ))} |
| 409 | </div> |
| 410 | </div> |
| 411 | <Range label="Rim width" value={bezel} min={1} max={40} step={1} onChange={setBezel} /> |
| 412 | <Range label="Depth" value={thickness} min={0} max={60} step={1} onChange={setThickness} /> |
| 413 | <Range label="Refractive index" value={refractiveIndex} min={1} max={2.4} step={0.05} onChange={setRefractiveIndex} /> |
| 414 | <Range label="Edge" value={edge} min={0} max={3} step={0.25} onChange={setEdge} /> |
| 415 | <Range label="Glints" value={glint} min={0} max={1} step={0.05} onChange={setGlint} /> |
| 416 | <button type="button" onClick={reset} className="justify-self-start text-sm font-medium opacity-70 transition-opacity hover:opacity-100"> |
| 417 | Reset shape |
| 418 | </button> |
| 419 | </> |
| 420 | ); |
| 421 |
| 422 | return ( |
| 423 | <section id="playground" className="mx-auto grid max-w-5xl scroll-mt-24 gap-4 px-4 sm:px-6"> |
| 424 | <div ref={stage} className="relative h-[520px] overflow-hidden rounded-[28px]"> |
| 425 | <Scene appearance={appearance} /> |
| 426 | <div |
| 427 | role="slider" |
| 428 | tabIndex={0} |
| 429 | aria-label="Glass position. Drag it, or move it with the arrow keys." |
| 430 | aria-valuetext={`${Math.round(position.x * 100)}% across, ${Math.round(position.y * 100)}% down`} |
| 431 | onPointerDown={(event) => { |
| 432 | const rect = event.currentTarget.getBoundingClientRect(); |
| 433 | grab.current = { dx: event.clientX - rect.left - rect.width / 2, dy: event.clientY - rect.top - rect.height / 2 }; |
| 434 | event.currentTarget.setPointerCapture(event.pointerId); |
| 435 | }} |
| 436 | onPointerMove={(event) => moveTo(event.clientX, event.clientY)} |
| 437 | onPointerUp={() => { |
| 438 | grab.current = null; |
| 439 | }} |
| 440 | onKeyDown={(event) => { |
| 441 | const step = event.shiftKey ? 0.08 : 0.02; |
| 442 | const moves: Record<string, [number, number]> = { |
| 443 | ArrowLeft: [-step, 0], |
| 444 | ArrowRight: [step, 0], |
| 445 | ArrowUp: [0, -step], |
| 446 | ArrowDown: [0, step], |
| 447 | }; |
| 448 | const delta = moves[event.key]; |
| 449 | if (!delta) return; |
| 450 | event.preventDefault(); |
| 451 | setPosition((current) => ({ |
| 452 | x: Math.min(0.95, Math.max(0.05, current.x + delta[0])), |
| 453 | y: Math.min(0.95, Math.max(0.05, current.y + delta[1])), |
| 454 | })); |
| 455 | }} |
| 456 | className={cx( |
| 457 | "absolute flex cursor-grab touch-none items-center justify-center text-center outline-none select-none active:cursor-grabbing", |
| 458 | "focus-visible:ring-2 focus-visible:ring-white/70", |
| 459 | appearance === "dark" ? "text-white" : "text-neutral-900", |
| 460 | )} |
| 461 | style={{ |
| 462 | left: `${position.x * 100}%`, |
| 463 | top: `${position.y * 100}%`, |
| 464 | width: size.width, |
| 465 | height: size.height, |
| 466 | borderRadius: size.radius, |
| 467 | translate: "-50% -50%", |
| 468 | }} |
| 469 | > |
| 470 | <LiquidGlass |
| 471 | appearance={appearance} |
| 472 | variant={variant} |
| 473 | hue={hue} |
| 474 | geometry={{ bezel, thickness, refractiveIndex, edge, glint }} |
| 475 | /> |
| 476 | {shape === "capsule" ? <span className="text-base font-semibold">Drag the glass</span> : null} |
| 477 | {shape === "card" ? ( |
| 478 | <span className="grid gap-1 px-7 text-left"> |
| 479 | <span className="text-lg font-semibold">{variant === "regular" ? "Regular" : "Clear"}</span> |
| 480 | <span className="text-sm opacity-80"> |
| 481 | {variant === "regular" |
| 482 | ? "Diffuses and evens out the page, so text stays legible over anything." |
| 483 | : "Only bends and lights the page. For media and calm backgrounds."} |
| 484 | </span> |
| 485 | </span> |
| 486 | ) : null} |
| 487 | </div> |
| 488 |
| 489 | <div |
| 490 | className={cx( |
| 491 | "absolute inset-y-4 right-4 hidden w-72 overflow-y-auto rounded-[24px] p-5 md:grid md:content-start md:gap-5", |
| 492 | appearance === "dark" ? "text-white" : "text-neutral-900", |
| 493 | )} |
| 494 | > |
| 495 | <LiquidGlass appearance={appearance} fit="scroll" /> |
| 496 | {controls} |
| 497 | </div> |
| 498 | </div> |
| 499 |
| 500 | <div className="grid gap-4"> |
| 501 | <div className="panel grid gap-5 rounded-[24px] p-5 md:hidden"> |
| 502 | {controls} |
| 503 | </div> |
| 504 | <Code code={snippet({ variant, hue, bezel, thickness, refractiveIndex, edge, glint })} /> |
| 505 | </div> |
| 506 | </section> |
| 507 | ); |
| 508 | } |
| 509 |
| 510 | function Player({ appearance }: { appearance: GlassAppearance }) { |
| 511 | const [playing, setPlaying] = React.useState(true); |
| 512 | return ( |
| 513 | <div className="relative h-80 overflow-hidden rounded-[28px]"> |
| 514 | <Scene appearance={appearance} words={false} /> |
| 515 | <div |
| 516 | className={cx( |
| 517 | "absolute inset-x-5 bottom-5 flex items-center gap-3 rounded-full py-2 pr-2 pl-5", |
| 518 | appearance === "dark" ? "text-white" : "text-neutral-900", |
| 519 | )} |
| 520 | > |
| 521 | <LiquidGlass appearance={appearance} interactive /> |
| 522 | <span className="grid min-w-0 flex-1 leading-tight"> |
| 523 | <span className="truncate text-sm font-semibold">Afterglow</span> |
| 524 | <span className="truncate text-sm opacity-70">Tidal Index</span> |
| 525 | </span> |
| 526 | <button type="button" aria-label="Previous" className="flex size-10 items-center justify-center rounded-full opacity-80 hover:opacity-100"> |
| 527 | <IconSkip back /> |
| 528 | </button> |
| 529 | <button |
| 530 | type="button" |
| 531 | aria-label={playing ? "Pause" : "Play"} |
| 532 | onClick={() => setPlaying((current) => !current)} |
| 533 | className="liquid-glass-well flex size-10 items-center justify-center rounded-full" |
| 534 | > |
| 535 | {playing ? <IconPause /> : <IconPlay />} |
| 536 | </button> |
| 537 | <button type="button" aria-label="Next" className="flex size-10 items-center justify-center rounded-full opacity-80 hover:opacity-100"> |
| 538 | <IconSkip /> |
| 539 | </button> |
| 540 | </div> |
| 541 | </div> |
| 542 | ); |
| 543 | } |
| 544 |
| 545 | const MENU_ITEMS = ["Share", "Add to queue", "Download", "Go to album"]; |
| 546 |
| 547 | function Menu({ appearance }: { appearance: GlassAppearance }) { |
| 548 | const [open, setOpen] = React.useState(false); |
| 549 | const [leaving, setLeaving] = React.useState(false); |
| 550 | const [active, setActive] = React.useState<number | null>(null); |
| 551 | const glass = React.useRef<LiquidGlassHandle | null>(null); |
| 552 | const button = React.useRef<HTMLButtonElement>(null); |
| 553 |
| 554 | React.useLayoutEffect(() => { |
| 555 | // The glass grows with the menu's contents (app.css), through its inset. |
| 556 | if (open) void glass.current?.materialize({ duration: 240, scale: 0.96 }); |
| 557 | }, [open]); |
| 558 |
| 559 | const close = React.useCallback(async () => { |
| 560 | setLeaving(true); |
| 561 | await glass.current?.dematerialize({ duration: 160, scale: 0.97 }); |
| 562 | setOpen(false); |
| 563 | setLeaving(false); |
| 564 | setActive(null); |
| 565 | }, []); |
| 566 |
| 567 | React.useEffect(() => { |
| 568 | if (!open) return; |
| 569 | const onKey = (event: KeyboardEvent) => { |
| 570 | if (event.key !== "Escape") return; |
| 571 | void close(); |
| 572 | button.current?.focus(); |
| 573 | }; |
| 574 | window.addEventListener("keydown", onKey); |
| 575 | return () => window.removeEventListener("keydown", onKey); |
| 576 | }, [open, close]); |
| 577 |
| 578 | const tone = appearance === "dark" ? "text-white" : "text-neutral-900"; |
| 579 | return ( |
| 580 | <div |
| 581 | className="relative h-80 overflow-hidden rounded-[28px]" |
| 582 | onClick={(event) => { |
| 583 | // A click anywhere outside the menu and its button closes it. |
| 584 | if (open && !(event.target as Element).closest("[role=menu], button")) void close(); |
| 585 | }} |
| 586 | > |
| 587 | <Scene appearance={appearance} words={false} /> |
| 588 | <button |
| 589 | ref={button} |
| 590 | type="button" |
| 591 | aria-label="More" |
| 592 | aria-expanded={open} |
| 593 | onClick={() => (open ? void close() : setOpen(true))} |
| 594 | className={cx("absolute top-5 right-5 flex size-11 items-center justify-center rounded-full", tone)} |
| 595 | > |
| 596 | <LiquidGlass appearance={appearance} interactive /> |
| 597 | <IconMore /> |
| 598 | </button> |
| 599 | {open ? ( |
| 600 | <div role="menu" aria-label="Track" className={cx("absolute top-[76px] right-5 w-52 rounded-[22px] p-1.5", tone)}> |
| 601 | <LiquidGlass appearance={appearance} glassRef={glass} floating /> |
| 602 | <div className={cx("menu-content grid", leaving && "menu-content-leaving")}> |
| 603 | {MENU_ITEMS.map((item, index) => ( |
| 604 | <button |
| 605 | key={item} |
| 606 | type="button" |
| 607 | role="menuitem" |
| 608 | onPointerEnter={() => setActive(index)} |
| 609 | onFocus={() => setActive(index)} |
| 610 | onClick={() => void close()} |
| 611 | className={cx("rounded-[16px] px-3.5 py-2 text-left text-sm font-medium outline-none", active === index && "liquid-glass-well")} |
| 612 | > |
| 613 | {item} |
| 614 | </button> |
| 615 | ))} |
| 616 | </div> |
| 617 | </div> |
| 618 | ) : null} |
| 619 | </div> |
| 620 | ); |
| 621 | } |
| 622 |
| 623 | const PERIODS = [ |
| 624 | { value: "day", label: "Day" }, |
| 625 | { value: "week", label: "Week" }, |
| 626 | { value: "month", label: "Month" }, |
| 627 | { value: "year", label: "Year" }, |
| 628 | ] as const; |
| 629 |
| 630 | type Period = (typeof PERIODS)[number]["value"]; |
| 631 |
| 632 | function Tabs({ appearance }: { appearance: GlassAppearance }) { |
| 633 | const [period, setPeriod] = React.useState<Period>("week"); |
| 634 | return ( |
| 635 | <div className="relative grid h-56 place-items-center overflow-hidden rounded-[28px] md:col-span-2"> |
| 636 | <Scene appearance={appearance} /> |
| 637 | <GlassTabs |
| 638 | label="Period" |
| 639 | value={period} |
| 640 | onChange={setPeriod} |
| 641 | appearance={appearance} |
| 642 | options={[...PERIODS]} |
| 643 | className={cx("w-full max-w-sm", appearance === "dark" ? "text-white" : "text-neutral-900")} |
| 644 | /> |
| 645 | </div> |
| 646 | ); |
| 647 | } |
| 648 |
| 649 | function InUse({ appearance }: { appearance: GlassAppearance }) { |
| 650 | return ( |
| 651 | <section className="mx-auto grid max-w-5xl gap-6 px-4 pt-24 sm:px-6"> |
| 652 | <div className="grid gap-2 px-2"> |
| 653 | <h2 className="text-2xl font-semibold tracking-tight">In use</h2> |
| 654 | <p className="max-w-xl opacity-70"> |
| 655 | The selected tab lifts into a lens that travels to the next one, and you can drag it across the others. Glass |
| 656 | that holds controls lights up under the pointer, and menus materialize from clear glass as they open. |
| 657 | </p> |
| 658 | </div> |
| 659 | <div className="grid gap-4 md:grid-cols-2"> |
| 660 | <Tabs appearance={appearance} /> |
| 661 | <Player appearance={appearance} /> |
| 662 | <Menu appearance={appearance} /> |
| 663 | </div> |
| 664 | </section> |
| 665 | ); |
| 666 | } |
| 667 |
| 668 | const REACT_USAGE = `import { LiquidGlass } from "@colineapp/liquid-glass/react"; |
| 669 | import "@colineapp/liquid-glass/styles.css"; |
| 670 |
| 671 | export function Toolbar() { |
| 672 | return ( |
| 673 | <nav className="relative rounded-full px-6 py-3"> |
| 674 | <LiquidGlass appearance="class" /> |
| 675 | … |
| 676 | </nav> |
| 677 | ); |
| 678 | }`; |
| 679 |
| 680 | const VANILLA_USAGE = `import { createLiquidGlass } from "@colineapp/liquid-glass"; |
| 681 | import "@colineapp/liquid-glass/styles.css"; |
| 682 |
| 683 | const glass = createLiquidGlass(document.querySelector(".toolbar"), { |
| 684 | variant: "clear", |
| 685 | }); |
| 686 |
| 687 | // Later |
| 688 | glass.update({ variant: "regular" }); |
| 689 | glass.destroy();`; |
| 690 |
| 691 | const TABS_USAGE = `import { useLiquidGlassTabs } from "@colineapp/liquid-glass/react"; |
| 692 |
| 693 | export function Period({ value, onChange }) { |
| 694 | const track = useRef(null); |
| 695 | useLiquidGlassTabs(track); |
| 696 | return ( |
| 697 | <div ref={track} role="tablist" className="flex rounded-full p-1"> |
| 698 | {["Day", "Week", "Month"].map((label) => ( |
| 699 | <button key={label} role="tab" aria-selected={label === value} onClick={() => onChange(label)}> |
| 700 | {label} |
| 701 | </button> |
| 702 | ))} |
| 703 | </div> |
| 704 | ); |
| 705 | }`; |
| 706 |
| 707 | function Install() { |
| 708 | return ( |
| 709 | <section id="install" className="mx-auto grid max-w-5xl scroll-mt-24 gap-6 px-4 pt-24 sm:px-6"> |
| 710 | <div className="grid gap-2 px-2"> |
| 711 | <h2 className="text-2xl font-semibold tracking-tight">Install</h2> |
| 712 | <p className="max-w-xl opacity-70"> |
| 713 | Put the glass first inside a positioned, rounded element. It takes the element’s corner radius and sits behind its |
| 714 | content. Paint nothing over it: no background, border or shadow on the element. |
| 715 | </p> |
| 716 | </div> |
| 717 | <Code code={INSTALL} /> |
| 718 | <div className="grid gap-4 md:grid-cols-2"> |
| 719 | <div className="grid min-w-0 content-start gap-3"> |
| 720 | <h3 className="px-2 font-semibold">React</h3> |
| 721 | <Code code={REACT_USAGE} /> |
| 722 | </div> |
| 723 | <div className="grid min-w-0 content-start gap-3"> |
| 724 | <h3 className="px-2 font-semibold">Any element</h3> |
| 725 | <Code code={VANILLA_USAGE} /> |
| 726 | </div> |
| 727 | </div> |
| 728 | <div className="grid min-w-0 content-start gap-3"> |
| 729 | <h3 className="px-2 font-semibold">Tabs</h3> |
| 730 | <p className="max-w-xl px-2 text-sm opacity-70"> |
| 731 | Your tabs and selection stay yours. The thumb follows aria-selected, aria-checked or Radix’s data-state, and |
| 732 | a tab it is dragged onto gets clicked. |
| 733 | </p> |
| 734 | <Code code={TABS_USAGE} /> |
| 735 | </div> |
| 736 | </section> |
| 737 | ); |
| 738 | } |
| 739 |
| 740 | const DETAILS = [ |
| 741 | { |
| 742 | title: "A real lens", |
| 743 | body: "Each rim pixel traces a ray through a squircle-shaped bevel with Snell’s law. The shift is capped where it would fold the page into mirrored echoes, so content compresses toward the rim instead.", |
| 744 | }, |
| 745 | { |
| 746 | title: "An edge that catches the light", |
| 747 | body: "A darkened contour separates the glass from what is behind it, with the rim’s hairline just inside. Light from the top-left gathers into glints where it catches the curve.", |
| 748 | }, |
| 749 | { |
| 750 | title: "Any size, mid-animation", |
| 751 | body: "The lens is rendered once as nine slices: fixed corners and stretched edges. Surfaces can resize and change radius while the glass stays on.", |
| 752 | }, |
| 753 | { |
| 754 | title: "A lens that travels", |
| 755 | body: "The tab thumb moves like iOS 27’s tab bar: it lifts into a lens that magnifies the labels beneath, stretches with its speed and squashes as it lands. Four samples a pixel keep the labels it bends from stair-stepping.", |
| 756 | }, |
| 757 | { |
| 758 | title: "Every browser", |
| 759 | body: "Refraction needs SVG filters in backdrop-filter, which Chromium renders. Safari and Firefox get the same material without the bend.", |
| 760 | }, |
| 761 | { |
| 762 | title: "Accessible", |
| 763 | body: "Reduced transparency swaps the glass for a solid fill, increased contrast darkens the sides and edge, and forced colours hide it. Reduced motion keeps the tab thumb still.", |
| 764 | }, |
| 765 | ]; |
| 766 |
| 767 | function Details() { |
| 768 | return ( |
| 769 | <section className="mx-auto grid max-w-5xl gap-4 px-4 pt-24 sm:grid-cols-2 sm:px-6"> |
| 770 | {DETAILS.map((detail) => ( |
| 771 | <div key={detail.title} className="panel grid content-start gap-2 rounded-[24px] p-6"> |
| 772 | <h3 className="font-semibold">{detail.title}</h3> |
| 773 | <p className="text-sm leading-6 opacity-70">{detail.body}</p> |
| 774 | </div> |
| 775 | ))} |
| 776 | </section> |
| 777 | ); |
| 778 | } |
| 779 |
| 780 | function Footer() { |
| 781 | return ( |
| 782 | <footer className="mx-auto flex max-w-5xl flex-wrap items-center justify-between gap-3 px-6 pt-24 pb-12 text-sm opacity-70"> |
| 783 | <span>MIT license. Made by Coline.</span> |
| 784 | <span className="flex gap-4"> |
| 785 | <ExternalLink href={SOURCE_URL} className="hover:underline">upstream.codes</ExternalLink> |
| 786 | <ExternalLink href={GITHUB_URL} className="hover:underline">GitHub</ExternalLink> |
| 787 | </span> |
| 788 | </footer> |
| 789 | ); |
| 790 | } |
| 791 |
| 792 | export function Site() { |
| 793 | const [appearance, setAppearance] = React.useState<GlassAppearance>("dark"); |
| 794 | return ( |
| 795 | <div className={cx("site min-h-dvh", appearance === "dark" ? "site-dark" : "site-light")}> |
| 796 | <Nav appearance={appearance} onToggle={() => setAppearance((current) => (current === "dark" ? "light" : "dark"))} /> |
| 797 | <Hero /> |
| 798 | <Playground appearance={appearance} /> |
| 799 | <InUse appearance={appearance} /> |
| 800 | <Install /> |
| 801 | <Details /> |
| 802 | <Footer /> |
| 803 | </div> |
| 804 | ); |
| 805 | } |
| 806 |
| 807 | // The app's home inside Coline. The public site mounts from site.tsx. |
| 808 | const root = document.getElementById("root"); |
| 809 | if (root && IN_SANDBOX) { |
| 810 | createRoot(root).render( |
| 811 | <ColineAppProvider> |
| 812 | <Site /> |
| 813 | </ColineAppProvider>, |
| 814 | ); |
| 815 | } |
| 816 |