revojs 0.0.23 → 0.0.24

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.
@@ -1,5 +1,5 @@
1
1
  import { type HtmlAttributes } from "../jsx";
2
- import { type Descriptor, Hooks, Scope, type State, type Value } from "../signals";
2
+ import { type Descriptor, Scope, type State, type Value } from "../signals";
3
3
  export type TypeOf<T> = {
4
4
  (): T;
5
5
  };
@@ -45,7 +45,6 @@ export interface ComponentOptions<TEvents extends Events, TAttributes extends At
45
45
  }
46
46
  export interface Component<TEvents extends Events, TAttributes extends Attributes> {
47
47
  readonly scope: Scope;
48
- readonly hooks: Hooks;
49
48
  readonly context: Map<string, unknown>;
50
49
  readonly events: EventOutput<TEvents>;
51
50
  readonly attributes: State<AttributeOutput<TAttributes>>;
@@ -53,7 +52,6 @@ export interface Component<TEvents extends Events, TAttributes extends Attribute
53
52
  readonly host?: CustomElement<TEvents, TAttributes>;
54
53
  getContext: <T>(input: Descriptor<T>) => T;
55
54
  setContext: <T>(input: Descriptor<T>, value: T) => void;
56
- onMounted: (invoke: (input: HTMLElement) => void | Promise<void>) => void;
57
55
  setup: () => Slot | Promise<Slot>;
58
56
  }
