sigula 1.0.3 → 2.0.0

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/dist/sigula.d.ts CHANGED
@@ -1,154 +1,799 @@
1
- //#region src/boundary.d.ts
1
+ //#region src/core/boundary.d.ts
2
+ /**
3
+ * An inclusive range of sibling nodes (`start` through `end`).
4
+ *
5
+ * @group Low-level API
6
+ */
2
7
  export interface Boundary {
8
+ /** The first node in the range. */
3
9
  start: Node;
10
+ /** The last node in the range. */
4
11
  end: Node;
5
12
  }
13
+ /**
14
+ * Visits every node from `b.start` through `b.end`.
15
+ *
16
+ * @param b - the boundary to walk.
17
+ * @param fn - called with each node.
18
+ * @group Low-level API
19
+ */
20
+ export declare const walkBoundary: (b: Boundary, fn: (node: Node) => void) => void;
21
+ /**
22
+ * Removes every node in the boundary. No-op if the boundary has no parent.
23
+ *
24
+ * @param b - the boundary to remove.
25
+ * @group Low-level API
26
+ */
6
27
  export declare const removeBoundary: (b: Boundary) => void;
28
+ /**
29
+ * Wraps a node in a {@link Boundary}. A `DocumentFragment` spans its first and
30
+ * last child; any other node covers itself. Throws `E2` on an empty fragment.
31
+ *
32
+ * @param node - the node to wrap.
33
+ * @returns the node's boundary.
34
+ * @group Low-level API
35
+ */
7
36
  export declare const toBoundary: (node: Node) => Boundary;
37
+ /**
38
+ * Replaces an entire boundary with `node` and returns the new boundary. Throws
39
+ * `E3` if the old boundary has no parent.
40
+ *
41
+ * @param old - the boundary to replace.
42
+ * @param node - the replacement node.
43
+ * @returns the boundary of the inserted node.
44
+ * @group Low-level API
45
+ */
8
46
  export declare const replaceWithNode: (old: Boundary, node: Node) => Boundary;
9
47
  //#endregion
10
- //#region src/cmd.d.ts
48
+ //#region src/core/cmd.d.ts
49
+ /**
50
+ * Base type for command contexts: an arbitrary string-keyed record.
51
+ *
52
+ * @group Low-level API
53
+ */
11
54
  export interface CmdContext {
55
+ /** Any string key; values are unconstrained. */
12
56
  [key: string]: unknown;
13
57
  }
58
+ /**
59
+ * The unit of work a binding runs: it receives the current value and context.
60
+ *
61
+ * @typeParam T - the value type.
62
+ * @typeParam C - the context type.
63
+ * @group Low-level API
64
+ */
14
65
  export type Cmd<T, C extends CmdContext> = (val: T, context: C) => void;
66
+ /**
67
+ * A {@link Cmd} with erased value and context types.
68
+ *
69
+ * @group Low-level API
70
+ */
15
71
  export type AnyCmd = Cmd<any, any>;
16
72
  //#endregion
17
- //#region src/eq.d.ts
73
+ //#region src/core/eq.d.ts
74
+ /**
75
+ * Implement this on a value type to give `eq` custom equality semantics.
76
+ *
77
+ * @group Reactivity
78
+ */
18
79
  export interface Equatable {
80
+ /** Returns whether `this` and `other` are equal. */
19
81
  equals(other: unknown): boolean;
20
82
  }
83
+ /**
84
+ * A value equality function.
85
+ *
86
+ * @typeParam T - the value type.
87
+ * @group Reactivity
88
+ */
89
+ export type Eq<T> = (a: T, b: T) => boolean;
90
+ /**
91
+ * Convenience alias for an arbitrary string-keyed object.
92
+ *
93
+ * @group Reactivity
94
+ */
21
95
  export type UnknownRecord = Record<string, unknown>;
22
- export declare const isEqual: <T>(a: T, b: T) => boolean;
96
+ /**
97
+ * Deep structural equality. Compares primitives, arrays, `Date`, `RegExp`,
98
+ * `Map`, `Set`, and plain objects, and defers to `a.equals(b)` when `a`
99
+ * implements `Equatable`. This is the default comparator for `Sig.update` and
100
+ * `repeat`. Values with different prototypes are never equal.
101
+ *
102
+ * @typeParam T - the value type.
103
+ * @param a - the first value.
104
+ * @param b - the second value.
105
+ * @returns `true` when `a` and `b` are deeply equal.
106
+ * @group Reactivity
107
+ */
108
+ export declare const eq: <T>(a: T, b: T) => boolean;
23
109
  //#endregion
