|
| 1 | +# Usage |
| 2 | + |
| 3 | +API, node shape, layout and style — create schema from definition, render to DOM. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Quick Start](#quick-start) |
| 8 | +- [Flow Overview](#flow-overview) |
| 9 | +- [API Overview](#api-overview) |
| 10 | +- [Node Shape](#node-shape) |
| 11 | +- [Layout and Style](#layout-and-style) |
| 12 | +- [Void Tags and Special Elements](#void-tags-and-special-elements) |
| 13 | +- [API Reference](#api-reference) |
| 14 | +- [Type Reference](#type-reference) |
| 15 | +- [Reference](#reference) |
| 16 | + |
| 17 | +## Quick Start |
| 18 | + |
| 19 | +**Flow:** definition (`{ root: Node[] }`) → `create(definition)` → schema → `render(schema, container)`. |
| 20 | + |
| 21 | +```typescript |
| 22 | +import { create, render } from '@neabyte/schema2ui' |
| 23 | + |
| 24 | +// 1. Define root: array of nodes (here: a div and a link) |
| 25 | +const schema = create({ |
| 26 | + root: [ |
| 27 | + { type: 'div', id: 'box', layout: { width: 200, height: 100 }, content: 'Hello' }, |
| 28 | + { type: 'a', attrs: { href: '/about' }, content: 'About' } |
| 29 | + ] |
| 30 | +}) |
| 31 | + |
| 32 | +// 2. Render the schema into a container element |
| 33 | +render(schema, document.getElementById('app')) |
| 34 | +``` |
| 35 | + |
| 36 | +## Flow Overview |
| 37 | + |
| 38 | +1. **Definition** — Plain object `{ root: Node[] }`. Each node has at least `type` (HTML tag name); optional: `id`, `layout`, `style`, `attrs`, `content`, `src`/`alt` (img), `children`. |
| 39 | +2. **Create** — `create(definition)` validates the definition (root array, node shape, allowed keys, void tags must not have children). Returns a frozen, normalized `Schema`. Throws on invalid input. |
| 40 | +3. **Render** — `render(schema, container)` walks `schema.root`, creates DOM elements (or SVG via `createElementNS` where needed), applies `attrs`, `layout`, `style`, sets `content`/`src`/`alt`, appends children. Results are appended to `container` (HTMLElement). |
| 41 | + |
| 42 | +## API Overview |
| 43 | + |
| 44 | +| Function / export | Returns | Description | |
| 45 | +| :----------------------------------------------------- | :------- | :--------------------------------------------- | |
| 46 | +| [`create(definition)`](#createdefinition) | `Schema` | Validate and freeze definition; return schema. | |
| 47 | +| [`render(schema, container)`](#renderschema-container) | `void` | Build DOM from schema and append to container. | |
| 48 | + |
| 49 | +Named exports: `create`, `render`, and all types from `Types` (re-exported). Default export: `{ create, render }`. |
| 50 | + |
| 51 | +## Node Shape |
| 52 | + |
| 53 | +Each node is an object with: |
| 54 | + |
| 55 | +| Key | Type | Required | Description | |
| 56 | +| :--------- | :------- | :------- | :---------------------------------------------------------- | |
| 57 | +| `type` | `string` | yes | HTML tag name (lowercase), e.g. `div`, `a`, `table`, `svg`. | |
| 58 | +| `id` | `string` | no | Element `id` attribute. | |
| 59 | +| `layout` | `Layout` | no | Width, height, position, flex, gap → CSS. | |
| 60 | +| `style` | `Style` | no | Fill, stroke, font, border → CSS. | |
| 61 | +| `attrs` | `Attrs` | no | Arbitrary HTML attributes (href, class, etc.). | |
| 62 | +| `content` | `string` | no | Text content for the node. | |
| 63 | +| `src` | `string` | no | Image (or similar) source URL; use with `type: 'img'`. | |
| 64 | +| `alt` | `string` | no | Alt text for img. | |
| 65 | +| `children` | `Node[]` | no | Child nodes. Not allowed on void tags. | |
| 66 | + |
| 67 | +Allowed keys are exactly the above. Extra keys cause validation to throw. |
| 68 | + |
| 69 | +## Layout and Style |
| 70 | + |
| 71 | +- **Layout** — `width`, `height`, `x`, `y`, `flex`, `gap`. Number values are converted to `Npx`; strings (e.g. `'100%'`, `'auto'`) are used as-is. Applied to the element’s `style` (width, height, left, top, flex, gap). Setting `flex` or `gap` may set `display` to `block` or `flex` when needed. |
| 72 | +- **Style** — `fill` → `backgroundColor`, `stroke` → `border`, `font` → `font`, `border` → `border`. All string values. |
| 73 | + |
| 74 | +## Void Tags and Special Elements |
| 75 | + |
| 76 | +- **Void tags** — No `children`. List includes: `area`, `base`, `br`, `col`, `embed`, `hr`, `img`, `input`, `link`, `meta`, `param`, `source`, `track`, `wbr`. If a void tag has non-empty `children`, `create()` throws. |
| 77 | +- **template** — Child nodes are appended to `element.content` (DocumentFragment), not to the template element itself. |
| 78 | +- **svg** — SVG elements are created with `createElementNS(SVG_NS, type)`; descendants stay in SVG namespace when under an `svg` node. |
| 79 | + |
| 80 | +## API Reference |
| 81 | + |
| 82 | +### create(definition) |
| 83 | + |
| 84 | +Validate the definition and return a frozen schema. Normalizes node keys (e.g. lowercases `type`). Deep-freezes the schema and all nested nodes. |
| 85 | + |
| 86 | +- `definition` `<Definition | unknown>`: Object with `root: Node[]`. Can be a plain object; validation ensures shape. |
| 87 | +- **Returns:** `<Schema>` Frozen object `{ root: readonly Node[] }`. |
| 88 | +- **Throws:** When `definition` is not an object, `root` is not an array, or any node is invalid (unknown key, wrong type for a field, void tag with children). |
| 89 | + |
| 90 | +### render(schema, container) |
| 91 | + |
| 92 | +Build DOM from `schema.root` and append each resulting node into `container`. Uses the container’s `ownerDocument` (or `document`). Handles `template` (children into `template.content`) and SVG (namespace). Does not clear `container` before appending. |
| 93 | + |
| 94 | +- `schema` `<Schema>`: Frozen schema from `create()`. |
| 95 | +- `container` `<HTMLElement>`: Target element to append to. |
| 96 | +- **Returns:** `void`. |
| 97 | + |
| 98 | +## Type Reference |
| 99 | + |
| 100 | +Types are re-exported from the package: `import type { Attrs, Definition, Layout, Node, Schema, Style } from '@neabyte/schema2ui'`. |
| 101 | + |
| 102 | +- **Definition:** `{ root: readonly Node[] }` — Input to `create`. |
| 103 | +- **Schema:** `{ readonly root: readonly Node[] }` — Output of `create`; frozen. |
| 104 | +- **Node:** Object with `type` (string) and optional `id`, `layout`, `style`, `attrs`, `content`, `src`, `alt`, `children` (readonly array of Node). Void tags must not have `children`. |
| 105 | +- **Layout:** Optional `width`, `height`, `x`, `y`, `flex`, `gap` — each `number | string`. |
| 106 | +- **Style:** Optional `fill`, `stroke`, `font`, `border` — each `string`. |
| 107 | +- **Attrs:** `Record<string, string | number | boolean>` — HTML attributes. Special handling for `class`/`className`, `style` (string), `value`, `checked`, `disabled` on form elements. |
| 108 | + |
| 109 | +## Reference |
| 110 | + |
| 111 | +- [README](README.md) — Installation and quick start. |
| 112 | +- Tests under `tests/` — Create, Constant, Validator, Render. |
0 commit comments