import { useId, useRef, useState, type MouseEvent, type PointerEvent } from 'react' import { useClickAway } from 'react-use' import { cn } from 'ui' import { GlyphLines } from 'ui-patterns/GlyphLines' import { COLUMNS, fromHandleView, INITIAL_SHAPES, LETTER_PATHS, offsetPoint, ROWS, SLANT, SLANT_ORIGINS, SYMBOL_HEIGHT, SYMBOL_OUTLINE, SYMBOL_WIDTH, toHandleView, toPathData, WORDMARK_COLUMNS, WORDMARK_ROWS, WORDMARK_WIDTH, type PathNode, type Point, } from './logo-geometry' export interface VectorLogoProps { guideText: string className?: string } type DragTarget = (typeof DRAG_TARGETS)[number] interface NodeRef { shape: number subpath: number node: number } // gradientId names the overlay gradient, and the drop shadow filter is `${gradientId}-shadow` interface RaisedPathsProps { paths: { key: string; d: string }[] gradientId: string } interface Drag { ref: NodeRef target: DragTarget start: Point node: PathNode } // px per symbol unit. the viewBox draws 1:1, so a 1 unit stroke stays 1px at any scale const SCALE = 1.1 const PADDING = 24 // a guide runs this far from the symbol. it must pass every edge of the hero const REACH = 2400 const HATCH_SPACING = 6 const MORPH_TRANSITION = 'transition-[transform,opacity] duration-500 ease-out motion-reduce:transition-none' const DRAG_TARGETS = ['point', 'in', 'out'] as const const getAnchorSize = (shapeIndex: number) => (shapeIndex === 0 ? 7 : 5) // lines draw in order, and undraw in reverse after the fill fades out const getDrawDelay = ({ index, count, isDrawn, }: { index: number count: number isDrawn: boolean }) => (isDrawn ? index * 60 : 300 + (count - 1 - index) * 60) export const VectorLogo = ({ guideText, className }: VectorLogoProps) => { const [mode, setMode] = useState<'icon' | 'wordmark'>('icon') const [shapes, setShapes] = useState(INITIAL_SHAPES) const [selected, setSelected] = useState(null) const dragRef = useRef(null) const canvasRef = useRef(null) // the guide lines have no stroke. GlyphLines draws them, and marks the svg ready so the draw-in waits for hydration const guidesRef = useRef(null) const gradientId = useId() const hatchId = useId() const isWordmark = mode === 'wordmark' const subjectWidth = isWordmark ? WORDMARK_WIDTH : SYMBOL_WIDTH const width = WORDMARK_WIDTH * SCALE + PADDING * 2 const height = SYMBOL_HEIGHT * SCALE + PADDING * 2 const toView = (value: number) => value * SCALE + PADDING const shiftStyle = { transform: `translateX(${((WORDMARK_WIDTH - subjectWidth) / 2) * SCALE}px)`, } const contentTransform = `translate(${PADDING} ${PADDING}) scale(${SCALE})` const [symbolShape, ...letterShapes] = shapes const symbolPath = toPathData(symbolShape) const letterPaths = letterShapes.map(toPathData) const isShapeShown = (shapeIndex: number) => shapeIndex === 0 || isWordmark const selectedNode = selected && isShapeShown(selected.shape) ? shapes[selected.shape][selected.subpath][selected.node] : null const handleViews = selectedNode ? (['in', 'out'] as const).flatMap((target) => { const handle = selectedNode[target] return handle ? [{ target, view: toHandleView({ anchor: selectedNode.point, handle, scale: SCALE }) }] : [] }) : [] const slantLength = Math.hypot(SLANT.dx, SLANT.dy) const guides = [ ...ROWS.map((y) => [-REACH, toView(y), REACH, toView(y)]), ...COLUMNS.map((x) => [toView(x), -REACH, toView(x), REACH]), ...SLANT_ORIGINS.map(([x, y]) => { const dx = (SLANT.dx / slantLength) * REACH const dy = (SLANT.dy / slantLength) * REACH return [toView(x) - dx, toView(y) - dy, toView(x) + dx, toView(y) + dy] }), ] const wordmarkGuides = [ ...WORDMARK_ROWS.map((y) => [-REACH, toView(y), REACH, toView(y)]), ...WORDMARK_COLUMNS.map((x) => [toView(x), -REACH, toView(x), REACH]), ] const svgProps = { width, height, viewBox: `0 0 ${width} ${height}`, fill: 'none', } const handleCanvasPointerDown = () => setSelected(null) const handleLogoClick = () => setMode(isWordmark ? 'icon' : 'wordmark') useClickAway(canvasRef, () => setSelected(null)) // every point carries its place in data attributes, so one handler serves all of them const handlePointPointerDown = (event: PointerEvent) => { if (event.button !== 0) return event.stopPropagation() const { dataset } = event.currentTarget const ref = { shape: Number(dataset.shape), subpath: Number(dataset.subpath), node: Number(dataset.node), } const target = DRAG_TARGETS.find((value) => value === dataset.target) ?? 'point' event.currentTarget.setPointerCapture(event.pointerId) dragRef.current = { ref, target, start: [event.clientX, event.clientY], node: shapes[ref.shape][ref.subpath][ref.node], } if (target === 'point') setSelected(ref) } const handlePointPointerMove = (event: PointerEvent) => { const drag = dragRef.current if (!drag) return const { node, target } = drag const dx = (event.clientX - drag.start[0]) / SCALE const dy = (event.clientY - drag.start[1]) / SCALE const handle = target === 'point' ? undefined : node[target] const moved = handle ? { ...node, [target]: fromHandleView({ anchor: node.point, view: offsetPoint({ point: toHandleView({ anchor: node.point, handle, scale: SCALE }), dx, dy, }) ?? handle, scale: SCALE, }), } : { id: node.id, point: [node.point[0] + dx, node.point[1] + dy] satisfies Point, in: offsetPoint({ point: node.in, dx, dy }), out: offsetPoint({ point: node.out, dx, dy }), } setShapes((current) => { const next = structuredClone(current) next[drag.ref.shape][drag.ref.subpath][drag.ref.node] = moved return next }) } // capture also ends on pointerup, and on a cancelled touch that sends no pointerup const handlePointDragEnd = () => { dragRef.current = null } const handlePointDoubleClick = (event: MouseEvent) => { const { dataset } = event.currentTarget const shape = Number(dataset.shape) const subpath = Number(dataset.subpath) const node = Number(dataset.node) setShapes((current) => { const next = structuredClone(current) next[shape][subpath][node] = INITIAL_SHAPES[shape][subpath][node] return next }) } return (
{guides.map(([x1, y1, x2, y2], index) => ( ))} {wordmarkGuides.map(([x1, y1, x2, y2], index) => ( ))}
({ key, d: letterPaths[index] }))} gradientId={gradientId} /> {LETTER_PATHS.map((key, index) => ( ))} {selected && selectedNode ? ( {handleViews.map(({ target, view }) => ( ))} ) : null} {shapes.map((shape, shapeIndex) => ( 0 && MORPH_TRANSITION, isShapeShown(shapeIndex) ? 'pointer-events-auto opacity-100' : 'opacity-0' )} > {shape.map((nodes, subpathIndex) => nodes.map((node, nodeIndex) => ( )) )} ))} {selected && selectedNode ? ( {handleViews.map(({ target, view }) => ( ))} ) : null}
{/* the drawing is aria-hidden, so a visually hidden button is the keyboard path to the same toggle */}
) } // muted is translucent, so an opaque base under it carries the drop shadow and keeps the guides from showing through const RaisedPaths = ({ paths, gradientId }: RaisedPathsProps) => ( <> {paths.map(({ key, d }) => ( ))} {paths.map(({ key, d }) => ( ))} {paths.map(({ key, d }) => ( ))} )