24
- //#region src/sig.bind.d.ts
110
+ //#region src/core/sig.bind.d.ts
111
+ /**
112
+ * A registered binding: the signal, its command context, and the command that
113
+ * runs when the signal changes.
114
+ *
115
+ * @typeParam T - the signal's value type.
116
+ * @typeParam C - the command context type.
117
+ * @group Reactivity
118
+ */
25
119
  export interface Bind<T, C extends CmdContext> {
120
+ /** The signal this binding observes. */
26
121
  sig: Sig<T>;
122
+ /** The context passed to `cmd` on each run. */
27
123
  context: C;
124
+ /** The command run with the current value and context. */
28
125
  cmd: Cmd<T, C>;
126
+ /** Set when the binding is detached; a removed binding is skipped. */
29
127
  removed: boolean;
128
+ /** Queue flag; true while the binding is queued for the next flush. */
30
129
  queued?: boolean;
31
130
  }
131
+ /**
132
+ * A {@link Bind} with erased value and context types.
133
+ *
134
+ * @group Reactivity
135
+ */
32
136
  export type AnyBind = Bind<any, any>;
137
+ /**
138
+ * The core reactive value. A `Sig` holds a value and a set of bindings that run
139
+ * when it changes; writes are queued and coalesced in a microtask.
140
+ *
141
+ * @typeParam T - the value type.
142
+ * @group Reactivity
143
+ */
33
144
  export declare class Sig<T> implements Equatable {
34
145
  private _val;
35
146
  private _binds;
36
- constructor(val: T);
147
+ private _eq;
148
+ constructor(val: T, opts?: {
149
+ eq?: Eq<T>;
150
+ });
151
+ /** `Equatable` implementation; two `Sig`s are equal when their values are deeply equal. */
37
152
  equals(b: unknown): boolean;
153
+ /** Reads the current value. */
38
154
  get(): T;
155
+ /** Enqueues dependents without changing the value; use after in-place mutation. */
156
+ notify(): void;
157
+ /** Sets the value and always notifies dependents, even when deeply equal. */
39
158
  forceUpdate(v: T): void;
159
+ /** Sets the value and notifies dependents only when `eq(v, current)` is false. */
40
160
  update(v: T): void;
161
+ /** Applies `fn` to the current value via `update`, so an equal result is skipped. */
41
162
  trans(fn: (v: T) => T): void;
163
+ /** Registers a binding. Prefer `createBind` or the `patch`/`text`/`view` APIs. */
42
164
  addBind<C extends CmdContext>(bind: Bind<T, C>): void;
43
- removeBind(bind: Bind<T, CmdContext>): void;
165
+ /** Unregisters a binding; runs `cleanup()` when the last one goes away. */
166
+ removeBind<C extends CmdContext>(bind: Bind<T, C>): void;
167
+ /** Returns the current bindings. */
44
168
  getBinds(): Bind<T, CmdContext>[];
169
+ /** Overridable hook called when a signal loses all bindings. No-op on `Sig`. */
45
170
  cleanup(): void;
46
171
  }
172
+ /**
173
+ * A `Sig` produced by {@link compute}. Extends `Sig` and additionally tracks the
174
+ * source bindings that feed it: it detaches from its sources when it loses its
175
+ * last consumer, and re-links and recomputes once when a consumer is added
176
+ * again.
177
+ *
178
+ * @typeParam T - the derived value type.
179
+ * @group Reactivity
180
+ */
47
181
  export declare class DerivedSig<T> extends Sig<T> {
48
182
  private _fromBinds;
49
183
  private _linked;
184
+ /** Registers a source binding. */
50
185
  addFromBind<S, C extends CmdContext>(bind: Bind<S, C>): void;
186
+ /** Registers a consumer; re-links to sources and recomputes once if detached. */
51
187
  addBind<C extends CmdContext>(bind: Bind<T, C>): void;
188
+ /** Removes every source binding when the derived signal has no consumers. */
52
189
  cleanup(): void;
53
190
  }
191
+ /**
192
+ * Creates a writable signal holding `v`.
193
+ *
194
+ * @param v - the initial value.
195
+ * @returns a new `Sig` for `v`.
196
+ * @example
197
+ * ```ts
198
+ * const count = sig(0);
199
+ * count.get(); // 0
200
+ * count.update(1); // schedules dependents
201
+ * ```
202
+ * @group Reactivity
203
+ */
54
204
  export declare const sig: <T>(v: T) => Sig<T>;
