@molecule/app-svelte 3.13.0 → 3.14.1

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.
package/README.md CHANGED
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
3
3
  Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
4
4
  Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
5
5
  To change this document, edit the module-level JSDoc in src/index.ts.
6
- Generated: 2026-11-23T19:31:56.893Z
6
+ Generated: 2026-11-24T00:46:18.586Z
7
7
  -->
8
8
 
9
9
  # @molecule/app-svelte
@@ -2825,6 +2825,19 @@ function contentNodes(node: Node): ChildNode[]
2825
2825
 
2826
2826
  **Returns:** Those children, in order.
2827
2827
 
2828
+ #### `isContentNode(node)`
2829
+
2830
+ Whether one node renders something: an element, or a text node with text. Comments and empty
2831
+ text nodes are Svelte's own anchors (and other components' placeholders), not content.
2832
+
2833
+ ```typescript
2834
+ function isContentNode(node: Node): boolean
2835
+ ```
2836
+
2837
+ - `node` — The node.
2838
+
2839
+ **Returns:** True when it renders something.
2840
+
2828
2841
  #### `rendersContent(node)`
2829
2842
 
2830
2843
  Whether a node's children render anything (see {@link contentNodes}). A Svelte component
@@ -4937,6 +4950,12 @@ function createVersionStores(): {
4937
4950
  const BodyPortal: LegacyComponentType
4938
4951
  ```
4939
4952
 
4953
+ #### `ContentOrElse`
4954
+
4955
+ ```typescript
4956
+ const ContentOrElse: LegacyComponentType
4957
+ ```
4958
+
4940
4959
  #### `PresentWhileContent`
4941
4960
 
4942
4961
  ```typescript
@@ -0,0 +1,105 @@
1
+ <!--
2
+ ContentOrElse: its content in the page, with no element of its own, while that content renders
3
+ something (an element, or text), and `otherwise` in its place while it renders nothing. React's
4
+ `slot(emptyState) ? <>{emptyState}</> : <div>…</div>`: a feed's empty state takes the place of
5
+ the feed's element only while it shows something, which a Svelte component cannot decide from a
6
+ snippet's value (an `{#each}` over no rows, an `{#if false}`).
7
+
8
+ The content renders once, between two anchor comments that Svelte owns and moves with this
9
+ component: the blocks of two `svelte:element`s whose elements leave the page as soon as they are
10
+ created. Its components therefore live exactly as long as this component, and nothing else is
11
+ rendered between the two anchors, so the range read is the content's. `otherwise` waits for the
12
+ first decision, made once the content has rendered, so content that shows something at mount
13
+ never creates it; a `MutationObserver` then decides again after every change, in a microtask
14
+ before the browser paints.
15
+
16
+ `bind:present` reports the decision, for an element around the content that another component
17
+ renders (`<PresentWhileContent present={…}>` around a Button whose label is the content: the
18
+ Button's own whitespace is not the label). Use `PresentWhileContent` alone when the content sits
19
+ directly in the element that comes and goes.
20
+
21
+ It is `undefined` until the first decision, so a sibling that renders only while the content
22
+ is empty (`{#if present === false}`) is never created for content that shows something.
23
+ -->
24
+ <script lang="ts">
25
+ import type { Snippet } from 'svelte'
26
+ import type { Attachment } from 'svelte/attachments'
27
+
28
+ import { isContentNode } from '../content.js'
29
+
30
+ let {
31
+ children,
32
+ otherwise,
33
+ present = $bindable(),
34
+ }: {
35
+ /** The content; in the page, without a wrapper, while it renders something. */
36
+ children?: Snippet
37
+ /** What renders in the content's place while the content renders nothing. */
38
+ otherwise?: Snippet
39
+ /**
40
+ * Whether the content renders something, read with `bind:present`: `undefined` until the
41
+ * content has rendered and been read once.
42
+ */
43
+ present?: boolean
44
+ } = $props()
45
+
46
+ /** Whether the content has rendered and been read once. */
47
+ let decided = $state(false)
48
+ /** The anchor comment in front of the content. */
49
+ let start: Node | null = null
50
+
51
+ /**
52
+ * Records the anchor that opens the content's range and takes the marker element out.
53
+ *
54
+ * @param element - The marker element.
55
+ */
56
+ const markStart: Attachment<HTMLElement> = (element) => {
57
+ start = element.nextSibling
58
+ element.remove()
59
+ }
60
+
61
+ /**
62
+ * Records the anchor that closes the content's range, takes the marker element out, decides,
63
+ * and keeps deciding after every change in the range's parent.
64
+ *
65
+ * @param element - The marker element.
66
+ * @returns The cleanup.
67
+ */
68
+ const markEnd: Attachment<HTMLElement> = (element) => {
69
+ const end = element.nextSibling
70
+ element.remove()
71
+ const from = start
72
+ if (!from || !end) return
73
+ const sync = (): void => {
74
+ let next = from.nextSibling
75
+ while (next && next !== end && !isContentNode(next)) next = next.nextSibling
76
+ present = !!next && next !== end
77
+ decided = true
78
+ }
79
+ sync()
80
+ // The range is created inside a fragment that Svelte then moves into the page, and a block
81
+ // can move it again: observe whatever parent it has now, and follow it when it changes.
82
+ let observed: Node | null = null
83
+ const observer = new MutationObserver(() => {
84
+ if (from.parentNode !== observed) observe()
85
+ sync()
86
+ })
87
+ const observe = (): void => {
88
+ observer.disconnect()
89
+ observed = from.parentNode
90
+ if (observed)
91
+ observer.observe(observed, { childList: true, characterData: true, subtree: true })
92
+ }
93
+ observe()
94
+ return () => {
95
+ observer.disconnect()
96
+ // Removed, the content renders nothing.
97
+ present = false
98
+ }
99
+ }
100
+ </script>
101
+
102
+ <svelte:element this={"i"} {@attach markStart} />{@render children?.()}<svelte:element
103
+ this={"i"}
104
+ {@attach markEnd}
105
+ />{#if decided && !present}{@render otherwise?.()}{/if}
@@ -0,0 +1,16 @@
1
+ import type { Snippet } from 'svelte';
2
+ type $$ComponentProps = {
3
+ /** The content; in the page, without a wrapper, while it renders something. */
4
+ children?: Snippet;
5
+ /** What renders in the content's place while the content renders nothing. */
6
+ otherwise?: Snippet;
7
+ /**
8
+ * Whether the content renders something, read with `bind:present`: `undefined` until the
9
+ * content has rendered and been read once.
10
+ */
11
+ present?: boolean;
12
+ };
13
+ declare const ContentOrElse: import("svelte").Component<$$ComponentProps, {}, "present">;
14
+ type ContentOrElse = ReturnType<typeof ContentOrElse>;
15
+ export default ContentOrElse;
16
+ //# sourceMappingURL=ContentOrElse.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ContentOrElse.svelte.d.ts","sourceRoot":"","sources":["../../src/components/ContentOrElse.svelte"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AAKpC,KAAK,gBAAgB,GAAI;IACtB,+EAA+E;IAC/E,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,6EAA6E;IAC7E,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB,CAAC;AA0EJ,QAAA,MAAM,aAAa,6DAAwC,CAAC;AAC5D,KAAK,aAAa,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC;AACtD,eAAe,aAAa,CAAC"}
@@ -10,24 +10,44 @@
10
10
  block, so it comes back in its place; a `MutationObserver` on it (which keeps reporting while
11
11
  it is out of the page) decides again after every change Svelte makes to the content. The
12
12
  observer reports in a microtask, before the browser paints.
13
+
14
+ `present`, when given, decides instead of the element's own content: for content whose
15
+ presence is a node nested in another component (a Button's label, read with
16
+ `<ContentOrElse bind:present>`), where the element always holds that component's markup.
17
+
18
+ One case differs from React: content whose only node is a component that renders nothing
19
+ leaves no node in the page, so the element leaves too, where React's `slot()` counts the
20
+ component's element and keeps an empty wrapper. Neither framework can see what a component
21
+ renders before it renders, and the page holds the same anchors either way.
13
22
  -->
14
23
  <script lang="ts">
15
- import type { Snippet } from 'svelte'
24
+ import { type Snippet, untrack } from 'svelte'
16
25
  import type { Attachment } from 'svelte/attachments'
17
- import type { HTMLAttributes } from 'svelte/elements'
26
+ import type { HTMLAnchorAttributes, HTMLAttributes } from 'svelte/elements'
18
27
 
19
28
  import { rendersContent } from '../content.js'
20
29
 
21
30
  let {
22
31
  tag = 'div',
23
32
  children,
33
+ present,
24
34
  ...attributes
25
- }: HTMLAttributes<HTMLElement> & {
26
- /** The element's tag. */
27
- tag?: string
28
- /** The content; the element is in the page only while it renders something. */
29
- children?: Snippet
30
- } = $props()
35
+ }: HTMLAttributes<HTMLElement> &
36
+ // A link's own attributes, for `tag="a"`.
37
+ Pick<
38
+ HTMLAnchorAttributes,
39
+ 'href' | 'target' | 'rel' | 'download' | 'hreflang' | 'referrerpolicy'
40
+ > & {
41
+ /** The element's tag; every other prop is an attribute of it. */
42
+ tag?: string
43
+ /** The content; the element is in the page only while it renders something. */
44
+ children?: Snippet
45
+ /** Decides instead of the content when given (see above). */
46
+ present?: boolean
47
+ } = $props()
48
+
49
+ /** Decides again; set while the element exists. */
50
+ let decide: (() => void) | null = null
31
51
 
32
52
  /**
33
53
  * Keeps the element in the page exactly while its content renders something.
@@ -39,7 +59,7 @@
39
59
  // The block's anchor: Svelte inserts the element in front of it and moves the two together.
40
60
  const anchor = element.nextSibling
41
61
  const sync = (): void => {
42
- if (rendersContent(element)) {
62
+ if (untrack(() => present) ?? rendersContent(element)) {
43
63
  if (!element.isConnected && anchor?.parentNode)
44
64
  anchor.parentNode.insertBefore(element, anchor)
45
65
  } else if (element.parentNode) {
@@ -47,10 +67,19 @@
47
67
  }
48
68
  }
49
69
  sync()
70
+ decide = sync
50
71
  const observer = new MutationObserver(sync)
51
72
  observer.observe(element, { childList: true, characterData: true, subtree: true })
52
- return () => observer.disconnect()
73
+ return () => {
74
+ observer.disconnect()
75
+ decide = null
76
+ }
53
77
  }
78
+
79
+ $effect.pre(() => {
80
+ void present
81
+ untrack(() => decide?.())
82
+ })
54
83
  </script>
55
84
 
56
85
  <svelte:element this={tag} {...attributes} {@attach presentWhileContent}
@@ -1,10 +1,12 @@
1
- import type { Snippet } from 'svelte';
2
- import type { HTMLAttributes } from 'svelte/elements';
3
- type $$ComponentProps = HTMLAttributes<HTMLElement> & {
4
- /** The element's tag. */
1
+ import { type Snippet } from 'svelte';
2
+ import type { HTMLAnchorAttributes, HTMLAttributes } from 'svelte/elements';
3
+ type $$ComponentProps = HTMLAttributes<HTMLElement> & Pick<HTMLAnchorAttributes, 'href' | 'target' | 'rel' | 'download' | 'hreflang' | 'referrerpolicy'> & {
4
+ /** The element's tag; every other prop is an attribute of it. */
5
5
  tag?: string;
6
6
  /** The content; the element is in the page only while it renders something. */
7
7
  children?: Snippet;
8
+ /** Decides instead of the content when given (see above). */
9
+ present?: boolean;
8
10
  };
9
11
  declare const PresentWhileContent: import("svelte").Component<$$ComponentProps, {}, "">;
10
12
  type PresentWhileContent = ReturnType<typeof PresentWhileContent>;
@@ -1 +1 @@
1
- {"version":3,"file":"PresentWhileContent.svelte.d.ts","sourceRoot":"","sources":["../../src/components/PresentWhileContent.svelte"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AAErC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAIpD,KAAK,gBAAgB,GAAI,cAAc,CAAC,WAAW,CAAC,GAAG;IACpD,yBAAyB;IACzB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,+EAA+E;IAC/E,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAC;AA2CJ,QAAA,MAAM,mBAAmB,sDAAwC,CAAC;AAClE,KAAK,mBAAmB,GAAG,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAClE,eAAe,mBAAmB,CAAC"}
1
+ {"version":3,"file":"PresentWhileContent.svelte.d.ts","sourceRoot":"","sources":["../../src/components/PresentWhileContent.svelte"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,OAAO,EAAW,MAAM,QAAQ,CAAA;AAE9C,OAAO,KAAK,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAI1E,KAAK,gBAAgB,GAAI,cAAc,CAAC,WAAW,CAAC,GAEjD,IAAI,CACF,oBAAoB,EACpB,MAAM,GAAG,QAAQ,GAAG,KAAK,GAAG,UAAU,GAAG,UAAU,GAAG,gBAAgB,CACvE,GAAG;IACF,iEAAiE;IACjE,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,+EAA+E;IAC/E,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,6DAA6D;IAC7D,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB,CAAC;AAwDN,QAAA,MAAM,mBAAmB,sDAAwC,CAAC;AAClE,KAAK,mBAAmB,GAAG,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAClE,eAAe,mBAAmB,CAAC"}
@@ -2,10 +2,13 @@
2
2
  * The components that are not tied to one service: `BodyPortal`, React's
3
3
  * `createPortal` for `document.body`, as a Svelte component, and
4
4
  * `PresentWhileContent`, an element in the page only while its content renders
5
- * something (React's `slot(children) !== undefined && <div>…</div>`).
5
+ * something (React's `slot(children) !== undefined && <div>…</div>`), and
6
+ * `ContentOrElse`, its content alone while it renders something and `otherwise` in
7
+ * its place while it does not (React's `slot(node) ? <>{node}</> : <div>…</div>`).
6
8
  *
7
9
  * @module
8
10
  */
9
11
  export { default as BodyPortal } from './BodyPortal.svelte';
12
+ export { default as ContentOrElse } from './ContentOrElse.svelte';
10
13
  export { default as PresentWhileContent } from './PresentWhileContent.svelte';
11
14
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAC3D,OAAO,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,8BAA8B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAC3D,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,wBAAwB,CAAA;AACjE,OAAO,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,8BAA8B,CAAA"}
@@ -2,9 +2,12 @@
2
2
  * The components that are not tied to one service: `BodyPortal`, React's
3
3
  * `createPortal` for `document.body`, as a Svelte component, and
4
4
  * `PresentWhileContent`, an element in the page only while its content renders
5
- * something (React's `slot(children) !== undefined && <div>…</div>`).
5
+ * something (React's `slot(children) !== undefined && <div>…</div>`), and
6
+ * `ContentOrElse`, its content alone while it renders something and `otherwise` in
7
+ * its place while it does not (React's `slot(node) ? <>{node}</> : <div>…</div>`).
6
8
  *
7
9
  * @module
8
10
  */
9
11
  export { default as BodyPortal } from './BodyPortal.svelte';
12
+ export { default as ContentOrElse } from './ContentOrElse.svelte';
10
13
  export { default as PresentWhileContent } from './PresentWhileContent.svelte';
package/dist/content.d.ts CHANGED
@@ -18,6 +18,19 @@
18
18
  * @returns Those children, in order.
19
19
  */
20
20
  export declare function contentNodes(node: Node): ChildNode[];
21
+ /**
22
+ * Whether one node renders something: an element, or a text node with text. Comments and empty
23
+ * text nodes are Svelte's own anchors (and other components' placeholders), not content.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * isContentNode(document.createComment('')) // false
28
+ * ```
29
+ *
30
+ * @param node - The node.
31
+ * @returns True when it renders something.
32
+ */
33
+ export declare function isContentNode(node: Node): boolean;
21
34
  /**
22
35
  * Whether a node's children render anything (see {@link contentNodes}). A Svelte component
23
36
  * cannot tell from a snippet's value whether it renders anything (`{#each}` over no rows,
@@ -1 +1 @@
1
- {"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../src/content.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,SAAS,EAAE,CAIpD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAElD"}
1
+ {"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../src/content.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,SAAS,EAAE,CAEpD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAEjD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAElD"}
package/dist/content.js CHANGED
@@ -18,7 +18,22 @@
18
18
  * @returns Those children, in order.
19
19
  */
20
20
  export function contentNodes(node) {
21
- return Array.from(node.childNodes).filter((child) => child.nodeType === 1 || (child.nodeType === 3 && (child.textContent ?? '') !== ''));
21
+ return Array.from(node.childNodes).filter(isContentNode);
22
+ }
23
+ /**
24
+ * Whether one node renders something: an element, or a text node with text. Comments and empty
25
+ * text nodes are Svelte's own anchors (and other components' placeholders), not content.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * isContentNode(document.createComment('')) // false
30
+ * ```
31
+ *
32
+ * @param node - The node.
33
+ * @returns True when it renders something.
34
+ */
35
+ export function isContentNode(node) {
36
+ return node.nodeType === 1 || (node.nodeType === 3 && (node.textContent ?? '') !== '');
22
37
  }
23
38
  /**
24
39
  * Whether a node's children render anything (see {@link contentNodes}). A Svelte component
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/app-svelte",
3
- "version": "3.13.0",
3
+ "version": "3.14.1",
4
4
  "description": "Svelte framework bindings for molecule.dev",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",