|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Layouts All the Way Down" |
| 4 | +publishDate: "2024-07-04T09:00:00.000Z" |
| 5 | +description: "How domstack's nested layout system works and why it keeps things simple." |
| 6 | +tags: |
| 7 | + - domstack |
| 8 | + - layouts |
| 9 | +--- |
| 10 | + |
| 11 | +# Layouts All the Way Down |
| 12 | + |
| 13 | +Domstack layouts are just functions. The `post` layout is a TypeScript function that |
| 14 | +receives `children` (the rendered page content), wraps it in article markup, and |
| 15 | +delegates to the `root` layout for the full HTML shell: |
| 16 | + |
| 17 | +```ts |
| 18 | +const postLayout: LayoutFunction<PostVars> = (args) => { |
| 19 | + const { children, ...rest } = args |
| 20 | + const wrappedChildren = render(html` |
| 21 | + <article class="h-entry"> |
| 22 | + <header>...</header> |
| 23 | + <div class="e-content">${children}</div> |
| 24 | + </article> |
| 25 | + `) |
| 26 | + return rootLayout({ ...rest, children: wrappedChildren }) |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +No magic inheritance, no template partials, no special syntax. Just function composition. |
| 31 | + |
| 32 | +## Styles follow the same pattern |
| 33 | + |
| 34 | +`post.layout.css` imports `root.layout.css` with a plain CSS `@import`. esbuild |
| 35 | +bundles them together. Each layout advertises its own stylesheet and client script, |
| 36 | +and domstack injects the right ones automatically based on which layout a page uses. |
| 37 | + |
| 38 | +## The `vars` merge order |
| 39 | + |
| 40 | +``` |
| 41 | +{ ...globalVars, ...globalDataVars, ...pageVars, ...builderVars } |
| 42 | +``` |
| 43 | + |
| 44 | +`globalDataVars` sits between global and page vars, so `global.data.ts` output is |
| 45 | +available everywhere but can be overridden per-page if needed. |
0 commit comments