55
- export declare const removeBind: (bind: Bind<unknown, CmdContext>) => void;
205
+ /**
206
+ * Detaches a binding and marks it removed so queued runs are skipped.
207
+ *
208
+ * @typeParam T - the signal's value type.
209
+ * @typeParam C - the command context type.
210
+ * @param bind - the binding to remove.
211
+ * @group Reactivity
212
+ */
213
+ export declare const removeBind: <T, C extends CmdContext>(bind: Bind<T, C>) => void;
214
+ /**
215
+ * Wires `cmd(sig.get(), context)` to run whenever `sig` changes.
216
+ *
217
+ * @typeParam T - the signal's value type.
218
+ * @typeParam C - the command context type.
219
+ * @param sig - the signal to observe.
220
+ * @param context - the context passed to `cmd`.
221
+ * @param cmd - the command run on change.
222
+ * @returns the registered binding.
223
+ * @group Reactivity
224
+ */
56
225
  export declare const createBind: <T, C extends CmdContext>(sig: Sig<T>, context: C, cmd: Cmd<T, C>) => Bind<T, C>;
226
+ /**
227
+ * A value that may be plain, a `Sig`, or `undefined`.
228
+ *
229
+ * @typeParam T - the underlying value type.
230
+ * @group Reactivity
231
+ */
232
+ export type Reactive<T> = T | Sig<T> | undefined;
57
233
  //#endregion
58
- //#region src/commit.d.ts
59
- export interface Commit<T, C extends CmdContext = any> {
60
- binds: Bind<T, C> | Bind<T, C>[] | undefined;
61
- children?: Commit<unknown, any>[] | undefined;
62
- }
63
- export declare const cleanCommit: (commit: Commit<unknown, CmdContext>) => void;
64
- //#endregion
65
- //#region src/compute.d.ts
234
+ //#region src/core/compute.d.ts
235
+ /**
236
+ * A record whose values are signals, used by the record overload of
237
+ * {@link compute}.
238
+ *
239
+ * @group Reactivity
240
+ */
66
241
  export interface SigRecord {
242
+ /** Each key maps to a signal of any value type. */
67
243
  [key: string]: Sig<any>;
68
244
  }
245
+ /**
246
+ * Maps a {@link SigRecord} to a record of the signals' values.
247
+ *
248
+ * @typeParam K - the signal record type.
249
+ * @group Reactivity
250
+ */
69
251
  export type ValRecord<K extends SigRecord> = { [P in keyof K]: K[P] extends Sig<infer U> ? U : never; };
252
+ /**
253
+ * Derives a signal from one source signal. The result recomputes whenever
254
+ * `source` changes.
255
+ *
256
+ * @typeParam S - the source value type.
257
+ * @typeParam T - the derived value type.
258
+ * @param source - the source signal.
259
+ * @param fn - maps the source value to the derived value.
260
+ * @returns a `DerivedSig` for the mapped value.
261
+ * @example
262
+ * ```ts
263
+ * const x = sig(1);
264
+ * const doubled = compute(x, (v) => v * 2);
265
+ * ```
266
+ * @group Reactivity
267
+ */
70
268
  export declare function compute<S, T>(source: Sig<S>, fn: (v: S) => T): DerivedSig<T>;
269
+ /**
270
+ * Derives a signal from a record of signals; `fn` receives the matching record
271
+ * of values. The result recomputes whenever any source changes.
272
+ *
273
+ * @typeParam S - the signal record type.
274
+ * @typeParam T - the derived value type.
275
+ * @param source - a record of signals.
276
+ * @param fn - maps the record of values to the derived value.
277
+ * @returns a `DerivedSig` for the mapped value.
278
+ * @example
279
+ * ```ts
280
+ * const sum = compute({x, y}, (v) => v.x + v.y);
281
+ * ```
282
+ * @group Reactivity
283
+ */
71
284
  export declare function compute<S extends SigRecord, T>(source: S, fn: (v: ValRecord<S>) => T): DerivedSig<T>;
72
285
  //#endregion
73
- //#region src/patch.d.ts
286
+ //#region src/core/err.d.ts
287
+ /**
288
+ * Throws an `Error` whose message is the short `code` (for example `E2` or
289
+ * `E11:1:2`). The full text for each code lives in the README error table, so
290
+ * string tables stay out of the bundle.
291
+ *
292
+ * @param code - the coded error message.
293
+ * @group Low-level API
294
+ */
295
+ export declare function err(code: string): never;
296
+ //#endregion
297
+ //#region src/core/patch.core.d.ts
298
+ /**
299
+ * A collection of deferred bindings to apply to one element, produced by
300
+ * {@link patch}.
301
+ *
302
+ * @group DOM bindings
303
+ */
304
+ export interface Patch {
305
+ /** Discriminant identifying a patch. */
306
+ type: 'patch';
307
+ /** Deferred patch-item factories, resolved against the target element on mount. */
308
+ toPatchItems: ToAnyPatchItem[];
309
+ /** Detaches the bindings created when the patch was committed. */
310
+ cleanBinds: () => void;
311
+ }
312
+ /**
313
+ * Context passed to patch commands: the target node plus any extra arguments.
314
+ *
315
+ * @group DOM bindings
316
+ */
74
317
  export interface PatchContext extends CmdContext {
318
+ /** The element the binding applies to. */
75
319
  node: Node;
320
+ /** Extra arguments for the command, such as the attribute or style key. */
76
321
  extra?: unknown[];
77
322
  }
