@molecule/app-svelte 3.11.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 +78 -4
- package/dist/components/BodyPortal.svelte +43 -0
- package/dist/components/BodyPortal.svelte.d.ts +11 -0
- package/dist/components/BodyPortal.svelte.d.ts.map +1 -0
- package/dist/components/ContentOrElse.svelte +105 -0
- package/dist/components/ContentOrElse.svelte.d.ts +16 -0
- package/dist/components/ContentOrElse.svelte.d.ts.map +1 -0
- package/dist/components/PortalContent.svelte +12 -0
- package/dist/components/PortalContent.svelte.d.ts +8 -0
- package/dist/components/PortalContent.svelte.d.ts.map +1 -0
- package/dist/components/PresentWhileContent.svelte +87 -0
- package/dist/components/PresentWhileContent.svelte.d.ts +14 -0
- package/dist/components/PresentWhileContent.svelte.d.ts.map +1 -0
- package/dist/components/index.d.ts +14 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +13 -0
- package/dist/content.d.ts +50 -0
- package/dist/content.d.ts.map +1 -0
- package/dist/content.js +55 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -0
- package/package.json +2 -2
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-
|
|
6
|
+
Generated: 2026-11-24T00:46:18.586Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/app-svelte
|
|
@@ -135,6 +135,15 @@ then consume services anywhere below via store factories
|
|
|
135
135
|
>
|
|
136
136
|
```
|
|
137
137
|
|
|
138
|
+
```svelte
|
|
139
|
+
<!-- PortaledNode.svelte — content in document.body, exactly as React renders it -->
|
|
140
|
+
<script lang="ts">
|
|
141
|
+
import { BodyPortal } from '@molecule/app-svelte'
|
|
142
|
+
</script>
|
|
143
|
+
|
|
144
|
+
<BodyPortal><div role="dialog">…</div></BodyPortal>
|
|
145
|
+
```
|
|
146
|
+
|
|
138
147
|
Session state is a three-way decision, not a boolean: `status === 'loading'`
|
|
139
148
|
is undecided, so wait for it and redirect only on
|
|
140
149
|
`status === 'unauthenticated'`; `'unavailable'` keeps the session candidate
|
|
@@ -1041,8 +1050,8 @@ interface Router {
|
|
|
1041
1050
|
* link, the back button, `navigate`), with the new location and the navigation action.
|
|
1042
1051
|
* A router calls it before the browser paints the new page, so a component that renders
|
|
1043
1052
|
* from a listener (the framework bindings' `useLocation` and `useParams` do) shows the new
|
|
1044
|
-
* location in the same paint as the new page.
|
|
1045
|
-
*
|
|
1053
|
+
* location in the same paint as the new page. Molecule's router conformance tests check
|
|
1054
|
+
* that at every paint.
|
|
1046
1055
|
*
|
|
1047
1056
|
* @param listener - Called with the new location and the navigation action.
|
|
1048
1057
|
* @returns Stops calling `listener`.
|
|
@@ -2802,6 +2811,49 @@ type SignupStore<T = unknown> = PromiseStore<AuthResult<T>, [RegisterData]>
|
|
|
2802
2811
|
|
|
2803
2812
|
### Functions
|
|
2804
2813
|
|
|
2814
|
+
#### `contentNodes(node)`
|
|
2815
|
+
|
|
2816
|
+
The children of a node that render something: elements, and text nodes with text. Comments
|
|
2817
|
+
and empty text nodes are Svelte's own anchors (and other components' placeholders), not
|
|
2818
|
+
content.
|
|
2819
|
+
|
|
2820
|
+
```typescript
|
|
2821
|
+
function contentNodes(node: Node): ChildNode[]
|
|
2822
|
+
```
|
|
2823
|
+
|
|
2824
|
+
- `node` — The node whose children are read.
|
|
2825
|
+
|
|
2826
|
+
**Returns:** Those children, in order.
|
|
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
|
+
|
|
2841
|
+
#### `rendersContent(node)`
|
|
2842
|
+
|
|
2843
|
+
Whether a node's children render anything (see {@link contentNodes}). A Svelte component
|
|
2844
|
+
cannot tell from a snippet's value whether it renders anything (`{#each}` over no rows,
|
|
2845
|
+
`{#if false}`), so a component that renders a wrapper only while its content has something to
|
|
2846
|
+
show, as React's `slot(children) !== undefined && <div>…</div>` does, asks the rendered DOM
|
|
2847
|
+
instead (see `PresentWhileContent`).
|
|
2848
|
+
|
|
2849
|
+
```typescript
|
|
2850
|
+
function rendersContent(node: Node): boolean
|
|
2851
|
+
```
|
|
2852
|
+
|
|
2853
|
+
- `node` — The node whose children are read.
|
|
2854
|
+
|
|
2855
|
+
**Returns:** True when one of them renders something.
|
|
2856
|
+
|
|
2805
2857
|
#### `setMoleculeContext(config)`
|
|
2806
2858
|
|
|
2807
2859
|
Set all molecule providers in Svelte context.
|
|
@@ -4892,6 +4944,24 @@ function createVersionStores(): {
|
|
|
4892
4944
|
|
|
4893
4945
|
### Constants
|
|
4894
4946
|
|
|
4947
|
+
#### `BodyPortal`
|
|
4948
|
+
|
|
4949
|
+
```typescript
|
|
4950
|
+
const BodyPortal: LegacyComponentType
|
|
4951
|
+
```
|
|
4952
|
+
|
|
4953
|
+
#### `ContentOrElse`
|
|
4954
|
+
|
|
4955
|
+
```typescript
|
|
4956
|
+
const ContentOrElse: LegacyComponentType
|
|
4957
|
+
```
|
|
4958
|
+
|
|
4959
|
+
#### `PresentWhileContent`
|
|
4960
|
+
|
|
4961
|
+
```typescript
|
|
4962
|
+
const PresentWhileContent: LegacyComponentType
|
|
4963
|
+
```
|
|
4964
|
+
|
|
4895
4965
|
#### `Link`
|
|
4896
4966
|
|
|
4897
4967
|
```typescript
|
|
@@ -5112,7 +5182,7 @@ Peer dependencies:
|
|
|
5112
5182
|
- `@molecule/app-ui` ^1.0.1
|
|
5113
5183
|
- `@molecule/app-utilities` ^1.0.1
|
|
5114
5184
|
- `@molecule/app-version` ^1.0.1
|
|
5115
|
-
- `svelte` ^5.
|
|
5185
|
+
- `svelte` ^5.43.15
|
|
5116
5186
|
|
|
5117
5187
|
### Runtime Dependencies
|
|
5118
5188
|
|
|
@@ -5151,6 +5221,10 @@ Peer dependencies:
|
|
|
5151
5221
|
- In-app links are `Link` (`<Link to="/settings">`): it navigates through the router with
|
|
5152
5222
|
react-router `<Link>`'s click rules. A hand-written click handler that calls `navigate()`
|
|
5153
5223
|
breaks ctrl/cmd-click (new tab) unless it repeats those rules.
|
|
5224
|
+
- `BodyPortal` is React's `createPortal(children, document.body)` for Svelte: Svelte itself
|
|
5225
|
+
renders the content in the target (a nested `mount`), so no node is moved after Svelte
|
|
5226
|
+
placed it. Nothing renders on the server; `target` replaces the body and `null` renders
|
|
5227
|
+
nothing until a target exists.
|
|
5154
5228
|
|
|
5155
5229
|
## Translations
|
|
5156
5230
|
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
BodyPortal: renders its children as direct children of `document.body` (or of
|
|
3
|
+
`target`), with no wrapper element — React's `createPortal(children, target)`.
|
|
4
|
+
|
|
5
|
+
Svelte itself places the nodes there: a second `mount()` whose content is this
|
|
6
|
+
component's `children` snippet. No node is ever moved after Svelte placed it, so
|
|
7
|
+
every block range, anchor and keyed item inside the portal stays where Svelte
|
|
8
|
+
put it, and the portal's content is unmounted with this component.
|
|
9
|
+
|
|
10
|
+
Context flows through (`getAllContexts`), the snippet stays reactive, delegated
|
|
11
|
+
events reach the content (the mount registers them on the target) and nothing
|
|
12
|
+
renders on the server, as with React's portals.
|
|
13
|
+
-->
|
|
14
|
+
<script lang="ts">
|
|
15
|
+
import { flushSync, getAllContexts, mount, type Snippet, unmount } from 'svelte'
|
|
16
|
+
|
|
17
|
+
import PortalContent from './PortalContent.svelte'
|
|
18
|
+
|
|
19
|
+
let {
|
|
20
|
+
target,
|
|
21
|
+
children,
|
|
22
|
+
}: {
|
|
23
|
+
/** Where the children render. Defaults to `document.body`; `null` renders nothing yet. */
|
|
24
|
+
target?: Element | null
|
|
25
|
+
/** The portaled content. */
|
|
26
|
+
children?: Snippet
|
|
27
|
+
} = $props()
|
|
28
|
+
|
|
29
|
+
const context = getAllContexts()
|
|
30
|
+
|
|
31
|
+
$effect(() => {
|
|
32
|
+
const parent = target === undefined ? document.body : target
|
|
33
|
+
const content = children
|
|
34
|
+
if (!parent || !content) return
|
|
35
|
+
const instance = mount(PortalContent, { target: parent, props: { children: content }, context })
|
|
36
|
+
// The content's own bindings, actions and effects run now, before the effects of the
|
|
37
|
+
// component that declared the portal: the order inline markup would give them.
|
|
38
|
+
flushSync()
|
|
39
|
+
return () => {
|
|
40
|
+
void unmount(instance)
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
</script>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Snippet } from 'svelte';
|
|
2
|
+
type $$ComponentProps = {
|
|
3
|
+
/** Where the children render. Defaults to `document.body`; `null` renders nothing yet. */
|
|
4
|
+
target?: Element | null;
|
|
5
|
+
/** The portaled content. */
|
|
6
|
+
children?: Snippet;
|
|
7
|
+
};
|
|
8
|
+
declare const BodyPortal: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
9
|
+
type BodyPortal = ReturnType<typeof BodyPortal>;
|
|
10
|
+
export default BodyPortal;
|
|
11
|
+
//# sourceMappingURL=BodyPortal.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BodyPortal.svelte.d.ts","sourceRoot":"","sources":["../../src/components/BodyPortal.svelte"],"names":[],"mappings":"AAGA,OAAO,EAAoC,KAAK,OAAO,EAAW,MAAM,QAAQ,CAAA;AAI/E,KAAK,gBAAgB,GAAI;IACtB,0FAA0F;IAC1F,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI,CAAA;IACvB,4BAA4B;IAC5B,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAC;AA8BJ,QAAA,MAAM,UAAU,sDAAwC,CAAC;AACzD,KAAK,UAAU,GAAG,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;AAChD,eAAe,UAAU,CAAC"}
|
|
@@ -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"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
The root a `BodyPortal` mounts in its target: the portal's children and nothing
|
|
3
|
+
else, so the target receives exactly the nodes the declaring component rendered
|
|
4
|
+
(React's `createPortal(children, target)` renders no wrapper either).
|
|
5
|
+
-->
|
|
6
|
+
<script lang="ts">
|
|
7
|
+
import type { Snippet } from 'svelte'
|
|
8
|
+
|
|
9
|
+
let { children }: { children: Snippet } = $props()
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
{@render children?.()}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
type $$ComponentProps = {
|
|
3
|
+
children: Snippet;
|
|
4
|
+
};
|
|
5
|
+
declare const PortalContent: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
6
|
+
type PortalContent = ReturnType<typeof PortalContent>;
|
|
7
|
+
export default PortalContent;
|
|
8
|
+
//# sourceMappingURL=PortalContent.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PortalContent.svelte.d.ts","sourceRoot":"","sources":["../../src/components/PortalContent.svelte"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAErC,KAAK,gBAAgB,GAAI;IAAE,QAAQ,EAAE,OAAO,CAAA;CAAE,CAAC;AAYhD,QAAA,MAAM,aAAa,sDAAwC,CAAC;AAC5D,KAAK,aAAa,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC;AACtD,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
PresentWhileContent: an element that is in the page only while its content renders something
|
|
3
|
+
(an element, or text) — React's `slot(children) !== undefined ? <div>{children}</div> : null`,
|
|
4
|
+
which a Svelte component cannot decide from a snippet's value (an `{#each}` over no rows, an
|
|
5
|
+
`{#if false}`). `tag` names the element (`div` by default); every other prop is an attribute of
|
|
6
|
+
it.
|
|
7
|
+
|
|
8
|
+
The element is the only node of its own `svelte:element` block. While its content is empty it
|
|
9
|
+
leaves the page and waits just before that block's anchor comment, which Svelte moves with the
|
|
10
|
+
block, so it comes back in its place; a `MutationObserver` on it (which keeps reporting while
|
|
11
|
+
it is out of the page) decides again after every change Svelte makes to the content. The
|
|
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.
|
|
22
|
+
-->
|
|
23
|
+
<script lang="ts">
|
|
24
|
+
import { type Snippet, untrack } from 'svelte'
|
|
25
|
+
import type { Attachment } from 'svelte/attachments'
|
|
26
|
+
import type { HTMLAnchorAttributes, HTMLAttributes } from 'svelte/elements'
|
|
27
|
+
|
|
28
|
+
import { rendersContent } from '../content.js'
|
|
29
|
+
|
|
30
|
+
let {
|
|
31
|
+
tag = 'div',
|
|
32
|
+
children,
|
|
33
|
+
present,
|
|
34
|
+
...attributes
|
|
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
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Keeps the element in the page exactly while its content renders something.
|
|
54
|
+
*
|
|
55
|
+
* @param element - The element.
|
|
56
|
+
* @returns The cleanup.
|
|
57
|
+
*/
|
|
58
|
+
const presentWhileContent: Attachment<HTMLElement> = (element) => {
|
|
59
|
+
// The block's anchor: Svelte inserts the element in front of it and moves the two together.
|
|
60
|
+
const anchor = element.nextSibling
|
|
61
|
+
const sync = (): void => {
|
|
62
|
+
if (untrack(() => present) ?? rendersContent(element)) {
|
|
63
|
+
if (!element.isConnected && anchor?.parentNode)
|
|
64
|
+
anchor.parentNode.insertBefore(element, anchor)
|
|
65
|
+
} else if (element.parentNode) {
|
|
66
|
+
element.remove()
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
sync()
|
|
70
|
+
decide = sync
|
|
71
|
+
const observer = new MutationObserver(sync)
|
|
72
|
+
observer.observe(element, { childList: true, characterData: true, subtree: true })
|
|
73
|
+
return () => {
|
|
74
|
+
observer.disconnect()
|
|
75
|
+
decide = null
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
$effect.pre(() => {
|
|
80
|
+
void present
|
|
81
|
+
untrack(() => decide?.())
|
|
82
|
+
})
|
|
83
|
+
</script>
|
|
84
|
+
|
|
85
|
+
<svelte:element this={tag} {...attributes} {@attach presentWhileContent}
|
|
86
|
+
>{@render children?.()}</svelte:element
|
|
87
|
+
>
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
tag?: string;
|
|
6
|
+
/** The content; the element is in the page only while it renders something. */
|
|
7
|
+
children?: Snippet;
|
|
8
|
+
/** Decides instead of the content when given (see above). */
|
|
9
|
+
present?: boolean;
|
|
10
|
+
};
|
|
11
|
+
declare const PresentWhileContent: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
12
|
+
type PresentWhileContent = ReturnType<typeof PresentWhileContent>;
|
|
13
|
+
export default PresentWhileContent;
|
|
14
|
+
//# sourceMappingURL=PresentWhileContent.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The components that are not tied to one service: `BodyPortal`, React's
|
|
3
|
+
* `createPortal` for `document.body`, as a Svelte component, and
|
|
4
|
+
* `PresentWhileContent`, an element in the page only while its content renders
|
|
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>`).
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
export { default as BodyPortal } from './BodyPortal.svelte';
|
|
12
|
+
export { default as ContentOrElse } from './ContentOrElse.svelte';
|
|
13
|
+
export { default as PresentWhileContent } from './PresentWhileContent.svelte';
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The components that are not tied to one service: `BodyPortal`, React's
|
|
3
|
+
* `createPortal` for `document.body`, as a Svelte component, and
|
|
4
|
+
* `PresentWhileContent`, an element in the page only while its content renders
|
|
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>`).
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
export { default as BodyPortal } from './BodyPortal.svelte';
|
|
12
|
+
export { default as ContentOrElse } from './ContentOrElse.svelte';
|
|
13
|
+
export { default as PresentWhileContent } from './PresentWhileContent.svelte';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rendered DOM content, read by React's `slot(node)` rule.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The children of a node that render something: elements, and text nodes with text. Comments
|
|
8
|
+
* and empty text nodes are Svelte's own anchors (and other components' placeholders), not
|
|
9
|
+
* content.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const nodes = contentNodes(titleEl)
|
|
14
|
+
* const text = nodes.length === 1 && nodes[0].nodeType === 3 ? nodes[0].textContent : ''
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @param node - The node whose children are read.
|
|
18
|
+
* @returns Those children, in order.
|
|
19
|
+
*/
|
|
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;
|
|
34
|
+
/**
|
|
35
|
+
* Whether a node's children render anything (see {@link contentNodes}). A Svelte component
|
|
36
|
+
* cannot tell from a snippet's value whether it renders anything (`{#each}` over no rows,
|
|
37
|
+
* `{#if false}`), so a component that renders a wrapper only while its content has something to
|
|
38
|
+
* show, as React's `slot(children) !== undefined && <div>…</div>` does, asks the rendered DOM
|
|
39
|
+
* instead (see `PresentWhileContent`).
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* rendersContent(panel) // false while the panel's snippet renders an empty list
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @param node - The node whose children are read.
|
|
47
|
+
* @returns True when one of them renders something.
|
|
48
|
+
*/
|
|
49
|
+
export declare function rendersContent(node: Node): boolean;
|
|
50
|
+
//# sourceMappingURL=content.d.ts.map
|
|
@@ -0,0 +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,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
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rendered DOM content, read by React's `slot(node)` rule.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The children of a node that render something: elements, and text nodes with text. Comments
|
|
8
|
+
* and empty text nodes are Svelte's own anchors (and other components' placeholders), not
|
|
9
|
+
* content.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const nodes = contentNodes(titleEl)
|
|
14
|
+
* const text = nodes.length === 1 && nodes[0].nodeType === 3 ? nodes[0].textContent : ''
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @param node - The node whose children are read.
|
|
18
|
+
* @returns Those children, in order.
|
|
19
|
+
*/
|
|
20
|
+
export function contentNodes(node) {
|
|
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 ?? '') !== '');
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether a node's children render anything (see {@link contentNodes}). A Svelte component
|
|
40
|
+
* cannot tell from a snippet's value whether it renders anything (`{#each}` over no rows,
|
|
41
|
+
* `{#if false}`), so a component that renders a wrapper only while its content has something to
|
|
42
|
+
* show, as React's `slot(children) !== undefined && <div>…</div>` does, asks the rendered DOM
|
|
43
|
+
* instead (see `PresentWhileContent`).
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* rendersContent(panel) // false while the panel's snippet renders an empty list
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @param node - The node whose children are read.
|
|
51
|
+
* @returns True when one of them renders something.
|
|
52
|
+
*/
|
|
53
|
+
export function rendersContent(node) {
|
|
54
|
+
return contentNodes(node).length > 0;
|
|
55
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -119,6 +119,16 @@
|
|
|
119
119
|
* >{$favorite.count}</button>
|
|
120
120
|
* ```
|
|
121
121
|
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```svelte
|
|
124
|
+
* <!-- PortaledNode.svelte — content in document.body, exactly as React renders it -->
|
|
125
|
+
* <script lang="ts">
|
|
126
|
+
* import { BodyPortal } from '@molecule/app-svelte'
|
|
127
|
+
* </script>
|
|
128
|
+
*
|
|
129
|
+
* <BodyPortal><div role="dialog">…</div></BodyPortal>
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
122
132
|
* Session state is a three-way decision, not a boolean: `status === 'loading'`
|
|
123
133
|
* is undecided, so wait for it and redirect only on
|
|
124
134
|
* `status === 'unauthenticated'`; `'unavailable'` keeps the session candidate
|
|
@@ -138,10 +148,16 @@
|
|
|
138
148
|
* - In-app links are `Link` (`<Link to="/settings">`): it navigates through the router with
|
|
139
149
|
* react-router `<Link>`'s click rules. A hand-written click handler that calls `navigate()`
|
|
140
150
|
* breaks ctrl/cmd-click (new tab) unless it repeats those rules.
|
|
151
|
+
* - `BodyPortal` is React's `createPortal(children, document.body)` for Svelte: Svelte itself
|
|
152
|
+
* renders the content in the target (a nested `mount`), so no node is moved after Svelte
|
|
153
|
+
* placed it. Nothing renders on the server; `target` replaces the body and `null` renders
|
|
154
|
+
* nothing until a target exists.
|
|
141
155
|
*
|
|
142
156
|
* @module
|
|
143
157
|
*/
|
|
144
158
|
export * from './agent-identity.js';
|
|
159
|
+
export * from './components/index.js';
|
|
160
|
+
export * from './content.js';
|
|
145
161
|
export * from './context.js';
|
|
146
162
|
export * from './hooks/index.js';
|
|
147
163
|
export * from './link/index.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4JG;AAEH,cAAc,qBAAqB,CAAA;AACnC,cAAc,uBAAuB,CAAA;AACrC,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA;AACpC,cAAc,wBAAwB,CAAA;AACtC,cAAc,0BAA0B,CAAA;AACxC,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -119,6 +119,16 @@
|
|
|
119
119
|
* >{$favorite.count}</button>
|
|
120
120
|
* ```
|
|
121
121
|
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```svelte
|
|
124
|
+
* <!-- PortaledNode.svelte — content in document.body, exactly as React renders it -->
|
|
125
|
+
* <script lang="ts">
|
|
126
|
+
* import { BodyPortal } from '@molecule/app-svelte'
|
|
127
|
+
* </script>
|
|
128
|
+
*
|
|
129
|
+
* <BodyPortal><div role="dialog">…</div></BodyPortal>
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
122
132
|
* Session state is a three-way decision, not a boolean: `status === 'loading'`
|
|
123
133
|
* is undecided, so wait for it and redirect only on
|
|
124
134
|
* `status === 'unauthenticated'`; `'unavailable'` keeps the session candidate
|
|
@@ -138,10 +148,16 @@
|
|
|
138
148
|
* - In-app links are `Link` (`<Link to="/settings">`): it navigates through the router with
|
|
139
149
|
* react-router `<Link>`'s click rules. A hand-written click handler that calls `navigate()`
|
|
140
150
|
* breaks ctrl/cmd-click (new tab) unless it repeats those rules.
|
|
151
|
+
* - `BodyPortal` is React's `createPortal(children, document.body)` for Svelte: Svelte itself
|
|
152
|
+
* renders the content in the target (a nested `mount`), so no node is moved after Svelte
|
|
153
|
+
* placed it. Nothing renders on the server; `target` replaces the body and `null` renders
|
|
154
|
+
* nothing until a target exists.
|
|
141
155
|
*
|
|
142
156
|
* @module
|
|
143
157
|
*/
|
|
144
158
|
export * from './agent-identity.js';
|
|
159
|
+
export * from './components/index.js';
|
|
160
|
+
export * from './content.js';
|
|
145
161
|
export * from './context.js';
|
|
146
162
|
export * from './hooks/index.js';
|
|
147
163
|
export * from './link/index.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/app-svelte",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.14.1",
|
|
4
4
|
"description": "Svelte framework bindings for molecule.dev",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"@molecule/app-ui": "^1.0.1",
|
|
60
60
|
"@molecule/app-utilities": "^1.0.1",
|
|
61
61
|
"@molecule/app-version": "^1.0.1",
|
|
62
|
-
"svelte": "^5.
|
|
62
|
+
"svelte": "^5.43.15"
|
|
63
63
|
},
|
|
64
64
|
"peerDependenciesMeta": {
|
|
65
65
|
"@molecule/app-code-editor": {
|