Searching Puzzle

Type to search Puzzle.

App Structure

How a Puzzle app fits together: views, router, store, and CLI, and how data flows between them.

Puzzle will feel new even if you've shipped apps in other frameworks. This page gives you the mental model before you write any code: what each folder is for, which subsystem owns which job, and what actually happens between a click and a re-render. The three pages after this one put the model to work by building a small app.

The big idea

A Puzzle app is made of single-file components (.pzl files holding a template, a script, and optional styles) compiled by a Go toolchain into a small browser bundle. Around those components, the framework ships everything an app needs as one coherent system:

  • Views render pages. Each view declares the data it needs, and the template reads from that data. There are no hooks and no JSX.
  • The router maps URLs to views, wraps them in layouts, and handles navigation, scroll, and focus. It's built in, not a package you choose.
  • The store holds your records. Models describe them with a schema (types, defaults, validation), and views that query the store re-render automatically when matching records change.
  • The CLI scaffolds, serves, and builds. One binary owns the dev server, the compiler, and static output.

Because these pieces are designed together, most of the wiring you'd normally do yourself, connecting a router to a state library to a build tool, simply doesn't exist here.

The folder map

Every Puzzle app shares one layout, so any Puzzle project reads like the last one:

my-app/
├── puzzle.config.js       # build config (styles pipeline, output mode)
└── app/
    ├── app.js             # boots the app: routes + models + config
    ├── routes.js          # URL → view map (pure data)
    ├── public/
    │   └── index.html     # the HTML shell the browser loads
    ├── styles/
    │   └── styles.css     # global stylesheet / Tailwind entry
    ├── layouts/           # chrome around pages        (.pzl)
    ├── views/             # one page component per route (.pzl)
    ├── components/        # reusable UI                (.pzl)
    └── models/            # data schemas               (.js)

The split that matters: views/ is pages (one per route), layouts/ is chrome around pages (headers, sidebars), components/ is reusable UI, and models/ is data definitions. Two plain JavaScript files wire them together: routes.js declares which URL shows which view, and app.js boots the app.

Views own the screen

A view is a class with a data() method. Whatever that method returns is what the template can read, and that's the whole contract:

<puzzle-view>
  <h1>{ greeting }</h1>
</puzzle-view>

<script>
import { PuzzleView } from '@magic-spells/puzzle';

export default class Hello extends PuzzleView {
  data(params, props) {
    return { greeting: 'Hello, Puzzle' };
  }
}
</script>

Three methods cover the reactivity you'd reach for hooks to do elsewhere:

  • data(params, props) computes the view's data. It re-runs automatically when store records it queried change.
  • setData() holds local UI state (open panels, drafts) and re-renders without recomputing data().
  • refresh() re-runs data() on demand, for when local state feeds a derived value.

The router is built in

app/routes.js is pure data: an array mapping paths to views, each optionally wrapped in a layout:

import Home from '@/views/Home.pzl';
import About from '@/views/About.pzl';
import DefaultLayout from '@/layouts/Default.pzl';

export default [
  { path: '/',      name: 'home',  view: Home,  layout: DefaultLayout },
  { path: '/about', name: 'about', view: About, layout: DefaultLayout },
];

A layout is itself a .pzl file with a <Slot/> marker where the routed view renders. Navigation is plain links: the router intercepts them, swaps views, restores scroll, moves focus, and announces the new page to screen readers, none of which you write.

Models and the store

Data lives in one reactive store, and models give it shape. A model is a class with a static schema, every field typed, with defaults and validation rules inline:

import { PuzzleModel, Puzzle } from '@magic-spells/puzzle';

export default class Spell extends PuzzleModel {
  static schema = {
    id:      Puzzle.string().primary(),
    name:    Puzzle.string().required(),
    level:   Puzzle.number().min(1).max(9),
    learned: Puzzle.boolean().default(false),
  };
}

Views reach the store as this.ctx.store inside data(). Here's the part that makes Puzzle feel different: querying is subscribing. A view that calls findMany('spell') during data() re-renders whenever a matching record is created, updated, or destroyed, with no selectors, context providers, or manual subscriptions. Turn on server sync and the same query also fetches whatever the store is missing, so a view's data layer is still just data().

From a click to a re-render

Tracing one interaction end-to-end ties the model together. Say a button toggles a record's flag:

  1. The template's @click={ toggle } fires a handler from the view's events field.
  2. The handler calls record.update(patch) on a record. The store validates the patch against the schema and applies it.
  3. The store notifies every view whose data() query matches the changed record.
  4. Each subscribed view re-runs data() and patches only the DOM that changed.

One direction, no ceremony: events write to the store, the store re-runs data(), and templates re-read. Local-only state skips the store and uses setData() instead.

Coming from other frameworks

The concepts map cleanly, but the spellings differ. These are the habits to unlearn first:

  • No hooks. Lifecycle is class methods: mounted() for browser setup, destroyed() for cleanup. Derived state is just code inside data().
  • No $emit and no event bubbling between components. A child talks to its parent through callback props: the parent passes @save={ savePost }, and the child calls this.props.save(...).
  • No JSX. Templates are HTML plus a small grammar: { expr } interpolation, {#if} conditionals, {#for} loops. Each brace holds one JavaScript-shaped expression (paths, operators, string and array methods, arrow functions as call arguments, and display functions like currency(price)), but never this: every value a template reads comes from data().
  • No state library to pick. The store ships with the framework, and models replace the reducer/action layer with schema-validated records.
  • No loading state to manage. A view's server reads settle before it commits, so a null record means "not found", never "still loading".
  • Form inputs bind themselves. An <input> whose value is a plain data key stays in sync without an @input handler.

Continue: build it

The next three pages turn this model into a working app, a small spellbook, one subsystem at a time:

  • Build a View: scaffold the app and render a live, searchable list.
  • Wire the Router: add a layout, a detail page, and navigation.
  • Add Models: move the data into the store and watch views react on their own.