59
57
  export interface ComponentConstructor<TEvents extends Events, TAttributes extends Attributes> {
@@ -68,8 +66,11 @@ export interface CustomElement<TEvents extends Events, TAttributes extends Attri
68
66
  export interface CustomElementConstructor<TEvents extends Events, TAttributes extends Attributes> {
69
67
  new (): CustomElement<TEvents, TAttributes>;
70
68
  }
69
+ export declare class MountedEvent extends Event {
70
+ constructor(event?: EventInit);
71
+ }
71
72
  export declare const isTemplate: (value: object) => value is Template;
72
- export declare const createElement: <TEvents extends Events, TAttributes extends Attributes>(input: string | Template | ComponentConstructor<TEvents, TAttributes>, attributes?: AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
73
+ export declare const createElement: <TEvents extends Events, TAttributes extends Attributes>(input: string | ComponentConstructor<TEvents, TAttributes>, attributes?: AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
73
74
  export declare const toString: (slot: Slot) => string;
74
75
  export declare const toFragment: (nodes: Array<Node>) => DocumentFragment;
75
76
  export declare const renderToString: (slot: Slot, context: Map<string, unknown>) => Promise<string>;
@@ -78,8 +79,8 @@ export declare const defineComponent: <TEvents extends Events = {}, TAttributes
78
79
  export declare const toCustomElement: <TEvents extends Events = {}, TAttributes extends Attributes = {}>(component: ComponentConstructor<TEvents, TAttributes>) => CustomElementConstructor<TEvents, TAttributes>;
79
80
  export declare const registerComponent: <TEvents extends Events = {}, TAttributes extends Attributes = {}>(component: ComponentConstructor<TEvents, TAttributes>) => ComponentConstructor<TEvents, TAttributes>;
80
81
  export declare const getGlobalStyles: () => CSSStyleSheet[];
82
+ export declare const useEvent: (scope: Scope, target: EventTarget | undefined, event: string, input: EventListener<Event>, options?: AddEventListenerOptions) => number;
81
83
  export declare const getCustomElement: (node: Node | null) => CustomElement<Events, Attributes> | undefined;
82
- export declare const eventListener: <T extends Event>(event: T, invoke?: EventListener<T>) => void;
83
84
  export declare const isClient: () => boolean;
84
85
  export declare const isServer: () => boolean;
85
86
  export declare const preventDefault: (event: Event) => void;
@@ -87,3 +88,8 @@ export declare const stopPropagation: (event: Event) => void;
87
88
  export declare const stopImmediatePropagation: (event: Event) => void;
88
89
  export declare const MOUNTED_HOOK: Descriptor<HTMLElement>;
89
90
  export declare const components: Map<string, ComponentConstructor<Events, Attributes>>;
91
+ declare global {
92
+ interface HTMLElementEventMap {
93
+ mounted: MountedEvent;
94
+ }
95
+ }
package/dist/index.js CHANGED
@@ -208,12 +208,16 @@ const targets = new WeakMap();
208
208
 
209
209
  //#endregion
210
210
  //#region src/html/index.ts
211
+ var MountedEvent = class extends Event {
212
+ constructor(event) {
213
+ super("mounted", event);
214
+ }
215
+ };
211
216
  const isTemplate = (value) => {
212
217
  return "tag" in value && "attributes" in value && "children" in value;
213
218
  };
214
219
  const createElement = (input, attributes, ...children) => {
215
- if (typeof input === "object") return input;
216
- else return {
220
+ return {
217
221
  tag: typeof input === "function" ? input.$name : input,
218
222
  attributes: attributes ?? {},
219
223
  children
@@ -321,12 +325,8 @@ const renderToNode = async (scope, slot) => {
321
325
  const element = document.createElementNS(namespace(slot.tag), slot.tag);
322
326
  for (const name in slot.attributes) {
323
327
  const value = slot.attributes[name];
324
- if (name.startsWith("on")) {
325
- const event = name.substring(2).toLowerCase();
326
- const controller = new AbortController();
327
- element.addEventListener(event, (event$1) => eventListener(event$1, value), { signal: controller.signal });
328
- scope.dispose.push(() => controller.abort());
329
- } else createCompute(scope, () => {
328
+ if (name.startsWith("on")) useEvent(scope, element, name.substring(2).toLowerCase(), value);
329
+ else createCompute(scope, () => {
330
330
  const set = toString(value);
331
331
  if (set === "" || set === "false") return element.removeAttribute(name);
332
332
  return element.setAttribute(name, set);
@@ -346,7 +346,6 @@ const defineComponent = (options) => {
346
346
  static $events = options.events ?? {};
347
347
  static $attributes = options.attributes ?? {};
348
348
  scope;
349
- hooks;
350
349
  context;
351
350
  events;
352
351
  attributes;
@@ -354,7 +353,6 @@ const defineComponent = (options) => {
354
353
  host;
355
354
  constructor(input, context, host) {
356
355
  this.scope = new Scope();
357
- this.hooks = new Hooks();
358
356
  this.context = context ?? new Map();
359
357
  this.events = Object.keys(options.events ?? {}).reduce((output, name) => {
360
358
  Reflect.set(output, name, input?.[name], output);
@@ -373,9 +371,6 @@ const defineComponent = (options) => {
373
371
  setContext = (input, value) => {
374
372
  this.context.set(input, value);
375
373
  };
376
- onMounted = (invoke) => {
377
- return this.hooks.on(this.scope, MOUNTED_HOOK, invoke);
378
- };
379
374
  setup = () => options.setup(this);
380
375
  });
381
376
  };
@@ -399,7 +394,7 @@ const toCustomElement = (component) => {
399
394
  }, this.component.events);
400
395
  if (parentNode) for (const [name, value] of parentNode.component.context) this.component.context.set(name, value);
401
396
  rootNode.replaceChildren(await renderToNode(this.component.scope, await this.component.setup()));
402
- this.component.hooks.dispatch(MOUNTED_HOOK, this);
397
+ this.dispatchEvent(new MountedEvent());
403
398
  }
404
399
  attributeChangedCallback(name, oldValue, value) {
405
400
  if (value === oldValue) return;
@@ -447,16 +442,23 @@ const getGlobalStyles = () => {
447
442
  return sheet;
448
443
  });
449
444
  };
445
+ const useEvent = (scope, target, event, input, options) => {
446
+ const controller = new AbortController();
447
+ target?.addEventListener(event, (event$1) => {
448
+ if (Array.isArray(input)) for (const target$1 of input) target$1?.(event$1);
449
+ else input?.(event$1);
450
+ }, {
451
+ signal: controller.signal,
452
+ ...options
453
+ });
454
+ return scope.dispose.push(() => controller.abort());
455
+ };
450
456
  const getCustomElement = (node) => {
451
457
  if (node) {
452
458
  if ("component" in node) return node;
453
459
  return getCustomElement(node.parentNode);
454
460
  }
455
461
  };
456
- const eventListener = (event, invoke) => {
457
- if (Array.isArray(invoke)) for (const target of invoke) target?.(event);
458
- else invoke?.(event);
459
- };
460
462
  const isClient = () => typeof window !== "undefined";
461
463
  const isServer = () => typeof window === "undefined";
462
464
  const preventDefault = (event) => event.preventDefault();
@@ -789,4 +791,4 @@ const anchorNavigate = (event) => {
789
791
  };
790
792
 
791
793
  //#endregion
792
- export { $fetch, Compute, Handler, Hooks, MOUNTED_HOOK, Outlet, RUNTIME_CONTEXT, Radix, Scope, activeCompute, anchorNavigate, components, createApp, createCompute, createElement, createEvent, createMemo, createRuntime, createState, defineComponent, defineContext, defineHook, defineRoute, eventListener, fileName, fromValue, getAssets, getCookies, getCustomElement, getGlobalStyles, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, isClient, isServer, isTemplate, markdownToSlot, navigate, preventDefault, registerComponent, renderToNode, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toCustomElement, toFragment, toPath, toString };
794
+ export { $fetch, Compute, Handler, Hooks, MOUNTED_HOOK, MountedEvent, Outlet, RUNTIME_CONTEXT, Radix, Scope, activeCompute, anchorNavigate, components, createApp, createCompute, createElement, createEvent, createMemo, createRuntime, createState, defineComponent, defineContext, defineHook, defineRoute, fileName, fromValue, getAssets, getCookies, getCustomElement, getGlobalStyles, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, isClient, isServer, isTemplate, markdownToSlot, navigate, preventDefault, registerComponent, renderToNode, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toCustomElement, toFragment, toPath, toString, useEvent };
@@ -364,4 +364,4 @@ export declare namespace JSX {
364
364
  }
365
365
  export declare const svgElements: Set<string>;
366
366
  export declare const namespace: (tag: string) => "http://www.w3.org/1999/xhtml" | "http://www.w3.org/2000/svg";
367
- export declare const h: <TEvents extends import("..").Events, TAttributes extends import("..").Attributes>(input: string | import("..").Template | import("..").ComponentConstructor<TEvents, TAttributes>, attributes?: import("..").AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
367
+ export declare const h: <TEvents extends import("..").Events, TAttributes extends import("..").Attributes>(input: string | import("..").ComponentConstructor<TEvents, TAttributes>, attributes?: import("..").AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
package/dist/jsx/index.js CHANGED
@@ -7,8 +7,7 @@ function defineHook(key) {
7
7
  //#endregion
8
8
  //#region src/html/index.ts
9
9
  const createElement = (input, attributes, ...children) => {
10
- if (typeof input === "object") return input;
11
- else return {
10
+ return {
12
11
  tag: typeof input === "function" ? input.$name : input,
13
12
  attributes: attributes ?? {},
14
13
  children
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",