revojs 0.0.38 → 0.0.40

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.
@@ -39,7 +39,7 @@ export type AttributeOptions<T = unknown> = {
39
39
  type: T;
40
40
  default?: Infer<T>;
41
41
  };
42
- export type ShadowRootOptions = Partial<ShadowRootInit> & {
42
+ export type ShadowRootOptions = ShadowRootInit & {
43
43
  globalStyles?: boolean;
44
44
  };
45
45
  export type Events = Record<string, EventOptions>;
@@ -48,7 +48,7 @@ export interface ComponentOptions<TEvents extends Events, TAttributes extends At
48
48
  name: string;
49
49
  events?: TEvents;
50
50
  attributes?: TAttributes;
51
- shadowRoot?: false | ShadowRootOptions;
51
+ shadowRoot?: Partial<ShadowRootOptions>;
52
52
  styles?: Array<string>;
53
53
  setup: (component: Component<TEvents, TAttributes>) => Slot | Promise<Slot>;
54
54
  }
@@ -56,7 +56,7 @@ export interface Component<TEvents extends Events, TAttributes extends Attribute
56
56
  readonly scope: Scope;
57
57
  readonly events: EventOutput<TEvents>;
58
58
  readonly attributes: State<AttributeOutput<TAttributes>>;
59
- readonly shadowRoot: false | ShadowRootOptions;
59
+ readonly shadowRoot?: ShadowRootOptions;
60
60
  setup: () => Slot | Promise<Slot>;
61
61
  }
