revojs 0.0.37 → 0.0.38

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.
@@ -7,6 +7,10 @@ export type Infer<T> = T extends TypeOf<infer U> ? U : unknown;
7
7
  export type Slot = unknown | Template | Array<Slot> | (() => Slot);
8
8
  export type Hydration = Comment | Text | Element | Array<Hydration>;
9
9
  export type EventListener<T extends Event> = ((event: T) => void) | Array<(event: T) => void>;
10
+ export type HostContext = {
11
+ host?: CustomElement<Events, Attributes>;
12
+ internals?: ElementInternals;
13
+ };
10
14
  export type Template = {
11
15
  tag: string;
12
16
  attributes: Record<string, unknown>;
@@ -53,7 +57,6 @@ export interface Component<TEvents extends Events, TAttributes extends Attribute
53
57
  readonly events: EventOutput<TEvents>;
54
58
  readonly attributes: State<AttributeOutput<TAttributes>>;
55
59
  readonly shadowRoot: false | ShadowRootOptions;
56
- readonly host?: CustomElement<TEvents, TAttributes>;
57
60
  setup: () => Slot | Promise<Slot>;
58
61
  }
59
62
  export interface ComponentConstructor<TEvents extends Events, TAttributes extends Attributes> {
@@ -61,10 +64,9 @@ export interface ComponentConstructor<TEvents extends Events, TAttributes extend
61
64
  $events: TEvents;
62
65
  $attributes: TAttributes;
63
66
  $styles: Array<string>;
64
- new (input?: Input<TEvents, TAttributes>, scope?: Scope, host?: CustomElement<TEvents, TAttributes>): Component<TEvents, TAttributes>;
67
+ new (input?: Input<TEvents, TAttributes>, scope?: Scope): Component<TEvents, TAttributes>;
65
68
  }
66
69
  export interface CustomElement<TEvents extends Events, TAttributes extends Attributes> extends HTMLElement {
67
- readonly internals: ElementInternals;
68
70
  readonly component: Component<TEvents, TAttributes>;
69
71
  }
