@typed-grid-areas/vue 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/REFERENCE.md ADDED
@@ -0,0 +1,1406 @@
1
+ # typed-grid-areas
2
+
3
+ Type-safe, declarative CSS grid layouts for components whose slots come and
4
+ go. Describe a grid once, as its areas and a few text templates, and the
5
+ library works out the layout for every combination of present slots, at
6
+ every breakpoint, as generated CSS: no `:has()` rules or layout JavaScript
7
+ of your own. Everything that names an area is checked at compile time.
8
+
9
+ Framework-free at the core (`typed-grid-areas`); each UI framework's adapter
10
+ is a package of its own. The React adapter, `@typed-grid-areas/react`,
11
+ renders a definition as typed slot components (`<Card>`, `<Card.icon>`).
12
+
13
+ [Docs](https://michaelbuss.github.io/typed-grid-areas/) ·
14
+ [Try it in your browser](https://michaelbuss.github.io/typed-grid-areas/try-it/)
15
+
16
+ This is the complete API reference, for every adapter, as of the version it
17
+ ships with: every package carries it next to its [README](README.md), the
18
+ overview.
19
+
20
+ ## Install
21
+
22
+ ```sh
23
+ npm i typed-grid-areas @typed-grid-areas/react
24
+ ```
25
+
26
+ `@typed-grid-areas/react` needs `react` 19 or later. The core on its own
27
+ (`npm i typed-grid-areas`) has no framework dependency.
28
+
29
+ Moving from `typed-grid-areas/react` (0.1)? Install `@typed-grid-areas/react`
30
+ and import `createGridComponent` from it instead; nothing else changes.
31
+
32
+ ### Agent skill
33
+
34
+ The `typed-grid-areas` package ships an agent skill (in its
35
+ `skills/typed-grid-areas/`) that walks a coding agent through writing,
36
+ converting and debugging layouts, versioned with each release. Enable it in your project with
37
+ [TanStack Intent](https://tanstack.com/intent):
38
+
39
+ ```sh
40
+ npx @tanstack/intent@latest install
41
+ ```
42
+
43
+ The first run asks which packages' skills to enable, so it needs an
44
+ interactive terminal, and Intent needs Node.js 20.12 or newer.
45
+
46
+ ## At a glance
47
+
48
+ ```tsx
49
+ import { defineLayout } from "typed-grid-areas";
50
+ import { createGridComponent } from "@typed-grid-areas/react";
51
+
52
+ const Card = createGridComponent(
53
+ defineLayout({
54
+ areas: ["icon?", "information", "cardTag?"],
55
+ layouts: [
56
+ {
57
+ template: "icon information cardTag",
58
+ columns: "min-content minmax(0, 1fr) auto",
59
+ },
60
+ ],
61
+ }),
62
+ );
63
+
64
+ function Example({ tag }: { tag?: string }) {
65
+ return (
66
+ <Card className="gap-2">
67
+ <Card.icon>★</Card.icon>
68
+ <Card.information>Weekly digest</Card.information>
69
+ {tag && <Card.cardTag>{tag}</Card.cardTag>}
70
+ </Card>
71
+ );
72
+ }
73
+ ```
74
+
75
+ Without a tag, the library's derived layout for "no `cardTag`" applies:
76
+ `icon information`, with the tag's column gone. See
77
+ [the examples](#examples-preferencecard-and-id-card) for complete cards.
78
+
79
+ ## Defining a grid: `defineLayout()`
80
+
81
+ Describe a grid once: its areas, and the layouts that arrange them, drawn as
82
+ text templates. Everything that names an area is checked against `areas` at
83
+ the type level.
84
+
85
+ ```ts
86
+ import { defineLayout } from "typed-grid-areas";
87
+
88
+ const cardGrid = defineLayout({
89
+ areas: ["icon?", "information", "cardTag?"],
90
+ layouts: [
91
+ {
92
+ template: `
93
+ icon cardTag
94
+ information information`,
95
+ columns: "min-content minmax(0, 1fr)",
96
+ align: { cardTag: { justify: "end" } },
97
+ breakpoints: {
98
+ "@sm": {
99
+ template: "icon information cardTag",
100
+ columns: "min-content minmax(0, 1fr) auto",
101
+ },
102
+ },
103
+ },
104
+ { unless: ["cardTag"], template: "icon information" },
105
+ ],
106
+ });
107
+ ```
108
+
109
+ - **Areas:** a trailing `?` marks an area optional (its slot may be absent);
110
+ the area is still called `icon`.
111
+ - **Templates:** rows are separated by newlines or `/`, cells by whitespace.
112
+ `.` is an empty cell, and `base+layer` draws `layer` over `base`.
113
+ - **Track lists:** `rows` and `columns` are one CSS track list each.
114
+ Whitespace inside parentheses doesn't split a track, so
115
+ `"min-content minmax(0, 1fr)"` is two tracks.
116
+
117
+ Compile-time checks, each reported on the offending property:
118
+
119
+ - an unknown area in a cell (also inside `a+b`): `{ _unknownArea: "iconn" }`
120
+ on `template`
121
+ - rows with different cell counts:
122
+ `{ _raggedRow: { row: 2; cells: 1; expected: 2 } }` on `template`
123
+ - a track count that doesn't match the template:
124
+ `{ _columnCountError: { expected: 2; got: 3 } }` on `columns` (and
125
+ `_rowCountError` on `rows`); skipped for `repeat(` and non-literal strings
126
+ - unknown `align` keys and unknown `when`/`unless` values
127
+
128
+ There is no autocomplete inside a template string, and each template reports
129
+ one problem at a time.
130
+
131
+ In development, `defineLayout` also runs `validateGridConfig()` once per
132
+ definition and logs what types can't check: areas and layers that aren't
133
+ rectangles, and malformed cells. `validateGridConfig()` returns those
134
+ warnings as strings, so it is also useful for definitions assembled
135
+ dynamically.
136
+
137
+ ## Optional areas and derived layouts
138
+
139
+ Mark an area optional with a `?` suffix in `areas`; everywhere else it is
140
+ named without the `?`. Every other area is required: its slot is always
141
+ expected to be rendered. Draw each layout with every area present, and the
142
+ library derives a layout for every combination of absent optional areas that
143
+ the layout uses and its condition doesn't mention:
144
+
145
+ ```ts
146
+ defineLayout({
147
+ areas: ["icon?", "information", "cardTag?", "children?"],
148
+ layouts: [
149
+ {
150
+ template: `
151
+ icon cardTag
152
+ information information
153
+ children children`,
154
+ },
155
+ ],
156
+ });
157
+ // without children: icon cardTag / information information
158
+ // without cardTag: icon / information / children
159
+ ```
160
+
161
+ A derived layout is the authored one (and each of its breakpoint
162
+ arrangements) with the absent areas taken out:
163
+
164
+ 1. A row or column is dropped, with its track size, when it held an absent
165
+ area and everything else in it is also absent or continues into a
166
+ neighbouring track (a spanning area just gets one track shorter).
167
+ 2. A hole that can't be dropped is filled by a neighbouring area (left, then
168
+ right, up, down) if that area stays a rectangle; otherwise it stays empty.
169
+ 3. Cells written as `"."` are never dropped or filled.
170
+
171
+ The generated CSS grows with the number of combinations (2ⁿ for n optional
172
+ areas in one grid), so it is deduplicated, and nesting grids keeps n small
173
+ per grid: the two-grid PreferenceCard below stays under 10 KB.
174
+
175
+ ## Overrides: `when` and `unless`
176
+
177
+ Where a derived layout isn't what you want, write an **override**: a layout
178
+ with a **condition**, declared after the layout it replaces. A condition is
179
+ `when` (areas that must be present) plus `unless` (areas that must be
180
+ absent); areas named in neither don't affect it. A layout without a
181
+ condition is a **base** layout, which always applies.
182
+
183
+ ```ts
184
+ defineLayout({
185
+ areas: ["icon?", "information", "cardTag?", "children?"],
186
+ layouts: [
187
+ {
188
+ template: `
189
+ icon cardTag
190
+ information information
191
+ children children`,
192
+ columns: "min-content minmax(0, 1fr)",
193
+ },
194
+ // The derived layout without a tag would stack icon and information in
195
+ // the min-content column; put them side by side instead.
196
+ { unless: ["cardTag"], template: "icon information / children children" },
197
+ ],
198
+ });
199
+ ```
200
+
201
+ Conditions are checked with `:has()` / `:not(:has())` on the grid's direct
202
+ children, so layouts react to which slots are actually rendered, without any
203
+ layout JavaScript. When several layouts match, the one declared **last**
204
+ wins, however many areas each one mentions. An override gets derived layouts
205
+ of its own for the optional areas its condition doesn't mention.
206
+
207
+ Every layout is self-contained: while it applies it sets the tracks, the
208
+ placement and the alignment (`justify` / `align`: `start`, `center`, `end`,
209
+ `stretch`, `baseline`) of every area, so nothing leaks in from another
210
+ matching layout:
211
+
212
+ ```ts
213
+ defineLayout({
214
+ areas: ["icon", "info", "tag"],
215
+ layouts: [
216
+ { template: "info tag", align: { tag: { justify: "end" } } },
217
+ {
218
+ when: ["icon"],
219
+ unless: ["tag"],
220
+ template: "icon info",
221
+ align: { icon: { justify: "center", align: "start" } },
222
+ },
223
+ ],
224
+ });
225
+ ```
226
+
227
+ An area a layout's template leaves out is **hidden** (`display: none
228
+ !important`) while that layout applies.
229
+
230
+ All generated CSS sits in the `typed-grid-areas` cascade layer, so your own
231
+ unlayered classes on the grid or a slot (say `justify-self`) always win. The
232
+ one exception is hiding: it is `!important`, so a `display` class on a slot
233
+ can't bring back an area the current layout leaves out
234
+ ([ADR 0003](https://github.com/MichaelBuss/typed-grid-areas/blob/main/docs/adr/0003-hidden-areas-are-important.md)).
235
+
236
+ ## Breakpoints
237
+
238
+ `breakpoints` give a layout different arrangements once the grid is given
239
+ at least a certain width. Keys are Tailwind's default `@container` sizes
240
+ (`@xs` through `@7xl`) or an arbitrary length in Tailwind's arbitrary-value
241
+ syntax (`"@[40rem]"`):
242
+
243
+ ```ts
244
+ defineLayout({
245
+ areas: ["icon", "info"],
246
+ layouts: [
247
+ {
248
+ template: "info",
249
+ breakpoints: {
250
+ "@sm": { template: "icon info" },
251
+ "@[40rem]": { template: "info icon", columns: "1fr auto" },
252
+ },
253
+ },
254
+ ],
255
+ });
256
+ ```
257
+
258
+ A breakpoint arrangement beats its own layout's base arrangement, but a
259
+ later layout still beats an earlier layout's breakpoints. Breakpoint
260
+ arrangements take their own `rows`, `columns` and `align`; nothing is
261
+ inherited from the layout. Derived layouts apply to each breakpoint
262
+ arrangement too.
263
+
264
+ **A breakpoint measures the space the grid itself is given**, never the
265
+ viewport or a container somewhere above it, so a grid behaves the same
266
+ wherever it's placed. A container query can't measure the element it
267
+ styles, so a grid with breakpoints renders inside its own size container: a
268
+ wrapper `div` with `container-type: inline-size` and a container name unique
269
+ to the definition, which its `@container` queries target by name (see
270
+ [ADR 0002](https://github.com/MichaelBuss/typed-grid-areas/blob/main/docs/adr/0002-grid-owns-its-size-container.md)). Grids without
271
+ breakpoints get no wrapper and are not size containers.
272
+
273
+ Inline-size containment has a cost: **a grid with breakpoints can't size to
274
+ its content.** That's fine in a `1fr`, `minmax(0, 1fr)` or stretched track,
275
+ but in an `auto` or `min-content` track of a parent grid it collapses to zero
276
+ width. A nested grid with breakpoints also switches at a wider outer width
277
+ than a flat one would, since it measures only its own area.
278
+
279
+ ## Layers: `base+layer`
280
+
281
+ A cell written `base+layer` (or `base+layer+layer…`) is owned by `base` as
282
+ usual, and also has `layer` drawn on top of it. A layer covers the bounding
283
+ rectangle of the cells it appears in and is drawn above its base, whatever
284
+ their order in the DOM. Several layers can share a host, each in its own
285
+ cells, and an area can be a layer in one layout and own cells in another:
286
+
287
+ ```ts
288
+ defineLayout({
289
+ areas: ["photo", "identity", "details", "logo?", "qr?", "badge?"],
290
+ layouts: [
291
+ {
292
+ template: `
293
+ photo+logo photo photo+badge
294
+ photo photo photo
295
+ photo+identity photo+identity photo+identity
296
+ details details qr`,
297
+ rows: "40px 80px 40px 60px",
298
+ columns: "60px 1fr 60px",
299
+ breakpoints: {
300
+ "@md": {
301
+ template: `
302
+ photo photo+badge identity logo
303
+ photo photo details qr`,
304
+ rows: "40px 120px",
305
+ columns: "100px 40px 1fr 80px",
306
+ },
307
+ },
308
+ },
309
+ ],
310
+ });
311
+ ```
312
+
313
+ On narrow cards `logo`, `badge` and `identity` sit in the photo's corners;
314
+ from `@md` the photo is a column of its own and only `badge` stays on it.
315
+ Use `align` to pin a layer within its cells.
316
+
317
+ In derived layouts, a missing optional layer just disappears from its cells:
318
+ it never leaves a hole for a neighbour to fill. A row or column is only
319
+ dropped if every layer in it also continues into a neighbouring track.
320
+
321
+ **Track sizing.** Layers sit in grid tracks, so their content takes part in
322
+ sizing those tracks just like the base's does. Give the host area's tracks
323
+ fixed or `minmax()` sizes, or a large layer can stretch the area it sits on.
324
+
325
+ ### Restyling layered content: `--tga-over`
326
+
327
+ Every layered slot gets the custom property `--tga-over`, set to the name of
328
+ the area underneath it (the one owning its top-left cell); every other slot
329
+ gets `none`. Content inside a slot can restyle itself with a container style
330
+ query, for example white text while it sits over a photo:
331
+
332
+ ```css
333
+ @container style(--tga-over: photo) {
334
+ .identity-name {
335
+ color: white;
336
+ }
337
+ }
338
+ ```
339
+
340
+ Every element is a style container, so the query reads the nearest
341
+ ancestor's value: put the rule on the slot's content, not the slot itself.
342
+
343
+ Browser support for style queries on custom properties
344
+ ([MDN browser-compat-data](https://github.com/mdn/browser-compat-data/blob/main/css/at-rules/container.json)
345
+ `css.at-rules.container.style_queries_for_custom_properties`, and
346
+ [caniuse](https://caniuse.com/css-container-queries-style), checked
347
+ 2026-10-31):
348
+
349
+ | Browser | Supported since |
350
+ | ------- | --------------- |
351
+ | Chrome | 111 |
352
+ | Safari | 18 |
353
+ | Firefox | 151 |
354
+
355
+ All on by default. Only custom-property queries are supported (which is all
356
+ `--tga-over` needs); the range syntax (`style(--x > 1)`) isn't in Safari.
357
+ The library's own tests only run in Chromium. In older browsers the query
358
+ simply never matches, so write the unlayered look as the default and the
359
+ layered look inside the query.
360
+
361
+ ## Rendering with React: grid components
362
+
363
+ `createGridComponent(definition)` turns a definition into the component it
364
+ renders as. Create it once, at module level:
365
+
366
+ ```tsx
367
+ import { defineLayout } from "typed-grid-areas";
368
+ import { createGridComponent } from "@typed-grid-areas/react";
369
+
370
+ const Card = createGridComponent(
371
+ defineLayout({
372
+ areas: ["icon", "info"],
373
+ layouts: [{ template: "icon info", columns: "48px 1fr" }],
374
+ }),
375
+ );
376
+
377
+ function Example() {
378
+ return (
379
+ <Card className="gap-4" aria-label="Card">
380
+ <Card.icon>★</Card.icon>
381
+ <Card.info className="truncate">Information</Card.info>
382
+ </Card>
383
+ );
384
+ }
385
+ ```
386
+
387
+ - **`<Card>`** renders the grid: a `div` with the definition's
388
+ `data-grid-id`, which the generated CSS gives `display: grid` (layered, so
389
+ your own class can change it), taking `className`, `style`, `ref`,
390
+ `children` and any other `div` props. When the definition has breakpoints
391
+ the grid sits inside its own size container (a wrapper `div`); your props
392
+ still go on the grid itself.
393
+ - **`<Card.icon>`** is the slot for the `icon` area: a static component per
394
+ area, named exactly like the area, so `<Card.iconn>` is a type error. It
395
+ takes the same props and renders a `div` marked `data-area="icon"`.
396
+ Placement lives in the generated CSS, not on the slot.
397
+ - **`asChild`** (on slots and on the grid) renders your own single child
398
+ element instead of a `div`, with the props merged onto it (Radix `Slot`),
399
+ so it needs no extra wrapper:
400
+
401
+ ```tsx
402
+ import { defineLayout } from "typed-grid-areas";
403
+ import { createGridComponent } from "@typed-grid-areas/react";
404
+
405
+ const Row = createGridComponent(
406
+ defineLayout({ areas: ["input", "content"], layouts: [{ template: "input content" }] }),
407
+ );
408
+
409
+ function Example() {
410
+ return (
411
+ <Row>
412
+ <Row.input asChild>
413
+ <label htmlFor="notify">
414
+ <input id="notify" type="checkbox" />
415
+ </label>
416
+ </Row.input>
417
+ <Row.content>Notify me</Row.content>
418
+ </Row>
419
+ );
420
+ }
421
+ ```
422
+
423
+ Slots must be **direct children** of their grid (fragments and conditionals
424
+ are fine). Render an area's slot even while the current layout hides it;
425
+ leave it out only to make an optional area absent.
426
+
427
+ Grid components work with React Compiler and in Strict Mode, and the
428
+ adapter's tests run under both.
429
+
430
+ ### Nesting: `nest`
431
+
432
+ A grid can fill another grid's area. `nest` makes the area's slot the root
433
+ of the nested grid, and reaches that grid's slots through it:
434
+
435
+ ```tsx
436
+ import { defineLayout } from "typed-grid-areas";
437
+ import { createGridComponent } from "@typed-grid-areas/react";
438
+
439
+ const Content = createGridComponent(
440
+ defineLayout({ areas: ["icon?", "title"], layouts: [{ template: "icon title" }] }),
441
+ );
442
+
443
+ const Card = createGridComponent(
444
+ defineLayout({ areas: ["input?", "content"], layouts: [{ template: "input content" }] }),
445
+ { nest: { content: Content } },
446
+ );
447
+
448
+ function Example() {
449
+ return (
450
+ <Card>
451
+ <Card.content className="p-4">
452
+ <Card.content.icon>★</Card.content.icon>
453
+ <Card.content.title>Title</Card.content.title>
454
+ </Card.content>
455
+ </Card>
456
+ );
457
+ }
458
+ ```
459
+
460
+ `<Card.content>` is both the `content` slot of `Card` and the root of
461
+ `Content`. If the nested grid has breakpoints, the outer grid's area goes on
462
+ its size container, the outermost element. Nest where the design has panels
463
+ of their own (padding, background, border): it also keeps each grid's
464
+ optional areas, and so its CSS, small.
465
+
466
+ ### CSS and development warnings
467
+
468
+ Each definition's CSS goes into a `<style href precedence>` element, which
469
+ React 19 hoists into `<head>` and emits once, however many instances render.
470
+
471
+ In development (`process.env.NODE_ENV === "development"`), a grid warns on
472
+ the console when:
473
+
474
+ - a required area's slot isn't rendered, or
475
+ - it has a direct child that isn't one of its slots.
476
+
477
+ `defineLayout` also warns, once per definition, about areas and layers that
478
+ aren't rectangles.
479
+
480
+ ## Examples: PreferenceCard and ID card
481
+
482
+ **PreferenceCard:** an optional input panel (a checkbox, as a `<label>`)
483
+ beside a content panel with its own padding. The content panel is a nested
484
+ grid of an optional icon, the information, an optional tag and optional
485
+ children. With both an icon and a tag, the two share the top row above
486
+ the information on narrow cards, and from `@sm` all three share one row.
487
+ With only one of them, it sits beside the information at every width (the
488
+ second override: the derived layout without an icon would stack the tag
489
+ above the information). Children always take a full-width last row. The
490
+ library's browser acceptance tests render this design in every combination.
491
+
492
+ ```tsx
493
+ import type { ReactNode } from "react";
494
+ import { defineLayout } from "typed-grid-areas";
495
+ import { createGridComponent } from "@typed-grid-areas/react";
496
+
497
+ const PreferenceContent = createGridComponent(
498
+ defineLayout({
499
+ areas: ["icon?", "information", "cardTag?", "children?"],
500
+ layouts: [
501
+ {
502
+ template: `
503
+ icon cardTag
504
+ information information
505
+ children children`,
506
+ columns: "min-content minmax(0, 1fr)",
507
+ align: { cardTag: { justify: "end" } },
508
+ breakpoints: {
509
+ "@sm": {
510
+ template: "icon information cardTag / children children children",
511
+ columns: "min-content minmax(0, 1fr) auto",
512
+ },
513
+ },
514
+ },
515
+ { unless: ["cardTag"], template: "icon information / children children" },
516
+ {
517
+ when: ["cardTag"],
518
+ unless: ["icon"],
519
+ template: "information cardTag / children children",
520
+ columns: "minmax(0, 1fr) auto",
521
+ },
522
+ ],
523
+ }),
524
+ );
525
+
526
+ const Card = createGridComponent(
527
+ defineLayout({
528
+ areas: ["input?", "content"],
529
+ // `content` has breakpoints, so it needs a `minmax(0, 1fr)` track, not `auto`.
530
+ layouts: [{ template: "input content", columns: "auto minmax(0, 1fr)" }],
531
+ }),
532
+ { nest: { content: PreferenceContent } },
533
+ );
534
+
535
+ type PreferenceCardProps = {
536
+ input?: ReactNode;
537
+ icon?: ReactNode;
538
+ title: ReactNode;
539
+ tag?: ReactNode;
540
+ children?: ReactNode;
541
+ };
542
+
543
+ export function PreferenceCard({ input, icon, title, tag, children }: PreferenceCardProps) {
544
+ return (
545
+ <Card className="rounded-lg border">
546
+ {input && (
547
+ <Card.input asChild className="border-r p-4">
548
+ <label>{input}</label>
549
+ </Card.input>
550
+ )}
551
+ <Card.content className="gap-2 p-4">
552
+ {icon && <Card.content.icon>{icon}</Card.content.icon>}
553
+ <Card.content.information>{title}</Card.content.information>
554
+ {tag && <Card.content.cardTag>{tag}</Card.content.cardTag>}
555
+ {children && <Card.content.children>{children}</Card.content.children>}
556
+ </Card.content>
557
+ </Card>
558
+ );
559
+ }
560
+ ```
561
+
562
+ **ID card:** the layered card from [Layers](#layers-baselayer). On narrow
563
+ cards the logo, badge and identity sit in the photo's corners, and the name
564
+ turns white over the photo; from `@md` the photo is a column of its own and
565
+ everything else moves beside it.
566
+
567
+ ```tsx
568
+ import { defineLayout } from "typed-grid-areas";
569
+ import { createGridComponent } from "@typed-grid-areas/react";
570
+
571
+ const Card = createGridComponent(
572
+ defineLayout({
573
+ areas: ["photo", "identity", "details", "logo?", "qr?", "badge?"],
574
+ layouts: [
575
+ {
576
+ template: `
577
+ photo+logo photo photo+badge
578
+ photo photo photo
579
+ photo+identity photo+identity photo+identity
580
+ details details qr`,
581
+ rows: "40px 80px 40px 60px",
582
+ columns: "60px 1fr 60px",
583
+ breakpoints: {
584
+ "@md": {
585
+ template: `
586
+ photo photo+badge identity logo
587
+ photo photo details qr`,
588
+ rows: "40px 120px",
589
+ columns: "100px 40px 1fr 80px",
590
+ },
591
+ },
592
+ },
593
+ ],
594
+ }),
595
+ );
596
+
597
+ type Person = { name: string; role: string; photoUrl: string; verified?: boolean };
598
+
599
+ export function IdCard({ person, qrUrl }: { person: Person; qrUrl?: string }) {
600
+ return (
601
+ <Card className="overflow-hidden rounded-xl">
602
+ <Card.photo asChild>
603
+ <img src={person.photoUrl} alt="" className="h-full w-full object-cover" />
604
+ </Card.photo>
605
+ <Card.logo>ACME</Card.logo>
606
+ {person.verified && <Card.badge>✓</Card.badge>}
607
+ <Card.identity>
608
+ {/* White over the photo, through `@container style(--tga-over: photo)`. */}
609
+ <span className="identity-name">{person.name}</span>
610
+ </Card.identity>
611
+ <Card.details>{person.role}</Card.details>
612
+ {qrUrl && (
613
+ <Card.qr asChild>
614
+ <img src={qrUrl} alt="QR code" />
615
+ </Card.qr>
616
+ )}
617
+ </Card>
618
+ );
619
+ }
620
+ ```
621
+
622
+ ## Rendering with Angular: grid directives
623
+
624
+ ```sh
625
+ npm i typed-grid-areas @typed-grid-areas/angular
626
+ ```
627
+
628
+ `@typed-grid-areas/angular` supports Angular 22 (`@angular/core`
629
+ `^22.0.0`). Instead of components, it gives you three standalone attribute
630
+ directives that go on your own elements, and a typed reference per
631
+ definition that the template names grids and areas through:
632
+
633
+ ```ts angular
634
+ import { Component, input } from "@angular/core";
635
+ import { defineLayout } from "typed-grid-areas";
636
+ import { createGrid, TgaArea, TgaGrid, TgaSizeContainer } from "@typed-grid-areas/angular";
637
+
638
+ const Content = createGrid(
639
+ defineLayout({
640
+ areas: ["icon?", "title"],
641
+ layouts: [{ template: "title", breakpoints: { "@sm": { template: "icon title" } } }],
642
+ }),
643
+ );
644
+
645
+ const Card = createGrid(
646
+ defineLayout({ areas: ["input?", "content"], layouts: [{ template: "input content" }] }),
647
+ { nest: { content: Content } },
648
+ );
649
+
650
+ @Component({
651
+ selector: "app-card",
652
+ imports: [TgaGrid, TgaArea, TgaSizeContainer],
653
+ template: `
654
+ <div [tgaGrid]="Card" class="gap-2">
655
+ @if (selectable()) {
656
+ <label [tgaArea]="Card.input"><input type="checkbox" /></label>
657
+ }
658
+ <div tgaSizeContainer>
659
+ <div [tgaGrid]="Card.content" class="p-4">
660
+ <span [tgaArea]="Card.content.icon">★</span>
661
+ <h2 [tgaArea]="Card.content.title">Title</h2>
662
+ </div>
663
+ </div>
664
+ </div>
665
+ `,
666
+ })
667
+ export class CardComponent {
668
+ protected readonly Card = Card;
669
+ readonly selectable = input(false);
670
+ }
671
+ ```
672
+
673
+ - **`createGrid(definition, { nest })`** returns the definition's reference:
674
+ the grid itself, with one property per area (`Card.input`). An area nested
675
+ through `nest` is the nested grid's reference (`Card.content`), whose
676
+ properties are its areas (`Card.content.icon`). Create references at module
677
+ level and expose them to the template from the component
678
+ (`protected readonly Card = Card`).
679
+ - **`[tgaGrid]="Card"`** makes the element it's on the grid. It takes a grid
680
+ reference, so with `strictTemplates`, `[tgaGrid]="Card.input"` doesn't
681
+ compile.
682
+ - **`[tgaArea]="Card.input"`** makes the element it's on the slot that fills
683
+ an area. It takes an area reference, not a string, so a misspelled area
684
+ (`Card.inputt`, `Card.content.iconn`), a plain string or a grid reference
685
+ doesn't compile. Slots must be direct children of their grid's element;
686
+ make an optional area absent with `@if`.
687
+ - **`tgaSizeContainer`** marks the element around a grid with breakpoints as
688
+ its size container (a directive can't wrap its own element). It gets the
689
+ container styles, and when the grid is nested, the outer grid's
690
+ `data-area`. Leave it out for grids without breakpoints.
691
+
692
+ **Your own element as a slot** needs nothing extra: put `[tgaArea]` (or
693
+ `[tgaGrid]`) on any element, including a component's host element
694
+ (`<app-avatar [tgaArea]="Card.content.icon" />`). Your classes and
695
+ attributes stay where you wrote them.
696
+
697
+ **CSS.** Each definition's CSS goes into a
698
+ `<style data-typed-grid-areas="…">` in the `<head>` of Angular's `DOCUMENT`,
699
+ once, however many instances render, carrying the `CSP_NONCE` value when
700
+ your app provides one. Without server rendering it's inserted when the
701
+ first grid of a definition renders. With server rendering (`@angular/ssr`)
702
+ it's in the rendered `<head>`, and client hydration reuses it rather than
703
+ adding a second copy. Nothing to set up either way.
704
+
705
+ **Development warnings.** In development mode (`isDevMode()`), in the
706
+ browser, a grid warns on the console after every render when a required
707
+ area's slot is missing, when it has a direct child that isn't one of its
708
+ slots, or when it has breakpoints but its parent isn't its size container.
709
+ Production builds and server rendering log nothing.
710
+
711
+ ## Rendering with Vue: `@typed-grid-areas/vue`
712
+
713
+ ```sh
714
+ npm i typed-grid-areas @typed-grid-areas/vue
715
+ ```
716
+
717
+ `@typed-grid-areas/vue` needs `vue` 3.5 or later. It has the React
718
+ adapter's shape: `createGridComponent(definition, { nest })` returns the
719
+ grid's component, with a member component per area that templates use as a
720
+ tag (`<Card.input>`):
721
+
722
+ ```vue
723
+ <script setup lang="ts">
724
+ import { defineLayout } from "typed-grid-areas";
725
+ import { createGridComponent } from "@typed-grid-areas/vue";
726
+
727
+ defineProps<{ selectable: boolean }>();
728
+
729
+ const Content = createGridComponent(
730
+ defineLayout({ areas: ["icon?", "title"], layouts: [{ template: "icon title" }] }),
731
+ );
732
+ const Card = createGridComponent(
733
+ defineLayout({ areas: ["input?", "content"], layouts: [{ template: "input content" }] }),
734
+ { nest: { content: Content } },
735
+ );
736
+ </script>
737
+
738
+ <template>
739
+ <Card class="gap-4" aria-label="Card">
740
+ <Card.input v-if="selectable" as-child>
741
+ <label><input type="checkbox" /></label>
742
+ </Card.input>
743
+ <Card.content class="p-4">
744
+ <Card.content.icon>★</Card.content.icon>
745
+ <Card.content.title>Title</Card.content.title>
746
+ </Card.content>
747
+ </Card>
748
+ </template>
749
+ ```
750
+
751
+ - **`<Card>`** renders the grid, a `div` taking `class`, `style` and any other
752
+ attribute (inside its own size container when the definition has
753
+ breakpoints; your attributes still go on the grid). **`<Card.input>`** is
754
+ the slot for the `input` area, a `div` marked `data-area="input"`.
755
+ **`<Card.content>`** is both the `content` slot and the nested grid, so
756
+ `<Card.content.icon>` is one of its slots. Leave an optional area out with
757
+ `v-if`; slots must be direct children of their grid.
758
+ - **Bind grid components in `<script setup>`** (or return them from
759
+ `setup()`): Vue resolves member tags like `<Card.input>` only from there.
760
+ Registering them under `components:` (Options API) and in-DOM templates
761
+ aren't supported. Run `vue-tsc` to type-check templates: `<Card.inputt>`
762
+ and `<Card.content.iconn>` are type errors.
763
+ - **`as-child`** (on slots and on the grid) renders your own single child
764
+ element instead of a `div`, the way Reka UI's `as-child` does: the slot's
765
+ attributes are merged into the child's (the child's own win, classes and
766
+ styles combine, event handlers both run), it always gets the slot's
767
+ `data-area` (or the grid's `data-grid-id`), and its own `ref` still
768
+ reaches it.
769
+ - **Area names Vue reserves** on a component (`length`, `props`, `emits`,
770
+ `slots`, `inheritAttrs`, `displayName`, `compatConfig`, and any starting
771
+ with `__`) can't be members: `createGridComponent` rejects them, as a type
772
+ error and at runtime.
773
+
774
+ **CSS.** In the browser, each grid puts its definition's CSS into `<head>`
775
+ once (a `<style data-typed-grid-areas>`), however many instances render, and
776
+ leaves it there. Vue has no API for `<head>` on the server, so when you
777
+ server-render, add the grids' style tags to your page's `<head>` in your
778
+ server entry with `renderGridStyles`:
779
+
780
+ ```js
781
+ import { renderToString } from "vue/server-renderer";
782
+ import { renderGridStyles } from "@typed-grid-areas/vue";
783
+
784
+ /** @param {import("vue").App} app the app from `createSSRApp` */
785
+ export async function render(app) {
786
+ const ctx = {};
787
+ const html = await renderToString(app, ctx);
788
+ return { html, head: renderGridStyles(ctx) };
789
+ }
790
+ ```
791
+
792
+ It returns one tag per definition rendered with that `ctx`, and hydration
793
+ reuses them instead of adding a second. Without it, server-rendered grids
794
+ still work: their CSS is inserted on hydration, so the page lays out once
795
+ the app's script runs.
796
+
797
+ **Development warnings.** In development (`process.env.NODE_ENV ===
798
+ "development"`), a grid warns on the console after it mounts and after each
799
+ update when a required area's slot isn't rendered, or when it has a direct
800
+ child that isn't one of its slots. Production builds and server rendering
801
+ log nothing.
802
+
803
+ ## Rendering with Solid: `@typed-grid-areas/solid`
804
+
805
+ ```sh
806
+ npm i typed-grid-areas @typed-grid-areas/solid
807
+ ```
808
+
809
+ `@typed-grid-areas/solid` supports Solid 1.x: it needs `solid-js` 1.9 or
810
+ later within 1.x. Solid 2 will be supported by a new major of the adapter.
811
+
812
+ The API is the React adapter's: `createGridComponent(definition, { nest })`
813
+ returns the grid component, with a slot component per area (`<Card.icon>`),
814
+ and a nested grid's slots reached through its area (`<Card.content.icon>`).
815
+ Create it once, at module level. Slots and the grid take `class`, `style`,
816
+ `ref`, `children` and any other attribute of the element they render.
817
+
818
+ ```tsx solid
819
+ import { createSignal, Show } from "solid-js";
820
+ import { defineLayout } from "typed-grid-areas";
821
+ import { createGridComponent } from "@typed-grid-areas/solid";
822
+
823
+ const Content = createGridComponent(
824
+ defineLayout({ areas: ["icon?", "title", "tag?"], layouts: [{ template: "icon title tag" }] }),
825
+ );
826
+
827
+ const Card = createGridComponent(
828
+ defineLayout({ areas: ["input?", "content"], layouts: [{ template: "input content" }] }),
829
+ { nest: { content: Content } },
830
+ );
831
+
832
+ function Example(props: { tag?: string }) {
833
+ const [hasIcon, setHasIcon] = createSignal(true);
834
+ return (
835
+ <Card class="gap-4" aria-label="Card">
836
+ <Card.input as="label" for="notify">
837
+ <input id="notify" type="checkbox" onChange={() => setHasIcon(!hasIcon())} />
838
+ </Card.input>
839
+ <Card.content class="p-4">
840
+ <Show when={hasIcon()}>
841
+ <Card.content.icon>★</Card.content.icon>
842
+ </Show>
843
+ <Card.content.title>Weekly digest</Card.content.title>
844
+ <Show when={props.tag}>
845
+ <Card.content.tag>{props.tag}</Card.content.tag>
846
+ </Show>
847
+ </Card.content>
848
+ </Card>
849
+ );
850
+ }
851
+ ```
852
+
853
+ - **Optional areas** are absent when their slot isn't rendered: use Solid's
854
+ own `<Show>` (or any conditional). Slots stay reactive, through a nested
855
+ grid's root too, and so does the layout.
856
+ - **Your own element as a slot: `as`.** Solid's JSX creates real DOM nodes,
857
+ so there's no `asChild`; instead `as` (on slots and the grid) renders the
858
+ slot as a tag name, a component or a render function, with no wrapper.
859
+ The other props are typed from `as`: with `as="label"`, `for` type-checks
860
+ and `ref` gets an `HTMLLabelElement`, and an attribute a label doesn't take
861
+ is an error. A component (or render function) gets the slot's props,
862
+ including its `data-area`, and must spread them onto its root element.
863
+ Annotate a render function's `props` for the slot to take that element's
864
+ attributes:
865
+
866
+ ```tsx solid
867
+ import type { JSX } from "solid-js";
868
+ import { defineLayout } from "typed-grid-areas";
869
+ import { createGridComponent } from "@typed-grid-areas/solid";
870
+
871
+ const Row = createGridComponent(
872
+ defineLayout({ areas: ["icon", "input"], layouts: [{ template: "icon input" }] }),
873
+ );
874
+
875
+ function Icon(props: JSX.SvgSVGAttributes<SVGSVGElement> & { size: number }) {
876
+ return <svg {...props} width={props.size} height={props.size} />;
877
+ }
878
+
879
+ function Example() {
880
+ return (
881
+ <Row as="section">
882
+ <Row.icon as={Icon} size={16} />
883
+ <Row.input
884
+ as={(props: JSX.LabelHTMLAttributes<HTMLLabelElement>) => <label {...props} />}
885
+ for="notify"
886
+ >
887
+ <input id="notify" type="checkbox" />
888
+ </Row.input>
889
+ </Row>
890
+ );
891
+ }
892
+ ```
893
+
894
+ - **CSS.** Each definition's CSS goes on the page once, however many
895
+ instances render. In the browser, the grid puts its style tag in `<head>`
896
+ as it's created, reusing one the server rendered, so hydration adds no
897
+ second copy. On the server, the grid registers its style tag with Solid's
898
+ `useAssets`, once per definition per render, and `renderToString`,
899
+ `renderToStringAsync` and `renderToStream` put it before `</head>` when the
900
+ app renders the whole document (as SolidStart does). If your app renders
901
+ only part of the page, put `getAssets()` from `solid-js/web` in the
902
+ `<head>` you render around it. Nothing else is needed: no `@solidjs/meta`.
903
+ - **Development warnings** are React's: in development
904
+ (`process.env.NODE_ENV === "development"`), after mount and after every
905
+ change to its slots, a grid warns about a missing required slot or a
906
+ direct child that isn't a slot, once per distinct set of problems. Never on
907
+ the server, never in production.
908
+ - **Builds.** The package's `solid` export is Solid JSX for your bundler's
909
+ Solid plugin (`vite-plugin-solid` and the like) to compile, for the browser
910
+ or the server; its `import` export is compiled for the browser.
911
+ - **Server rendering needs a Solid-aware bundler.** Only the `solid` export
912
+ renders on the server, so the server build must resolve the `solid`
913
+ export condition, as `vite-plugin-solid` (and SolidStart) does. Without
914
+ that condition, plain Node loads the browser build, and importing it
915
+ throws Solid's "Client-only API called on the server side".
916
+
917
+ ## Rendering with Svelte: `@typed-grid-areas/svelte`
918
+
919
+ ```sh
920
+ npm i typed-grid-areas @typed-grid-areas/svelte
921
+ ```
922
+
923
+ Needs Svelte 5.57.0 or later, in runes mode. The package ships `.svelte`
924
+ files under the `svelte` export condition, so build with a Svelte-aware
925
+ bundler (Vite with `@sveltejs/vite-plugin-svelte`, or SvelteKit).
926
+
927
+ `createGridComponent(definition, { nest })` turns a definition into a
928
+ component with one member per area, like the React adapter. A `.svelte`
929
+ file can't be a generic factory, so create grid components in a module: a
930
+ `.ts` file, or a component's `<script module>`:
931
+
932
+ ```svelte
933
+ <script module lang="ts">
934
+ import { defineLayout } from "typed-grid-areas";
935
+ import { createGridComponent } from "@typed-grid-areas/svelte";
936
+
937
+ const Content = createGridComponent(
938
+ defineLayout({
939
+ areas: ["icon?", "information", "cardTag?"],
940
+ layouts: [
941
+ {
942
+ template: "icon information cardTag",
943
+ columns: "min-content minmax(0, 1fr) auto",
944
+ },
945
+ ],
946
+ }),
947
+ );
948
+
949
+ const Card = createGridComponent(
950
+ defineLayout({
951
+ areas: ["input?", "content"],
952
+ layouts: [{ template: "input content", columns: "auto minmax(0, 1fr)" }],
953
+ }),
954
+ { nest: { content: Content } },
955
+ );
956
+ </script>
957
+
958
+ <script lang="ts">
959
+ let { selectable = false, tag }: { selectable?: boolean; tag?: string } = $props();
960
+ </script>
961
+
962
+ <Card class="gap-2" aria-label="Card">
963
+ {#if selectable}
964
+ <Card.input>
965
+ {#snippet child({ props })}
966
+ <label {...props}><input type="checkbox" /></label>
967
+ {/snippet}
968
+ </Card.input>
969
+ {/if}
970
+ <Card.content class="p-4">
971
+ <Card.content.icon>★</Card.content.icon>
972
+ <Card.content.information>Weekly digest</Card.content.information>
973
+ {#if tag}
974
+ <Card.content.cardTag>{tag}</Card.content.cardTag>
975
+ {/if}
976
+ </Card.content>
977
+ </Card>
978
+ ```
979
+
980
+ - **`<Card>`** renders the grid, and **`<Card.icon>`** the slot for the
981
+ `icon` area, exactly as in React: the same DOM, the same size container
982
+ for a definition with breakpoints (your attributes still go on the grid),
983
+ and `<Card.iconn>` is an error in `svelte-check`. Both take any attribute
984
+ a `div` takes (`class`, `style`, `aria-*`, event handlers) and children.
985
+ - **Optional areas:** leave a slot out with an ordinary `{#if}`; the
986
+ derived layout for its absence applies. Slots must be direct children of
987
+ their grid (`{#if}`, `{#each}` and snippets rendered in place are fine).
988
+ - **Your own element as a slot (or the grid):** pass a `child` snippet
989
+ instead of children, and spread its `props` onto your element, Bits UI's
990
+ convention: `{#snippet child({ props })}<label {...props}>…</label>{/snippet}`.
991
+ `props` carries the slot's `data-area` (or the grid's attributes) and
992
+ every attribute you gave the component.
993
+ - **Nesting:** `nest: { content: Content }` makes `<Card.content>` both the
994
+ `content` slot and the `Content` grid, reaching its slots as
995
+ `<Card.content.icon>`.
996
+
997
+ **Where the CSS goes.** Each definition's CSS is a
998
+ `<style data-typed-grid-areas="<grid id>">` in `<svelte:head>`, rendered by
999
+ exactly one instance of the definition, however many render:
1000
+
1001
+ - **With server rendering**, `render()` from `svelte/server` puts it in
1002
+ `head` once per render; SvelteKit puts `head` into the page for you, and
1003
+ with plain `svelte/server` you put it in your HTML's `<head>`. Hydration
1004
+ reuses the server's tag.
1005
+ - **Without server rendering**, the first mounted instance adds it to
1006
+ `document.head`; when that instance unmounts another one takes it over,
1007
+ and it goes away with the last instance.
1008
+
1009
+ **Development warnings.** Under the `development` export condition (Vite's
1010
+ dev server, Vitest), a grid warns on the console when a required area's
1011
+ slot isn't rendered or it has a direct child that isn't one of its slots,
1012
+ once per distinct set of problems, after mount and after each update.
1013
+ Production builds and server rendering log nothing.
1014
+
1015
+ ## Rendering with Preact: `@typed-grid-areas/preact`
1016
+
1017
+ ```sh
1018
+ npm i typed-grid-areas @typed-grid-areas/preact
1019
+ ```
1020
+
1021
+ `@typed-grid-areas/preact` is a native Preact adapter (no `preact/compat`)
1022
+ for Preact 11 (`preact` `^11.0.0`; checked with 11.1). Its API is the React
1023
+ adapter's: `createGridComponent(definition, { nest })` returns `<Card>`,
1024
+ with a slot component per area (`<Card.icon>`), each taking anything a
1025
+ `div` takes (`class` or `className`, `style`, `ref`, …):
1026
+
1027
+ ```tsx preact
1028
+ import { defineLayout } from "typed-grid-areas";
1029
+ import { createGridComponent } from "@typed-grid-areas/preact";
1030
+
1031
+ const Card = createGridComponent(
1032
+ defineLayout({
1033
+ areas: ["icon?", "information", "cardTag?"],
1034
+ layouts: [
1035
+ {
1036
+ template: "icon information cardTag",
1037
+ columns: "min-content minmax(0, 1fr) auto",
1038
+ },
1039
+ ],
1040
+ }),
1041
+ );
1042
+
1043
+ function Example({ tag }: { tag?: string }) {
1044
+ return (
1045
+ <Card class="gap-2">
1046
+ <Card.icon>★</Card.icon>
1047
+ <Card.information>Weekly digest</Card.information>
1048
+ {tag && <Card.cardTag>{tag}</Card.cardTag>}
1049
+ </Card>
1050
+ );
1051
+ }
1052
+ ```
1053
+
1054
+ The rendered DOM, `nest`, and the development warnings are the React
1055
+ adapter's ([Rendering with React](#rendering-with-react-grid-components)).
1056
+
1057
+ **Your own element as a slot: `asChild`.** On a slot or on the grid,
1058
+ `asChild` renders your single child element instead of a `div`. Classes
1059
+ (`class` and `className`) from both are joined, `style` objects merge (a
1060
+ string style is appended after the slot's), a handler on both runs the
1061
+ child's then the slot's, and both refs are set. The slot's `data-area` (or
1062
+ the grid's `data-grid-id`) always wins; any other prop of the child wins.
1063
+
1064
+ ```tsx preact
1065
+ import { defineLayout } from "typed-grid-areas";
1066
+ import { createGridComponent } from "@typed-grid-areas/preact";
1067
+
1068
+ const Row = createGridComponent(
1069
+ defineLayout({ areas: ["input", "content"], layouts: [{ template: "input content" }] }),
1070
+ );
1071
+
1072
+ function Example() {
1073
+ return (
1074
+ <Row>
1075
+ <Row.input asChild class="p-4">
1076
+ <label for="notify">
1077
+ <input id="notify" type="checkbox" />
1078
+ </label>
1079
+ </Row.input>
1080
+ <Row.content>Notify me</Row.content>
1081
+ </Row>
1082
+ );
1083
+ }
1084
+ ```
1085
+
1086
+ **Where the CSS goes.** In the browser, each definition's CSS goes into one
1087
+ `<style data-typed-grid-areas>` in `<head>`, inserted while the first
1088
+ instance renders and kept for the page's lifetime; nothing to set up.
1089
+ Preact has no way to reach `<head>` from the server, so a server render
1090
+ collects it with `createGridStyleSheet()`, a new one per request, and puts
1091
+ it in `<head>` itself:
1092
+
1093
+ ```tsx preact
1094
+ import { renderToString } from "preact-render-to-string";
1095
+ import { createGridStyleSheet } from "@typed-grid-areas/preact";
1096
+
1097
+ function App() {
1098
+ return <main>…</main>;
1099
+ }
1100
+
1101
+ const sheet = createGridStyleSheet();
1102
+ const body = renderToString(sheet.collect(<App />));
1103
+ const html = `<!doctype html><html><head>${sheet}</head><body>${body}</body></html>`;
1104
+ ```
1105
+
1106
+ When the page hydrates, the grids reuse the server's style tags. In
1107
+ development, a grid rendered on the server outside a sheet warns that its
1108
+ CSS is missing.
1109
+
1110
+ ## Rendering with Lit: grid template functions
1111
+
1112
+ ```sh
1113
+ npm i typed-grid-areas @typed-grid-areas/lit
1114
+ ```
1115
+
1116
+ `@typed-grid-areas/lit` needs `lit` 3. `createGrid(definition)` turns a
1117
+ definition into a template function; each area is a member of it, typed as
1118
+ the area's name. Create grids at module level, once per definition:
1119
+
1120
+ ```ts lit
1121
+ import { css, html, LitElement, nothing } from "lit";
1122
+ import { defineLayout } from "typed-grid-areas";
1123
+ import { createGrid, gridStyles } from "@typed-grid-areas/lit";
1124
+
1125
+ const Content = createGrid(
1126
+ defineLayout({
1127
+ areas: ["icon?", "title"],
1128
+ layouts: [{ template: "icon title", columns: "min-content minmax(0, 1fr)" }],
1129
+ }),
1130
+ );
1131
+
1132
+ const Card = createGrid(
1133
+ defineLayout({
1134
+ areas: ["input?", "content"],
1135
+ layouts: [{ template: "input content", columns: "auto minmax(0, 1fr)" }],
1136
+ }),
1137
+ { nest: { content: Content } },
1138
+ );
1139
+
1140
+ class PreferenceCard extends LitElement {
1141
+ static styles = [
1142
+ gridStyles(Card),
1143
+ css`
1144
+ .gap {
1145
+ gap: 1rem;
1146
+ }
1147
+ `,
1148
+ ];
1149
+ static properties = { selectable: { type: Boolean } };
1150
+ declare selectable: boolean;
1151
+
1152
+ constructor() {
1153
+ super();
1154
+ this.selectable = false;
1155
+ }
1156
+
1157
+ render() {
1158
+ return Card(
1159
+ html`
1160
+ ${
1161
+ this.selectable
1162
+ ? html`<label data-area=${Card.input}><input type="checkbox" /></label>`
1163
+ : nothing
1164
+ }
1165
+ ${Card.content(
1166
+ html`
1167
+ <span data-area=${Card.content.icon}>★</span>
1168
+ <div data-area=${Card.content.title}>Weekly digest</div>
1169
+ `,
1170
+ { class: "p-4" },
1171
+ )}
1172
+ `,
1173
+ { class: "gap" },
1174
+ );
1175
+ }
1176
+ }
1177
+
1178
+ customElements.define("preference-card", PreferenceCard);
1179
+ ```
1180
+
1181
+ - **`Card(content, { class, style, ref })`** renders the grid: a `div` with
1182
+ the definition's `data-grid-id`, with `content` as its slots. When the
1183
+ definition has breakpoints the grid sits inside its own size container;
1184
+ the options still go on the grid. Lit can't spread arbitrary attributes,
1185
+ so the grid takes only `class`, `style` (text, or an object as `styleMap`
1186
+ takes it) and `ref` (as Lit's `ref` directive takes it): for other
1187
+ attributes or event listeners on the grid, use `ref`.
1188
+ - **`Card.input`** is the area's name, `"input"`, typed as that literal.
1189
+ A slot is any element with `data-area=${Card.input}`: your own `<label>`,
1190
+ `<img>` or `<button>` is the slot itself, with nothing to opt into and no
1191
+ wrapper. `Card.inptu` is a type error. A slot here is an element with
1192
+ `data-area`, never a `<slot>`.
1193
+ - **`nest`** makes the area's member the nested grid's template function:
1194
+ `Card.content(…)` renders `Content` as the `content` slot of `Card`, and
1195
+ `Card.content.icon` reaches its areas.
1196
+
1197
+ Slots must be **direct children** of their grid, in the grid's own DOM:
1198
+ grids render in light DOM, wherever your template is (usually your
1199
+ component's shadow root), since the generated CSS's child selectors can't see
1200
+ through a shadow boundary into `<slot>`ted children. Leave an optional
1201
+ area's slot out with an ordinary conditional (`nothing`) to make it absent.
1202
+
1203
+ ### CSS, server rendering and development warnings
1204
+
1205
+ Each grid puts its definition's CSS in the root it renders in, after each
1206
+ update:
1207
+
1208
+ - **In a shadow root**, one shared constructable style sheet per definition
1209
+ is adopted into it, once per shadow root rather than once per page. Your
1210
+ component's own `static styles` still beat the grid's layered CSS, and an
1211
+ area its layout leaves out stays hidden whatever `display` its slot sets.
1212
+ - **In the document**, a `<style data-typed-grid-areas>` goes into `<head>`,
1213
+ once per definition.
1214
+
1215
+ `gridStyles(Card)` returns the CSS of `Card` and the grids nested in it, as a
1216
+ `CSSResult` for `static styles`. In the browser it's optional (a grid already
1217
+ there isn't adopted again). **Server rendering needs it**: with
1218
+ `@lit-labs/ssr`, Lit's server renderer, the CSS reaches each component's
1219
+ declarative shadow root only through `static styles`, and hydration with
1220
+ `@lit-labs/ssr-client` takes over from there. Grids rendered on the server
1221
+ outside any shadow root need their style tags in `<head>`: see
1222
+ `gridStyleTags` [below](#without-react-typed-grid-areasadapter).
1223
+
1224
+ In development (`process.env.NODE_ENV === "development"`), a grid warns on
1225
+ the console after an update when a required area's slot isn't rendered, or
1226
+ when it has a direct child that isn't one of its slots, once per distinct
1227
+ set of problems.
1228
+
1229
+ Supported: Lit 3, and `@lit-labs/ssr` 4 (still labelled pre-release by the
1230
+ Lit team) for server rendering.
1231
+
1232
+ ## Rendering with Octane: `@typed-grid-areas/octane` (experimental)
1233
+
1234
+ ```sh
1235
+ npm i typed-grid-areas @typed-grid-areas/octane
1236
+ ```
1237
+
1238
+ `@typed-grid-areas/octane` renders a definition as typed slot components in
1239
+ [Octane](https://octanejs.dev), natively (not through Octane's React
1240
+ compatibility layer). It is **experimental until Octane 1.0**: it supports
1241
+ the current Octane minor only, **Octane 0.16** (`octane@^0.16.0`), and each
1242
+ new Octane minor needs a release of the adapter.
1243
+
1244
+ The package ships its source (`.tsx`), not a build: your app's Octane
1245
+ bundler plugin (`@octanejs/vite-plugin`, Rspack, Rsbuild) compiles it like
1246
+ your own components, and plain `tsc` type-checks it.
1247
+
1248
+ ```tsrx
1249
+ import { defineLayout } from "typed-grid-areas";
1250
+ import { createGridComponent } from "@typed-grid-areas/octane";
1251
+
1252
+ const Content = createGridComponent(
1253
+ defineLayout({ areas: ["icon?", "title"], layouts: [{ template: "icon title" }] }),
1254
+ );
1255
+
1256
+ const Card = createGridComponent(
1257
+ defineLayout({ areas: ["input?", "content"], layouts: [{ template: "input content" }] }),
1258
+ { nest: { content: Content } },
1259
+ );
1260
+
1261
+ export function Example(props: { selectable: boolean }) @{
1262
+ <Card class="gap-4" render={<section />}>
1263
+ @if (props.selectable) {
1264
+ <Card.input render={<label for="notify" />}><input id="notify" type="checkbox" /></Card.input>
1265
+ }
1266
+ <Card.content>
1267
+ <Card.content.icon>{'★'}</Card.content.icon>
1268
+ <Card.content.title>{'Notify me'}</Card.content.title>
1269
+ </Card.content>
1270
+ </Card>
1271
+ }
1272
+ ```
1273
+
1274
+ - **The API is React's** (see [Rendering with React](#rendering-with-react-grid-components)):
1275
+ `createGridComponent(definition, { nest })`, `<Card>` for the grid, a
1276
+ static slot component per area (`<Card.input>`, and `<Card.content.icon>`
1277
+ through `nest`), each taking `class`, `style`, `ref`, `children` and any
1278
+ other `div` props. Member tags work in `.tsx` and `.tsrx`, including
1279
+ inside `@if`; a misspelled area is a type error (`tsrx-tsc` checks
1280
+ `.tsrx`).
1281
+ - **Your own element as a slot: `render={<el />}`**, on slots and on the
1282
+ grid, instead of React's `asChild` (Octane's `.tsrx` compiles children to
1283
+ functions, which can't be cloned). The element is rendered with the
1284
+ slot's props merged in: the adapter's `data-area` / `data-grid-id` always
1285
+ win; otherwise the element's own props win; classes compose, styles merge,
1286
+ event handlers chain (the element's first), both refs attach; the slot's
1287
+ children become the element's children.
1288
+ - **Optional areas:** leave a slot out with Octane's ordinary conditional
1289
+ (`@if` in `.tsrx`, `{cond && …}` in `.tsx`); the derived layout applies.
1290
+ - **CSS:** each definition's CSS goes on the page once, through Octane's
1291
+ `injectStyle`: one `<style data-octane>` tag per definition in `<head>`,
1292
+ however many instances render. Without server rendering there is nothing
1293
+ to do. With it, `renderToString` (and `prerender`) return the CSS in
1294
+ `css`, separate from `html`: put `result.css` before your markup (in
1295
+ `<head>`), as Octane's scoped styles require. A stream
1296
+ (`renderToReadableStream`, `renderToPipeableStream`) leads its shell with
1297
+ it. Hydration adopts the server's tags instead of adding second copies.
1298
+ - **Development warnings** are React's: a missing required slot or a stray
1299
+ direct child warns once (after mount, and after an update that introduces
1300
+ it); production and server rendering log nothing.
1301
+
1302
+ ```ts octane
1303
+ // entry-server.ts
1304
+ import { renderToString } from "octane/server";
1305
+ import { App } from "./App.tsrx";
1306
+
1307
+ const { html, css } = renderToString(App);
1308
+ const page = `<!doctype html><html><head>${css}</head><body><div id="root">${html}</div></body></html>`;
1309
+ ```
1310
+
1311
+ ## Without React: `typed-grid-areas/adapter`
1312
+
1313
+ The core has no React dependency. To render grids in another framework, build
1314
+ on `typed-grid-areas/adapter`: it says exactly what to render for a
1315
+ definition, so your adapter renders the same DOM the React one does.
1316
+
1317
+ ```ts
1318
+ import { defineLayout } from "typed-grid-areas";
1319
+ import {
1320
+ createSlotCheck,
1321
+ gridMarkup,
1322
+ gridStyleTags,
1323
+ insertGridStyle,
1324
+ } from "typed-grid-areas/adapter";
1325
+
1326
+ const cardGrid = defineLayout({
1327
+ areas: ["icon?", "info"],
1328
+ layouts: [{ template: "icon info", breakpoints: { "@sm": { template: "info icon" } } }],
1329
+ });
1330
+
1331
+ // Once per definition.
1332
+ const markup = gridMarkup(cardGrid);
1333
+
1334
+ // Per instance. Pass the outer grid's area when this grid fills one.
1335
+ const { sizeContainer, grid } = markup.elements("content");
1336
+ // sizeContainer: undefined without breakpoints; otherwise the element that
1337
+ // wraps the grid: { attributes: { "data-area": "content" }, style, styleText }
1338
+ // grid.attributes: { "data-grid-id": markup.id }, plus the outer "data-area"
1339
+ // when there's no size container
1340
+
1341
+ markup.slot("info"); // { "data-area": "info" }, for each direct child of the grid
1342
+
1343
+ // The CSS, once per page: in the browser...
1344
+ insertGridStyle(document, cardGrid);
1345
+ // ...or in server-rendered <head>.
1346
+ const head = gridStyleTags([cardGrid]);
1347
+
1348
+ // In development, after each render: what to warn about the grid's slots.
1349
+ try {
1350
+ if (process.env.NODE_ENV === "development") {
1351
+ const checkSlots = createSlotCheck(cardGrid, {
1352
+ strayChildRemedy: "make it a slot with asChild",
1353
+ });
1354
+ const gridElement = document.querySelector(`[data-grid-id="${markup.id}"]`);
1355
+ if (gridElement !== null) {
1356
+ checkSlots(gridElement).forEach(warning => console.warn(warning));
1357
+ }
1358
+ }
1359
+ } catch (error) {
1360
+ // Without a bundler, `process` may not exist: then this isn't development.
1361
+ if (!(error instanceof ReferenceError)) throw error;
1362
+ }
1363
+ ```
1364
+
1365
+ - **Elements.** Render the size container (when there is one) with its
1366
+ attributes and style, the grid inside it with its attributes, and each slot
1367
+ as a direct child of the grid with its attributes. The CSS makes the grid a
1368
+ grid and places the slots (see [Breakpoints](#breakpoints) for why a grid
1369
+ with breakpoints has its own size container).
1370
+ - **CSS.** `markup.css` belongs on the page once per definition, before its
1371
+ grids lay out. `insertGridStyle` puts a `<style data-typed-grid-areas="<grid
1372
+ id>">` in `<head>` (ahead of the page's other styles, reusing a tag the
1373
+ server rendered), and `gridStyleTags` renders those tags as an HTML string.
1374
+ A framework with its own way to emit a style once can key it by
1375
+ `markup.cssKey` instead.
1376
+ - **Slot check.** One check per grid instance; it reports a set of problems
1377
+ once. `strayChildRemedy` says how your framework makes an element a slot.
1378
+ Gate it on your framework's development flag, or read
1379
+ `process.env.NODE_ENV` as above: bundlers replace it, so production builds
1380
+ drop the check. Behind a call, such as `isDevelopment()`, they keep it.
1381
+
1382
+ The main entry's `createSlots`, `generateCss` and `gridId` remain for
1383
+ rendering by hand.
1384
+
1385
+ ## Development
1386
+
1387
+ An npm-workspaces monorepo: one folder per package under `packages/`
1388
+ (`core` is `typed-grid-areas`, `react` is `@typed-grid-areas/react`, and the
1389
+ private `acceptance` holds the browser acceptance tests every adapter runs).
1390
+ `npm install` at the root sets them all up.
1391
+
1392
+ ```sh
1393
+ just test # unit tests (happy-dom), the release scripts and the packed packages
1394
+ just test-browser # in-browser layout tests (Chromium via Playwright)
1395
+ just typecheck # tsc --noEmit, including the type tests
1396
+ just build # compile each package's src/ into its dist/, as npm pack does
1397
+ ```
1398
+
1399
+ Each runs every package's own script of that name (or via npm directly:
1400
+ `npm test`, `npm run typecheck`, `npm run build`). Each package owns its
1401
+ toolchain, down to its TypeScript version; see
1402
+ [docs/agents/workspaces.md](https://github.com/MichaelBuss/typed-grid-areas/blob/main/docs/agents/workspaces.md) for adding an adapter
1403
+ and pinning its own TypeScript.
1404
+
1405
+ A change to a package carries a changeset (`npm run changeset`); see
1406
+ [docs/agents/release.md](https://github.com/MichaelBuss/typed-grid-areas/blob/main/docs/agents/release.md) for how releases ship.