62
62
  export interface ComponentConstructor<TEvents extends Events, TAttributes extends Attributes> {
@@ -79,9 +79,6 @@ export declare const isTemplate: (value?: any) => value is Template;
79
79
  export declare const useHost: (scope: Scope) => HostContext;
80
80
  export declare const createElement: <TEvents extends Events, TAttributes extends Attributes>(input: string | ComponentConstructor<TEvents, TAttributes>, attributes?: AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
81
81
  export declare const toString: (slot: Slot) => string;
82
- export declare const isTextNode: (hydration?: Hydration) => hydration is Text;
83
- export declare const isCommentNode: (hydration?: Hydration) => hydration is Comment;
84
- export declare const isElementNode: (hydration?: Hydration, tagName?: string) => hydration is Element;
85
82
  export declare const toArray: (hydration: Hydration) => Array<Node>;
86
83
  export declare const toRange: (hydration: Hydration) => Range;
87
84
  export declare const toFragment: (hydration: Hydration) => DocumentFragment;
package/dist/index.js CHANGED
@@ -248,18 +248,6 @@ const toString = (slot) => {
248
248
  default: return "";
249
249
  }
250
250
  };
251
- const isTextNode = (hydration) => {
252
- if (Array.isArray(hydration)) return false;
253
- return hydration?.nodeType === Node.TEXT_NODE;
254
- };
255
- const isCommentNode = (hydration) => {
256
- if (Array.isArray(hydration)) return false;
257
- return hydration?.nodeType === Node.COMMENT_NODE;
258
- };
259
- const isElementNode = (hydration, tagName) => {
260
- if (Array.isArray(hydration)) return false;
261
- return hydration?.nodeType === Node.ELEMENT_NODE && hydration.nodeName.toUpperCase() === tagName?.toUpperCase();
262
- };
263
251
  const toArray = (hydration) => {
264
252
  if (Array.isArray(hydration)) return hydration.reduce((items, child) => items.concat(toArray(child)), new Array());
265
253
  else return [hydration];
@@ -283,43 +271,36 @@ const hydrate = async (scope, parentNode, slot, index, previous) => {
283
271
  let hydration = parentNode.childNodes.item(index);
284
272
  if (Array.isArray(slot)) {
285
273
  const items = new Array();
286
- for (const [index$1, childSlot] of slot.entries()) items.push(await hydrate(scope, parentNode, childSlot, index$1));
274
+ for (const [index$1, childSlot] of slot.entries()) items.push(await hydrate(scope, parentNode, childSlot, index$1, previous));
287
275
  if (items.length) hydration = items;
288
- else if (isCommentNode(hydration) === false) hydration = document.createComment("");
289
- }
290
- if (typeof slot === "function") {
291
- let previous$1;
292
- await createCompute(scope, async (scope$1) => {
293
- let input = slot;
294
- while (typeof input === "function") input = await input();
295
- hydration = await hydrate(scope$1, parentNode, input, index, previous$1);
296
- if (previous$1 && hydration !== previous$1) if (Array.isArray(hydration)) if (Array.isArray(previous$1)) {
297
- const range = toRange(previous$1);
298
- range.deleteContents();
299
- for (const childNode of toArray(hydration)) range.insertNode(childNode);
300
- } else parentNode.replaceChild(toFragment(hydration), previous$1);
301
- else if (Array.isArray(previous$1)) {
302
- const range = toRange(previous$1);
303
- range.deleteContents();
304
- range.insertNode(hydration);
305
- } else parentNode.replaceChild(hydration, previous$1);
306
- previous$1 = hydration;
307
- });
308
- }
309
- if (slot === null || slot === void 0) {
310
- if (isCommentNode(hydration) === false) hydration = document.createComment("");
276
+ else if (previous || !(hydration instanceof Comment)) hydration = document.createComment("");
311
277
  }
278
+ if (typeof slot === "function") await createCompute(scope, async (scope$1) => {
279
+ let input = slot;
280
+ while (typeof input === "function") input = await input();
281
+ hydration = await hydrate(scope$1, parentNode, input, index, previous);
282
+ if (previous && hydration !== previous) if (Array.isArray(hydration)) if (Array.isArray(previous)) {
283
+ const range = toRange(previous);
284
+ range.deleteContents();
285
+ for (const childNode of toArray(hydration)) range.insertNode(childNode);
286
+ } else parentNode.replaceChild(toFragment(hydration), previous);
287
+ else if (Array.isArray(previous)) {
288
+ const range = toRange(previous);
289
+ range.deleteContents();
290
+ range.insertNode(hydration);
291
+ } else parentNode.replaceChild(hydration, previous);
292
+ previous = hydration;
293
+ });
294
+ if ((slot === null || slot === void 0) && (previous || !(hydration instanceof Comment))) hydration = document.createComment("");
312
295
  if (typeof slot === "number" || typeof slot === "bigint" || typeof slot === "boolean" || typeof slot === "string" || typeof slot === "symbol") {
313
296
  const textContent = slot.toString();
314
- if (isTextNode(hydration)) {
315
- if (previous) hydration.textContent = textContent;
316
- else if (textContent !== hydration.textContent) hydration = parentNode.replaceChild(document.createTextNode(textContent), hydration);
317
- } else hydration = document.createTextNode(textContent);
297
+ if (previous || !(hydration instanceof Text)) hydration = document.createTextNode(textContent);
298
+ else if (textContent !== hydration.textContent) hydration = parentNode.replaceChild(document.createTextNode(textContent), hydration);
318
299
  }
319
300
  if (isTemplate(slot)) {
320
301
  const Component = components.get(slot.tag);
321
302
  if (Component) registerComponent(Component);
322
- if (isElementNode(hydration, slot.tag) === false) hydration = document.createElementNS(namespace(slot.tag), slot.tag);
303
+ if (previous || !(hydration instanceof Element && hydration.tagName.toUpperCase() === slot.tag.toUpperCase())) hydration = document.createElementNS(namespace(slot.tag), slot.tag);
323
304
  for (const [name, value] of Object.entries(slot.attributes)) createCompute(scope, (childScope) => {
324
305
  const target = hydration;
325
306
  if (name.startsWith("on")) {
@@ -332,7 +313,7 @@ const hydrate = async (scope, parentNode, slot, index, previous) => {
332
313
  }
333
314
  scope.onStop(() => childScope.stop());
334
315
  });
335
- for (const [index$1, childSlot] of slot.children.entries()) await hydrate(scope, hydration, childSlot, index$1);
316
+ for (const [index$1, childSlot] of slot.children.entries()) await hydrate(scope, hydration, childSlot, index$1, previous);
336
317
  }
337
318
  hydration ??= document.createComment("");
338
319
  if (parentNode.childNodes.item(index) === null) parentNode.appendChild(toFragment(hydration));
@@ -397,7 +378,7 @@ const defineComponent = (options) => {
397
378
  Reflect.set(attributes.value, name, fromValue(input?.[name]) ?? attribute.default, attributes.value);
398
379
  return attributes;
399
380
  }, createState({}));
400
- this.shadowRoot = options.shadowRoot ?? { mode: "open" };
381
+ if (options.shadowRoot) this.shadowRoot = defu(options.shadowRoot, { mode: "open" });
401
382
  }
402
383
  setup = () => options.setup(this);
403
384
  }
@@ -408,6 +389,7 @@ const toCustomElement = (Component) => {
408
389
  return class extends HTMLElement {
409
390
  static formAssociated = true;
410
391
  component = new Component();
392
+ internals = this.attachInternals();
411
393
  async connectedCallback() {
412
394
  let rootNode = this;
413
395
  const findParent = (node) => {
@@ -419,8 +401,7 @@ const toCustomElement = (Component) => {
419
401
  const parentNode = findParent(this.parentNode);
420
402
  if (parentNode) this.component.scope.parentScope = parentNode.component.scope;
421
403
  if (this.component.shadowRoot) {
422
- const options = defu(this.component.shadowRoot, { mode: "open" });
423
- rootNode = this.shadowRoot ?? this.attachShadow(options);
404
+ rootNode = this.shadowRoot ?? this.attachShadow(this.component.shadowRoot);
424
405
  if (this.component.shadowRoot.globalStyles) rootNode.adoptedStyleSheets = globalStyles;
425
406
  }
426
407
  for (const [name, event] of Object.entries(Component.$events)) Reflect.set(this.component.events, name, (value) => {
@@ -432,7 +413,7 @@ const toCustomElement = (Component) => {
432
413
  });
433
414
  this.component.scope.setContext(HOST_CONTEXT, {
434
415
  host: this,
435
- internals: this.attachInternals()
416
+ internals: this.internals
436
417
  });
437
418
  await hydrate(this.component.scope, rootNode, await this.component.setup(), 0);
438
419
  this.dispatchEvent(new MountedEvent());
@@ -775,7 +756,6 @@ const useRouter = (scope, context) => {
775
756
  };
776
757
  const Page = defineComponent({
777
758
  name: "x-page",
778
- shadowRoot: false,
779
759
  setup: ({ scope }) => {
780
760
  const { route } = scope.getContext(ROUTER_CONTEXT);
781
761
  return () => route.value;
@@ -924,4 +904,4 @@ const markdownToSlot = (input, options) => {
924
904
  };
925
905
 
926
906
  //#endregion
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 };
907
+ 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, isServer, isTemplate, 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.38",
3
+ "version": "0.0.40",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",