323
+ /**
324
+ * One resolved patch binding: a source value, its context, and the command.
325
+ *
326
+ * @typeParam T - the source value type.
327
+ * @group DOM bindings
328
+ */
78
329
  export interface PatchItem<T> {
330
+ /** The plain value or `Sig` the command binds. */
79
331
  source: T | Sig<T>;
332
+ /** The context passed to `cmd`. */
80
333
  context: PatchContext;
334
+ /** The command run with the value and context. */
81
335
  cmd: Cmd<T, PatchContext>;
82
336
  }
337
+ /**
338
+ * A factory that defers reading the target element until mount.
339
+ *
340
+ * @typeParam T - the source value type.
341
+ * @group DOM bindings
342
+ */
83
343
  export type ToPatchItem<T> = (el: Element) => PatchItem<T>;
344
+ /**
345
+ * A {@link PatchItem} with an erased value type.
346
+ *
347
+ * @group DOM bindings
348
+ */
84
349
  export type AnyPatchItem = PatchItem<any>;
350
+ /**
351
+ * A {@link ToPatchItem} with an erased value type.
352
+ *
353
+ * @group DOM bindings
354
+ */
85
355
  export type ToAnyPatchItem = (el: Element) => AnyPatchItem;
86
- export interface Patch {
87
- type: 'patch';
88
- toPatchItems: ToAnyPatchItem[];
89
- }
90
- export declare const patch: (...toPatchItems: ToAnyPatchItem[]) => Patch;
91
- export declare const id: <T>(source: T | Sig<T>) => ToPatchItem<T>;
92
- export declare const val: <T>(source: T | Sig<T>) => ToPatchItem<T>;
93
- export declare const attr: <T>(source: T | Sig<T>, key: string) => ToPatchItem<T>;
94
- export type WritableStyleKey = { [K in keyof CSSStyleDeclaration]: CSSStyleDeclaration[K] extends string ? K : never; }[keyof CSSStyleDeclaration];
95
- export declare const style: <T>(source: T | Sig<T>, key: WritableStyleKey) => ToPatchItem<T>;
96
- export declare const styleProperty: <T>(source: T | Sig<T>, key: string) => ToPatchItem<T>;
97
- export declare const toggleClass: <T>(source: T | Sig<T>, token: string) => ToPatchItem<T>;
98
- export declare const toggleClasses: <T>(source: T | Sig<T>, ...tokens: string[]) => ToPatchItem<T>;
99
- export type ActFn<T> = (node: Node, val?: T) => void;
100
- export declare const act: <T>(source: T | Sig<T>, fn: ActFn<T>) => ToPatchItem<T>;
101
- type _Listener<K extends keyof HTMLElementEventMap> = (this: HTMLElement, ev: HTMLElementEventMap[K]) => unknown;
102
- export declare const on: <K extends keyof HTMLElementEventMap>(type: K, listener: _Listener<K>, options?: boolean | AddEventListenerOptions) => ToPatchItem<_Listener<K>>;
103
356
  //#endregion
104
- //#region src/view.d.ts
357
+ //#region src/core/utils.d.ts
358
+ /**
359
+ * Reads `arr[index]`, throwing `E1:<index>` when it is out of range.
360
+ *
361
+ * @typeParam T - the element type.
362
+ * @param arr - the array to read from.
363
+ * @param index - the index to read.
364
+ * @returns the element at `index`.
365
+ * @group Low-level API
366
+ */
367
+ export declare const at: <T>(arr: T[], index: number) => T;
368
+ //#endregion
369
+ //#region src/core/view.core.d.ts
370
+ /**
371
+ * An item that can occupy a slot in a view's children: a view or a patch.
372
+ *
373
+ * @group Templates
374
+ */
375
+ export type ChildView = AnyView | Patch;
376
+ /**
377
+ * The unit returned by `html`, `text`, `raw`, `view`, `repeat`, `list`, and
378
+ * `frag`.
379
+ *
380
+ * @typeParam T - the bound value type.
381
+ * @typeParam C - the bind context type.
382
+ * @group Templates
383
+ */
105
384
  export interface View<T = unknown, C extends CmdContext = any> {
385
+ /** Discriminant identifying a view. */
106
386
  type: 'view';
387
+ /** The DOM node or `DocumentFragment` the view occupies. */
107
388
  node: Node;
389
+ /** The view's own binding, when it is reactive. */
108
390
  bind?: Bind<T, C> | undefined;
109
- childCommits?: Commit<unknown, CmdContext>[];
110
- live?: () => Boundary | undefined;
391
+ /** Detaches the view's bindings and, recursively, those of its children. */
392
+ cleanBinds: () => void;
393
+ /** Returns the nodes the view currently occupies. */
394
+ boundary: () => Boundary;
395
+ /** The interpolated children of a template view. */
396
+ children?: ChildView[] | undefined;
111
397
  }
