@b9g/crank 0.5.0-beta.4 → 0.5.0-beta.5

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/crank.d.ts CHANGED
@@ -1,481 +1,2 @@
1
- /**
2
- * A type which represents all valid values for an element tag.
3
- */
4
- export declare type Tag = string | symbol | Component;
5
- /**
6
- * A helper type to map the tag of an element to its expected props.
7
- *
8
- * @template TTag - The tag associated with the props. Can be a string, symbol
9
- * or a component function.
10
- */
11
- export declare type TagProps<TTag extends Tag> = TTag extends string ? JSX.IntrinsicElements[TTag] : TTag extends Component<infer TProps> ? TProps : Record<string, unknown>;
12
- /***
13
- * SPECIAL TAGS
14
- *
15
- * Crank provides a couple tags which have special meaning for the renderer.
16
- ***/
17
- /**
18
- * A special tag for grouping multiple children within the same parent.
19
- *
20
- * All non-string iterables which appear in the element tree are implicitly
21
- * wrapped in a fragment element.
22
- *
23
- * This tag is just the empty string, and you can use the empty string in
24
- * createElement calls or transpiler options directly to avoid having to
25
- * reference this export.
26
- */
27
- export declare const Fragment = "";
28
- export declare type Fragment = typeof Fragment;
29
- /**
30
- * A special tag for rendering into a new root node via a root prop.
31
- *
32
- * This tag is useful for creating element trees with multiple roots, for
33
- * things like modals or tooltips.
34
- *
35
- * Renderer.prototype.render() will implicitly wrap top-level element trees in
36
- * a Portal element.
37
- */
38
- export declare const Portal: any;
39
- export declare type Portal = typeof Portal;
40
- /**
41
- * A special tag which preserves whatever was previously rendered in the
42
- * element’s position.
43
- *
44
- * Copy elements are useful for when you want to prevent a subtree from
45
- * rerendering as a performance optimization. Copy elements can also be keyed,
46
- * in which case the previously rendered keyed element will be copied.
47
- */
48
- export declare const Copy: any;
49
- export declare type Copy = typeof Copy;
50
- /**
51
- * A special tag for injecting raw nodes or strings via a value prop.
52
- *
53
- * If the value prop is a string, Renderer.prototype.parse() will be called on
54
- * the string and the result will be set as the element’s value.
55
- */
56
- export declare const Raw: any;
57
- export declare type Raw = typeof Raw;
58
- /**
59
- * Describes all valid values of an element tree, excluding iterables.
60
- *
61
- * Arbitrary objects can also be safely rendered, but will be converted to a
62
- * string using the toString() method. We exclude them from this type to catch
63
- * potential mistakes.
64
- */
65
- export declare type Child = Element | string | number | boolean | null | undefined;
66
- /**
67
- * An arbitrarily nested iterable of Child values.
68
- *
69
- * We use a recursive interface here rather than making the Children type
70
- * directly recursive because recursive type aliases were added in TypeScript
71
- * 3.7.
72
- *
73
- * You should avoid referencing this type directly, as it is mainly exported to
74
- * prevent TypeScript errors.
75
- */
76
- export interface ChildIterable extends Iterable<Child | ChildIterable> {
77
- }
78
- /**
79
- * Describes all valid values of an element tree, including arbitrarily nested
80
- * iterables of such values.
81
- */
82
- export declare type Children = Child | ChildIterable;
83
- /**
84
- * Represents all functions which can be used as a component.
85
- *
86
- * @template [TProps=*] - The expected props for the component.
87
- */
88
- export declare type Component<TProps extends Record<string, unknown> = any> = (this: Context<TProps>, props: TProps) => Children | PromiseLike<Children> | Iterator<Children, Children | void, any> | AsyncIterator<Children, Children | void, any>;
89
- /**
90
- * A type to keep track of keys. Any value can be a key, though null and
91
- * undefined are ignored.
92
- */
93
- declare type Key = unknown;
94
- declare const ElementSymbol: unique symbol;
95
- export interface Element<TTag extends Tag = Tag> {
96
- /**
97
- * @internal
98
- * A unique symbol to identify elements as elements across versions and
99
- * realms, and to protect against basic injection attacks.
100
- * https://overreacted.io/why-do-react-elements-have-typeof-property/
101
- *
102
- * This property is defined on the element prototype rather than per
103
- * instance, because it is the same for every Element.
104
- */
105
- $$typeof: typeof ElementSymbol;
106
- /**
107
- * The tag of the element. Can be a string, symbol or function.
108
- */
109
- tag: TTag;
110
- /**
111
- * An object containing the “properties” of an element. These correspond to
112
- * the attribute syntax from JSX.
113
- */
114
- props: TagProps<TTag>;
115
- /**
116
- * A value which uniquely identifies an element from its siblings so that it
117
- * can be added/updated/moved/removed by key rather than position.
118
- *
119
- * Passed in createElement() as the prop "c-key".
120
- */
121
- key: Key;
122
- /**
123
- * A callback which is called with the element’s result when it is committed.
124
- *
125
- * Passed in createElement() as the prop "c-ref".
126
- */
127
- ref: ((value: unknown) => unknown) | undefined;
128
- /**
129
- * A possible boolean which indicates that element should NOT be rerendered.
130
- * If the element has never been rendered, this property has no effect.
131
- *
132
- * Passed in createElement() as the prop "c-static".
133
- */
134
- static_: boolean | undefined;
135
- }
136
- /**
137
- * Elements are the basic building blocks of Crank applications. They are
138
- * JavaScript objects which are interpreted by special classes called renderers
139
- * to produce and manage stateful nodes.
140
- *
141
- * @template {Tag} [TTag=Tag] - The type of the tag of the element.
142
- *
143
- * @example
144
- * // specific element types
145
- * let div: Element<"div">;
146
- * let portal: Element<Portal>;
147
- * let myEl: Element<MyComponent>;
148
- *
149
- * // general element types
150
- * let host: Element<string | symbol>;
151
- * let component: Element<Component>;
152
- *
153
- * Typically, you use a helper function like createElement to create elements
154
- * rather than instatiating this class directly.
155
- */
156
- export declare class Element<TTag extends Tag = Tag> {
157
- constructor(tag: TTag, props: TagProps<TTag>, key: Key, ref?: ((value: unknown) => unknown) | undefined, static_?: boolean | undefined);
158
- }
159
- export declare function isElement(value: any): value is Element;
160
- /**
161
- * Creates an element with the specified tag, props and children.
162
- *
163
- * This function is usually used as a transpilation target for JSX transpilers,
164
- * but it can also be called directly. It additionally extracts special props so
165
- * they aren’t accessible to renderer methods or components, and assigns the
166
- * children prop according to any additional arguments passed to the function.
167
- */
168
- export declare function createElement<TTag extends Tag>(tag: TTag, props?: TagProps<TTag> | null | undefined, ...children: Array<unknown>): Element<TTag>;
169
- /** Clones a given element, shallowly copying the props object. */
170
- export declare function cloneElement<TTag extends Tag>(el: Element<TTag>): Element<TTag>;
171
- /**
172
- * A helper type which repesents all possible rendered values of an element.
173
- *
174
- * @template TNode - The node type for the element provided by the renderer.
175
- *
176
- * When asking the question, what is the “value” of a specific element, the
177
- * answer varies depending on the tag:
178
- *
179
- * For host elements, the value is the nodes created for the element.
180
- *
181
- * For fragments, the value is usually an array of nodes.
182
- *
183
- * For portals, the value is undefined, because a Portal element’s root and
184
- * children are opaque to its parent.
185
- *
186
- * For components, the value can be any of the above, because the value of a
187
- * component is determined by its immediate children.
188
- *
189
- * Rendered values can also be strings or arrays of nodes and strings, in the
190
- * case of component or fragment elements with strings or multiple children.
191
- *
192
- * All of these possible values are reflected in this utility type.
193
- */
194
- export declare type ElementValue<TNode> = Array<TNode | string> | TNode | string | undefined;
195
- /**
196
- * @internal
197
- * The internal nodes which are cached and diffed against new elements when
198
- * rendering element trees.
199
- */
200
- declare class Retainer<TNode> {
201
- /**
202
- * The element associated with this retainer.
203
- */
204
- el: Element;
205
- /**
206
- * The context associated with this element. Will only be defined for
207
- * component elements.
208
- */
209
- ctx: ContextImpl<TNode> | undefined;
210
- /**
211
- * The retainer children of this element. Retainers form a tree which mirrors
212
- * elements. Can be a single child or undefined as a memory optimization.
213
- */
214
- children: Array<RetainerChild<TNode>> | RetainerChild<TNode>;
215
- /**
216
- * The value associated with this element.
217
- */
218
- value: ElementValue<TNode>;
219
- /**
220
- * The cached child values of this element. Only host and component elements
221
- * will use this property.
222
- */
223
- cachedChildValues: ElementValue<TNode>;
224
- /**
225
- * The child which this retainer replaces. This property is used when an
226
- * async retainer tree replaces previously rendered elements, so that the
227
- * previously rendered elements can remain visible until the async tree
228
- * fulfills. Will be set to undefined once this subtree fully renders.
229
- */
230
- fallbackValue: RetainerChild<TNode>;
231
- inflightValue: Promise<ElementValue<TNode>> | undefined;
232
- onNextValues: Function | undefined;
233
- constructor(el: Element);
234
- }
235
- /**
236
- * The retainer equivalent of ElementValue
237
- */
238
- declare type RetainerChild<TNode> = Retainer<TNode> | string | undefined;
239
- export interface RendererImpl<TNode, TScope, TRoot extends TNode = TNode, TResult = ElementValue<TNode>> {
240
- scope<TTag extends string | symbol>(scope: TScope | undefined, tag: TTag, props: TagProps<TTag>): TScope | undefined;
241
- create<TTag extends string | symbol>(tag: TTag, props: TagProps<TTag>, scope: TScope | undefined): TNode;
242
- /**
243
- * Called when an element’s rendered value is exposed via render, schedule,
244
- * refresh, refs, or generator yield expressions.
245
- *
246
- * @param value - The value of the element being read. Can be a node, a
247
- * string, undefined, or an array of nodes and strings, depending on the
248
- * element.
249
- *
250
- * @returns Varies according to the specific renderer subclass. By default,
251
- * it exposes the element’s value.
252
- *
253
- * This is useful for renderers which don’t want to expose their internal
254
- * nodes. For instance, the HTML renderer will convert all internal nodes to
255
- * strings.
256
- */
257
- read(value: ElementValue<TNode>): TResult;
258
- /**
259
- * Called for each string in an element tree.
260
- *
261
- * @param text - The string child.
262
- * @param scope - The current scope.
263
- *
264
- * @returns The escaped string.
265
- *
266
- * Rather than returning text nodes for whatever environment we’re rendering
267
- * to, we defer that step for Renderer.prototype.arrange. We do this so that
268
- * adjacent strings can be concatenated and the actual element tree can be
269
- * rendered in a normalized form.
270
- */
271
- escape(text: string, scope: TScope | undefined): string;
272
- /**
273
- * Called for each Raw element whose value prop is a string.
274
- *
275
- * @param text - The string child.
276
- * @param scope - The current scope.
277
- *
278
- * @returns The parsed node or string.
279
- */
280
- parse(text: string, scope: TScope | undefined): ElementValue<TNode>;
281
- patch<TTag extends string | symbol, TName extends string>(tag: TTag, node: TNode, name: TName, value: TagProps<TTag>[TName], oldValue: TagProps<TTag>[TName] | undefined, scope: TScope): unknown;
282
- arrange<TTag extends string | symbol>(tag: TTag, node: TNode, props: TagProps<TTag>, children: Array<TNode | string>, oldProps: TagProps<TTag> | undefined, oldChildren: Array<TNode | string> | undefined): unknown;
283
- dispose<TTag extends string | symbol>(tag: TTag, node: TNode, props: TagProps<TTag>): unknown;
284
- flush(root: TRoot): unknown;
285
- }
286
- declare const _RendererImpl: unique symbol;
287
- /**
288
- * An abstract class which is subclassed to render to different target
289
- * environments. This class is responsible for kicking off the rendering
290
- * process and caching previous trees by root.
291
- *
292
- * @template TNode - The type of the node for a rendering environment.
293
- * @template TScope - Data which is passed down the tree.
294
- * @template TRoot - The type of the root for a rendering environment.
295
- * @template TResult - The type of exposed values.
296
- */
297
- export declare class Renderer<TNode extends object = object, TScope = unknown, TRoot extends TNode = TNode, TResult = ElementValue<TNode>> {
298
- /**
299
- * @internal
300
- * A weakmap which stores element trees by root.
301
- */
302
- cache: WeakMap<object, Retainer<TNode>>;
303
- [_RendererImpl]: RendererImpl<TNode, TScope, TRoot, TResult>;
304
- constructor(impl: Partial<RendererImpl<TNode, TScope, TRoot, TResult>>);
305
- /**
306
- * Renders an element tree into a specific root.
307
- *
308
- * @param children - An element tree. You can render null with a previously
309
- * used root to delete the previously rendered element tree from the cache.
310
- * @param root - The node to be rendered into. The renderer will cache
311
- * element trees per root.
312
- * @param bridge - An optional context that will be the ancestor context of all
313
- * elements in the tree. Useful for connecting different renderers so that
314
- * events/provisions properly propagate. The context for a given root must be
315
- * the same or an error will be thrown.
316
- *
317
- * @returns The result of rendering the children, or a possible promise of
318
- * the result if the element tree renders asynchronously.
319
- */
320
- render(children: Children, root?: TRoot | undefined, bridge?: Context | undefined): Promise<TResult> | TResult;
321
- }
322
- export interface Context extends Crank.Context {
323
- }
324
- /**
325
- * An interface which can be extended to provide strongly typed provisions.
326
- * See Context.prototype.consume and Context.prototype.provide.
327
- */
328
- export interface ProvisionMap extends Crank.ProvisionMap {
329
- }
330
- /**
331
- * @internal
332
- * The internal class which holds context data.
333
- */
334
- declare class ContextImpl<TNode = unknown, TScope = unknown, TRoot extends TNode = TNode, TResult = unknown> {
335
- /** A bitmask. See CONTEXT FLAGS above. */
336
- f: number;
337
- /** The actual context associated with this impl. */
338
- owner: Context<unknown, TResult>;
339
- /**
340
- * The renderer which created this context.
341
- */
342
- renderer: RendererImpl<TNode, TScope, TRoot, TResult>;
343
- /** The root node as set by the nearest ancestor portal. */
344
- root: TRoot | undefined;
345
- /**
346
- * The nearest ancestor host or portal retainer.
347
- *
348
- * When refresh is called, the host element will be arranged as the last step
349
- * of the commit, to make sure the parent’s children properly reflects the
350
- * components’s children.
351
- */
352
- host: Retainer<TNode>;
353
- /** The parent context impl. */
354
- parent: ContextImpl<TNode, TScope, TRoot, TResult> | undefined;
355
- /** The value of the scope at the point of element’s creation. */
356
- scope: TScope | undefined;
357
- /** The internal node associated with this context. */
358
- ret: Retainer<TNode>;
359
- /**
360
- * The iterator returned by the component function.
361
- *
362
- * Existence of this property implies that the component is a generator
363
- * component. It is deleted when a component is returned.
364
- */
365
- iterator: Iterator<Children, Children | void, unknown> | AsyncIterator<Children, Children | void, unknown> | undefined;
366
- inflightBlock: Promise<unknown> | undefined;
367
- inflightValue: Promise<ElementValue<TNode>> | undefined;
368
- enqueuedBlock: Promise<unknown> | undefined;
369
- enqueuedValue: Promise<ElementValue<TNode>> | undefined;
370
- onProps: ((props: Record<string, any>) => unknown) | undefined;
371
- onPropsRequested: Function | undefined;
372
- constructor(renderer: RendererImpl<TNode, TScope, TRoot, TResult>, root: TRoot | undefined, host: Retainer<TNode>, parent: ContextImpl<TNode, TScope, TRoot, TResult> | undefined, scope: TScope | undefined, ret: Retainer<TNode>);
373
- }
374
- declare const _ContextImpl: unique symbol;
375
- declare type ComponentProps<T> = T extends (props: infer U) => any ? U : T;
376
- /**
377
- * A class which is instantiated and passed to every component as its this
378
- * value. Contexts form a tree just like elements and all components in the
379
- * element tree are connected via contexts. Components can use this tree to
380
- * communicate data upwards via events and downwards via provisions.
381
- *
382
- * @template [TProps=*] - The expected shape of the props passed to the
383
- * component. Used to strongly type the Context iterator methods.
384
- * @template [TResult=*] - The readable element value type. It is used in
385
- * places such as the return value of refresh and the argument passed to
386
- * schedule and cleanup callbacks.
387
- */
388
- export declare class Context<TProps = any, TResult = any> implements EventTarget {
389
- /**
390
- * @internal
391
- */
392
- [_ContextImpl]: ContextImpl<unknown, unknown, unknown, TResult>;
393
- constructor(impl: ContextImpl<unknown, unknown, unknown, TResult>);
394
- /**
395
- * The current props of the associated element.
396
- *
397
- * Typically, you should read props either via the first parameter of the
398
- * component or via the context iterator methods. This property is mainly for
399
- * plugins or utilities which wrap contexts.
400
- */
401
- get props(): ComponentProps<TProps>;
402
- /**
403
- * The current value of the associated element.
404
- *
405
- * Typically, you should read values via refs, generator yield expressions,
406
- * or the refresh, schedule, cleanup, or flush methods. This property is
407
- * mainly for plugins or utilities which wrap contexts.
408
- */
409
- get value(): TResult;
410
- [Symbol.iterator](): Generator<ComponentProps<TProps>>;
411
- [Symbol.asyncIterator](): AsyncGenerator<ComponentProps<TProps>>;
412
- /**
413
- * Re-executes a component.
414
- *
415
- * @returns The rendered value of the component or a promise thereof if the
416
- * component or its children execute asynchronously.
417
- *
418
- * The refresh method works a little differently for async generator
419
- * components, in that it will resume the Context’s props async iterator
420
- * rather than resuming execution. This is because async generator components
421
- * are perpetually resumed independent of updates, and rely on the props
422
- * async iterator to suspend.
423
- */
424
- refresh(): Promise<TResult> | TResult;
425
- /**
426
- * Registers a callback which fires when the component commits. Will only
427
- * fire once per callback and update.
428
- */
429
- schedule(callback: (value: TResult) => unknown): void;
430
- /**
431
- * Registers a callback which fires when the component’s children are
432
- * rendered into the root. Will only fire once per callback and render.
433
- */
434
- flush(callback: (value: TResult) => unknown): void;
435
- /**
436
- * Registers a callback which fires when the component unmounts. Will only
437
- * fire once per callback.
438
- */
439
- cleanup(callback: (value: TResult) => unknown): void;
440
- consume<TKey extends keyof ProvisionMap>(key: TKey): ProvisionMap[TKey];
441
- consume(key: unknown): any;
442
- provide<TKey extends keyof ProvisionMap>(key: TKey, value: ProvisionMap[TKey]): void;
443
- provide(key: unknown, value: any): void;
444
- addEventListener<T extends string>(type: T, listener: MappedEventListenerOrEventListenerObject<T> | null, options?: boolean | AddEventListenerOptions): void;
445
- removeEventListener<T extends string>(type: T, listener: MappedEventListenerOrEventListenerObject<T> | null, options?: EventListenerOptions | boolean): void;
446
- dispatchEvent(ev: Event): boolean;
447
- }
448
- /**
449
- * A map of event type strings to Event subclasses. Can be extended via
450
- * TypeScript module augmentation to have strongly typed event listeners.
451
- */
452
- export interface EventMap extends Crank.EventMap {
453
- [type: string]: Event;
454
- }
455
- declare type MappedEventListener<T extends string> = (ev: EventMap[T]) => unknown;
456
- declare type MappedEventListenerOrEventListenerObject<T extends string> = MappedEventListener<T> | {
457
- handleEvent: MappedEventListener<T>;
458
- };
459
- declare global {
460
- namespace Crank {
461
- interface EventMap {
462
- }
463
- interface ProvisionMap {
464
- }
465
- interface Context {
466
- }
467
- }
468
- namespace JSX {
469
- interface IntrinsicElements {
470
- [tag: string]: any;
471
- }
472
- interface ElementChildrenAttribute {
473
- children: {};
474
- }
475
- }
476
- }
477
- declare const _default: {
478
- createElement: typeof createElement;
479
- Fragment: string;
480
- };
481
- export default _default;
1
+ export * from "./core.js";
2
+ export * from "./tags.js";