70
72
  export interface CustomElementConstructor<TEvents extends Events, TAttributes extends Attributes> {
@@ -74,6 +76,7 @@ export declare class MountedEvent extends Event {
74
76
  constructor();
75
77
  }
76
78
  export declare const isTemplate: (value?: any) => value is Template;
79
+ export declare const useHost: (scope: Scope) => HostContext;
77
80
  export declare const createElement: <TEvents extends Events, TAttributes extends Attributes>(input: string | ComponentConstructor<TEvents, TAttributes>, attributes?: AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
78
81
  export declare const toString: (slot: Slot) => string;
79
82
  export declare const isTextNode: (hydration?: Hydration) => hydration is Text;
@@ -85,7 +88,7 @@ export declare const toFragment: (hydration: Hydration) => DocumentFragment;
85
88
  export declare const hydrate: (scope: Scope, parentNode: Node, slot: Slot, index: number, previous?: Hydration) => Promise<Hydration>;
86
89
  export declare const renderToString: (scope: Scope, slot: Slot) => Promise<string>;
87
90
  export declare const defineComponent: <TEvents extends Events, TAttributes extends Attributes>(options: ComponentOptions<TEvents, TAttributes>) => ComponentConstructor<TEvents, TAttributes>;
88
- export declare const toCustomElement: <TEvents extends Events, TAttributes extends Attributes>(component: ComponentConstructor<TEvents, TAttributes>) => CustomElementConstructor<TEvents, TAttributes>;
91
+ export declare const toCustomElement: <TEvents extends Events, TAttributes extends Attributes>(Component: ComponentConstructor<TEvents, TAttributes>) => CustomElementConstructor<TEvents, TAttributes>;
89
92
  export declare const registerComponent: <TEvents extends Events, TAttributes extends Attributes>(component: ComponentConstructor<TEvents, TAttributes>) => ComponentConstructor<TEvents, TAttributes>;
90
93
  export declare function useEvent<T extends keyof ElementEventMap>(scope: Scope, target: EventTarget | undefined | null, event: T, input: EventListener<ElementEventMap[T]>, options?: AddEventListenerOptions): void;
91
94
  export declare function useEvent<T extends keyof WindowEventMap>(scope: Scope, target: Window | undefined | null, event: T, input: EventListener<WindowEventMap[T]>, options?: AddEventListenerOptions): void;
@@ -98,6 +101,7 @@ export declare const stopPropagation: (event: Event) => void;
98
101
  export declare const stopImmediatePropagation: (event: Event) => void;
99
102
  export declare const components: Map<string, ComponentConstructor<Events, Attributes>>;
100
103
  export declare const globalStyles: CSSStyleSheet[];
104
+ export declare const HOST_CONTEXT: import("..").Descriptor<HostContext>;
101
105
  declare global {
102
106
  interface HTMLElementEventMap {
103
107
  mounted: MountedEvent;
package/dist/index.js CHANGED
@@ -221,12 +221,20 @@ var MountedEvent = class extends Event {
221
221
  const isTemplate = (value) => {
222
222
  return typeof value === "object" && "tag" in value && "attributes" in value && "children" in value;
223
223
  };
224
+ const useHost = (scope) => {
225
+ return scope.getContext(HOST_CONTEXT);
226
+ };
224
227
  const createElement = (input, attributes, ...children) => {
225
- return {
228
+ const template = {
226
229
  tag: typeof input === "function" ? input.$name : input,
227
230
  attributes: attributes ?? {},
228
231
  children
229
232
  };
233
+ if (typeof input === "function" && input.$styles.length) {
234
+ const classes = template.attributes["class"];
235
+ template.attributes["class"] = (classes ? [classes, ...input.$styles] : input.$styles).join(" ");
236
+ }
237
+ return template;
230
238
  };
231
239
  const toString = (slot) => {
232
240
  switch (typeof slot) {
@@ -379,8 +387,7 @@ const defineComponent = (options) => {
379
387
  events;
380
388
  attributes;
381
389
  shadowRoot;
382
- host;
383
- constructor(input, scope, host) {
390
+ constructor(input, scope) {
384
391
  this.scope = new Scope(scope);
385
392
  this.events = Object.keys(options.events ?? {}).reduce((output, name) => {
386
393
  Reflect.set(output, name, input?.[name], output);
@@ -391,53 +398,48 @@ const defineComponent = (options) => {
391
398
  return attributes;
392
399
  }, createState({}));
393
400
  this.shadowRoot = options.shadowRoot ?? { mode: "open" };
394
- this.host = host;
395
401
  }
396
- setup = () => {
397
- const findParent = (node) => {
398
- if (node) {
399
- if ("component" in node) return node;
400
- return findParent(node.parentNode);
401
- }
402
- };
403
- const parentNode = findParent(this.host?.parentNode);
404
- if (parentNode) this.scope.parentScope = parentNode.component.scope;
405
- return options.setup(this);
406
- };
402
+ setup = () => options.setup(this);
407
403
  }
408
404
  components.set(options.name, Instance);
409
405
  return Instance;
410
406
  };
411
- const toCustomElement = (component) => {
407
+ const toCustomElement = (Component) => {
412
408
  return class extends HTMLElement {
413
409
  static formAssociated = true;
414
- internals;
415
- component;
416
- constructor() {
417
- super();
418
- this.internals = this.attachInternals();
419
- this.component = new component(void 0, void 0, this);
420
- }
410
+ component = new Component();
421
411
  async connectedCallback() {
422
412
  let rootNode = this;
413
+ const findParent = (node) => {
414
+ if (node) {
415
+ if ("component" in node) return node;
416
+ return findParent(node.parentNode);
417
+ }
418
+ };
419
+ const parentNode = findParent(this.parentNode);
420
+ if (parentNode) this.component.scope.parentScope = parentNode.component.scope;
423
421
  if (this.component.shadowRoot) {
424
422
  const options = defu(this.component.shadowRoot, { mode: "open" });
425
423
  rootNode = this.shadowRoot ?? this.attachShadow(options);
426
424
  if (this.component.shadowRoot.globalStyles) rootNode.adoptedStyleSheets = globalStyles;
427
425
  }
428
- for (const [name, event] of Object.entries(component.$events)) Reflect.set(this.component.events, name, (value) => {
426
+ for (const [name, event] of Object.entries(Component.$events)) Reflect.set(this.component.events, name, (value) => {
429
427
  if (value instanceof Event) return;
430
428
  this.dispatchEvent(new CustomEvent(name.substring(2).toLowerCase(), {
431
429
  ...event,
432
430
  detail: value
433
431
  }));
434
432
  });
433
+ this.component.scope.setContext(HOST_CONTEXT, {
434
+ host: this,
435
+ internals: this.attachInternals()
436
+ });
435
437
  await hydrate(this.component.scope, rootNode, await this.component.setup(), 0);
436
438
  this.dispatchEvent(new MountedEvent());
437
439
  }
438
440
  attributeChangedCallback(name, oldValue, value) {
439
441
  if (value === oldValue) return;
440
- const attribute = component.$attributes?.[name];
442
+ const attribute = Component.$attributes?.[name];
441
443
  if (attribute) {
442
444
  let convertedValue;
443
445
  if (value) switch (attribute.type) {
@@ -461,7 +463,7 @@ const toCustomElement = (component) => {
461
463
  this.component.scope.stop();
462
464
  }
463
465
  static get observedAttributes() {
464
- return Object.keys(component.$attributes ?? {});
466
+ return Object.keys(Component.$attributes ?? {});
465
467
  }
466
468
  };
467
469
  };
@@ -501,6 +503,7 @@ const globalStyles = Array.from(isClient() ? document.styleSheets : []).map((sty
501
503
  sheet.replaceSync(css);
502
504
  return sheet;
503
505
  });
506
+ const HOST_CONTEXT = defineContext("HOST_CONTEXT");
504
507
 
505
508
  //#endregion
506
509
  //#region src/http/index.ts
@@ -921,4 +924,4 @@ const markdownToSlot = (input, options) => {
921
924
  };
922
925
 
923
926
  //#endregion
924
- export { $fetch, Compute, Handler, LOCALE_CONTEXT, MountedEvent, NavigateEvent, Page, ROUTER_CONTEXT, RUNTIME_CONTEXT, Radix, Scope, StopEvent, activeCompute, components, createApp, createCompute, createElement, createEvent, createLocale, createMemo, createRouter, createRuntime, createState, defineComponent, defineContext, defineRoute, fileName, fromValue, getAssets, getCookies, getCustomElement, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, globalStyles, hydrate, isClient, isCommentNode, isElementNode, isServer, isTemplate, isTextNode, markdownToSlot, preventDefault, registerComponent, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toArray, toCustomElement, toFragment, toPath, toRange, toString, useEvent, useLocale, useRouter, useRuntime };
927
+ export { $fetch, Compute, HOST_CONTEXT, Handler, LOCALE_CONTEXT, MountedEvent, NavigateEvent, Page, ROUTER_CONTEXT, RUNTIME_CONTEXT, Radix, Scope, StopEvent, activeCompute, components, createApp, createCompute, createElement, createEvent, createLocale, createMemo, createRouter, createRuntime, createState, defineComponent, defineContext, defineRoute, fileName, fromValue, getAssets, getCookies, getCustomElement, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, globalStyles, hydrate, isClient, isCommentNode, isElementNode, isServer, isTemplate, isTextNode, markdownToSlot, preventDefault, registerComponent, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toArray, toCustomElement, toFragment, toPath, toRange, toString, useEvent, useHost, useLocale, useRouter, useRuntime };
package/dist/jsx/index.js CHANGED
@@ -39,14 +39,25 @@ const defuArrayFn = createDefu((object, key, currentValue) => {
39
39
  }
40
40
  });
41
41
 
42
+ //#endregion
43
+ //#region src/signals/index.ts
44
+ function defineContext(key) {
45
+ return key;
46
+ }
47
+
42
48
  //#endregion
43
49
  //#region src/html/index.ts
44
50
  const createElement = (input, attributes, ...children) => {
45
- return {
51
+ const template = {
46
52
  tag: typeof input === "function" ? input.$name : input,
47
53
  attributes: attributes ?? {},
48
54
  children
49
55
  };
56
+ if (typeof input === "function" && input.$styles.length) {
57
+ const classes = template.attributes["class"];
58
+ template.attributes["class"] = (classes ? [classes, ...input.$styles] : input.$styles).join(" ");
59
+ }
60
+ return template;
50
61
  };
51
62
  const isClient = () => typeof window !== "undefined";
52
63
  const globalStyles = Array.from(isClient() ? document.styleSheets : []).map((style) => {
@@ -55,6 +66,7 @@ const globalStyles = Array.from(isClient() ? document.styleSheets : []).map((sty
55
66
  sheet.replaceSync(css);
56
67
  return sheet;
57
68
  });
69
+ const HOST_CONTEXT = defineContext("HOST_CONTEXT");
58
70
 
59
71
  //#endregion
60
72
  //#region src/jsx/index.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.37",
3
+ "version": "0.0.38",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",