398
+ /**
399
+ * A {@link View} with erased value and context types.
400
+ *
401
+ * @group Templates
402
+ */
112
403
  export type AnyView = View<any, any>;
404
+ /**
405
+ * Context for the `view` command: the current inner view and the view factory.
406
+ *
407
+ * @typeParam T - the value type.
408
+ * @group Templates
409
+ */
113
410
  export interface ViewContext<T> extends CmdContext {
114
- boundary: Boundary;
115
- bind?: AnyBind | undefined;
116
- childCommits?: Commit<unknown, CmdContext>[] | undefined;
117
- viewFn: (val: T) => View;
411
+ /** The currently mounted inner view. */
412
+ inner: AnyView;
413
+ /** Builds the next inner view from a new value. */
414
+ viewFn: (val: T) => AnyView;
118
415
  }
119
- export declare const view: <T>(sig: Sig<T>, viewFn: (val: T) => AnyView) => View<T, ViewContext<T>>;
120
- export declare const extractBoundary: (view: View) => Boundary;
416
+ /**
417
+ * Replaces an existing boundary with a view's node and returns the new boundary.
418
+ *
419
+ * @param old - the boundary to replace.
420
+ * @param view - the view to mount.
421
+ * @returns the boundary of the mounted view.
422
+ * @group Templates
423
+ */
121
424
  export declare const replaceWithView: (old: Boundary, view: View) => Boundary;
122
425
  //#endregion
426
+ //#region src/frag.d.ts
427
+ /**
428
+ * Composes several views into one content-position view. The views' nodes are
429
+ * inserted as flat siblings, in order, with no wrapper element; nested
430
+ * fragments flatten. Reactivity comes from the child views. `frag()` with no
431
+ * arguments renders nothing.
432
+ *
433
+ * @param views - the views to compose.
434
+ * @returns a `View` rendering the views as siblings.
435
+ * @example
436
+ * ```ts
437
+ * html`<div>${frag(text('a'), html`<b>${text('b')}</b>`)}</div>`;
438
+ * ```
439
+ * @group Control flow
440
+ */
441
+ export declare const frag: (...views: AnyView[]) => View;
442
+ //#endregion
123
443
  //#region src/html.d.ts
