This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Motif is a tiny (~200 lines) TypeScript library for creating data-driven SVGs with a declarative API. It has zero dependencies.
npm run build # Compile TypeScript to dist/
npm run dev # Watch mode compilation
npm run example # Build and run examples/bar-chart.ts
npm run examples # Build and run all examples
# Run a single example directly
npx tsx examples/mandala.tsThe library exports two main functions:
-
svg(definition, options)- Low-level function that converts a declarative object into an SVG string. Handles element rendering, attribute processing, and iteration. -
plot(config, options)- High-level charting function with automatic axis scaling, tick generation, and support for scatter, line, bar, and area chart types. Built on top ofsvg().
- src/svg.ts - Core rendering engine with
$each,$if,$text,$rawdirectives - src/plot.ts - Charting layer with scale utilities and axis rendering
- src/index.ts - Re-exports both modules
Context Object - Functions in definitions receive { d, i, data, parent }:
d(aliasvalue) - current data itemi(aliasindex) - current iteration indexdata- full array being iteratedparent- parent context for nested$each
Special Directives:
$each- iterate over array, creating element per item$if- conditional rendering$text- text content for<text>elements$raw- inject pre-rendered SVG strings
Element vs Attribute Keys - Keys matching SVG element names (rect, circle, path, etc.) create child elements; all other keys become attributes.
Open playground.html in a browser for interactive experimentation - it includes the library inline and several example visualizations.