revojs 0.0.33 → 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) => {
@@ -894,4 +904,4 @@ const markdownToSlot = (input, options) => {
894
904
  };
895
905
 
896
906
  //#endregion
897
- 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, useRuntime };
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.33",
3
+ "version": "0.0.34",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",