@popcraft/kit
v2.2.3
Published
The PopCraft design kit: everything PopCraft's own templates are made with — the Kit, every helper, style system and theme kit, every official template source to read and remix, the build and every check the official templates pass — plus the design-scrip
Readme
@popcraft/kit
The library PopCraft designs are made with. PopCraft's own templates are written with this Kit,
and so are the design scripts its in-editor agent and the popcraft command line run. Code written against it builds
ordinary, editable PopCraft layers: brand colour roles, text styles, auto-layout stacks, components, motion, size
variants and music.
npm i @popcraft/kit # Node 24 or newerTwo ways to use it
Run a design script, the same text the write_design command takes, in a sandbox, and get the document and the
checks back:
import { runDesign, toPopcraft } from '@popcraft/kit'
import { writeFileSync } from 'node:fs'
const { document, issues, error } = await runDesign(`
k.brand({ paper: '#F4F1EA', ink: '#141414', hot: '#E8361E' })
k.textStyle('Hook', { size: 120, fontWeight: 900, lineHeight: 110 })
k.flowSheet(k.sheet, 32, 96, 'paper')
k.text(k.sheet, 'Half price, this week only.', { style: 'Hook', color: 'ink', width: 'FILL' })
`, { size: 'ig-post', name: 'Sale' })
if (error) throw new Error(`line ${error.line}: ${error.message}`)
for (const i of issues) console.log(i.kind, i.message) // contrast, small text, safe areas, overlap, seams…
writeFileSync('sale.popcraft', toPopcraft(document!))Build directly, with the Kit and its helpers as ordinary imports:
import { Kit, preset, recipes, beat } from '@popcraft/kit'
const k = new Kit('Landing', preset('web-desktop'))
// … k.brand, k.textStyle, recipes.web.page, recipes.web.nav, k.stack, k.text …
const document = k.finish()Templates, as PopCraft's own are made
Everything PopCraft's gallery is made with is in this package:
- Every official template's source. 1,275 of them, readable in
src/templates/and loaded withofficialModule('templates/<path>')(see "Public API" below).dist/templates/index.jsoncatalogues them by id. - Every module they use. The Kit, the helpers, twelve SaaS style systems, fifty-odd theme kits, the comic and
anime engines, the Lottie kit, 3D devices and stages, and the music library. The app modules the sources import are
officialModule('lib/<path>'). - The official build (
buildFromSource), with real type first (prepareFonts) and PopCraft's music fetched as needed. - Every check the official templates pass (
checkTemplate): practices, contrast, type size, safe areas, the trim, finished timelines and golden poses for every cue, real-type layout and hugging, responsive web, brand-kit re-skins and editability.
import { prepareFonts, buildFromSource, checkTemplate, officialModule } from '@popcraft/kit'
const { default: official } = await officialModule('templates/social/promo-sale-drop')
const mine = { ...official, id: 'my-sale-drop' }
await prepareFonts() // real type: every character the official templates draw
const template = await buildFromSource(mine) // what template.json holds; `popcraft template publish` uploads it
console.log(await checkTemplate(template, { source: mine })) // [] when it passes every official checkThe command line runs all of it, plus the gallery's pictures and a review render of every size:
popcraft template list|new|check|look|pictures|publish. The guide is
Making templates.
A design script (below) can also become a template with toTemplate(document, listing).
Public API
The package root (@popcraft/kit) is the public API, covered by semver: every name in the table below, and the types
exported beside them (PagePreset, CustomTrack, TrackRef, Score, Bed, TemplateRule, CheckIssue,
CheckTemplateOptions, TemplateCheck, Inset, TemplateSource, TemplateUpload, Document…).
There are no other entries but @popcraft/kit/design-script (types for scripts). The subpaths
@popcraft/kit/lib/*, @popcraft/kit/templates/* and @popcraft/kit/migrations/* went in 1.0.0 (see the
CHANGELOG to move). officialModule('templates/…' | 'lib/…' | 'migrations/…') loads one of the modules the official
sources are written with, which is what a source copied out with popcraft template new --from uses: the function is
covered by semver, the modules' contents are the app's own and change with it.
Extending it
import { registerPreset, registerTrack, preset, beat, checkTemplate, recipes } from '@popcraft/kit'
registerPreset({ id: 'acme-billboard', name: 'Acme billboard', category: 'social', width: 1200, height: 400, unit: 'px' })
new Kit('Board', preset('acme-billboard')) // and `{ preset: 'acme-billboard' }` in variants
registerTrack('acme-jingle', { bpm: 100, durationMs: 4800, hash: media('./jingle.wav') }) // or { url: 'https://…' }
beat.score('acme-jingle') // anywhere a TrackId goes; inline objects work too
await checkTemplate(template, {
disable: ['end-screen'], // any TEMPLATE_CHECKS entry, a rule's id, or 'official'
rules: [{ id: 'acme-logo', check: t => (hasLogo(t) ? [] : [{ check: 'acme-logo', message: 'No logo' }]) }],
componentIds: myEditorIds, fontDir: './fonts', // instead of the kit's own
})
// House style is opt-in, and its platform rules are data:
recipes.social.postVariants({ portrait: 72 }, {}, { ...recipes.social.SOCIAL_SIZES, verticalSafe: { top: 300, right: 80, bottom: 320, left: 80 } })A url track is downloaded once by the kit's build (buildFromSource, buildTemplateSource) into
~/.cache/popcraft/media and written beside template.json like any media file; in a sandboxed design script give
its hash. prepareFonts(texts, { fontDir, official: false }) measures with faces of your own and skips warming the
official templates' characters.
Expression functions (the =… bindings layers evaluate) cannot be extended yet: the runtime has no registration hook.
Types for scripts
A design script runs with k, W, H, NAME, TRACKS and the helpers as globals. Start one with
/// <reference types="@popcraft/kit/design-script" />for completion and checking in your editor.
What is in it
| Export | What it is |
| --- | --- |
| Kit, hex, preset, ease | The kit, a colour from hex, a size by its preset id, animation curves |
| registerPreset, unregisterPreset, findPreset | Page sizes of your own |
| registerTrack, unregisterTrack | Music of your own, by id (or pass a CustomTrack inline) |
| recipes | PopCraft's house style, opt in: recipes.social, .video, .site, .web, .effects |
| draw, marks | Vector paths: boxes, lines, curves; grids, dots, confetti, bursts, arrows, charts |
| texture, overlays | Shadows, glows, halos, grain; scanlines, rain, snow, halftone |
| motion, beat, captions, pointer | Motion recipes, music beds and beat timing, caption tracks, cursor paths |
| fit, type, windows, devices | Laying a design out at another size, font pairs, app and browser windows, device frames |
| runDesign, checkDesign, tracks | Run a script in a sandbox; the design checks; every music track's tempo |
| toPopcraft, fromPopcraft | A document as a .popcraft file's text, and back |
| defineTemplate, buildFromSource (buildTemplateSource without fetching), prepareFonts, checkTemplate, TEMPLATE_CHECKS, media, templatePopcraft | Templates as the official ones are made: a source, its build, every official check, your own media, a file to look at |
| toTemplate | A design script's document as a template |
| officialModule | A module the official sources are written with, by path (what a copied-out source loads) |
| mediaHash, TEMPLATE_CATEGORIES, TEMPLATE_PLATFORMS, TEMPLATE_FORMATS, TEMPLATE_USE_CASES | A media file's media:// hash; the gallery's vocabulary |
Examples
In getpopcraft/examples: event cards from a JSON file, a design check for CI, a typed design script.
Documentation
- Design reference: the kit by topic, editing designs, how decisions are made, and worked examples that build with no failing check.
- Designing with an AI agent: the build, check, look and fix loop.
- Command line:
popcraft run file.popcraft write_design --script @design.js.
Release notes: CHANGELOG.md.