124
- export declare const html: (strs: TemplateStringsArray, ...items: (Patch | AnyView)[]) => View;
444
+ type TextValue = string | number | boolean | bigint | null | undefined;
445
+ type HtmlItem = Patch | AnyView | TextValue | Sig<any>;
446
+ /**
447
+ * Tagged template that parses native HTML and returns a {@link View}. Three
448
+ * kinds of interpolation are supported:
449
+ *
450
+ * - a `View` fills a content position;
451
+ * - a `Patch` (from `patch(...)`) fills an attribute position;
452
+ * - a plain value or a `Sig` fills a content position as text — a `Sig` binds
453
+ * reactively and any other value becomes `String(value)`.
454
+ *
455
+ * Templates are cached per call site, so repeated renders skip parsing. Throws
456
+ * `E10` for an empty template, `E11:<expected>:<got>` for an interpolation-count
457
+ * mismatch, and `E12` for an unmatched interpolation (a `patch` in content
458
+ * position, or a text value in an attribute position).
459
+ *
460
+ * @param strs - the static template strings.
461
+ * @param rawItems - the interpolated views, patches, or text values.
462
+ * @returns the parsed `View`.
463
+ * @example
464
+ * ```ts
465
+ * html`<p>Hello, ${name}!</p>`;
466
+ * html`<button ${patch(on('click', handler))}>Go</button>`;
467
+ * ```
468
+ * @group Templates
469
+ */
470
+ export declare const html: (strs: TemplateStringsArray, ...rawItems: HtmlItem[]) => View;
471
+ //#endregion
472
+ //#region src/list.d.ts
473
+ /**
474
+ * Renders a fixed array in order. `viewFn` is called once per item with the item
475
+ * and its 0-based index, and each returned `AnyView` is appended in sequence.
476
+ * There is no keying or reconciliation and no reactive source; any reactivity
477
+ * comes from the views `viewFn` returns. An empty array renders
478
+ * `<!--empty-list-->`.
479
+ *
480
+ * @typeParam T - the item type.
481
+ * @param items - the items to render.
482
+ * @param viewFn - builds the view for an item and its index.
483
+ * @returns a `View` rendering the items.
484
+ * @example
485
+ * ```ts
486
+ * html`<ul>${list(items, (item, i) => html`<li>${i}: ${text(item)}</li>`)}</ul>`;
487
+ * ```
488
+ * @group Control flow
489
+ */
490
+ export declare const list: <T>(items: readonly T[], viewFn: (item: T, index: number) => AnyView) => View;
491
+ //#endregion
492
+ //#region src/patch.d.ts
493
+ /**
494
+ * Object form for {@link patch}, desugared into commands in key order:
495
+ * `id`, `val`, `class` (per entry, via `toggleClass`), `style` (per entry,
496
+ * via `style`), `styleProp` (per entry), `on` (per entry), and any other key
497
+ * via `attr`. A key whose value is `undefined` is skipped.
498
+ *
499
+ * @group DOM bindings
500
+ */
501
+ export interface PatchProps {
502
+ /** Sets the element's `id`. */
503
+ id?: Reactive<string>;
504
+ /** Sets the element's `value` property. */
505
+ val?: Reactive<string>;
506
+ /** Toggles each class from the truthiness of its value. */
507
+ class?: Record<string, Reactive<boolean>>;
508
+ /** Sets inline style properties by typed name. */
509
+ style?: Partial<Record<WritableStyleKey, Reactive<string>>>;
510
+ /** Sets style properties via `setProperty` (custom properties, untyped names). */
511
+ styleProp?: Record<string, Reactive<string>>;
512
+ /** Registers DOM event listeners. */
513
+ on?: { [K in keyof HTMLElementEventMap]?: (ev: HTMLElementEventMap[K]) => void; };
514
+ /** Any other key is set as an attribute via `attr`. */
515
+ [attr: string]: unknown;
516
+ }
517
+ /**
518
+ * Declares one or more bindings to apply to the same element; must be
519
+ * interpolated in an attribute position. Each command receives a plain value
520
+ * (applied once) or a `Sig` (applied on mount and re-applied on change).
521
+ *
522
+ * The first argument may be a {@link PatchProps} object, desugared into the
523
+ * commands below in key order, optionally followed by command items.
524
+ *
525
+ * @param props - a props object.
526
+ * @param items - command items applied after the props.
527
+ * @returns a `Patch` for `html` to commit.
528
+ * @example
529
+ * ```ts
530
+ * html`<input ${patch({val: name, placeholder: 'name'})} />`;
531
+ * ```
532
+ * @group DOM bindings
533
+ */
534
+ export declare function patch(props: PatchProps, ...items: ToAnyPatchItem[]): Patch;
535
+ /**
536
+ * Declares one or more command bindings to apply to the same element.
537
+ *
538
+ * @param toPatchItems - the command items to apply.
539
+ * @returns a `Patch` for `html` to commit.
540
+ * @example
541
+ * ```ts
542
+ * html`<input ${patch(val(name), attr('name', placeholder))} />`;
543
+ * ```
544
+ * @group DOM bindings
545
+ */
546
+ export declare function patch(...toPatchItems: ToAnyPatchItem[]): Patch;
547
+ /**
548
+ * Sets the element's `id`.
549
+ *
550
+ * @typeParam T - the value type.
551
+ * @param source - a plain value or a `Sig`.
552
+ * @returns a deferred patch item.
553
+ * @group DOM bindings
554
+ */
555
+ export declare const id: <T>(source: T | Sig<T>) => ToPatchItem<T>;
556
+ /**
557
+ * Sets the element's `value` property (form controls).
558
+ *
559
+ * @typeParam T - the value type.
560
+ * @param source - a plain value or a `Sig`.
561
+ * @returns a deferred patch item.
562
+ * @group DOM bindings
563
+ */
564
+ export declare const val: <T>(source: T | Sig<T>) => ToPatchItem<T>;
565
+ /**
566
+ * Sets attribute `key`. Use this for boolean/ARIA/data attributes.
567
+ *
568
+ * @typeParam T - the value type.
569
+ * @param key - the attribute name.
570
+ * @param source - a plain value or a `Sig`.
571
+ * @returns a deferred patch item.
572
+ * @group DOM bindings
573
+ */
574
+ export declare const attr: <T>(key: string, source: T | Sig<T>) => ToPatchItem<T>;
575
+ /**
576
+ * The union of `CSSStyleDeclaration` keys whose values are strings.
577
+ *
578
+ * @group DOM bindings
579
+ */
580
+ export type WritableStyleKey = { [K in keyof CSSStyleDeclaration]: CSSStyleDeclaration[K] extends string ? K : never; }[keyof CSSStyleDeclaration];
581
+ /**
582
+ * Sets an inline style property by typed name.
583
+ *
584
+ * @typeParam T - the value type.
585
+ * @param key - the typed style property name.
586
+ * @param source - a plain value or a `Sig`.
587
+ * @returns a deferred patch item.
588
+ * @example
589
+ * ```ts
590
+ * html`<span ${patch(style('color', color))}>text</span>`;
591
+ * ```
592
+ * @group DOM bindings
593
+ */
594
+ export declare const style: <T>(key: WritableStyleKey, source: T | Sig<T>) => ToPatchItem<T>;
595
+ /**
596
+ * Sets a style property via `CSSStyleDeclaration.setProperty`; use this for
597
+ * custom properties (`--my-var`) or untyped names.
598
+ *
599
+ * @typeParam T - the value type.
600
+ * @param key - the style property name.
601
+ * @param source - a plain value or a `Sig`.
602
+ * @returns a deferred patch item.
603
+ * @example
604
+ * ```ts
605
+ * html`<div ${patch(styleProp('--size', size))}></div>`;
606
+ * ```
607
+ * @group DOM bindings
608
+ */
609
+ export declare const styleProp: <T>(key: string, source: T | Sig<T>) => ToPatchItem<T>;
610
+ /**
611
+ * Toggles a single class from the truthiness of the value.
612
+ *
613
+ * @typeParam T - the value type.
614
+ * @param token - the class token.
615
+ * @param source - a plain value or a `Sig`.
616
+ * @returns a deferred patch item.
617
+ * @group DOM bindings
618
+ */
619
+ export declare const toggleClass: <T>(token: string, source: T | Sig<T>) => ToPatchItem<T>;
620
+ /**
621
+ * Toggles several classes from one value.
622
+ *
623
+ * @typeParam T - the value type.
624
+ * @param tokens - the class tokens.
625
+ * @param source - a plain value or a `Sig`.
626
+ * @returns a deferred patch item.
627
+ * @group DOM bindings
628
+ */
629
+ export declare const toggleClasses: <T>(tokens: readonly string[], source: T | Sig<T>) => ToPatchItem<T>;
630
+ /**
631
+ * A custom patch callback run on mount and on change.
632
+ *
633
+ * @typeParam T - the value type.
634
+ * @group DOM bindings
635
+ */
636
+ export type ActFn<T> = (elem: Element, val?: T) => void;
637
+ /**
638
+ * Runs arbitrary code with the bound node and value, on mount and again on
639
+ * change. The escape hatch for anything the built-in commands do not cover.
640
+ *
641
+ * @typeParam T - the value type.
642
+ * @param source - a plain value or a `Sig`.
643
+ * @param fn - called with the node and current value.
644
+ * @returns a deferred patch item.
645
+ * @example
646
+ * ```ts
647
+ * html`<canvas ${patch(act(frame, (node, v) => draw(node, v)))}></canvas>`;
648
+ * ```
649
+ * @group DOM bindings
650
+ */
651
+ export declare const act: <T>(source: T | Sig<T>, fn: ActFn<T>) => ToPatchItem<T>;
652
+ type _Listener<K extends keyof HTMLElementEventMap> = (this: HTMLElement, ev: HTMLElementEventMap[K]) => unknown;
653
+ /**
654
+ * Adds a DOM event listener. The listener is registered once at mount and is
655
+ * not a reactive source; combine it with `sig` writes to drive updates.
656
+ *
657
+ * @typeParam K - the event type.
658
+ * @param type - the event name.
659
+ * @param listener - the event listener.
660
+ * @param options - standard `addEventListener` options.
661
+ * @returns a deferred patch item.
662
+ * @group DOM bindings
663
+ */
664
+ export declare const on: <K extends keyof HTMLElementEventMap>(type: K, listener: _Listener<K>, options?: boolean | AddEventListenerOptions) => ToPatchItem<_Listener<K>>;
665
+ //#endregion
666
+ //#region src/raw.d.ts
667
+ /**
668
+ * Parses its value as HTML and mounts the resulting nodes with no wrapper
669
+ * element. Unlike `text`, the value is **not** escaped, so only pass trusted
670
+ * HTML; inline handlers and other vectors still apply once the nodes connect.
671
+ * A `Sig` re-parses and replaces the content on change; an empty string renders
672
+ * nothing.
673
+ *
674
+ * @param source - an HTML string or a `Sig` of one.
675
+ * @returns a content-position `View`.
676
+ * @example
677
+ * ```ts
678
+ * html`<article>${raw(post.bodyHtml)}</article>`;
679
+ * ```
680
+ * @group Templates
681
+ */
682
+ export declare const raw: (source: string | Sig<string>) => AnyView;
125
683
  //#endregion
