@bevelo/motion 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +49 -2
- package/dist/index.cjs +6364 -0
- package/dist/index.d.cts +1832 -0
- package/dist/index.d.ts +1832 -0
- package/dist/index.js +6278 -0
- package/dist/live/flip.cjs +78 -0
- package/dist/live/flip.d.cts +21 -0
- package/dist/live/flip.d.ts +21 -0
- package/dist/live/flip.js +77 -0
- package/dist/live/presence.cjs +7 -0
- package/dist/live/presence.d.cts +2 -0
- package/dist/live/presence.d.ts +2 -0
- package/dist/live/presence.js +1 -0
- package/dist/live/spring-value.cjs +4 -0
- package/dist/live/spring-value.d.cts +41 -0
- package/dist/live/spring-value.d.ts +41 -0
- package/dist/live/spring-value.js +1 -0
- package/dist/presence-BadXSYGX.cjs +499 -0
- package/dist/presence-CbHtzolg.d.cts +126 -0
- package/dist/presence-CbHtzolg.d.ts +126 -0
- package/dist/presence-Dt789xG9.js +470 -0
- package/dist/spring-CfdnEGs4.d.cts +118 -0
- package/dist/spring-CfdnEGs4.d.ts +118 -0
- package/dist/spring-value-D7dvds3M.cjs +373 -0
- package/dist/spring-value-fKpX3pAG.js +338 -0
- package/dist/timeline.cjs +53 -0
- package/dist/timeline.d.cts +61 -0
- package/dist/timeline.d.ts +61 -0
- package/dist/timeline.js +52 -0
- package/package.json +63 -3
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { ReadableStore } from "@bevelo/runtime";
|
|
2
|
+
/** Where one element is in its mount lifecycle. Every state except `closed` is mounted. */
|
|
3
|
+
type PresenceState = "closed" | "entering" | "open" | "exiting";
|
|
4
|
+
/** What moves a presence: the host's wish (`show`, `hide`) or the end of a phase's run. */
|
|
5
|
+
type PresenceEvent = {
|
|
6
|
+
readonly type: "show";
|
|
7
|
+
} | {
|
|
8
|
+
readonly type: "hide";
|
|
9
|
+
} | {
|
|
10
|
+
readonly type: "settled";
|
|
11
|
+
readonly phase: "entering" | "exiting";
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* The single-element transition table: closed-show->entering; entering-settled(entering)->open;
|
|
15
|
+
* entering|open-hide->exiting; exiting-show->entering; exiting-settled(exiting)->closed.
|
|
16
|
+
* Anything else returns `state` itself, so a stale settle or a repeated wish changes nothing.
|
|
17
|
+
*/
|
|
18
|
+
declare function presenceStep(state: PresenceState, event: PresenceEvent): PresenceState;
|
|
19
|
+
/**
|
|
20
|
+
* How a group orders exits against enters.
|
|
21
|
+
* - `sync`: enters and exits run together.
|
|
22
|
+
* - `wait`: a new key stays `closed` until no entry is exiting.
|
|
23
|
+
* - `popLayout`: like `sync`; the live layer takes exiting elements out of flow.
|
|
24
|
+
*/
|
|
25
|
+
type PresenceMode = "sync" | "wait" | "popLayout";
|
|
26
|
+
/** One keyed member of a group. */
|
|
27
|
+
interface PresenceGroupEntry {
|
|
28
|
+
readonly key: string;
|
|
29
|
+
readonly state: PresenceState;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A keyed group in render order. An entry whose exit settled is removed; a `closed`
|
|
33
|
+
* entry is a new key that `wait` mode holds back.
|
|
34
|
+
*/
|
|
35
|
+
interface PresenceGroupState {
|
|
36
|
+
readonly mode: PresenceMode;
|
|
37
|
+
readonly entries: readonly PresenceGroupEntry[];
|
|
38
|
+
}
|
|
39
|
+
/** What moves a group: the host's current keys, or one entry's settled run. */
|
|
40
|
+
type PresenceGroupEvent = {
|
|
41
|
+
readonly type: "sync";
|
|
42
|
+
readonly keys: readonly string[];
|
|
43
|
+
} | {
|
|
44
|
+
readonly type: "settled";
|
|
45
|
+
readonly key: string;
|
|
46
|
+
readonly phase: "entering" | "exiting";
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* The group reducer. `sync` makes the entries follow `keys`: a missing key starts its exit and
|
|
50
|
+
* keeps its place after its last surviving neighbour, a returning key reverses its exit, a new key
|
|
51
|
+
* enters. `wait` mode holds new keys `closed` until nothing exits. `settled` runs
|
|
52
|
+
* {@link presenceStep} on one entry and drops it once its exit settled. Returns `state` itself when
|
|
53
|
+
* nothing changed.
|
|
54
|
+
*/
|
|
55
|
+
declare function presenceGroupStep(state: PresenceGroupState, event: PresenceGroupEvent): PresenceGroupState;
|
|
56
|
+
/** Runs one phase's visuals; resolves when done; stops when `signal` aborts. */
|
|
57
|
+
type PresenceRunner = (el: Element, phase: "entering" | "exiting", signal: AbortSignal) => Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Writes data-state, waits one animation frame so @starting-style and transitions exist, then
|
|
60
|
+
* awaits every animation of the element and its subtree (`getAnimations({ subtree: true })`).
|
|
61
|
+
* An element with no running animation settles after that one frame. The wait uses the
|
|
62
|
+
* element's window directly, so the entry carries no frame loop.
|
|
63
|
+
*/
|
|
64
|
+
declare const cssRunner: PresenceRunner;
|
|
65
|
+
/** What a presence accepts at creation and on every `update`. */
|
|
66
|
+
interface PresenceOptions {
|
|
67
|
+
/** Plays each phase. Defaults to {@link cssRunner}. */
|
|
68
|
+
readonly runner?: PresenceRunner;
|
|
69
|
+
/** False: the first mount starts `open` instead of entering. Defaults to true. */
|
|
70
|
+
readonly initial?: boolean;
|
|
71
|
+
/** Called when an enter settles into `open`. */
|
|
72
|
+
readonly onEnterComplete?: () => void;
|
|
73
|
+
/** Called when an exit settles into `closed`. */
|
|
74
|
+
readonly onExitComplete?: () => void;
|
|
75
|
+
}
|
|
76
|
+
/** What a host renders from: whether to keep the element mounted, and its state. */
|
|
77
|
+
interface PresenceSnapshot {
|
|
78
|
+
readonly mounted: boolean;
|
|
79
|
+
readonly state: PresenceState;
|
|
80
|
+
}
|
|
81
|
+
/** One element's presence. */
|
|
82
|
+
interface Presence extends ReadableStore<PresenceSnapshot> {
|
|
83
|
+
/** Shows or hides. Idempotent. */
|
|
84
|
+
setOpen(open: boolean): void;
|
|
85
|
+
/**
|
|
86
|
+
* Binds the element: writes `data-state` now and on each change, and plays phases on it.
|
|
87
|
+
* Returns the detach, which aborts the running phase; attaching again restarts it.
|
|
88
|
+
*/
|
|
89
|
+
attach(el: Element): () => void;
|
|
90
|
+
/** Replaces the options; a new runner plays from the next phase. */
|
|
91
|
+
update(options: PresenceOptions): void;
|
|
92
|
+
/** Aborts the running phase and stops responding. */
|
|
93
|
+
dispose(): void;
|
|
94
|
+
}
|
|
95
|
+
/** Creates one element's presence, open or closed. */
|
|
96
|
+
declare function createPresence(open: boolean, options?: PresenceOptions): Presence;
|
|
97
|
+
/** One member of a group as a host renders it. */
|
|
98
|
+
interface PresenceEntry<T> {
|
|
99
|
+
readonly key: string;
|
|
100
|
+
/** The latest value for the key; an exiting entry keeps the last value it had. */
|
|
101
|
+
readonly value: T;
|
|
102
|
+
readonly state: PresenceState;
|
|
103
|
+
}
|
|
104
|
+
/** What a group accepts at creation and on every `update`. */
|
|
105
|
+
type PresenceGroupOptions = PresenceOptions & {
|
|
106
|
+
/** How exits are ordered against enters. Defaults to `sync`. */
|
|
107
|
+
readonly mode?: PresenceMode;
|
|
108
|
+
};
|
|
109
|
+
/** A keyed list's presence. The store holds the mounted entries in render order. */
|
|
110
|
+
interface PresenceGroup<T> extends ReadableStore<readonly PresenceEntry<T>[]> {
|
|
111
|
+
/** Makes the entries follow `values`; exiting entries keep their last value. */
|
|
112
|
+
sync(values: readonly T[], keyOf: (value: T) => string): void;
|
|
113
|
+
/**
|
|
114
|
+
* Binds a key's element. In `popLayout` mode an exiting element's offset box is read and it is
|
|
115
|
+
* written `position: absolute` at that box, so its siblings reflow at once; the container needs
|
|
116
|
+
* to be its offset parent. Returns the detach.
|
|
117
|
+
*/
|
|
118
|
+
attach(key: string, el: HTMLElement): () => void;
|
|
119
|
+
/** Replaces the options; a mode change applies from the next `sync`. */
|
|
120
|
+
update(options: PresenceGroupOptions): void;
|
|
121
|
+
/** Aborts every running phase and stops responding. */
|
|
122
|
+
dispose(): void;
|
|
123
|
+
}
|
|
124
|
+
/** Creates a keyed presence group with no entries. Call `sync` with the first values. */
|
|
125
|
+
declare function createPresenceGroup<T>(options?: PresenceGroupOptions): PresenceGroup<T>;
|
|
126
|
+
export { presenceGroupStep as _, PresenceOptions as a, createPresence as c, PresenceEvent as d, PresenceGroupEntry as f, PresenceState as g, PresenceMode as h, PresenceGroupOptions as i, createPresenceGroup as l, PresenceGroupState as m, PresenceEntry as n, PresenceRunner as o, PresenceGroupEvent as p, PresenceGroup as r, PresenceSnapshot as s, Presence as t, cssRunner as u, presenceStep as v };
|
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
// src/pure/presence.ts
|
|
2
|
+
/**
|
|
3
|
+
* The single-element transition table: closed-show->entering; entering-settled(entering)->open;
|
|
4
|
+
* entering|open-hide->exiting; exiting-show->entering; exiting-settled(exiting)->closed.
|
|
5
|
+
* Anything else returns `state` itself, so a stale settle or a repeated wish changes nothing.
|
|
6
|
+
*/
|
|
7
|
+
function presenceStep(state, event) {
|
|
8
|
+
switch (event.type) {
|
|
9
|
+
case "show": return state === "closed" || state === "exiting" ? "entering" : state;
|
|
10
|
+
case "hide": return state === "entering" || state === "open" ? "exiting" : state;
|
|
11
|
+
case "settled":
|
|
12
|
+
if (event.phase !== state) return state;
|
|
13
|
+
return state === "entering" ? "open" : "closed";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The group reducer. `sync` makes the entries follow `keys`: a missing key starts its exit and
|
|
18
|
+
* keeps its place after its last surviving neighbour, a returning key reverses its exit, a new key
|
|
19
|
+
* enters. `wait` mode holds new keys `closed` until nothing exits. `settled` runs
|
|
20
|
+
* {@link presenceStep} on one entry and drops it once its exit settled. Returns `state` itself when
|
|
21
|
+
* nothing changed.
|
|
22
|
+
*/
|
|
23
|
+
function presenceGroupStep(state, event) {
|
|
24
|
+
return finish(state, event.type === "sync" ? syncEntries(state.entries, event.keys) : settleEntry(state.entries, event));
|
|
25
|
+
}
|
|
26
|
+
function syncEntries(entries, keys) {
|
|
27
|
+
const wanted = new Set(keys);
|
|
28
|
+
const previous = /* @__PURE__ */ new Map();
|
|
29
|
+
for (const entry of entries) previous.set(entry.key, entry);
|
|
30
|
+
const leading = [];
|
|
31
|
+
const after = /* @__PURE__ */ new Map();
|
|
32
|
+
let anchor = null;
|
|
33
|
+
for (const entry of entries) {
|
|
34
|
+
if (wanted.has(entry.key)) {
|
|
35
|
+
anchor = entry.key;
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (entry.state === "closed") continue;
|
|
39
|
+
const leaving = {
|
|
40
|
+
key: entry.key,
|
|
41
|
+
state: presenceStep(entry.state, { type: "hide" })
|
|
42
|
+
};
|
|
43
|
+
if (anchor === null) {
|
|
44
|
+
leading.push(leaving);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const pinned = after.get(anchor);
|
|
48
|
+
if (pinned === void 0) after.set(anchor, [leaving]);
|
|
49
|
+
else pinned.push(leaving);
|
|
50
|
+
}
|
|
51
|
+
const next = leading;
|
|
52
|
+
const seen = /* @__PURE__ */ new Set();
|
|
53
|
+
for (const key of keys) {
|
|
54
|
+
if (seen.has(key)) continue;
|
|
55
|
+
seen.add(key);
|
|
56
|
+
const prior = previous.get(key);
|
|
57
|
+
next.push(prior === void 0 ? {
|
|
58
|
+
key,
|
|
59
|
+
state: "entering"
|
|
60
|
+
} : {
|
|
61
|
+
key,
|
|
62
|
+
state: presenceStep(prior.state, { type: "show" })
|
|
63
|
+
});
|
|
64
|
+
const pinned = after.get(key);
|
|
65
|
+
if (pinned !== void 0) next.push(...pinned);
|
|
66
|
+
}
|
|
67
|
+
return next;
|
|
68
|
+
}
|
|
69
|
+
function settleEntry(entries, event) {
|
|
70
|
+
const next = [];
|
|
71
|
+
for (const entry of entries) {
|
|
72
|
+
if (entry.key !== event.key) {
|
|
73
|
+
next.push(entry);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const state = presenceStep(entry.state, {
|
|
77
|
+
type: "settled",
|
|
78
|
+
phase: event.phase
|
|
79
|
+
});
|
|
80
|
+
if (state === "closed" && entry.state === "exiting") continue;
|
|
81
|
+
next.push(state === entry.state ? entry : {
|
|
82
|
+
key: entry.key,
|
|
83
|
+
state
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return next;
|
|
87
|
+
}
|
|
88
|
+
/** Applies the wait-mode hold, then returns `state` itself when no entry changed. */
|
|
89
|
+
function finish(state, candidate) {
|
|
90
|
+
const before = /* @__PURE__ */ new Map();
|
|
91
|
+
for (const entry of state.entries) before.set(entry.key, entry);
|
|
92
|
+
let entries = candidate;
|
|
93
|
+
if (state.mode === "wait") {
|
|
94
|
+
const exiting = entries.some((entry) => entry.state === "exiting");
|
|
95
|
+
entries = entries.map((entry) => {
|
|
96
|
+
if (!(!before.has(entry.key) || before.get(entry.key).state === "closed") || entry.state !== "entering" && entry.state !== "closed") return entry;
|
|
97
|
+
const held = exiting ? "closed" : "entering";
|
|
98
|
+
return held === entry.state ? entry : {
|
|
99
|
+
key: entry.key,
|
|
100
|
+
state: held
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
let changed = entries.length !== state.entries.length;
|
|
105
|
+
const reused = entries.map((entry, index) => {
|
|
106
|
+
const prior = before.get(entry.key);
|
|
107
|
+
if (state.entries[index]?.key !== entry.key) changed = true;
|
|
108
|
+
if (prior !== void 0 && prior.state === entry.state) return prior;
|
|
109
|
+
changed = true;
|
|
110
|
+
return entry;
|
|
111
|
+
});
|
|
112
|
+
return changed ? {
|
|
113
|
+
mode: state.mode,
|
|
114
|
+
entries: reused
|
|
115
|
+
} : state;
|
|
116
|
+
}
|
|
117
|
+
// src/live/presence.ts
|
|
118
|
+
function isRunning(state) {
|
|
119
|
+
return state === "entering" || state === "exiting";
|
|
120
|
+
}
|
|
121
|
+
/** Each root's animations by element, every ancestor of a target included, for one frame. */
|
|
122
|
+
const frameIndex = /* @__PURE__ */ new WeakMap();
|
|
123
|
+
/**
|
|
124
|
+
* The animations of `el` and its subtree, as `el.getAnimations({ subtree: true })` returns them.
|
|
125
|
+
* Element.getAnimations walks every animation in the document, so a group of n elements
|
|
126
|
+
* settling in one frame would cost n x n; this reads the root's animations once per frame
|
|
127
|
+
* `time` and indexes them by target and its ancestors.
|
|
128
|
+
*/
|
|
129
|
+
function animationsOf(el, time) {
|
|
130
|
+
const root = el.getRootNode();
|
|
131
|
+
if (typeof root.getAnimations !== "function") return typeof el.getAnimations === "function" ? el.getAnimations({ subtree: true }) : [];
|
|
132
|
+
let index = frameIndex.get(root);
|
|
133
|
+
if (index === void 0 || index.time !== time) {
|
|
134
|
+
const byElement = /* @__PURE__ */ new Map();
|
|
135
|
+
for (const animation of root.getAnimations()) for (let node = animation.effect?.target ?? null; node !== null; node = node.parentElement) {
|
|
136
|
+
const list = byElement.get(node);
|
|
137
|
+
if (list === void 0) byElement.set(node, [animation]);
|
|
138
|
+
else list.push(animation);
|
|
139
|
+
}
|
|
140
|
+
index = {
|
|
141
|
+
time,
|
|
142
|
+
byElement
|
|
143
|
+
};
|
|
144
|
+
frameIndex.set(root, index);
|
|
145
|
+
}
|
|
146
|
+
return index.byElement.get(el) ?? [];
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Writes data-state, waits one animation frame so @starting-style and transitions exist, then
|
|
150
|
+
* awaits every animation of the element and its subtree (`getAnimations({ subtree: true })`).
|
|
151
|
+
* An element with no running animation settles after that one frame. The wait uses the
|
|
152
|
+
* element's window directly, so the entry carries no frame loop.
|
|
153
|
+
*/
|
|
154
|
+
const cssRunner = (el, phase, signal) => new Promise((resolve) => {
|
|
155
|
+
if (signal.aborted) {
|
|
156
|
+
resolve();
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
el.setAttribute("data-state", phase);
|
|
160
|
+
const view = el.ownerDocument.defaultView;
|
|
161
|
+
const settle = (time) => {
|
|
162
|
+
if (signal.aborted) return;
|
|
163
|
+
Promise.allSettled(animationsOf(el, time).map((animation) => animation.finished)).then(() => resolve());
|
|
164
|
+
};
|
|
165
|
+
let cancel;
|
|
166
|
+
if (view !== null && typeof view.requestAnimationFrame === "function") {
|
|
167
|
+
const id = view.requestAnimationFrame(settle);
|
|
168
|
+
cancel = () => view.cancelAnimationFrame(id);
|
|
169
|
+
} else {
|
|
170
|
+
const id = setTimeout(() => settle(NaN), 16);
|
|
171
|
+
cancel = () => clearTimeout(id);
|
|
172
|
+
}
|
|
173
|
+
signal.addEventListener("abort", () => {
|
|
174
|
+
cancel();
|
|
175
|
+
resolve();
|
|
176
|
+
}, { once: true });
|
|
177
|
+
});
|
|
178
|
+
/** A small listener set shared by the two presence stores. */
|
|
179
|
+
function createEmitter() {
|
|
180
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
181
|
+
return {
|
|
182
|
+
subscribe(listener) {
|
|
183
|
+
const record = { listener };
|
|
184
|
+
listeners.add(record);
|
|
185
|
+
return () => {
|
|
186
|
+
listeners.delete(record);
|
|
187
|
+
};
|
|
188
|
+
},
|
|
189
|
+
emit() {
|
|
190
|
+
for (const record of [...listeners]) if (listeners.has(record)) record.listener();
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Starts `phase` on `el` and calls `settle` when it finishes, unless the run was aborted first.
|
|
196
|
+
* A runner that rejects still settles: an element must not stay mounted because its visuals failed.
|
|
197
|
+
*/
|
|
198
|
+
function startRun(runner, el, phase, settle) {
|
|
199
|
+
const controller = new AbortController();
|
|
200
|
+
const finish = () => {
|
|
201
|
+
if (!controller.signal.aborted) settle(phase);
|
|
202
|
+
};
|
|
203
|
+
runner(el, phase, controller.signal).then(finish, finish);
|
|
204
|
+
return {
|
|
205
|
+
phase,
|
|
206
|
+
controller
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/** Creates one element's presence, open or closed. */
|
|
210
|
+
function createPresence(open, options = {}) {
|
|
211
|
+
let current = options;
|
|
212
|
+
const emitter = createEmitter();
|
|
213
|
+
let snapshot = {
|
|
214
|
+
mounted: open,
|
|
215
|
+
state: open ? options.initial === false ? "open" : "entering" : "closed"
|
|
216
|
+
};
|
|
217
|
+
let el = null;
|
|
218
|
+
let run = null;
|
|
219
|
+
let disposed = false;
|
|
220
|
+
function abort() {
|
|
221
|
+
run?.controller.abort();
|
|
222
|
+
run = null;
|
|
223
|
+
}
|
|
224
|
+
/** Makes the run match the state: abort a run for a phase that was left, start one for a new phase. */
|
|
225
|
+
function reconcile() {
|
|
226
|
+
const { state } = snapshot;
|
|
227
|
+
if (run !== null && run.phase === state) return;
|
|
228
|
+
abort();
|
|
229
|
+
if (!isRunning(state)) return;
|
|
230
|
+
if (el === null) {
|
|
231
|
+
if (state === "exiting") dispatch("exiting");
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
run = startRun(current.runner ?? cssRunner, el, state, dispatch);
|
|
235
|
+
}
|
|
236
|
+
function apply(state) {
|
|
237
|
+
if (disposed || state === snapshot.state) return;
|
|
238
|
+
const previous = snapshot.state;
|
|
239
|
+
snapshot = {
|
|
240
|
+
mounted: state !== "closed",
|
|
241
|
+
state
|
|
242
|
+
};
|
|
243
|
+
el?.setAttribute("data-state", state);
|
|
244
|
+
emitter.emit();
|
|
245
|
+
if (previous === "entering" && state === "open") current.onEnterComplete?.();
|
|
246
|
+
if (previous === "exiting" && state === "closed") current.onExitComplete?.();
|
|
247
|
+
reconcile();
|
|
248
|
+
}
|
|
249
|
+
function dispatch(phase) {
|
|
250
|
+
apply(presenceStep(snapshot.state, {
|
|
251
|
+
type: "settled",
|
|
252
|
+
phase
|
|
253
|
+
}));
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
get: () => snapshot,
|
|
257
|
+
subscribe: emitter.subscribe,
|
|
258
|
+
setOpen(next) {
|
|
259
|
+
apply(presenceStep(snapshot.state, { type: next ? "show" : "hide" }));
|
|
260
|
+
},
|
|
261
|
+
attach(next) {
|
|
262
|
+
if (disposed) return () => {};
|
|
263
|
+
if (el !== next) abort();
|
|
264
|
+
el = next;
|
|
265
|
+
next.setAttribute("data-state", snapshot.state);
|
|
266
|
+
reconcile();
|
|
267
|
+
return () => {
|
|
268
|
+
if (el !== next) return;
|
|
269
|
+
el = null;
|
|
270
|
+
abort();
|
|
271
|
+
};
|
|
272
|
+
},
|
|
273
|
+
update(options) {
|
|
274
|
+
current = options;
|
|
275
|
+
},
|
|
276
|
+
dispose() {
|
|
277
|
+
disposed = true;
|
|
278
|
+
abort();
|
|
279
|
+
el = null;
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/** Creates a keyed presence group with no entries. Call `sync` with the first values. */
|
|
284
|
+
function createPresenceGroup(options = {}) {
|
|
285
|
+
let current = options;
|
|
286
|
+
const emitter = createEmitter();
|
|
287
|
+
let state = {
|
|
288
|
+
mode: options.mode ?? "sync",
|
|
289
|
+
entries: []
|
|
290
|
+
};
|
|
291
|
+
const members = /* @__PURE__ */ new Map();
|
|
292
|
+
let states = /* @__PURE__ */ new Map();
|
|
293
|
+
let snapshot = [];
|
|
294
|
+
let synced = false;
|
|
295
|
+
let disposed = false;
|
|
296
|
+
function pop(member) {
|
|
297
|
+
const el = member.el;
|
|
298
|
+
if (el === null || member.popped !== null) return;
|
|
299
|
+
const computed = el.ownerDocument.defaultView?.getComputedStyle(el);
|
|
300
|
+
const top = el.offsetTop - (parseFloat(computed?.marginTop ?? "") || 0);
|
|
301
|
+
const left = el.offsetLeft - (parseFloat(computed?.marginLeft ?? "") || 0);
|
|
302
|
+
const width = el.offsetWidth;
|
|
303
|
+
const height = el.offsetHeight;
|
|
304
|
+
const { style } = el;
|
|
305
|
+
member.popped = {
|
|
306
|
+
position: style.position,
|
|
307
|
+
top: style.top,
|
|
308
|
+
left: style.left,
|
|
309
|
+
width: style.width,
|
|
310
|
+
height: style.height
|
|
311
|
+
};
|
|
312
|
+
style.position = "absolute";
|
|
313
|
+
style.top = `${top}px`;
|
|
314
|
+
style.left = `${left}px`;
|
|
315
|
+
style.width = `${width}px`;
|
|
316
|
+
style.height = `${height}px`;
|
|
317
|
+
}
|
|
318
|
+
function unpop(member) {
|
|
319
|
+
const { el, popped } = member;
|
|
320
|
+
member.popped = null;
|
|
321
|
+
if (el === null || popped === null) return;
|
|
322
|
+
Object.assign(el.style, popped);
|
|
323
|
+
}
|
|
324
|
+
function settle(key, phase) {
|
|
325
|
+
commit(presenceGroupStep(state, {
|
|
326
|
+
type: "settled",
|
|
327
|
+
key,
|
|
328
|
+
phase
|
|
329
|
+
}));
|
|
330
|
+
}
|
|
331
|
+
/** Makes one member's run match its state. */
|
|
332
|
+
function reconcile(key, member, entryState) {
|
|
333
|
+
if (member.run !== null && member.run.phase === entryState) return;
|
|
334
|
+
member.run?.controller.abort();
|
|
335
|
+
member.run = null;
|
|
336
|
+
if (entryState !== "exiting") unpop(member);
|
|
337
|
+
if (!isRunning(entryState)) return;
|
|
338
|
+
if (member.el === null) {
|
|
339
|
+
if (entryState === "exiting") settle(key, "exiting");
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
member.run = startRun(current.runner ?? cssRunner, member.el, entryState, (phase) => settle(key, phase));
|
|
343
|
+
}
|
|
344
|
+
function commit(next) {
|
|
345
|
+
if (disposed || next === state) return;
|
|
346
|
+
const previous = new Map(state.entries.map((entry) => [entry.key, entry.state]));
|
|
347
|
+
state = next;
|
|
348
|
+
const changed = [];
|
|
349
|
+
states = /* @__PURE__ */ new Map();
|
|
350
|
+
for (const entry of next.entries) {
|
|
351
|
+
states.set(entry.key, entry.state);
|
|
352
|
+
const member = members.get(entry.key);
|
|
353
|
+
if (member === void 0) continue;
|
|
354
|
+
if (previous.get(entry.key) === entry.state) continue;
|
|
355
|
+
member.el?.setAttribute("data-state", entry.state);
|
|
356
|
+
if (entry.state === "exiting" && next.mode === "popLayout") pop(member);
|
|
357
|
+
changed.push([
|
|
358
|
+
entry.key,
|
|
359
|
+
member,
|
|
360
|
+
entry.state
|
|
361
|
+
]);
|
|
362
|
+
}
|
|
363
|
+
let exited = 0;
|
|
364
|
+
for (const [key, member] of members) {
|
|
365
|
+
if (states.has(key)) continue;
|
|
366
|
+
member.run?.controller.abort();
|
|
367
|
+
unpop(member);
|
|
368
|
+
members.delete(key);
|
|
369
|
+
if (previous.get(key) === "exiting") exited += 1;
|
|
370
|
+
}
|
|
371
|
+
publish();
|
|
372
|
+
for (const [key, , entryState] of changed) if (previous.get(key) === "entering" && entryState === "open") current.onEnterComplete?.();
|
|
373
|
+
for (let index = 0; index < exited; index += 1) current.onExitComplete?.();
|
|
374
|
+
for (const [key, member] of changed) {
|
|
375
|
+
const now = states.get(key);
|
|
376
|
+
if (now !== void 0 && members.get(key) === member) reconcile(key, member, now);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/** Rebuilds the snapshot, keeping every unchanged entry object and the array when nothing moved. */
|
|
380
|
+
function publish() {
|
|
381
|
+
const byKey = new Map(snapshot.map((entry) => [entry.key, entry]));
|
|
382
|
+
let moved = false;
|
|
383
|
+
const next = [];
|
|
384
|
+
for (const entry of state.entries) {
|
|
385
|
+
if (entry.state === "closed") continue;
|
|
386
|
+
const member = members.get(entry.key);
|
|
387
|
+
const prior = byKey.get(entry.key);
|
|
388
|
+
const item = prior !== void 0 && prior.state === entry.state && Object.is(prior.value, member.value) ? prior : {
|
|
389
|
+
key: entry.key,
|
|
390
|
+
value: member.value,
|
|
391
|
+
state: entry.state
|
|
392
|
+
};
|
|
393
|
+
if (snapshot[next.length] !== item) moved = true;
|
|
394
|
+
next.push(item);
|
|
395
|
+
}
|
|
396
|
+
if (!moved && next.length === snapshot.length) return;
|
|
397
|
+
snapshot = next;
|
|
398
|
+
emitter.emit();
|
|
399
|
+
}
|
|
400
|
+
return {
|
|
401
|
+
get: () => snapshot,
|
|
402
|
+
subscribe: emitter.subscribe,
|
|
403
|
+
sync(values, keyOf) {
|
|
404
|
+
if (disposed) return;
|
|
405
|
+
const keys = [];
|
|
406
|
+
for (const value of values) {
|
|
407
|
+
const key = keyOf(value);
|
|
408
|
+
keys.push(key);
|
|
409
|
+
const member = members.get(key);
|
|
410
|
+
if (member === void 0) members.set(key, {
|
|
411
|
+
value,
|
|
412
|
+
el: null,
|
|
413
|
+
run: null,
|
|
414
|
+
popped: null
|
|
415
|
+
});
|
|
416
|
+
else member.value = value;
|
|
417
|
+
}
|
|
418
|
+
if (state.mode !== (current.mode ?? "sync")) state = {
|
|
419
|
+
mode: current.mode ?? "sync",
|
|
420
|
+
entries: state.entries
|
|
421
|
+
};
|
|
422
|
+
const first = !synced;
|
|
423
|
+
synced = true;
|
|
424
|
+
let next = presenceGroupStep(state, {
|
|
425
|
+
type: "sync",
|
|
426
|
+
keys
|
|
427
|
+
});
|
|
428
|
+
if (first && current.initial === false) {
|
|
429
|
+
for (const entry of next.entries) if (entry.state === "entering") next = presenceGroupStep(next, {
|
|
430
|
+
type: "settled",
|
|
431
|
+
key: entry.key,
|
|
432
|
+
phase: "entering"
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
if (next === state) publish();
|
|
436
|
+
else commit(next);
|
|
437
|
+
},
|
|
438
|
+
attach(key, el) {
|
|
439
|
+
const member = members.get(key);
|
|
440
|
+
if (disposed || member === void 0) return () => {};
|
|
441
|
+
if (member.el !== el) {
|
|
442
|
+
member.run?.controller.abort();
|
|
443
|
+
member.run = null;
|
|
444
|
+
member.popped = null;
|
|
445
|
+
}
|
|
446
|
+
member.el = el;
|
|
447
|
+
const entryState = states.get(key) ?? "closed";
|
|
448
|
+
el.setAttribute("data-state", entryState);
|
|
449
|
+
if (entryState === "exiting" && state.mode === "popLayout") pop(member);
|
|
450
|
+
reconcile(key, member, entryState);
|
|
451
|
+
return () => {
|
|
452
|
+
if (members.get(key) !== member || member.el !== el) return;
|
|
453
|
+
member.run?.controller.abort();
|
|
454
|
+
member.run = null;
|
|
455
|
+
member.el = null;
|
|
456
|
+
};
|
|
457
|
+
},
|
|
458
|
+
update(options) {
|
|
459
|
+
current = options;
|
|
460
|
+
},
|
|
461
|
+
dispose() {
|
|
462
|
+
disposed = true;
|
|
463
|
+
for (const member of members.values()) {
|
|
464
|
+
member.run?.controller.abort();
|
|
465
|
+
member.run = null;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
export { presenceStep as a, presenceGroupStep as i, createPresenceGroup as n, cssRunner as r, createPresence as t };
|