@ydant/base 0.1.0 → 0.1.1
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/README.md +43 -36
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,8 +25,8 @@ pnpm add @ydant/base
|
|
|
25
25
|
## Usage
|
|
26
26
|
|
|
27
27
|
```typescript
|
|
28
|
-
import { mount } from "@ydant/core";
|
|
29
|
-
import { createBasePlugin, div, p, text, classes
|
|
28
|
+
import { mount, type Component } from "@ydant/core";
|
|
29
|
+
import { createBasePlugin, div, p, text, classes } from "@ydant/base";
|
|
30
30
|
|
|
31
31
|
const Greeting: Component = () =>
|
|
32
32
|
div(function* () {
|
|
@@ -53,7 +53,6 @@ The base plugin extends `RenderContext` and `PluginAPI`:
|
|
|
53
53
|
// RenderContext extensions
|
|
54
54
|
interface RenderContextExtensions {
|
|
55
55
|
isCurrentElementReused: boolean;
|
|
56
|
-
pendingKey: string | number | null;
|
|
57
56
|
keyedNodes: Map<string | number, KeyedNode>;
|
|
58
57
|
mountCallbacks: Array<() => void | (() => void)>;
|
|
59
58
|
unmountCallbacks: Array<() => void>;
|
|
@@ -73,10 +72,9 @@ interface PluginAPIExtensions {
|
|
|
73
72
|
onUnmount(callback: () => void): void;
|
|
74
73
|
addUnmountCallbacks(...callbacks: Array<() => void>): void;
|
|
75
74
|
executeMount(): void;
|
|
75
|
+
getUnmountCallbacks(): Array<() => void>;
|
|
76
76
|
|
|
77
77
|
// Keyed elements
|
|
78
|
-
readonly pendingKey: string | number | null;
|
|
79
|
-
setPendingKey(key: string | number | null): void;
|
|
80
78
|
getKeyedNode(key: string | number): KeyedNode | undefined;
|
|
81
79
|
setKeyedNode(key: string | number, node: KeyedNode): void;
|
|
82
80
|
deleteKeyedNode(key: string | number): void;
|
|
@@ -89,18 +87,25 @@ HTML elements: `div`, `span`, `p`, `button`, `input`, `h1`-`h3`, `ul`, `li`, `a`
|
|
|
89
87
|
|
|
90
88
|
SVG elements: `svg`, `circle`, `ellipse`, `line`, `path`, `polygon`, `polyline`, `rect`, `g`, `defs`, `use`, `clipPath`, `mask`, `linearGradient`, `radialGradient`, `stop`, `svgText`, `tspan`
|
|
91
89
|
|
|
90
|
+
Custom elements can be created with factory helpers:
|
|
91
|
+
|
|
92
|
+
| Function | Description |
|
|
93
|
+
| ------------------------ | ----------------------------------------- |
|
|
94
|
+
| `createHTMLElement(tag)` | Create an element factory for an HTML tag |
|
|
95
|
+
| `createSVGElement(tag)` | Create an element factory for an SVG tag |
|
|
96
|
+
|
|
92
97
|
### Primitives
|
|
93
98
|
|
|
94
|
-
| Function | Description
|
|
95
|
-
| --------------------- |
|
|
96
|
-
| `text(content)` | Create a text node
|
|
97
|
-
| `attr(key, value)` | Set an HTML attribute
|
|
98
|
-
| `classes(...names)` | Set class attribute
|
|
99
|
-
| `on(event, handler)` | Add event listener
|
|
100
|
-
| `style(styles)` | Set inline styles
|
|
101
|
-
| `key
|
|
102
|
-
| `onMount(callback)` | Lifecycle hook for mount
|
|
103
|
-
| `onUnmount(callback)` | Lifecycle hook for unmount
|
|
99
|
+
| Function | Description |
|
|
100
|
+
| --------------------- | ------------------------------------- |
|
|
101
|
+
| `text(content)` | Create a text node |
|
|
102
|
+
| `attr(key, value)` | Set an HTML attribute |
|
|
103
|
+
| `classes(...names)` | Set class attribute |
|
|
104
|
+
| `on(event, handler)` | Add event listener |
|
|
105
|
+
| `style(styles)` | Set inline styles |
|
|
106
|
+
| `keyed(key, factory)` | Wrap a factory with a key for diffing |
|
|
107
|
+
| `onMount(callback)` | Lifecycle hook for mount |
|
|
108
|
+
| `onUnmount(callback)` | Lifecycle hook for unmount |
|
|
104
109
|
|
|
105
110
|
#### `on()` Type Overloads
|
|
106
111
|
|
|
@@ -128,19 +133,19 @@ yield *
|
|
|
128
133
|
|
|
129
134
|
### Types
|
|
130
135
|
|
|
131
|
-
| Type | Description
|
|
132
|
-
| --------------- |
|
|
133
|
-
| `Slot` | `{ node: HTMLElement, refresh: (
|
|
134
|
-
| `SlotRef` |
|
|
135
|
-
| `
|
|
136
|
-
| `
|
|
137
|
-
| `
|
|
138
|
-
| `
|
|
139
|
-
| `
|
|
140
|
-
| `
|
|
141
|
-
| `
|
|
142
|
-
|
|
143
|
-
|
|
136
|
+
| Type | Description |
|
|
137
|
+
| --------------- | -------------------------------------------------------------- |
|
|
138
|
+
| `Slot` | `{ readonly node: HTMLElement, refresh: (children) => void }` |
|
|
139
|
+
| `SlotRef` | Reference holder for a `Slot`, created by `createSlotRef()` |
|
|
140
|
+
| `ElementRender` | `Generator<Element, Slot, ChildNext>` - Element factory return |
|
|
141
|
+
| `Element` | Tagged type for HTML/SVG elements |
|
|
142
|
+
| `Attribute` | Tagged type for attributes |
|
|
143
|
+
| `Listener` | Tagged type for event listeners |
|
|
144
|
+
| `Text` | Tagged type for text nodes |
|
|
145
|
+
| `Lifecycle` | Tagged type for lifecycle hooks |
|
|
146
|
+
| `Decoration` | Union type `Attribute \| Listener` |
|
|
147
|
+
|
|
148
|
+
> `Render`, `Component` types are defined in `@ydant/core`.
|
|
144
149
|
|
|
145
150
|
### createSlotRef
|
|
146
151
|
|
|
@@ -148,34 +153,36 @@ yield *
|
|
|
148
153
|
function createSlotRef(): SlotRef;
|
|
149
154
|
|
|
150
155
|
interface SlotRef {
|
|
151
|
-
current: Slot | null;
|
|
156
|
+
readonly current: Slot | null;
|
|
157
|
+
bind(slot: Slot): void;
|
|
158
|
+
refresh(children: Builder): void;
|
|
159
|
+
readonly node: HTMLElement | null;
|
|
152
160
|
}
|
|
153
161
|
```
|
|
154
162
|
|
|
155
|
-
Creates a
|
|
163
|
+
Creates a reference holder for a `Slot`. Use `bind()` to associate a Slot, then `refresh()` and `node` to interact with it:
|
|
156
164
|
|
|
157
165
|
```typescript
|
|
158
166
|
const ref = createSlotRef();
|
|
159
167
|
|
|
160
168
|
yield *
|
|
161
169
|
div(function* () {
|
|
162
|
-
ref.
|
|
170
|
+
ref.bind(yield* div(() => [text("Content")]));
|
|
163
171
|
});
|
|
164
172
|
|
|
165
173
|
// Later: update via ref
|
|
166
|
-
ref.
|
|
174
|
+
ref.refresh(() => [text("Updated!")]);
|
|
167
175
|
```
|
|
168
176
|
|
|
169
|
-
###
|
|
177
|
+
### keyed() and Element Reuse
|
|
170
178
|
|
|
171
|
-
|
|
179
|
+
`keyed()` wraps an element factory or component, attaching a key for list diffing. The same key will reuse the existing DOM element:
|
|
172
180
|
|
|
173
181
|
```typescript
|
|
174
182
|
yield *
|
|
175
183
|
ul(function* () {
|
|
176
184
|
for (const item of items) {
|
|
177
|
-
yield*
|
|
178
|
-
yield* li(() => [text(item.name)]);
|
|
185
|
+
yield* keyed(item.id, li)(() => [text(item.name)]);
|
|
179
186
|
}
|
|
180
187
|
});
|
|
181
188
|
```
|