126
684
  //#region src/render.d.ts
685
+ /**
686
+ * Mounts a view into `node` by appending its `node`. Accepts a `View` directly
687
+ * or a factory function that returns one. Returns a disposer that detaches every
688
+ * bind in the tree and removes the nodes from `node`; calling it twice is a
689
+ * no-op.
690
+ *
691
+ * @param viewArg - the view, or a function returning one.
692
+ * @param node - the node to mount into.
693
+ * @returns a disposer that unmounts the view.
694
+ * @example
695
+ * ```ts
696
+ * const dispose = render(App(), document.querySelector('#app')!);
697
+ * dispose();
698
+ * ```
699
+ * @group Rendering
700
+ */
127
701
  export declare const render: (viewArg: AnyView | (() => AnyView), node: Node) => (() => void);
128
702
  //#endregion
129
703
  //#region src/repeat.d.ts
704
+ /**
705
+ * Options for {@link repeat}.
706
+ *
707
+ * @typeParam T - the item type.
708
+ * @group Control flow
709
+ */
130
710
  export type RepeatProp<T> = {
711
+ /** Returns the unique, stable key for an item. */
131
712
  key: (item: T) => string;
713
+ /** Builds the view for an item. */
132
714
  view: (item: T) => AnyView;
133
- compare?: (a: T, b: T) => boolean;
715
+ /**
716
+ * Item comparator.
717
+ *
718
+ * @defaultValue `eq`
719
+ */
720
+ eq?: (a: T, b: T) => boolean;
134
721
  };
