regor 1.3.6 → 1.3.8

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 CHANGED
@@ -62,7 +62,7 @@ HTML:
62
62
  Defining component:
63
63
 
64
64
  ```ts
65
- import { createApp, createComponent, ref, html, type Ref } from 'regor'
65
+ import { createApp, defineComponent, ref, html, type Ref } from 'regor'
66
66
 
67
67
  interface MyComponent {
68
68
  message: Ref<string>
@@ -74,7 +74,7 @@ const template = html`<button @click="count++">
74
74
 
75
75
  const props = ['message']
76
76
 
77
- const myComponent = createComponent<MyComponent>(template, {
77
+ const myComponent = defineComponent<MyComponent>(template, {
78
78
  context: (head) => ({
79
79
  message: head.props.message,
80
80
  count: ref(0),
@@ -120,7 +120,7 @@ Example:
120
120
  ```
121
121
 
122
122
  ```ts
123
- const tableRow = createComponent(
123
+ const tableRow = defineComponent(
124
124
  html`<tr>
125
125
  <TableCell :value="row.name" />
126
126
  <TableCell :value="row.age" />
@@ -222,7 +222,7 @@ These directives empower you to create dynamic and interactive user interfaces,
222
222
  **App / Component Template Functions**
223
223
 
224
224
  - **`createApp`** Similar to Vue's `createApp`, it initializes a Regor application instance.
225
- - **`createComponent`** Creates a Regor component instance.
225
+ - **`defineComponent`** Creates a Regor component instance.
226
226
  - **`toFragment`** Converts a JSON template to a document fragment.
227
227
  - **`toJsonTemplate`** Converts a DOM element to a JSON template.
228
228
 
package/dist/regor.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
+ export type ContextClass<TValue> = abstract new (...args: never[]) => TValue;
3
4
  /**
4
5
  * Runtime metadata passed to a component's `context(head)` factory.
5
6
  *
@@ -11,7 +12,7 @@
11
12
  *
12
13
  * Typical usage:
13
14
  * ```ts
14
- * const Card = createComponent(
15
+ * const Card = defineComponent(
15
16
  * `<article><h3 r-text="title"></h3></article>`,
16
17
  * {
17
18
  * props: ['title'],
@@ -125,6 +126,50 @@ export declare class ComponentHead<TContext extends IRegorContext | object = IRe
125
126
  * ```
126
127
  */
127
128
  emit: (event: string, args: Record<string, unknown>) => void;
129
+ /**
130
+ * Finds a parent context instance by constructor type from the captured
131
+ * context stack.
132
+ *
133
+ * Matching uses `instanceof` and respects stack order.
134
+ *
135
+ * `occurrence` selects which matching instance to return:
136
+ * - `0` (default): first match
137
+ * - `1`: second match
138
+ * - `2`: third match
139
+ * - negative values: always `undefined`
140
+ *
141
+ * Example:
142
+ * ```ts
143
+ * // stack: [RootCtx, ParentCtx, ParentCtx]
144
+ * head.findContext(ParentCtx) // first ParentCtx
145
+ * head.findContext(ParentCtx, 1) // second ParentCtx
146
+ * ```
147
+ *
148
+ * @param constructor - Class constructor used for `instanceof` matching.
149
+ * @param occurrence - Zero-based index of the matching instance to return.
150
+ * @returns The matching parent context instance, or `undefined` when not found.
151
+ */
152
+ findContext<TValue>(constructor: ContextClass<TValue>, occurrence?: number): TValue | undefined;
153
+ /**
154
+ * Returns a parent context instance by constructor type from the captured
155
+ * context stack.
156
+ *
157
+ * The stack is scanned in order and each entry is checked with `instanceof`.
158
+ * `occurrence` is zero-based (`0` = first match, `1` = second match, ...).
159
+ * If no instance exists at the requested occurrence, this method throws.
160
+ *
161
+ * Example:
162
+ * ```ts
163
+ * const auth = head.requireContext(AuthContext) // first AuthContext
164
+ * const outer = head.requireContext(LayoutCtx, 1) // second LayoutCtx
165
+ * ```
166
+ *
167
+ * @param constructor - Class constructor used for `instanceof` matching.
168
+ * @param occurrence - Zero-based index of the instance to return.
169
+ * @returns The parent context instance at the requested occurrence.
170
+ * @throws Error when no matching instance exists at the requested occurrence.
171
+ */
172
+ requireContext<TValue>(constructor: ContextClass<TValue>, occurrence?: number): TValue;
128
173
  /**
129
174
  * Unmounts this component instance by removing nodes between `start` and `end`
130
175
  * and calling unmount lifecycle handlers for captured contexts.
@@ -249,36 +294,36 @@ export interface JSONTemplate {
249
294
  * - Define either 'selector' or 'element' to specify the mounting point.
250
295
  * - Optionally, 'html' or 'json' can be defined to override the inner HTML of the mounting point.
251
296
  * - If neither 'html' nor 'json' template is defined, the mounting point's inner HTML remains unchanged.
252
- * If used with 'createComponent':
297
+ * If used with 'defineComponent':
253
298
  * - Define only one option: 'selector', 'element', 'html', or 'json'. The single option defines the component's HTML template.
254
299
  */
255
300
  export interface Template {
256
301
  /**
257
302
  * If used with 'createApp', specifies the target root element for mounting the application.
258
- * If used with 'createComponent', identifies the component template using a selector.
303
+ * If used with 'defineComponent', identifies the component template using a selector.
259
304
  */
260
305
  selector?: string;
261
306
  /**
262
307
  * If used with 'createApp', represents the actual DOM element where the app will be mounted.
263
- * If used with 'createComponent', specifies the component template using an element.
308
+ * If used with 'defineComponent', specifies the component template using an element.
264
309
  * Use this property if you already have a reference to the target element.
265
310
  */
266
311
  element?: Node;
267
312
  /**
268
313
  * If used with 'createApp', HTML template string that will replace the content of the root element defined by 'selector' or 'element'.
269
- * If used with 'createComponent', this template populates the content of the component.
314
+ * If used with 'defineComponent', this template populates the content of the component.
270
315
  */
271
316
  template?: string;
272
317
  /**
273
318
  * JSON-based template representation, enabling rendering within secure contexts.
274
319
  * Can be a single JSONTemplate object or an array of JSONTemplate objects.
275
- * This property is applicable to both 'createApp' and 'createComponent'.
320
+ * This property is applicable to both 'createApp' and 'defineComponent'.
276
321
  */
277
322
  json?: JSONTemplate | JSONTemplate[];
278
323
  /**
279
324
  * Indicates whether the component template contains SVG elements.
280
325
  * Enable this flag if SVG content is present to ensure proper rendering.
281
- * This property is applicable to both 'createApp' and 'createComponent'.
326
+ * This property is applicable to both 'createApp' and 'defineComponent'.
282
327
  */
283
328
  isSVG?: boolean;
284
329
  }
@@ -320,7 +365,7 @@ export interface Component<TContext extends IRegorContext | object = IRegorConte
320
365
  }
321
366
  export type OnMounted = () => void;
322
367
  export type OnUnmounted = () => void;
323
- export interface CreateComponentOptions<TContext extends IRegorContext | object = IRegorContext> {
368
+ export interface DefineComponentOptions<TContext extends IRegorContext | object = IRegorContext> {
324
369
  /**
325
370
  * Enables interpolation transform inside the component template.
326
371
  *
@@ -387,7 +432,7 @@ export interface CreateComponentOptions<TContext extends IRegorContext | object
387
432
  *
388
433
  * Example:
389
434
  * ```ts
390
- * const Card = createComponent('<div>...</div>', { defaultName: 'CardView' })
435
+ * const Card = defineComponent('<div>...</div>', { defaultName: 'CardView' })
391
436
  * cfg.addComponent(Card)
392
437
  * // usable as <CardView></CardView>
393
438
  * ```
@@ -441,7 +486,7 @@ export declare const createApp: <TRegorContext extends IRegorContext | object =
441
486
  /**
442
487
  * Creates a reusable Regor component definition.
443
488
  *
444
- * `createComponent` prepares a template once, then Regor clones/binds it for each
489
+ * `defineComponent` prepares a template once, then Regor clones/binds it for each
445
490
  * component instance in the app.
446
491
  *
447
492
  * @typeParam TContext - Component context type.
@@ -455,7 +500,7 @@ export declare const createApp: <TRegorContext extends IRegorContext | object =
455
500
  *
456
501
  * @example
457
502
  * ```ts
458
- * const UserCard = createComponent(
503
+ * const UserCard = defineComponent(
459
504
  * `<article><h3 r-text="name"></h3></article>`,
460
505
  * {
461
506
  * props: ['name'],
@@ -469,13 +514,13 @@ export declare const createApp: <TRegorContext extends IRegorContext | object =
469
514
  * @example
470
515
  * ```ts
471
516
  * // Props shorthand:
472
- * const CounterLabel = createComponent(
517
+ * const CounterLabel = defineComponent(
473
518
  * `<span r-text="value"></span>`,
474
519
  * ['value'],
475
520
  * )
476
521
  * ```
477
522
  */
478
- export declare const createComponent: <TContext extends IRegorContext | object = IRegorContext>(template: Template | string, options?: CreateComponentOptions<TContext> | string[]) => Component<TContext>;
523
+ export declare const defineComponent: <TContext extends IRegorContext | object = IRegorContext>(template: Template | string, options?: DefineComponentOptions<TContext> | string[]) => Component<TContext>;
479
524
  export declare const toFragment: (json: JSONTemplate | JSONTemplate[], isSVG?: boolean, config?: RegorConfig) => DocumentFragment;
480
525
  export declare const toJsonTemplate: (node: Element | Element[]) => JSONTemplate | JSONTemplate[];
481
526
  export declare const addUnbinder: (node: Node, unbinder: Unbinder) => void;
@@ -68,7 +68,7 @@ __export(index_exports, {
68
68
  computeRef: () => computeRef,
69
69
  computed: () => computed,
70
70
  createApp: () => createApp,
71
- createComponent: () => createComponent,
71
+ defineComponent: () => defineComponent,
72
72
  drainUnbind: () => drainUnbind,
73
73
  endBatch: () => endBatch,
74
74
  entangle: () => entangle,
@@ -357,6 +357,66 @@ var ComponentHead = class {
357
357
  this.start = start;
358
358
  this.end = end;
359
359
  }
360
+ /**
361
+ * Finds a parent context instance by constructor type from the captured
362
+ * context stack.
363
+ *
364
+ * Matching uses `instanceof` and respects stack order.
365
+ *
366
+ * `occurrence` selects which matching instance to return:
367
+ * - `0` (default): first match
368
+ * - `1`: second match
369
+ * - `2`: third match
370
+ * - negative values: always `undefined`
371
+ *
372
+ * Example:
373
+ * ```ts
374
+ * // stack: [RootCtx, ParentCtx, ParentCtx]
375
+ * head.findContext(ParentCtx) // first ParentCtx
376
+ * head.findContext(ParentCtx, 1) // second ParentCtx
377
+ * ```
378
+ *
379
+ * @param constructor - Class constructor used for `instanceof` matching.
380
+ * @param occurrence - Zero-based index of the matching instance to return.
381
+ * @returns The matching parent context instance, or `undefined` when not found.
382
+ */
383
+ findContext(constructor, occurrence = 0) {
384
+ var _a;
385
+ if (occurrence < 0) return void 0;
386
+ let current = 0;
387
+ for (const value of (_a = this.ctx) != null ? _a : []) {
388
+ if (!(value instanceof constructor)) continue;
389
+ if (current === occurrence) return value;
390
+ ++current;
391
+ }
392
+ return void 0;
393
+ }
394
+ /**
395
+ * Returns a parent context instance by constructor type from the captured
396
+ * context stack.
397
+ *
398
+ * The stack is scanned in order and each entry is checked with `instanceof`.
399
+ * `occurrence` is zero-based (`0` = first match, `1` = second match, ...).
400
+ * If no instance exists at the requested occurrence, this method throws.
401
+ *
402
+ * Example:
403
+ * ```ts
404
+ * const auth = head.requireContext(AuthContext) // first AuthContext
405
+ * const outer = head.requireContext(LayoutCtx, 1) // second LayoutCtx
406
+ * ```
407
+ *
408
+ * @param constructor - Class constructor used for `instanceof` matching.
409
+ * @param occurrence - Zero-based index of the instance to return.
410
+ * @returns The parent context instance at the requested occurrence.
411
+ * @throws Error when no matching instance exists at the requested occurrence.
412
+ */
413
+ requireContext(constructor, occurrence = 0) {
414
+ const value = this.findContext(constructor, occurrence);
415
+ if (value !== void 0) return value;
416
+ throw new Error(
417
+ `${constructor} was not found in the context stack at occurrence ${occurrence}.`
418
+ );
419
+ }
360
420
  /**
361
421
  * Unmounts this component instance by removing nodes between `start` and `end`
362
422
  * and calling unmount lifecycle handlers for captured contexts.
@@ -5923,8 +5983,8 @@ var toJsonTemplate = (node) => {
5923
5983
  return json;
5924
5984
  };
5925
5985
 
5926
- // src/app/createComponent.ts
5927
- var createComponent = (template, options = {}) => {
5986
+ // src/app/defineComponent.ts
5987
+ var defineComponent = (template, options = {}) => {
5928
5988
  var _a, _b, _c, _d, _e, _f;
5929
5989
  if (isArray(options)) options = { props: options };
5930
5990
  if (isString(template)) template = { template };