revojs 0.0.32 → 0.0.34

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.
@@ -24,30 +24,34 @@ export type EventOutput<T extends Events> = {
24
24
  export type AttributeOutput<T extends Attributes> = {
25
25
  [K in keyof T]: T[K]["default"] extends undefined ? Infer<T[K]["type"]> | undefined : Infer<T[K]["type"]>;
26
26
  };
27
- export type EventOptions<T> = {
27
+ export type EventOptions<T = unknown> = {
28
28
  type: T;
29
29
  bubbles?: boolean;
30
30
  cancelable?: boolean;
31
31
  composed?: boolean;
32
32
  };
33
- export type AttributeOptions<T> = {
33
+ export type AttributeOptions<T = unknown> = {
34
34
  type: T;
35
35
  default?: Infer<T>;
36
36
  };
37
- export type Events = Record<string, EventOptions<unknown>>;
38
- export type Attributes = Record<string, AttributeOptions<unknown>>;
37
+ export type ShadowRootOptions = Partial<ShadowRootInit> & {
38
+ globalStyles?: boolean;
39
+ };
40
+ export type Events = Record<string, EventOptions>;
41
+ export type Attributes = Record<string, AttributeOptions>;
39
42
  export interface ComponentOptions<TEvents extends Events, TAttributes extends Attributes> {
40
43
  name: string;
41
44
  events?: TEvents;
42
45
  attributes?: TAttributes;
43
- shadowRoot?: false | ShadowRootInit;
46
+ shadowRoot?: false | ShadowRootOptions;
47
+ styles?: Array<string>;
44
48
  setup: (component: Component<TEvents, TAttributes>) => Slot | Promise<Slot>;
45
49
  }
46
50
  export interface Component<TEvents extends Events, TAttributes extends Attributes> {
47
51
  readonly scope: Scope;
48
52
  readonly events: EventOutput<TEvents>;
49
53
  readonly attributes: State<AttributeOutput<TAttributes>>;
50
- readonly shadowRoot: false | ShadowRootInit;
54
+ readonly shadowRoot: false | ShadowRootOptions;
51
55
  readonly host?: CustomElement<TEvents, TAttributes>;
52
56
  setup: () => Slot | Promise<Slot>;
53
57
  }
@@ -55,6 +59,7 @@ export interface ComponentConstructor<TEvents extends Events, TAttributes extend
55
59
  $name: string;
56
60
  $events: TEvents;
57
61
  $attributes: TAttributes;
62
+ $styles: Array<string>;
58
63
  new (input?: Input<TEvents, TAttributes>, scope?: Scope, host?: CustomElement<TEvents, TAttributes>): Component<TEvents, TAttributes>;
59
64
  }
60
65
  export interface CustomElement<TEvents extends Events, TAttributes extends Attributes> extends HTMLElement {
@@ -75,7 +80,6 @@ export declare const renderToNode: (scope: Scope, slot: Slot) => Promise<Node>;
75
80
  export declare const defineComponent: <TEvents extends Events = {}, TAttributes extends Attributes = {}>(options: ComponentOptions<TEvents, TAttributes>) => ComponentConstructor<TEvents, TAttributes>;
76
81
  export declare const toCustomElement: <TEvents extends Events = {}, TAttributes extends Attributes = {}>(component: ComponentConstructor<TEvents, TAttributes>) => CustomElementConstructor<TEvents, TAttributes>;
77
82
  export declare const registerComponent: <TEvents extends Events = {}, TAttributes extends Attributes = {}>(component: ComponentConstructor<TEvents, TAttributes>) => ComponentConstructor<TEvents, TAttributes>;
78
- export declare const getGlobalStyles: () => CSSStyleSheet[];
79
83
  export declare function useEvent<T extends keyof ElementEventMap>(scope: Scope, target: EventTarget | undefined, event: T, input: EventListener<ElementEventMap[T]>, options?: AddEventListenerOptions): void;
80
84
  export declare function useEvent<T extends keyof WindowEventMap>(scope: Scope, target: Window | undefined, event: T, input: EventListener<WindowEventMap[T]>, options?: AddEventListenerOptions): void;
81
85
  export declare function useEvent<T extends keyof HTMLElementEventMap>(scope: Scope, target: HTMLElement | undefined, event: T, input: EventListener<HTMLElementEventMap[T]>, options?: AddEventListenerOptions): void;
package/dist/index.js CHANGED
@@ -222,11 +222,16 @@ const isTemplate = (value) => {
222
222
  return "tag" in value && "attributes" in value && "children" in value;
223
223
  };
224
224
  const createElement = (input, attributes, ...children) => {
225
- return {
225
+ const template = {
226
226
  tag: typeof input === "function" ? input.$name : input,
227
227
  attributes: attributes ?? {},
228
228
  children
229
229
  };
230
+ if (typeof input === "function") {
231
+ const classes = template.attributes["class"];
232
+ template.attributes["class"] = (classes ? [classes, ...input.$styles] : input.$styles).join(" ");
233
+ }
234
+ return template;
230
235
  };
231
236
  const toString = (slot) => {
232
237
  switch (typeof slot) {
@@ -353,6 +358,7 @@ const defineComponent = (options) => {
353
358
  static $name = options.name;
354
359
  static $events = options.events ?? {};
355
360
  static $attributes = options.attributes ?? {};
361
+ static $styles = options.styles ?? [];
356
362
  scope;
357
363
  events;
358
364
  attributes;
@@ -393,7 +399,19 @@ const toCustomElement = (component) => {
393
399
  this.component = new component(void 0, void 0, this);
394
400
  }
395
401
  async connectedCallback() {
396
- const rootNode = this.component.shadowRoot ? this.attachShadow(this.component.shadowRoot) : this;
402
+ let rootNode = this;
403
+ if (this.component.shadowRoot) {
404
+ rootNode = this.attachShadow(defu(this.component.shadowRoot, { mode: "open" }));
405
+ if (this.component.shadowRoot.globalStyles) {
406
+ const globalStyles = Array.from(document.styleSheets).map((style) => {
407
+ const sheet = new CSSStyleSheet();
408
+ const css = Array.from(style.cssRules).map((rule) => rule.cssText).join(" ");
409
+ sheet.replaceSync(css);
410
+ return sheet;
411
+ });
412
+ rootNode.adoptedStyleSheets.push(...globalStyles);
413
+ }
414
+ }
397
415
  for (const [name, event] of Object.entries(component.$events)) Reflect.set(this.component.events, name, (value) => {
398
416
  if (value instanceof Event) return;
399
417
  this.dispatchEvent(new CustomEvent(name.substring(2).toLowerCase(), {
@@ -442,14 +460,6 @@ const registerComponent = (component) => {
442
460
  }
443
461
  return component;
444
462
  };
445
- const getGlobalStyles = () => {
446
- return Array.from(isServer() ? [] : document.styleSheets).map((style) => {
447
- const sheet = new CSSStyleSheet();
448
- const css = Array.from(style.cssRules).map((rule) => rule.cssText).join(" ");
449
- sheet.replaceSync(css);
450
- return sheet;
451
- });
452
- };
453
463
  function useEvent(scope, target, event, input, options) {
454
464
  const controller = new AbortController();
455
465
  target?.addEventListener(event, (event$1) => {
@@ -591,6 +601,9 @@ var Radix = class Radix {
591
601
 
592
602
  //#endregion
593
603
  //#region src/runtime/index.ts
604
+ const useRuntime = (scope, context) => {
605
+ return scope.getContext(context ?? RUNTIME_CONTEXT);
606
+ };
594
607
  const defineRoute = (route) => {
595
608
  return route;
596
609
  };
@@ -604,7 +617,7 @@ const toPath = (value) => {
604
617
  return split.length === 3 ? [split.at(0), split.at(1)] : [split.at(0)];
605
618
  };
606
619
  const $fetch = async (scope, input, init) => {
607
- const { event } = scope.getContext(RUNTIME_CONTEXT);
620
+ const { event } = useRuntime(scope);
608
621
  let response;
609
622
  if (event) {
610
623
  const url = new URL(input.toString(), event.request.url);
@@ -683,6 +696,7 @@ const ROUTER_CONTEXT = defineContext("ROUTER_CONTEXT");
683
696
  const createRouter = (options) => {
684
697
  const navigator = new EventTarget();
685
698
  const radix = new Radix();
699
+ const url = createState();
686
700
  const route = createState();
687
701
  const inputs = createState();
688
702
  for (const path in options.routes) {
@@ -693,10 +707,10 @@ const createRouter = (options) => {
693
707
  }
694
708
  }
695
709
  const registerRouterContext = async (scope) => {
696
- const { event } = scope.getContext(RUNTIME_CONTEXT);
710
+ const { event } = useRuntime(scope);
697
711
  const fetch$1 = async () => {
698
- const url = new URL(event ? event.request.url : window.location.href);
699
- const match = radix.match(url.pathname);
712
+ url.value = new URL(event?.request.url ?? window?.location.href);
713
+ const match = radix.match(url.value.pathname);
700
714
  inputs.value = match.inputs;
701
715
  const Page$1 = await match.value?.();
702
716
  if (Page$1) route.value = /* @__PURE__ */ h(Page$1, inputs.value);
@@ -707,6 +721,7 @@ const createRouter = (options) => {
707
721
  scope.setContext(ROUTER_CONTEXT, {
708
722
  options,
709
723
  navigator,
724
+ url,
710
725
  radix,
711
726
  route,
712
727
  inputs
@@ -719,7 +734,7 @@ const createRouter = (options) => {
719
734
  };
720
735
  };
721
736
  const useRouter = (scope, context) => {
722
- const { route, inputs, navigator } = scope.getContext(context ?? ROUTER_CONTEXT);
737
+ const { url, route, inputs, navigator } = scope.getContext(context ?? ROUTER_CONTEXT);
723
738
  const navigate = (path) => {
724
739
  if (isClient()) window.history.pushState(window.history.state, "", path);
725
740
  navigator.dispatchEvent(new NavigateEvent());
@@ -729,6 +744,7 @@ const useRouter = (scope, context) => {
729
744
  navigate(event.currentTarget?.getAttribute("href") ?? "/");
730
745
  };
731
746
  return {
747
+ url,
732
748
  route,
733
749
  inputs,
734
750
  navigator,
@@ -888,4 +904,4 @@ const markdownToSlot = (input, options) => {
888
904
  };
889
905
 
890
906
  //#endregion
891
- 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, getGlobalStyles, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, isClient, isServer, isTemplate, markdownToSlot, preventDefault, registerComponent, renderToNode, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toCustomElement, toFragment, toPath, toString, useEvent, useLocale, useRouter };
907
+ 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, isClient, isServer, isTemplate, markdownToSlot, preventDefault, registerComponent, renderToNode, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toCustomElement, toFragment, toPath, toString, useEvent, useLocale, useRouter, useRuntime };
package/dist/jsx/index.js CHANGED
@@ -1,11 +1,57 @@
1
1
 
2
+ //#region ../../node_modules/defu/dist/defu.mjs
3
+ function isPlainObject(value) {
4
+ if (value === null || typeof value !== "object") return false;
5
+ const prototype = Object.getPrototypeOf(value);
6
+ if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) return false;
7
+ if (Symbol.iterator in value) return false;
8
+ if (Symbol.toStringTag in value) return Object.prototype.toString.call(value) === "[object Module]";
9
+ return true;
10
+ }
11
+ function _defu(baseObject, defaults, namespace$1 = ".", merger) {
12
+ if (!isPlainObject(defaults)) return _defu(baseObject, {}, namespace$1, merger);
13
+ const object = Object.assign({}, defaults);
14
+ for (const key in baseObject) {
15
+ if (key === "__proto__" || key === "constructor") continue;
16
+ const value = baseObject[key];
17
+ if (value === null || value === void 0) continue;
18
+ if (merger && merger(object, key, value, namespace$1)) continue;
19
+ if (Array.isArray(value) && Array.isArray(object[key])) object[key] = [...value, ...object[key]];
20
+ else if (isPlainObject(value) && isPlainObject(object[key])) object[key] = _defu(value, object[key], (namespace$1 ? `${namespace$1}.` : "") + key.toString(), merger);
21
+ else object[key] = value;
22
+ }
23
+ return object;
24
+ }
25
+ function createDefu(merger) {
26
+ return (...arguments_) => arguments_.reduce((p, c) => _defu(p, c, "", merger), {});
27
+ }
28
+ const defu = createDefu();
29
+ const defuFn = createDefu((object, key, currentValue) => {
30
+ if (object[key] !== void 0 && typeof currentValue === "function") {
31
+ object[key] = currentValue(object[key]);
32
+ return true;
33
+ }
34
+ });
35
+ const defuArrayFn = createDefu((object, key, currentValue) => {
36
+ if (Array.isArray(object[key]) && typeof currentValue === "function") {
37
+ object[key] = currentValue(object[key]);
38
+ return true;
39
+ }
40
+ });
41
+
42
+ //#endregion
2
43
  //#region src/html/index.ts
3
44
  const createElement = (input, attributes, ...children) => {
4
- return {
45
+ const template = {
5
46
  tag: typeof input === "function" ? input.$name : input,
6
47
  attributes: attributes ?? {},
7
48
  children
8
49
  };
50
+ if (typeof input === "function") {
51
+ const classes = template.attributes["class"];
52
+ template.attributes["class"] = (classes ? [classes, ...input.$styles] : input.$styles).join(" ");
53
+ }
54
+ return template;
9
55
  };
10
56
 
11
57
  //#endregion
@@ -8,6 +8,7 @@ export type RouterOptions<T extends Routes = Routes> = {
8
8
  export type RouterContext<T extends RouterOptions = RouterOptions> = {
9
9
  options: T;
10
10
  navigator: EventTarget;
11
+ url: State<URL | undefined>;
11
12
  radix: Radix<() => Promise<ComponentConstructor<Events, Attributes>>>;
12
13
  route: State<Slot | undefined>;
13
14
  inputs: State<Record<string, string> | undefined>;
@@ -19,6 +20,7 @@ export declare const ROUTER_CONTEXT: Descriptor<RouterContext<RouterOptions<Rout
19
20
  export declare const createRouter: <T extends RouterOptions>(options: T) => {
20
21
  ROUTER_CONTEXT: Descriptor<RouterContext<T>>;
21
22
  registerRouterContext: (scope: Scope) => Promise<{
23
+ url: State<URL | undefined>;
22
24
  route: State<unknown>;
23
25
  inputs: State<Record<string, string> | undefined>;
24
26
  navigator: EventTarget;
@@ -27,6 +29,7 @@ export declare const createRouter: <T extends RouterOptions>(options: T) => {
27
29
  }>;
28
30
  };
29
31
  export declare const useRouter: <T extends RouterContext>(scope: Scope, context?: Descriptor<T>) => {
32
+ url: State<URL | undefined>;
30
33
  route: State<unknown>;
31
34
  inputs: State<Record<string, string> | undefined>;
32
35
  navigator: EventTarget;
@@ -1,6 +1,6 @@
1
1
  import { type Context, type Event, type Handle, type Middleware } from "../http";
2
2
  import { Radix } from "../radix";
3
- import { Scope } from "../signals";
3
+ import { type Descriptor, Scope } from "../signals";
4
4
  export type Route<T> = {
5
5
  fetch: Handle<T>;
6
6
  };
@@ -9,12 +9,14 @@ export type Runtime<T> = {
9
9
  middlewares: Array<Middleware<T>>;
10
10
  fetch: (request: Request, context: Context<T>) => Promise<Response>;
11
11
  };
12
+ export type RuntimeContext<T = Record<string, unknown>> = {
13
+ event?: Event<T>;
14
+ };
15
+ export declare const useRuntime: <T extends RuntimeContext>(scope: Scope, context?: Descriptor<T>) => RuntimeContext<Record<string, unknown>>;
12
16
  export declare const defineRoute: <T>(route: Route<T>) => Route<T>;
13
17
  export declare const fileName: (path: string) => string | undefined;
14
18
  export declare const toPath: (value: string) => (string | undefined)[];
15
19
  export declare const $fetch: <T>(scope: Scope, input: string | URL, init?: RequestInit) => Promise<T>;
16
20
  export declare const getVariables: <T>(event?: Event<T>) => T;
17
21
  export declare const createRuntime: <T>() => Promise<Runtime<T>>;
18
- export declare const RUNTIME_CONTEXT: import("..").Descriptor<{
19
- event?: Event;
20
- }>;
22
+ export declare const RUNTIME_CONTEXT: Descriptor<RuntimeContext<Record<string, unknown>>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.32",
3
+ "version": "0.0.34",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",