@tschk/moonshine-svelte 0.4.49 → 0.4.80
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 +46 -8
- package/package.json +17 -5
- package/src/abort-signal.ts +60 -0
- package/src/animate.ts +71 -0
- package/src/attachments.ts +93 -0
- package/src/derived.ts +35 -5
- package/src/easing.ts +176 -0
- package/src/effect.ts +17 -4
- package/src/events.ts +76 -0
- package/src/kit.ts +1973 -0
- package/src/legacy.ts +361 -0
- package/src/motion.ts +504 -0
- package/src/navigation.ts +1134 -0
- package/src/pre.ts +18 -4
- package/src/prop-id.ts +43 -0
- package/src/reactivity-window.ts +173 -0
- package/src/reactivity.ts +983 -0
- package/src/runes.ts +1 -0
- package/src/server.ts +56 -0
- package/src/ssr.ts +34 -3
- package/src/svelte.ts +30 -1
- package/src/tick-queue.ts +3 -0
- package/src/transition.ts +348 -0
package/src/runes.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { createState, stateSnapshot } from "./state";
|
|
|
3
3
|
export { createRuneEffect, createRuneRoot, runeTracking } from "./effect";
|
|
4
4
|
export { createDerived } from "./derived";
|
|
5
5
|
export { createProp } from "./prop";
|
|
6
|
+
export { propsId } from "./prop-id";
|
|
6
7
|
export { createRawState } from "./raw-state";
|
|
7
8
|
export { spreadProps } from "./spread-props";
|
|
8
9
|
export {
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** Independent Svelte 5 `svelte/server` `render(component, options)` runtime using Moonshine's native SSR & head collector. */
|
|
2
|
+
import { createComponent } from "@tschk/moonshine-dom/component";
|
|
3
|
+
import { renderWithHead } from "@tschk/moonshine-dom/head-ssr";
|
|
4
|
+
import { getOwner } from "@tschk/moonshine-dom/reactive";
|
|
5
|
+
import type { SSRChild } from "@tschk/moonshine-dom/ssr";
|
|
6
|
+
import { setRootIdPrefix } from "./prop-id";
|
|
7
|
+
|
|
8
|
+
export interface RenderOptions<
|
|
9
|
+
Props extends Record<string, any> = Record<string, any>,
|
|
10
|
+
> {
|
|
11
|
+
props?: Props;
|
|
12
|
+
context?: Map<any, any>;
|
|
13
|
+
idPrefix?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface RenderOutput {
|
|
17
|
+
body: string;
|
|
18
|
+
head: string;
|
|
19
|
+
html: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Renders a native Svelte server component to `{ body, head, html }`,
|
|
24
|
+
* supporting `props`, `context` (`getContext`/`setContext`), `idPrefix` (`$props.id()`),
|
|
25
|
+
* `<svelte:head>` / `<title>`, and server `onDestroy` hooks.
|
|
26
|
+
*/
|
|
27
|
+
export function render<Props extends Record<string, any> = Record<string, any>>(
|
|
28
|
+
component: (props: Props) => SSRChild,
|
|
29
|
+
options?: RenderOptions<Props>,
|
|
30
|
+
): RenderOutput {
|
|
31
|
+
if (typeof component !== "function") {
|
|
32
|
+
throw new TypeError("svelte/server render() requires a component function");
|
|
33
|
+
}
|
|
34
|
+
if (options?.context !== undefined && !(options.context instanceof Map)) {
|
|
35
|
+
throw new TypeError("svelte/server render() context must be a Map");
|
|
36
|
+
}
|
|
37
|
+
if (options?.idPrefix !== undefined && typeof options.idPrefix !== "string") {
|
|
38
|
+
throw new TypeError("svelte/server render() idPrefix must be a string");
|
|
39
|
+
}
|
|
40
|
+
const props = (options?.props ?? {}) as Props;
|
|
41
|
+
const { body, head } = renderWithHead(() => {
|
|
42
|
+
const owner = getOwner();
|
|
43
|
+
if (owner && options?.idPrefix) {
|
|
44
|
+
setRootIdPrefix(owner, options.idPrefix);
|
|
45
|
+
}
|
|
46
|
+
return createComponent(component, props, "ssr", {
|
|
47
|
+
contexts: options?.context,
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
body,
|
|
53
|
+
head,
|
|
54
|
+
html: body,
|
|
55
|
+
};
|
|
56
|
+
}
|
package/src/ssr.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/** Compiler-facing server bindings; use explicit native SSR factories. */
|
|
2
2
|
export { createServerProp } from "./server-prop";
|
|
3
|
-
export {
|
|
3
|
+
export { propsId } from "./prop-id";
|
|
4
|
+
export { getAbortSignal } from "./abort-signal";
|
|
5
|
+
export { afterRender, settle, untrack } from "@tschk/moonshine-dom/ssr";
|
|
4
6
|
export { spreadServerProps } from "./spread-props";
|
|
5
7
|
export {
|
|
6
8
|
spreadServerEntries,
|
|
@@ -14,10 +16,26 @@ export {
|
|
|
14
16
|
|
|
15
17
|
import { isSSRRendering } from "@tschk/moonshine-dom/ssr-owner";
|
|
16
18
|
import { peekComponentContext } from "@tschk/moonshine-dom/component";
|
|
19
|
+
import {
|
|
20
|
+
createAbortContext,
|
|
21
|
+
withAbortContext,
|
|
22
|
+
type AbortContext,
|
|
23
|
+
} from "./abort-signal";
|
|
17
24
|
|
|
18
25
|
/** Server tick resolves without flushing another renderer's pending work. */
|
|
19
26
|
export async function tick(): Promise<void> {}
|
|
20
27
|
|
|
28
|
+
/** Server settled resolves without flushing another renderer's pending work. */
|
|
29
|
+
export async function settled(): Promise<void> {}
|
|
30
|
+
|
|
31
|
+
/** Server flushSync invokes its optional callback synchronously. */
|
|
32
|
+
export function flushSync<T = void>(fn?: () => T): T {
|
|
33
|
+
if (fn !== undefined && typeof fn !== "function") {
|
|
34
|
+
throw new TypeError("flushSync callback must be a function");
|
|
35
|
+
}
|
|
36
|
+
return fn ? fn() : (undefined as T);
|
|
37
|
+
}
|
|
38
|
+
|
|
21
39
|
/** Server state is a plain value box; function values are never invoked. */
|
|
22
40
|
export function createServerState<T>(value: T): { value: T } {
|
|
23
41
|
return { value };
|
|
@@ -30,17 +48,30 @@ export function createServerDerived<T>(
|
|
|
30
48
|
): { value: T } {
|
|
31
49
|
let ready = false;
|
|
32
50
|
let cached!: T, updated: T | undefined;
|
|
51
|
+
let abortCtx: AbortContext | undefined;
|
|
52
|
+
const evaluate = (): T => {
|
|
53
|
+
abortCtx?.abort();
|
|
54
|
+
const runAbortCtx = createAbortContext();
|
|
55
|
+
abortCtx = runAbortCtx;
|
|
56
|
+
try {
|
|
57
|
+
return withAbortContext(runAbortCtx, compute);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
runAbortCtx.abort();
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
33
63
|
return {
|
|
34
64
|
get value() {
|
|
35
65
|
if (updated != null) return updated;
|
|
36
|
-
if (!cache) return
|
|
66
|
+
if (!cache) return evaluate();
|
|
37
67
|
if (!ready) {
|
|
38
|
-
cached =
|
|
68
|
+
cached = evaluate();
|
|
39
69
|
ready = true;
|
|
40
70
|
}
|
|
41
71
|
return cached;
|
|
42
72
|
},
|
|
43
73
|
set value(value: T) {
|
|
74
|
+
abortCtx?.abort();
|
|
44
75
|
updated = value;
|
|
45
76
|
},
|
|
46
77
|
};
|
package/src/svelte.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
hasContext as nativeHasContext,
|
|
14
14
|
} from "@tschk/moonshine-dom/component";
|
|
15
15
|
import { createRuneEffect } from "./effect";
|
|
16
|
-
import { drainTickWork } from "./tick-queue";
|
|
16
|
+
import { drainTickWork, hasPendingTickWork } from "./tick-queue";
|
|
17
17
|
import { withHistoryCleanup } from "./history";
|
|
18
18
|
|
|
19
19
|
/** Source snippet parameters; compiled render cells are private to Crepus. */
|
|
@@ -29,6 +29,15 @@ import {
|
|
|
29
29
|
runWithOwner,
|
|
30
30
|
untrack,
|
|
31
31
|
} from "@tschk/moonshine-dom/reactive";
|
|
32
|
+
export { untrack };
|
|
33
|
+
export { getAbortSignal } from "./abort-signal";
|
|
34
|
+
export { mount, unmount } from "./mount";
|
|
35
|
+
export type {
|
|
36
|
+
ComponentProps,
|
|
37
|
+
ComponentExports,
|
|
38
|
+
MountOptions,
|
|
39
|
+
UnmountOptions,
|
|
40
|
+
} from "./mount-types";
|
|
32
41
|
|
|
33
42
|
function componentOwner(hook: string) {
|
|
34
43
|
assertNotCleanupComputation();
|
|
@@ -76,6 +85,26 @@ export async function tick(): Promise<void> {
|
|
|
76
85
|
drainTickWork();
|
|
77
86
|
}
|
|
78
87
|
|
|
88
|
+
/** Synchronously run an optional callback and drain queued native rune work. */
|
|
89
|
+
export function flushSync<T = void>(fn?: () => T): T {
|
|
90
|
+
if (fn !== undefined && typeof fn !== "function") {
|
|
91
|
+
throw new TypeError("flushSync callback must be a function");
|
|
92
|
+
}
|
|
93
|
+
const result = fn ? fn() : (undefined as T);
|
|
94
|
+
drainTickWork();
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Wait until all queued native rune work (including chained effect ticks) settles. */
|
|
99
|
+
export async function settled(): Promise<void> {
|
|
100
|
+
await Promise.resolve();
|
|
101
|
+
drainTickWork();
|
|
102
|
+
while (hasPendingTickWork()) {
|
|
103
|
+
await Promise.resolve();
|
|
104
|
+
drainTickWork();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
79
108
|
export type EventDispatcher<EventMap extends Record<string, any>> = <
|
|
80
109
|
Type extends keyof EventMap,
|
|
81
110
|
>(
|
package/src/tick-queue.ts
CHANGED
|
@@ -7,6 +7,9 @@ export function queueTickWork(run: () => void, cancel: () => void): void {
|
|
|
7
7
|
export function removeTickWork(run: () => void): void {
|
|
8
8
|
pending.delete(run);
|
|
9
9
|
}
|
|
10
|
+
export function hasPendingTickWork(): boolean {
|
|
11
|
+
return pending.size > 0;
|
|
12
|
+
}
|
|
10
13
|
export function drainTickWork(): void {
|
|
11
14
|
const counts = new Map<() => void, number>();
|
|
12
15
|
let failed = false,
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
/** Independent Svelte 5 `svelte/transition` helpers (`fade`, `blur`, `fly`, `slide`, `scale`, `draw`, `crossfade`). */
|
|
2
|
+
import { cubicInOut, cubicOut, linear } from "./easing";
|
|
3
|
+
|
|
4
|
+
export type EasingFunction = (t: number) => number;
|
|
5
|
+
|
|
6
|
+
export interface TransitionConfig {
|
|
7
|
+
delay?: number;
|
|
8
|
+
duration?: number;
|
|
9
|
+
easing?: EasingFunction;
|
|
10
|
+
css?: (t: number, u: number) => string;
|
|
11
|
+
tick?: (t: number, u: number) => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface BlurParams {
|
|
15
|
+
delay?: number;
|
|
16
|
+
duration?: number;
|
|
17
|
+
easing?: EasingFunction;
|
|
18
|
+
amount?: number | string;
|
|
19
|
+
opacity?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface FadeParams {
|
|
23
|
+
delay?: number;
|
|
24
|
+
duration?: number;
|
|
25
|
+
easing?: EasingFunction;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface FlyParams {
|
|
29
|
+
delay?: number;
|
|
30
|
+
duration?: number;
|
|
31
|
+
easing?: EasingFunction;
|
|
32
|
+
x?: number | string;
|
|
33
|
+
y?: number | string;
|
|
34
|
+
opacity?: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SlideParams {
|
|
38
|
+
delay?: number;
|
|
39
|
+
duration?: number;
|
|
40
|
+
easing?: EasingFunction;
|
|
41
|
+
axis?: "x" | "y";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ScaleParams {
|
|
45
|
+
delay?: number;
|
|
46
|
+
duration?: number;
|
|
47
|
+
easing?: EasingFunction;
|
|
48
|
+
start?: number;
|
|
49
|
+
opacity?: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface DrawParams {
|
|
53
|
+
delay?: number;
|
|
54
|
+
speed?: number;
|
|
55
|
+
duration?: number | ((len: number) => number);
|
|
56
|
+
easing?: EasingFunction;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface CrossfadeParams {
|
|
60
|
+
delay?: number;
|
|
61
|
+
duration?: number | ((len: number) => number);
|
|
62
|
+
easing?: EasingFunction;
|
|
63
|
+
fallback?: (
|
|
64
|
+
node: Element,
|
|
65
|
+
params: CrossfadeParams,
|
|
66
|
+
intro: boolean,
|
|
67
|
+
) => TransitionConfig;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function splitCssUnit(value: number | string): [number, string] {
|
|
71
|
+
const split =
|
|
72
|
+
typeof value === "string" && value.match(/^\s*(-?[\d.]+)([^\s]*)\s*$/);
|
|
73
|
+
return split
|
|
74
|
+
? [Number.parseFloat(split[1]!), split[2]! || "px"]
|
|
75
|
+
: [Number(value), "px"];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function getElementStyle(
|
|
79
|
+
node: Element,
|
|
80
|
+
): CSSStyleDeclaration | Record<string, string> {
|
|
81
|
+
if (typeof getComputedStyle === "function") {
|
|
82
|
+
try {
|
|
83
|
+
return getComputedStyle(node);
|
|
84
|
+
} catch {
|
|
85
|
+
// Fallback for synthetic/detached elements in test environments
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return (node as HTMLElement).style ?? {};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function fade(
|
|
92
|
+
node: Element,
|
|
93
|
+
{ delay = 0, duration = 400, easing = linear }: FadeParams = {},
|
|
94
|
+
): TransitionConfig {
|
|
95
|
+
const style = getElementStyle(node);
|
|
96
|
+
const o = Number(style.opacity || 1);
|
|
97
|
+
return {
|
|
98
|
+
delay,
|
|
99
|
+
duration,
|
|
100
|
+
easing,
|
|
101
|
+
css: (t) => `opacity: ${t * o}`,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function blur(
|
|
106
|
+
node: Element,
|
|
107
|
+
{
|
|
108
|
+
delay = 0,
|
|
109
|
+
duration = 400,
|
|
110
|
+
easing = cubicInOut,
|
|
111
|
+
amount = 5,
|
|
112
|
+
opacity = 0,
|
|
113
|
+
}: BlurParams = {},
|
|
114
|
+
): TransitionConfig {
|
|
115
|
+
const style = getElementStyle(node);
|
|
116
|
+
const targetOpacity = Number(style.opacity || 1);
|
|
117
|
+
const f = style.filter === "none" || !style.filter ? "" : `${style.filter} `;
|
|
118
|
+
const od = targetOpacity * (1 - opacity);
|
|
119
|
+
const [value, unit] = splitCssUnit(amount);
|
|
120
|
+
return {
|
|
121
|
+
delay,
|
|
122
|
+
duration,
|
|
123
|
+
easing,
|
|
124
|
+
css: (_t, u) =>
|
|
125
|
+
`opacity: ${targetOpacity - od * u}; filter: ${f}blur(${u * value}${unit});`,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function fly(
|
|
130
|
+
node: Element,
|
|
131
|
+
{
|
|
132
|
+
delay = 0,
|
|
133
|
+
duration = 400,
|
|
134
|
+
easing = cubicOut,
|
|
135
|
+
x = 0,
|
|
136
|
+
y = 0,
|
|
137
|
+
opacity = 0,
|
|
138
|
+
}: FlyParams = {},
|
|
139
|
+
): TransitionConfig {
|
|
140
|
+
const style = getElementStyle(node);
|
|
141
|
+
const targetOpacity = Number(style.opacity || 1);
|
|
142
|
+
const transform =
|
|
143
|
+
style.transform === "none" || !style.transform ? "" : `${style.transform} `;
|
|
144
|
+
const od = targetOpacity * (1 - opacity);
|
|
145
|
+
const [xValue, xUnit] = splitCssUnit(x);
|
|
146
|
+
const [yValue, yUnit] = splitCssUnit(y);
|
|
147
|
+
return {
|
|
148
|
+
delay,
|
|
149
|
+
duration,
|
|
150
|
+
easing,
|
|
151
|
+
css: (t, u) =>
|
|
152
|
+
`
|
|
153
|
+
transform: ${transform}translate(${(1 - t) * xValue}${xUnit}, ${(1 - t) * yValue}${yUnit});
|
|
154
|
+
opacity: ${targetOpacity - od * u}
|
|
155
|
+
`.trim(),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function slide(
|
|
160
|
+
node: Element,
|
|
161
|
+
{
|
|
162
|
+
delay = 0,
|
|
163
|
+
duration = 400,
|
|
164
|
+
easing = cubicOut,
|
|
165
|
+
axis = "y",
|
|
166
|
+
}: SlideParams = {},
|
|
167
|
+
): TransitionConfig {
|
|
168
|
+
const style = getElementStyle(node);
|
|
169
|
+
const opacity = Number(style.opacity || 1);
|
|
170
|
+
const primaryProperty = axis === "y" ? "height" : "width";
|
|
171
|
+
const primaryValue = Number.parseFloat(style[primaryProperty] || "0") || 0;
|
|
172
|
+
const secondaryProperties =
|
|
173
|
+
axis === "y" ? ["top", "bottom"] : ["left", "right"];
|
|
174
|
+
const capitalizedSecondary = secondaryProperties.map(
|
|
175
|
+
(e) =>
|
|
176
|
+
`${e[0]!.toUpperCase()}${e.slice(1)}` as
|
|
177
|
+
"Top" | "Bottom" | "Left" | "Right",
|
|
178
|
+
);
|
|
179
|
+
const paddingStartValue =
|
|
180
|
+
Number.parseFloat(style[`padding${capitalizedSecondary[0]}`] || "0") || 0;
|
|
181
|
+
const paddingEndValue =
|
|
182
|
+
Number.parseFloat(style[`padding${capitalizedSecondary[1]}`] || "0") || 0;
|
|
183
|
+
const marginStartValue =
|
|
184
|
+
Number.parseFloat(style[`margin${capitalizedSecondary[0]}`] || "0") || 0;
|
|
185
|
+
const marginEndValue =
|
|
186
|
+
Number.parseFloat(style[`margin${capitalizedSecondary[1]}`] || "0") || 0;
|
|
187
|
+
const borderWidthStartValue =
|
|
188
|
+
Number.parseFloat(style[`border${capitalizedSecondary[0]}Width`] || "0") ||
|
|
189
|
+
0;
|
|
190
|
+
const borderWidthEndValue =
|
|
191
|
+
Number.parseFloat(style[`border${capitalizedSecondary[1]}Width`] || "0") ||
|
|
192
|
+
0;
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
delay,
|
|
196
|
+
duration,
|
|
197
|
+
easing,
|
|
198
|
+
css: (t) =>
|
|
199
|
+
`overflow: hidden; opacity: ${Math.min(t * 20, 1) * opacity}; ${primaryProperty}: ${t * primaryValue}px; padding-${secondaryProperties[0]}: ${t * paddingStartValue}px; padding-${secondaryProperties[1]}: ${t * paddingEndValue}px; margin-${secondaryProperties[0]}: ${t * marginStartValue}px; margin-${secondaryProperties[1]}: ${t * marginEndValue}px; border-${secondaryProperties[0]}-width: ${t * borderWidthStartValue}px; border-${secondaryProperties[1]}-width: ${t * borderWidthEndValue}px; min-${primaryProperty}: 0`,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function scale(
|
|
204
|
+
node: Element,
|
|
205
|
+
{
|
|
206
|
+
delay = 0,
|
|
207
|
+
duration = 400,
|
|
208
|
+
easing = cubicOut,
|
|
209
|
+
start = 0,
|
|
210
|
+
opacity = 0,
|
|
211
|
+
}: ScaleParams = {},
|
|
212
|
+
): TransitionConfig {
|
|
213
|
+
const style = getElementStyle(node);
|
|
214
|
+
const targetOpacity = Number(style.opacity || 1);
|
|
215
|
+
const transform =
|
|
216
|
+
style.transform === "none" || !style.transform ? "" : `${style.transform} `;
|
|
217
|
+
const sd = 1 - start;
|
|
218
|
+
const od = targetOpacity * (1 - opacity);
|
|
219
|
+
return {
|
|
220
|
+
delay,
|
|
221
|
+
duration,
|
|
222
|
+
easing,
|
|
223
|
+
css: (_t, u) =>
|
|
224
|
+
`
|
|
225
|
+
transform: ${transform}scale(${1 - sd * u});
|
|
226
|
+
opacity: ${targetOpacity - od * u}
|
|
227
|
+
`.trim(),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function draw(
|
|
232
|
+
node: SVGElement & { getTotalLength(): number },
|
|
233
|
+
{ delay = 0, speed, duration, easing = cubicInOut }: DrawParams = {},
|
|
234
|
+
): TransitionConfig {
|
|
235
|
+
let len = node.getTotalLength();
|
|
236
|
+
const style = getElementStyle(node);
|
|
237
|
+
if (style.strokeLinecap !== "butt") {
|
|
238
|
+
len += Number.parseInt(style.strokeWidth || "0", 10) || 0;
|
|
239
|
+
}
|
|
240
|
+
let resolvedDuration: number;
|
|
241
|
+
if (duration === undefined) {
|
|
242
|
+
if (speed === undefined) {
|
|
243
|
+
resolvedDuration = 800;
|
|
244
|
+
} else {
|
|
245
|
+
resolvedDuration = len / speed;
|
|
246
|
+
}
|
|
247
|
+
} else if (typeof duration === "function") {
|
|
248
|
+
resolvedDuration = duration(len);
|
|
249
|
+
} else {
|
|
250
|
+
resolvedDuration = duration;
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
delay,
|
|
254
|
+
duration: resolvedDuration,
|
|
255
|
+
easing,
|
|
256
|
+
css: (_t, u) =>
|
|
257
|
+
`
|
|
258
|
+
stroke-dasharray: ${len};
|
|
259
|
+
stroke-dashoffset: ${u * len};
|
|
260
|
+
`.trim(),
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function crossfade({ fallback, ...defaults }: CrossfadeParams = {}): [
|
|
265
|
+
(
|
|
266
|
+
node: Element,
|
|
267
|
+
params: CrossfadeParams & { key: any },
|
|
268
|
+
) => () => TransitionConfig,
|
|
269
|
+
(
|
|
270
|
+
node: Element,
|
|
271
|
+
params: CrossfadeParams & { key: any },
|
|
272
|
+
) => () => TransitionConfig,
|
|
273
|
+
] {
|
|
274
|
+
const toReceive = new Map<any, Element>();
|
|
275
|
+
const toSend = new Map<any, Element>();
|
|
276
|
+
|
|
277
|
+
function getCrossfade(
|
|
278
|
+
fromNode: Element,
|
|
279
|
+
node: Element,
|
|
280
|
+
params: CrossfadeParams,
|
|
281
|
+
): TransitionConfig {
|
|
282
|
+
const {
|
|
283
|
+
delay = 0,
|
|
284
|
+
duration = (d: number) => Math.sqrt(d) * 30,
|
|
285
|
+
easing = cubicOut,
|
|
286
|
+
} = { ...defaults, ...params };
|
|
287
|
+
|
|
288
|
+
const from =
|
|
289
|
+
typeof fromNode.getBoundingClientRect === "function"
|
|
290
|
+
? fromNode.getBoundingClientRect()
|
|
291
|
+
: { left: 0, top: 0, width: 0, height: 0 };
|
|
292
|
+
const to =
|
|
293
|
+
typeof node.getBoundingClientRect === "function"
|
|
294
|
+
? node.getBoundingClientRect()
|
|
295
|
+
: { left: 0, top: 0, width: 0, height: 0 };
|
|
296
|
+
const dx = from.left - to.left;
|
|
297
|
+
const dy = from.top - to.top;
|
|
298
|
+
const dw = to.width ? from.width / to.width : 1;
|
|
299
|
+
const dh = to.height ? from.height / to.height : 1;
|
|
300
|
+
const d = Math.hypot(dx, dy);
|
|
301
|
+
|
|
302
|
+
const style = getElementStyle(node);
|
|
303
|
+
const transform =
|
|
304
|
+
style.transform === "none" || !style.transform
|
|
305
|
+
? ""
|
|
306
|
+
: `${style.transform} `;
|
|
307
|
+
const opacity = Number(style.opacity || 1);
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
delay,
|
|
311
|
+
duration: typeof duration === "function" ? duration(d) : duration,
|
|
312
|
+
easing,
|
|
313
|
+
css: (t, u) =>
|
|
314
|
+
`
|
|
315
|
+
opacity: ${t * opacity};
|
|
316
|
+
transform-origin: top left;
|
|
317
|
+
transform: ${transform}translate(${u * dx}px,${u * dy}px) scale(${t + (1 - t) * dw}, ${t + (1 - t) * dh});
|
|
318
|
+
`.trim(),
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function transition(
|
|
323
|
+
items: Map<any, Element>,
|
|
324
|
+
counterparts: Map<any, Element>,
|
|
325
|
+
intro: boolean,
|
|
326
|
+
) {
|
|
327
|
+
return (node: Element, params: CrossfadeParams & { key: any }) => {
|
|
328
|
+
items.set(params.key, node);
|
|
329
|
+
return (): TransitionConfig => {
|
|
330
|
+
if (counterparts.has(params.key)) {
|
|
331
|
+
const otherNode = counterparts.get(params.key)!;
|
|
332
|
+
counterparts.delete(params.key);
|
|
333
|
+
return getCrossfade(otherNode, node, params);
|
|
334
|
+
}
|
|
335
|
+
items.delete(params.key);
|
|
336
|
+
if (fallback) {
|
|
337
|
+
return fallback(node, params, intro);
|
|
338
|
+
}
|
|
339
|
+
return {};
|
|
340
|
+
};
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return [
|
|
345
|
+
transition(toSend, toReceive, false),
|
|
346
|
+
transition(toReceive, toSend, true),
|
|
347
|
+
];
|
|
348
|
+
}
|