135
722
  interface Track<T> {
136
- boundary: Boundary;
137
- bind?: AnyBind | undefined;
138
- item: T;
139
723
  key: string;
140
- childCommits?: Commit<unknown, CmdContext>[] | undefined;
724
+ item: T;
725
+ view: AnyView;
141
726
  checked?: boolean;
142
727
  cleaned?: boolean;
143
728
  }
729
+ /**
730
+ * Context for the `repeat` command.
731
+ *
732
+ * @typeParam T - the item type.
733
+ * @group Control flow
734
+ */
144
735
  export interface RepeatContext<T> extends CmdContext {
736
+ /** The repeat options. */
145
737
  prop: RepeatProp<T>;
738
+ /** The current node boundary. */
146
739
  boundary: Boundary;
740
+ /** The tracked items and their views. */
147
741
  tracks: Track<T>[];
148
742
  }
743
+ /**
744
+ * Keyed list rendering. On each change `repeat` matches items by `key`, then
745
+ * reuses, moves, creates, or removes as few DOM nodes as possible. The item
746
+ * comparator defaults to `eq`; when an item is deeply equal to the track it
747
+ * already occupies, the track is reused without rebuilding its view. An empty
748
+ * array renders `<!--empty-list-->`.
749
+ *
750
+ * @typeParam T - the item type.
751
+ * @param sig - the signal holding the items.
752
+ * @param prop - the key/view/eq options.
753
+ * @returns a `View` rendering the list.
754
+ * @example
755
+ * ```ts
756
+ * html`<ul>${repeat(todos, {
757
+ * key: (item) => item.id.toString(),
758
+ * view: (item) => html`<li>${text(item.label)}</li>`,
759
+ * })}</ul>`;
760
+ * ```
761
+ * @group Control flow
762
+ */
149
763
  export declare const repeat: <T>(sig: Sig<T[]>, prop: RepeatProp<T>) => View<T[], RepeatContext<T>>;
150
764
  //#endregion
151
765
  //#region src/text.d.ts
766
+ /**
767
+ * Creates a text-node view. With a `Sig`, the text updates whenever the signal
768
+ * changes; a plain value is static.
769
+ *
770
+ * @typeParam T - the value type.
771
+ * @param source - a plain value or a `Sig`.
772
+ * @returns a text `View`.
773
+ * @example
774
+ * ```ts
775
+ * html`<span>${text(count)}</span>`;
776
+ * ```
777
+ * @group Templates
778
+ */
152
779
  export declare const text: <T>(source: T | Sig<T>) => View<T, PatchContext>;
153
780
  //#endregion
781
+ //#region src/view.d.ts
782
+ /**
783
+ * Conditionally renders one view or another. Whenever `sig` changes, `viewFn`
784
+ * runs with the new value, the previous view is torn down, and a new one is
785
+ * mounted in its place.
786
+ *
787
+ * @typeParam T - the value type.
788
+ * @param sig - the signal to switch on.
789
+ * @param viewFn - builds the view for a value.
790
+ * @returns a `View` that swaps its contents.
791
+ * @example
792
+ * ```ts
793
+ * html`<div>${view(isEmpty, (v) => (v ? text('empty') : listView))}</div>`;
794
+ * ```
795
+ * @group Control flow
796
+ */
797
+ export declare const view: <T>(sig: Sig<T>, viewFn: (val: T) => AnyView) => View<T, ViewContext<T>>;
798
+ //#endregion
154
799
  //# sourceMappingURL=sigula.d.ts.map