Glass Code Block
A premium frosted-glass code editor window with a regex tokenizer and line-by-line stagger reveal.
Source
components/kuchi/glass-code-block.tsx
1"use client";23import { interpolate, Sequence, useCurrentFrame } from "remotion";45export interface GlassCodeBlockProps {6 code?: string;7 title?: string;8 width?: number;9 height?: number;10 fontSize?: number;11 glassColor?: string;12 staggerFrames?: number;13 showTrafficLights?: boolean;14 aura?: boolean;15 speed?: number;16 className?: string;17}1819const FONT_MONO =20 "var(--font-geist-mono), ui-monospace, SFMono-Regular, monospace";2122const DEFAULT_CODE = `import { motion } from "remotion";2324// Generate a hero scene25export function Hero() {26 const frame = useCurrentFrame();27 const opacity = frame / 30;28 return <h1 style={{ opacity }}>Hello</h1>;29}`;3031// Minimal regex tokenizer. NOT a real syntax highlighter — just enough to32// give the eye color anchors. Order matters: comments → strings → keywords.33const KEYWORDS = new Set([34 "import",35 "from",36 "export",37 "function",38 "const",39 "let",40 "var",41 "return",42 "if",43 "else",44 "for",45 "while",46 "new",47 "class",48 "extends",49 "default",50 "true",51 "false",52 "null",53 "undefined",54]);5556type Token = {57 text: string;58 kind: "code" | "comment" | "string" | "keyword" | "number";59};6061function tokenizeLine(line: string): Token[] {62 // Whole-line comment.63 const trimmed = line.trimStart();64 if (trimmed.startsWith("//")) {65 return [{ text: line, kind: "comment" }];66 }6768 const tokens: Token[] = [];69 // Split keeping delimiters: words, strings, numbers, everything else.70 const re = /("[^"]*"|'[^']*'|`[^`]*`|\b\d+\b|\b[A-Za-z_$][\w$]*\b|[^\w"']+)/g;71 let match: RegExpExecArray | null = re.exec(line);72 while (match !== null) {73 const t = match[0];74 const first = t[0];75 if (first === '"' || first === "'" || first === "`") {76 tokens.push({ text: t, kind: "string" });77 } else if (/^\d+$/.test(t)) {78 tokens.push({ text: t, kind: "number" });79 } else if (/^[A-Za-z_$][\w$]*$/.test(t) && KEYWORDS.has(t)) {80 tokens.push({ text: t, kind: "keyword" });81 } else {82 tokens.push({ text: t, kind: "code" });83 }84 match = re.exec(line);85 }86 return tokens;87}8889const TOKEN_COLORS: Record<Token["kind"], string> = {90 code: "#e4e4e7",91 comment: "#52525b",92 string: "#86efac",93 keyword: "#c4b5fd",94 number: "#fcd34d",95};9697export function GlassCodeBlock({98 code = DEFAULT_CODE,99 title = "hero.tsx",100 width = 760,101 height = 460,102 fontSize = 16,103 glassColor = "rgba(10, 10, 10, 0.6)",104 staggerFrames = 4,105 showTrafficLights = true,106 aura = false,107 speed = 1,108 className,109}: GlassCodeBlockProps) {110 const lines = code.split("\n");111112 return (113 <div114 className={className}115 style={{116 position: "absolute",117 inset: 0,118 display: "flex",119 alignItems: "center",120 justifyContent: "center",121 }}122 >123 {aura && <BackdropAura />}124125 {/* 1px gradient ring acting as a microborder */}126 <div127 style={{128 position: "relative",129 padding: 1,130 borderRadius: 16,131 background:132 "linear-gradient(180deg, rgba(255,255,255,0.18) 0%, rgba(255,255,255,0) 100%)",133 width,134 height,135 boxShadow: "0 50px 120px rgba(0,0,0,0.55)",136 }}137 >138 <div139 style={{140 width: "100%",141 height: "100%",142 borderRadius: 15,143 background: glassColor,144 backdropFilter: "blur(16px)",145 WebkitBackdropFilter: "blur(16px)",146 boxShadow: "inset 0 1px 0 rgba(255,255,255,0.06)",147 display: "flex",148 flexDirection: "column",149 overflow: "hidden",150 fontFamily: FONT_MONO,151 }}152 >153 {/* Chrome */}154 <div155 style={{156 height: 40,157 display: "flex",158 alignItems: "center",159 gap: 8,160 padding: "0 16px",161 borderBottom: "1px solid rgba(255,255,255,0.06)",162 }}163 >164 {showTrafficLights && (165 <>166 <Light color="#ff5f57" />167 <Light color="#febc2e" />168 <Light color="#28c840" />169 </>170 )}171 <div172 style={{173 flex: 1,174 textAlign: "center",175 color: "#a1a1aa",176 fontSize: 12,177 letterSpacing: "0.02em",178 }}179 >180 {title}181 </div>182 </div>183184 {/* Code body */}185 <div186 style={{187 flex: 1,188 padding: "20px 24px",189 display: "flex",190 flexDirection: "column",191 gap: 4,192 fontSize,193 lineHeight: 1.55,194 }}195 >196 {lines.map((line, i) => (197 <Sequence198 key={i}199 from={Math.round((i * staggerFrames) / speed)}200 layout="none"201 >202 <CodeLine line={line} index={i} fontSize={fontSize} />203 </Sequence>204 ))}205 </div>206 </div>207 </div>208 </div>209 );210}211212function Light({ color }: { color: string }) {213 return (214 <div215 style={{216 width: 12,217 height: 12,218 borderRadius: "50%",219 background: color,220 opacity: 0.6,221 }}222 />223 );224}225226function CodeLine({227 line,228 index,229 fontSize,230}: {231 line: string;232 index: number;233 fontSize: number;234}) {235 const frame = useCurrentFrame();236 const opacity = interpolate(frame, [0, 8], [0, 1], {237 extrapolateLeft: "clamp",238 extrapolateRight: "clamp",239 });240 const ty = interpolate(frame, [0, 8], [4, 0], {241 extrapolateLeft: "clamp",242 extrapolateRight: "clamp",243 });244245 const tokens = tokenizeLine(line);246 // Render an empty line as a half-height spacer so blank lines still take247 // visual space without collapsing the gap.248 if (tokens.length === 0) {249 return <div style={{ height: fontSize * 0.8, opacity }} />;250 }251 return (252 <div253 style={{254 opacity,255 translate: `0 ${ty}px`,256 whiteSpace: "pre",257 display: "flex",258 gap: 0,259 }}260 >261 <span style={{ width: 28, color: "#3f3f46", userSelect: "none" }}>262 {String(index + 1).padStart(2, " ")}263 </span>264 <span>265 {tokens.map((t, i) => (266 <span key={i} style={{ color: TOKEN_COLORS[t.kind] }}>267 {t.text}268 </span>269 ))}270 </span>271 </div>272 );273}274275function BackdropAura() {276 // Slow, low-amplitude blob behind the glass so the backdrop blur has277 // perceptible content to refract.278 const frame = useCurrentFrame();279 const t = frame / 60;280 const x = 50 + Math.sin(t) * 20;281 const y = 50 + Math.cos(t * 0.7) * 15;282 return (283 <div284 style={{285 position: "absolute",286 inset: 0,287 pointerEvents: "none",288 background: `radial-gradient(circle at ${x}% ${y}%, rgba(56,189,248,0.22), transparent 50%), radial-gradient(circle at ${100 - x}% ${100 - y}%, rgba(168,85,247,0.18), transparent 55%)`,289 }}290 />291 );292}





