@tschk/moonshine-svelte 0.4.4 → 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 +171 -12
- 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/binding.ts +3 -2
- package/src/class.ts +57 -0
- package/src/derived.ts +35 -5
- package/src/easing.ts +176 -0
- package/src/effect.ts +25 -5
- 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 +98 -6
- package/src/prop-id.ts +43 -0
- package/src/reactivity-window.ts +173 -0
- package/src/reactivity.ts +983 -0
- package/src/runes.ts +5 -0
- package/src/server.ts +56 -0
- package/src/spread-element-dom.ts +343 -0
- package/src/spread-element-server.ts +106 -0
- package/src/spread-select.ts +86 -0
- package/src/ssr.ts +38 -3
- package/src/store-demand.ts +18 -0
- package/src/store-interop.ts +145 -0
- package/src/store.ts +7 -0
- package/src/svelte.ts +30 -1
- package/src/tick-queue.ts +3 -0
- package/src/transition.ts +348 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { createSignal, untrack } from "@tschk/moonshine";
|
|
2
|
+
import {
|
|
3
|
+
collectDeps,
|
|
4
|
+
hasValueDemand,
|
|
5
|
+
isTracking,
|
|
6
|
+
} from "@tschk/moonshine/tracking";
|
|
7
|
+
import { peekComponentContext } from "@tschk/moonshine-dom/component";
|
|
8
|
+
import { getOwner, onCleanup, type Owner } from "@tschk/moonshine-dom/reactive";
|
|
9
|
+
import { isSSRRendering } from "@tschk/moonshine-dom/ssr-owner";
|
|
10
|
+
import {
|
|
11
|
+
recordHistory,
|
|
12
|
+
trackHistoryRead,
|
|
13
|
+
withHistoryMutation,
|
|
14
|
+
} from "./history";
|
|
15
|
+
import { captureRuneToStoreScope, createRuneToStoreEffect } from "./pre";
|
|
16
|
+
import {
|
|
17
|
+
get as getStoreValue,
|
|
18
|
+
writable,
|
|
19
|
+
type Readable,
|
|
20
|
+
type Unsubscriber,
|
|
21
|
+
type Writable,
|
|
22
|
+
} from "./store";
|
|
23
|
+
import {
|
|
24
|
+
notifyTrackableUnsubscribe,
|
|
25
|
+
registerDemandCheck,
|
|
26
|
+
} from "./store-demand";
|
|
27
|
+
import { subscribeTo } from "./store-internal";
|
|
28
|
+
|
|
29
|
+
const trackedOwners = /* @__PURE__ */ new WeakSet<Owner>();
|
|
30
|
+
|
|
31
|
+
export function toStore<V>(get: () => V, set: (v: V) => void): Writable<V>;
|
|
32
|
+
export function toStore<V>(get: () => V): Readable<V>;
|
|
33
|
+
export function toStore<V>(
|
|
34
|
+
get: () => V,
|
|
35
|
+
set?: (v: V) => void,
|
|
36
|
+
): Writable<V> | Readable<V> {
|
|
37
|
+
const captured = captureRuneToStoreScope();
|
|
38
|
+
const server = isSSRRendering() || peekComponentContext()?.mode === "ssr";
|
|
39
|
+
const initValue = get();
|
|
40
|
+
const store = server
|
|
41
|
+
? writable(initValue)
|
|
42
|
+
: writable(initValue, (setStore) => {
|
|
43
|
+
if (isSSRRendering() || peekComponentContext()?.mode === "ssr") return;
|
|
44
|
+
let ran = initValue !== get();
|
|
45
|
+
const teardown = createRuneToStoreEffect(captured, () => {
|
|
46
|
+
const value = get();
|
|
47
|
+
if (ran) setStore(value);
|
|
48
|
+
});
|
|
49
|
+
ran = true;
|
|
50
|
+
return teardown;
|
|
51
|
+
});
|
|
52
|
+
if (set) {
|
|
53
|
+
return {
|
|
54
|
+
set,
|
|
55
|
+
update: (fn) => set(fn(get())),
|
|
56
|
+
subscribe: store.subscribe,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
subscribe: store.subscribe,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function fromStore<V>(store: Writable<V>): { current: V };
|
|
65
|
+
export function fromStore<V>(store: Readable<V>): { readonly current: V };
|
|
66
|
+
export function fromStore<V>(
|
|
67
|
+
store: Writable<V> | Readable<V>,
|
|
68
|
+
): { current: V } | { readonly current: V } {
|
|
69
|
+
let value = undefined as V;
|
|
70
|
+
let stop: Unsubscriber | undefined;
|
|
71
|
+
let unregisterDemand: (() => void) | undefined;
|
|
72
|
+
const version = createSignal(0);
|
|
73
|
+
const versionNode = collectDeps(() => version())[0]!;
|
|
74
|
+
const originalSubscribe = versionNode.subscribe;
|
|
75
|
+
versionNode.subscribe = (...args) => {
|
|
76
|
+
const unsubscribe = originalSubscribe(...args);
|
|
77
|
+
return () => {
|
|
78
|
+
unsubscribe();
|
|
79
|
+
notifyTrackableUnsubscribe();
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
function current(): V {
|
|
84
|
+
if (
|
|
85
|
+
!isSSRRendering() &&
|
|
86
|
+
peekComponentContext()?.mode !== "ssr" &&
|
|
87
|
+
isTracking() &&
|
|
88
|
+
!getOwner()?.disposed
|
|
89
|
+
) {
|
|
90
|
+
const owner = getOwner();
|
|
91
|
+
if (owner && !trackedOwners.has(owner)) {
|
|
92
|
+
trackedOwners.add(owner);
|
|
93
|
+
onCleanup(notifyTrackableUnsubscribe);
|
|
94
|
+
}
|
|
95
|
+
version();
|
|
96
|
+
trackHistoryRead(version, "value");
|
|
97
|
+
if (!stop) {
|
|
98
|
+
let ran = false;
|
|
99
|
+
stop = untrack(() =>
|
|
100
|
+
subscribeTo(store, (next) => {
|
|
101
|
+
if (ran) {
|
|
102
|
+
withHistoryMutation(() => {
|
|
103
|
+
recordHistory(version, "value", value, next);
|
|
104
|
+
value = next;
|
|
105
|
+
version.set((n) => n + 1);
|
|
106
|
+
});
|
|
107
|
+
} else {
|
|
108
|
+
value = next;
|
|
109
|
+
}
|
|
110
|
+
}),
|
|
111
|
+
);
|
|
112
|
+
ran = true;
|
|
113
|
+
unregisterDemand = registerDemandCheck(() => {
|
|
114
|
+
if (stop && !hasValueDemand(versionNode)) {
|
|
115
|
+
const cleanup = stop;
|
|
116
|
+
stop = undefined;
|
|
117
|
+
unregisterDemand?.();
|
|
118
|
+
unregisterDemand = undefined;
|
|
119
|
+
version.set((n) => n + 1);
|
|
120
|
+
untrack(() => cleanup());
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
return getStoreValue(store);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if ("set" in store) {
|
|
130
|
+
return {
|
|
131
|
+
get current() {
|
|
132
|
+
return current();
|
|
133
|
+
},
|
|
134
|
+
set current(v: V) {
|
|
135
|
+
store.set(v);
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
get current() {
|
|
142
|
+
return current();
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
package/src/store.ts
CHANGED
|
@@ -101,6 +101,11 @@ export function readable<T>(
|
|
|
101
101
|
return { subscribe: writable(initial, start).subscribe };
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
/** Expose subscriptions without forwarding a store's mutation methods. */
|
|
105
|
+
export function readonly<T>(store: Readable<T>): Readable<T> {
|
|
106
|
+
return { subscribe: store.subscribe.bind(store) };
|
|
107
|
+
}
|
|
108
|
+
|
|
104
109
|
type Stores = Readable<any> | readonly Readable<any>[];
|
|
105
110
|
type Values<S> =
|
|
106
111
|
S extends Readable<infer T>
|
|
@@ -206,3 +211,5 @@ export function get<T>(store: Readable<T>): T {
|
|
|
206
211
|
})();
|
|
207
212
|
return value;
|
|
208
213
|
}
|
|
214
|
+
|
|
215
|
+
export { fromStore, toStore } from "./store-interop";
|
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
|
+
}
|