Skip to content

Commit e5cc4c0

Browse files
committed
some docs
1 parent 21f9f3c commit e5cc4c0

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

packages/svelte/src/internal/client/dom/elements/transitions.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export function transition(flags, element, get_fn, get_params) {
138138
/** @type {'in' | 'out' | 'both'} */
139139
var direction = is_intro && is_outro ? 'both' : is_intro ? 'in' : 'out';
140140

141-
/** @type {import('#client').TransitionConfig | ((opts: { direction: 'in' | 'out' }) => import('#client').TransitionConfig) | undefined} */
141+
/** @type {import('#client').AnimationConfig | ((opts: { direction: 'in' | 'out' }) => import('#client').AnimationConfig) | undefined} */
142142
var current_options;
143143

144144
var inert = element.inert;
@@ -183,6 +183,8 @@ export function transition(flags, element, get_fn, get_params) {
183183
fn?.();
184184
});
185185

186+
// TODO arguably the outro should never null itself out until _all_ outros for this effect have completed...
187+
// in that case we wouldn't need to store `reset` separately
186188
reset = outro.reset;
187189
} else {
188190
fn?.();
@@ -213,15 +215,19 @@ export function transition(flags, element, get_fn, get_params) {
213215
}
214216

215217
/**
218+
* Animates an element, according to the provided configuration
216219
* @param {Element} element
217-
* @param {import('#client').TransitionConfig | ((opts: { direction: 'in' | 'out' }) => import('#client').TransitionConfig)} options
218-
* @param {import('#client').Animation | undefined} counterpart
219-
* @param {number} t2
220+
* @param {import('#client').AnimationConfig | ((opts: { direction: 'in' | 'out' }) => import('#client').AnimationConfig)} options
221+
* @param {import('#client').Animation | undefined} counterpart The corresponding intro/outro to this outro/intro
222+
* @param {number} t2 The target `t` value — `1` for intro, `0` for outro
220223
* @param {(() => void) | undefined} callback
221224
* @returns {import('#client').Animation}
222225
*/
223226
function animate(element, options, counterpart, t2, callback) {
224227
if (is_function(options)) {
228+
// In the case of a deferred transition (such as `crossfade`), `option` will be
229+
// a function rather than an `AnimationConfig`. We need to call this function
230+
// once DOM has been updated...
225231
/** @type {import('#client').Animation} */
226232
var a;
227233

@@ -230,23 +236,25 @@ function animate(element, options, counterpart, t2, callback) {
230236
a = animate(element, o, counterpart, t2, callback);
231237
});
232238

239+
// ...but we want to do so without using `async`/`await` everywhere, so
240+
// we return a facade that allows everything to remain synchronous
233241
return {
234242
abort: () => a.abort(),
235-
neuter: () => a.neuter(),
243+
deactivate: () => a.deactivate(),
236244
reset: () => a.reset(),
237-
p: (now) => a.p(now)
245+
t: (now) => a.t(now)
238246
};
239247
}
240248

241-
counterpart?.neuter();
249+
counterpart?.deactivate();
242250

243251
if (!options?.duration) {
244252
callback?.();
245253
return {
246254
abort: noop,
247-
neuter: noop,
255+
deactivate: noop,
248256
reset: noop,
249-
p: () => t2
257+
t: () => t2
250258
};
251259
}
252260

@@ -255,7 +263,7 @@ function animate(element, options, counterpart, t2, callback) {
255263
var { delay = 0, duration, css, tick, easing = linear } = options;
256264

257265
var start = raf.now() + delay;
258-
var t1 = counterpart?.p(start) ?? 1 - t2;
266+
var t1 = counterpart?.t(start) ?? 1 - t2;
259267
var delta = t2 - t1;
260268

261269
duration *= Math.abs(delta);
@@ -319,15 +327,15 @@ function animate(element, options, counterpart, t2, callback) {
319327
animation?.cancel();
320328
task?.abort();
321329
},
322-
neuter: () => {
330+
deactivate: () => {
323331
callback = undefined;
324332
},
325333
reset: () => {
326334
if (t2 === 0) {
327335
tick?.(1, 0);
328336
}
329337
},
330-
p: (now) => {
338+
t: (now) => {
331339
var t = t1 + delta * easing((now - start) / duration);
332340
return Math.min(1, Math.max(0, t));
333341
}

packages/svelte/src/internal/client/types.d.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,31 +169,38 @@ export interface TransitionManager {
169169
}
170170

171171
export interface AnimationManager {
172+
/** An element with an `animate:` directive */
172173
element: Element;
174+
/** Called during keyed each block reconciliation, before updates */
173175
measure: () => void;
176+
/** Called during keyed each block reconciliation, after updates — this triggers the animation */
174177
apply: () => void;
175178
}
176179

177180
export interface Animation {
181+
/** Abort the animation */
178182
abort: () => void;
179-
neuter: () => void;
183+
/** Allow the animation to continue running, but remove any callback. This prevents the removal of an outroing block if the corresponding intro has a `delay` */
184+
deactivate: () => void;
185+
/** Resets an animation to its starting state, if it uses `tick`. Exposed as a separate method so that an aborted `out:` can still reset even if the `outro` had already completed */
180186
reset: () => void;
181-
p: (now: number) => number;
187+
/** Get the `t` value (between `0` and `1`) of the animation, so that its counterpart can start from the right place */
188+
t: (now: number) => number;
182189
}
183190

184191
export type TransitionFn<P> = (
185192
element: Element,
186193
props: P,
187194
options: { direction?: 'in' | 'out' | 'both' }
188-
) => TransitionConfig | ((options: { direction?: 'in' | 'out' }) => TransitionConfig);
195+
) => AnimationConfig | ((options: { direction?: 'in' | 'out' }) => AnimationConfig);
189196

190197
export type AnimateFn<P> = (
191198
element: Element,
192199
rects: { from: DOMRect; to: DOMRect },
193200
props: P
194-
) => TransitionConfig;
201+
) => AnimationConfig;
195202

196-
export type TransitionConfig = {
203+
export type AnimationConfig = {
197204
delay?: number;
198205
duration?: number;
199206
easing?: (t: number) => number;

0 commit comments

Comments
 (0)