Skip to content

Commit 1daadc3

Browse files
authored
chore: add missing svelte/transition functions (#9391)
* chore: add missing svelte/transition functions * lint
1 parent 9a99554 commit 1daadc3

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

packages/svelte/src/transition/index.js

Lines changed: 132 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,34 @@ 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+
2898
/**
2999
* Slides an element in and out.
30100
*
@@ -69,6 +139,68 @@ export function slide(node, { delay = 0, duration = 400, easing = cubic_out, axi
69139
};
70140
}
71141

142+
/**
143+
* 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.
144+
*
145+
* https://svelte.dev/docs/svelte-transition#scale
146+
* @param {Element} node
147+
* @param {import('./public').ScaleParams} [params]
148+
* @returns {import('./public').TransitionConfig}
149+
*/
150+
export function scale(
151+
node,
152+
{ delay = 0, duration = 400, easing = cubic_out, start = 0, opacity = 0 } = {}
153+
) {
154+
const style = getComputedStyle(node);
155+
const target_opacity = +style.opacity;
156+
const transform = style.transform === 'none' ? '' : style.transform;
157+
const sd = 1 - start;
158+
const od = target_opacity * (1 - opacity);
159+
return {
160+
delay,
161+
duration,
162+
easing,
163+
css: (_t, u) => `
164+
transform: ${transform} scale(${1 - sd * u});
165+
opacity: ${target_opacity - od * u}
166+
`
167+
};
168+
}
169+
170+
/**
171+
* 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>`.
172+
*
173+
* https://svelte.dev/docs/svelte-transition#draw
174+
* @param {SVGElement & { getTotalLength(): number }} node
175+
* @param {import('./public').DrawParams} [params]
176+
* @returns {import('./public').TransitionConfig}
177+
*/
178+
export function draw(node, { delay = 0, speed, duration, easing = cubic_in_out } = {}) {
179+
let len = node.getTotalLength();
180+
const style = getComputedStyle(node);
181+
if (style.strokeLinecap !== 'butt') {
182+
len += parseInt(style.strokeWidth);
183+
}
184+
if (duration === undefined) {
185+
if (speed === undefined) {
186+
duration = 800;
187+
} else {
188+
duration = len / speed;
189+
}
190+
} else if (typeof duration === 'function') {
191+
duration = duration(len);
192+
}
193+
return {
194+
delay,
195+
duration,
196+
easing,
197+
css: (_, u) => `
198+
stroke-dasharray: ${len};
199+
stroke-dashoffset: ${u * len};
200+
`
201+
};
202+
}
203+
72204
/**
73205
* @template T
74206
* @template S

0 commit comments

Comments
 (0)