Convert src/runtime/transition/index.ts to JavaScript

pull/8569/head
S. Elliott Johnson 3 years ago
parent 0f69187008
commit ae9e0fd3d0

@ -1,32 +1,13 @@
import { cubicOut, cubicInOut, linear } from '../easing'; import { cubicOut, cubicInOut, linear } from '../easing';
import { assign, split_css_unit, is_function } from '../internal'; import { assign, split_css_unit, is_function } from '../internal';
/** @param {Element} node
export type EasingFunction = (t: number) => number; * @param {BlurParams}
* @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").TransitionConfig}
export interface TransitionConfig { */
delay?: number; export function blur(node, { delay = 0, duration = 400, easing = cubicInOut, amount = 5, opacity = 0 } = {}) {
duration?: number;
easing?: EasingFunction;
css?: (t: number, u: number) => string;
tick?: (t: number, u: number) => void;
}
export interface BlurParams {
delay?: number;
duration?: number;
easing?: EasingFunction;
amount?: number | string;
opacity?: number;
}
export function blur(
node: Element,
{ delay = 0, duration = 400, easing = cubicInOut, amount = 5, opacity = 0 }: BlurParams = {}
): TransitionConfig {
const style = getComputedStyle(node); const style = getComputedStyle(node);
const target_opacity = +style.opacity; const target_opacity = +style.opacity;
const f = style.filter === 'none' ? '' : style.filter; const f = style.filter === 'none' ? '' : style.filter;
const od = target_opacity * (1 - opacity); const od = target_opacity * (1 - opacity);
const [value, unit] = split_css_unit(amount); const [value, unit] = split_css_unit(amount);
return { return {
@ -36,19 +17,12 @@ export function blur(
css: (_t, u) => `opacity: ${target_opacity - od * u}; filter: ${f} blur(${u * value}${unit});` css: (_t, u) => `opacity: ${target_opacity - od * u}; filter: ${f} blur(${u * value}${unit});`
}; };
} }
/** @param {Element} node
export interface FadeParams { * @param {FadeParams}
delay?: number; * @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").TransitionConfig}
duration?: number; */
easing?: EasingFunction; export function fade(node, { delay = 0, duration = 400, easing = linear } = {}) {
}
export function fade(
node: Element,
{ delay = 0, duration = 400, easing = linear }: FadeParams = {}
): TransitionConfig {
const o = +getComputedStyle(node).opacity; const o = +getComputedStyle(node).opacity;
return { return {
delay, delay,
duration, duration,
@ -56,24 +30,14 @@ export function fade(
css: (t) => `opacity: ${t * o}` css: (t) => `opacity: ${t * o}`
}; };
} }
/** @param {Element} node
export interface FlyParams { * @param {FlyParams}
delay?: number; * @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").TransitionConfig}
duration?: number; */
easing?: EasingFunction; export function fly(node, { delay = 0, duration = 400, easing = cubicOut, x = 0, y = 0, opacity = 0 } = {}) {
x?: number | string;
y?: number | string;
opacity?: number;
}
export function fly(
node: Element,
{ delay = 0, duration = 400, easing = cubicOut, x = 0, y = 0, opacity = 0 }: FlyParams = {}
): TransitionConfig {
const style = getComputedStyle(node); const style = getComputedStyle(node);
const target_opacity = +style.opacity; const target_opacity = +style.opacity;
const transform = style.transform === 'none' ? '' : style.transform; const transform = style.transform === 'none' ? '' : style.transform;
const od = target_opacity * (1 - opacity); const od = target_opacity * (1 - opacity);
const [xValue, xUnit] = split_css_unit(x); const [xValue, xUnit] = split_css_unit(x);
const [yValue, yUnit] = split_css_unit(y); const [yValue, yUnit] = split_css_unit(y);
@ -86,42 +50,28 @@ export function fly(
opacity: ${target_opacity - od * u}` opacity: ${target_opacity - od * u}`
}; };
} }
/** @param {Element} node
export interface SlideParams { * @param {SlideParams}
delay?: number; * @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").TransitionConfig}
duration?: number; */
easing?: EasingFunction; export function slide(node, { delay = 0, duration = 400, easing = cubicOut, axis = 'y' } = {}) {
axis?: 'x' | 'y';
}
export function slide(
node: Element,
{ delay = 0, duration = 400, easing = cubicOut, axis = 'y' }: SlideParams = {}
): TransitionConfig {
const style = getComputedStyle(node); const style = getComputedStyle(node);
const opacity = +style.opacity; const opacity = +style.opacity;
const primary_property = axis === 'y' ? 'height' : 'width'; const primary_property = axis === 'y' ? 'height' : 'width';
const primary_property_value = parseFloat(style[primary_property]); const primary_property_value = parseFloat(style[primary_property]);
const secondary_properties = axis === 'y' ? ['top', 'bottom'] : ['left', 'right']; const secondary_properties = axis === 'y' ? ['top', 'bottom'] : ['left', 'right'];
const capitalized_secondary_properties = secondary_properties.map( const capitalized_secondary_properties = secondary_properties.map((e) => `${e[0].toUpperCase()}${e.slice(1)}`);
(e) => `${e[0].toUpperCase()}${e.slice(1)}`
);
const padding_start_value = parseFloat(style[`padding${capitalized_secondary_properties[0]}`]); const padding_start_value = parseFloat(style[`padding${capitalized_secondary_properties[0]}`]);
const padding_end_value = parseFloat(style[`padding${capitalized_secondary_properties[1]}`]); const padding_end_value = parseFloat(style[`padding${capitalized_secondary_properties[1]}`]);
const margin_start_value = parseFloat(style[`margin${capitalized_secondary_properties[0]}`]); const margin_start_value = parseFloat(style[`margin${capitalized_secondary_properties[0]}`]);
const margin_end_value = parseFloat(style[`margin${capitalized_secondary_properties[1]}`]); const margin_end_value = parseFloat(style[`margin${capitalized_secondary_properties[1]}`]);
const border_width_start_value = parseFloat( const border_width_start_value = parseFloat(style[`border${capitalized_secondary_properties[0]}Width`]);
style[`border${capitalized_secondary_properties[0]}Width`] const border_width_end_value = parseFloat(style[`border${capitalized_secondary_properties[1]}Width`]);
);
const border_width_end_value = parseFloat(
style[`border${capitalized_secondary_properties[1]}Width`]
);
return { return {
delay, delay,
duration, duration,
easing, easing,
css: (t) => css: (t) => 'overflow: hidden;' +
'overflow: hidden;' +
`opacity: ${Math.min(t * 20, 1) * opacity};` + `opacity: ${Math.min(t * 20, 1) * opacity};` +
`${primary_property}: ${t * primary_property_value}px;` + `${primary_property}: ${t * primary_property_value}px;` +
`padding-${secondary_properties[0]}: ${t * padding_start_value}px;` + `padding-${secondary_properties[0]}: ${t * padding_start_value}px;` +
@ -132,26 +82,16 @@ export function slide(
`border-${secondary_properties[1]}-width: ${t * border_width_end_value}px;` `border-${secondary_properties[1]}-width: ${t * border_width_end_value}px;`
}; };
} }
/** @param {Element} node
export interface ScaleParams { * @param {ScaleParams}
delay?: number; * @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").TransitionConfig}
duration?: number; */
easing?: EasingFunction; export function scale(node, { delay = 0, duration = 400, easing = cubicOut, start = 0, opacity = 0 } = {}) {
start?: number;
opacity?: number;
}
export function scale(
node: Element,
{ delay = 0, duration = 400, easing = cubicOut, start = 0, opacity = 0 }: ScaleParams = {}
): TransitionConfig {
const style = getComputedStyle(node); const style = getComputedStyle(node);
const target_opacity = +style.opacity; const target_opacity = +style.opacity;
const transform = style.transform === 'none' ? '' : style.transform; const transform = style.transform === 'none' ? '' : style.transform;
const sd = 1 - start; const sd = 1 - start;
const od = target_opacity * (1 - opacity); const od = target_opacity * (1 - opacity);
return { return {
delay, delay,
duration, duration,
@ -162,34 +102,27 @@ export function scale(
` `
}; };
} }
/** @param {SVGElement & { getTotalLength(): number }} node
export interface DrawParams { * @param {DrawParams}
delay?: number; * @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").TransitionConfig}
speed?: number; */
duration?: number | ((len: number) => number); export function draw(node, { delay = 0, speed, duration, easing = cubicInOut } = {}) {
easing?: EasingFunction;
}
export function draw(
node: SVGElement & { getTotalLength(): number },
{ delay = 0, speed, duration, easing = cubicInOut }: DrawParams = {}
): TransitionConfig {
let len = node.getTotalLength(); let len = node.getTotalLength();
const style = getComputedStyle(node); const style = getComputedStyle(node);
if (style.strokeLinecap !== 'butt') { if (style.strokeLinecap !== 'butt') {
len += parseInt(style.strokeWidth); len += parseInt(style.strokeWidth);
} }
if (duration === undefined) { if (duration === undefined) {
if (speed === undefined) { if (speed === undefined) {
duration = 800; duration = 800;
} else { }
else {
duration = len / speed; duration = len / speed;
} }
} else if (typeof duration === 'function') { }
else if (typeof duration === 'function') {
duration = duration(len); duration = duration(len);
} }
return { return {
delay, delay,
duration, duration,
@ -200,44 +133,24 @@ export function draw(
` `
}; };
} }
/**
export interface CrossfadeParams { * @param {CrossfadeParams & {
delay?: number; * fallback?: (node: Element, params: CrossfadeParams, intro: boolean) => TransitionConfig;
duration?: number | ((len: number) => number); * }}
easing?: EasingFunction; * @returns {[(node: any, params: import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").CrossfadeParams & { key: any; }) => () => import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").TransitionConfig, (node: any, params: import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").CrossfadeParams & { key: any; }) => () => import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").TransitionConfig]}
} */
export function crossfade({ fallback, ...defaults }) {
type ClientRectMap = Map<any, Element>; /** @type {ClientRectMap} */
const to_receive = new Map();
export function crossfade({ /** @type {ClientRectMap} */
fallback, const to_send = new Map();
...defaults /** @param {Element} from_node
}: CrossfadeParams & { * @param {Element} node
fallback?: (node: Element, params: CrossfadeParams, intro: boolean) => TransitionConfig; * @param {CrossfadeParams} params
}): [ * @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").TransitionConfig}
( */
node: Element, function crossfade(from_node, node, params) {
params: CrossfadeParams & { const { delay = 0, duration = (d) => Math.sqrt(d) * 30, easing = cubicOut } = assign(assign({}, defaults), params);
key: any;
}
) => () => TransitionConfig,
(
node: Element,
params: CrossfadeParams & {
key: any;
}
) => () => TransitionConfig
] {
const to_receive: ClientRectMap = new Map();
const to_send: ClientRectMap = new Map();
function crossfade(from_node: Element, node: Element, params: CrossfadeParams): TransitionConfig {
const {
delay = 0,
duration = (d) => Math.sqrt(d) * 30,
easing = cubicOut
} = assign(assign({}, defaults), params);
const from = from_node.getBoundingClientRect(); const from = from_node.getBoundingClientRect();
const to = node.getBoundingClientRect(); const to = node.getBoundingClientRect();
const dx = from.left - to.left; const dx = from.left - to.left;
@ -245,11 +158,9 @@ export function crossfade({
const dw = from.width / to.width; const dw = from.width / to.width;
const dh = from.height / to.height; const dh = from.height / to.height;
const d = Math.sqrt(dx * dx + dy * dy); const d = Math.sqrt(dx * dx + dy * dy);
const style = getComputedStyle(node); const style = getComputedStyle(node);
const transform = style.transform === 'none' ? '' : style.transform; const transform = style.transform === 'none' ? '' : style.transform;
const opacity = +style.opacity; const opacity = +style.opacity;
return { return {
delay, delay,
duration: is_function(duration) ? duration(d) : duration, duration: is_function(duration) ? duration(d) : duration,
@ -257,25 +168,24 @@ export function crossfade({
css: (t, u) => ` css: (t, u) => `
opacity: ${t * opacity}; opacity: ${t * opacity};
transform-origin: top left; transform-origin: top left;
transform: ${transform} translate(${u * dx}px,${u * dy}px) scale(${t + (1 - t) * dw}, ${ transform: ${transform} translate(${u * dx}px,${u * dy}px) scale(${t + (1 - t) * dw}, ${t + (1 - t) * dh});
t + (1 - t) * dh
});
` `
}; };
} }
/** @param {ClientRectMap} items
function transition(items: ClientRectMap, counterparts: ClientRectMap, intro: boolean) { * @param {ClientRectMap} counterparts
return (node: Element, params: CrossfadeParams & { key: any }) => { * @param {boolean} intro
* @returns {(node: any, params: import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").CrossfadeParams & { key: any; }) => () => import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").TransitionConfig}
*/
function transition(items, counterparts, intro) {
return (node, params) => {
items.set(params.key, node); items.set(params.key, node);
return () => { return () => {
if (counterparts.has(params.key)) { if (counterparts.has(params.key)) {
const other_node = counterparts.get(params.key); const other_node = counterparts.get(params.key);
counterparts.delete(params.key); counterparts.delete(params.key);
return crossfade(other_node, node, params); return crossfade(other_node, node, params);
} }
// if the node is disappearing altogether // if the node is disappearing altogether
// (i.e. wasn't claimed by the other list) // (i.e. wasn't claimed by the other list)
// then we need to supply an outro // then we need to supply an outro
@ -284,6 +194,61 @@ export function crossfade({
}; };
}; };
} }
return [transition(to_send, to_receive, false), transition(to_receive, to_send, true)]; return [transition(to_send, to_receive, false), transition(to_receive, to_send, true)];
} }
/** @typedef {(t: number) => number} EasingFunction */
/** @typedef {Map<any, Element>} ClientRectMap */
/** @typedef {Object} TransitionConfig
* @property {number} [delay]
* @property {number} [duration]
* @property {EasingFunction} [easing]
* @property {(t:number,u:number)=>string} [css]
* @property {(t:number,u:number)=>void} [tick]
*/
/** @typedef {Object} BlurParams
* @property {number} [delay]
* @property {number} [duration]
* @property {EasingFunction} [easing]
* @property {number|string} [amount]
* @property {number} [opacity]
*/
/** @typedef {Object} FadeParams
* @property {number} [delay]
* @property {number} [duration]
* @property {EasingFunction} [easing]
*/
/** @typedef {Object} FlyParams
* @property {number} [delay]
* @property {number} [duration]
* @property {EasingFunction} [easing]
* @property {number|string} [x]
* @property {number|string} [y]
* @property {number} [opacity]
*/
/** @typedef {Object} SlideParams
* @property {number} [delay]
* @property {number} [duration]
* @property {EasingFunction} [easing]
* @property {'x'|'y'} [axis]
*/
/** @typedef {Object} ScaleParams
* @property {number} [delay]
* @property {number} [duration]
* @property {EasingFunction} [easing]
* @property {number} [start]
* @property {number} [opacity]
*/
/** @typedef {Object} DrawParams
* @property {number} [delay]
* @property {number} [speed]
* @property {number|((len:number)=>number)} [duration]
* @property {EasingFunction} [easing]
*/
/** @typedef {Object} CrossfadeParams
* @property {number} [delay]
* @property {number|((len:number)=>number)} [duration]
* @property {EasingFunction} [easing]
*/
Loading…
Cancel
Save