Skip to content

Commit 166a792

Browse files
committed
chore: add missing svelte/transition functions
1 parent 304a29e commit 166a792

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

packages/svelte/src/transition/index.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,48 @@ function cubic_out(t) {
77
return f * f * f + 1.0;
88
}
99

10+
/**
11+
* https://svelte.dev/docs/svelte-easing
12+
* @param {number} t
13+
* @returns {number}
14+
*/
15+
export function cubic_in_out(t) {
16+
return t < 0.5 ? 4.0 * t * t * t : 0.5 * Math.pow(2.0 * t - 2.0, 3.0) + 1.0;
17+
}
18+
19+
/** @param {number | string} value
20+
* @returns {[number, string]}
21+
*/
22+
export function split_css_unit(value) {
23+
const split = typeof value === 'string' && value.match(/^\s*(-?[\d.]+)([^\s]*)\s*$/);
24+
return split ? [parseFloat(split[1]), split[2] || 'px'] : [/** @type {number} */ (value), 'px'];
25+
}
26+
27+
/**
28+
* Animates a `blur` filter alongside an element's opacity.
29+
*
30+
* https://svelte.dev/docs/svelte-transition#blur
31+
* @param {Element} node
32+
* @param {import('./public').BlurParams} [params]
33+
* @returns {import('./public').TransitionConfig}
34+
*/
35+
export function blur(
36+
node,
37+
{ delay = 0, duration = 400, easing = cubic_in_out, amount = 5, opacity = 0 } = {}
38+
) {
39+
const style = getComputedStyle(node);
40+
const target_opacity = +style.opacity;
41+
const f = style.filter === 'none' ? '' : style.filter;
42+
const od = target_opacity * (1 - opacity);
43+
const [value, unit] = split_css_unit(amount);
44+
return {
45+
delay,
46+
duration,
47+
easing,
48+
css: (_t, u) => `opacity: ${target_opacity - od * u}; filter: ${f} blur(${u * value}${unit});`
49+
};
50+
}
51+
1052
/**
1153
* Animates the opacity of an element from 0 to the current opacity for `in` transitions and from the current opacity to 0 for `out` transitions.
1254
*
@@ -25,6 +67,35 @@ export function fade(node, { delay = 0, duration = 400, easing = linear } = {})
2567
};
2668
}
2769

70+
/**
71+
* Animates the x and y positions and the opacity of an element. `in` transitions animate from the provided values, passed as parameters to the element's default values. `out` transitions animate from the element's default values to the provided values.
72+
*
73+
* https://svelte.dev/docs/svelte-transition#fly
74+
* @param {Element} node
75+
* @param {import('./public').FlyParams} [params]
76+
* @returns {import('./public').TransitionConfig}
77+
*/
78+
export function fly(
79+
node,
80+
{ delay = 0, duration = 400, easing = cubic_out, x = 0, y = 0, opacity = 0 } = {}
81+
) {
82+
const style = getComputedStyle(node);
83+
const target_opacity = +style.opacity;
84+
const transform = style.transform === 'none' ? '' : style.transform;
85+
const od = target_opacity * (1 - opacity);
86+
const [xValue, xUnit] = split_css_unit(x);
87+
const [yValue, yUnit] = split_css_unit(y);
88+
return {
89+
delay,
90+
duration,
91+
easing,
92+
css: (t, u) => `
93+
transform: ${transform} translate(${(1 - t) * xValue}${xUnit}, ${(1 - t) * yValue}${yUnit});
94+
opacity: ${target_opacity - od * u}`
95+
};
96+
}
97+
98+
2899
/**
29100
* Slides an element in and out.
30101
*
@@ -69,6 +140,68 @@ export function slide(node, { delay = 0, duration = 400, easing = cubic_out, axi
69140
};
70141
}
71142

143+
/**
144+
* Animates the opacity and scale of an element. `in` transitions animate from an element's current (default) values to the provided values, passed as parameters. `out` transitions animate from the provided values to an element's default values.
145+
*
146+
* https://svelte.dev/docs/svelte-transition#scale
147+
* @param {Element} node
148+
* @param {import('./public').ScaleParams} [params]
149+
* @returns {import('./public').TransitionConfig}
150+
*/
151+
export function scale(
152+
node,
153+
{ delay = 0, duration = 400, easing = cubic_out, start = 0, opacity = 0 } = {}
154+
) {
155+
const style = getComputedStyle(node);
156+
const target_opacity = +style.opacity;
157+
const transform = style.transform === 'none' ? '' : style.transform;
158+
const sd = 1 - start;
159+
const od = target_opacity * (1 - opacity);
160+
return {
161+
delay,
162+
duration,
163+
easing,
164+
css: (_t, u) => `
165+
transform: ${transform} scale(${1 - sd * u});
166+
opacity: ${target_opacity - od * u}
167+
`
168+
};
169+
}
170+
171+
/**
172+
* Animates the stroke of an SVG element, like a snake in a tube. `in` transitions begin with the path invisible and draw the path to the screen over time. `out` transitions start in a visible state and gradually erase the path. `draw` only works with elements that have a `getTotalLength` method, like `<path>` and `<polyline>`.
173+
*
174+
* https://svelte.dev/docs/svelte-transition#draw
175+
* @param {SVGElement & { getTotalLength(): number }} node
176+
* @param {import('./public').DrawParams} [params]
177+
* @returns {import('./public').TransitionConfig}
178+
*/
179+
export function draw(node, { delay = 0, speed, duration, easing = cubic_in_out } = {}) {
180+
let len = node.getTotalLength();
181+
const style = getComputedStyle(node);
182+
if (style.strokeLinecap !== 'butt') {
183+
len += parseInt(style.strokeWidth);
184+
}
185+
if (duration === undefined) {
186+
if (speed === undefined) {
187+
duration = 800;
188+
} else {
189+
duration = len / speed;
190+
}
191+
} else if (typeof duration === 'function') {
192+
duration = duration(len);
193+
}
194+
return {
195+
delay,
196+
duration,
197+
easing,
198+
css: (_, u) => `
199+
stroke-dasharray: ${len};
200+
stroke-dashoffset: ${u * len};
201+
`
202+
};
203+
}
204+
72205
/**
73206
* @template T
74207
* @template S

0 commit comments

Comments
 (0)