UNPKG

6.34 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var easing = require('../easing');
6var internal = require('../internal');
7
8/*! *****************************************************************************
9Copyright (c) Microsoft Corporation. All rights reserved.
10Licensed under the Apache License, Version 2.0 (the "License"); you may not use
11this file except in compliance with the License. You may obtain a copy of the
12License at http://www.apache.org/licenses/LICENSE-2.0
13
14THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17MERCHANTABLITY OR NON-INFRINGEMENT.
18
19See the Apache Version 2.0 License for specific language governing permissions
20and limitations under the License.
21***************************************************************************** */
22
23function __rest(s, e) {
24 var t = {};
25 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
26 t[p] = s[p];
27 if (s != null && typeof Object.getOwnPropertySymbols === "function")
28 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
29 t[p[i]] = s[p[i]];
30 return t;
31}
32
33function fade(node, { delay = 0, duration = 400 }) {
34 const o = +getComputedStyle(node).opacity;
35 return {
36 delay,
37 duration,
38 css: t => `opacity: ${t * o}`
39 };
40}
41function fly(node, { delay = 0, duration = 400, easing: easing$$1 = easing.cubicOut, x = 0, y = 0, opacity = 0 }) {
42 const style = getComputedStyle(node);
43 const target_opacity = +style.opacity;
44 const transform = style.transform === 'none' ? '' : style.transform;
45 const od = target_opacity * (1 - opacity);
46 return {
47 delay,
48 duration,
49 easing: easing$$1,
50 css: (t, u) => `
51 transform: ${transform} translate(${(1 - t) * x}px, ${(1 - t) * y}px);
52 opacity: ${target_opacity - (od * u)}`
53 };
54}
55function slide(node, { delay = 0, duration = 400, easing: easing$$1 = easing.cubicOut }) {
56 const style = getComputedStyle(node);
57 const opacity = +style.opacity;
58 const height = parseFloat(style.height);
59 const padding_top = parseFloat(style.paddingTop);
60 const padding_bottom = parseFloat(style.paddingBottom);
61 const margin_top = parseFloat(style.marginTop);
62 const margin_bottom = parseFloat(style.marginBottom);
63 const border_top_width = parseFloat(style.borderTopWidth);
64 const border_bottom_width = parseFloat(style.borderBottomWidth);
65 return {
66 delay,
67 duration,
68 easing: easing$$1,
69 css: t => `overflow: hidden;` +
70 `opacity: ${Math.min(t * 20, 1) * opacity};` +
71 `height: ${t * height}px;` +
72 `padding-top: ${t * padding_top}px;` +
73 `padding-bottom: ${t * padding_bottom}px;` +
74 `margin-top: ${t * margin_top}px;` +
75 `margin-bottom: ${t * margin_bottom}px;` +
76 `border-top-width: ${t * border_top_width}px;` +
77 `border-bottom-width: ${t * border_bottom_width}px;`
78 };
79}
80function scale(node, { delay = 0, duration = 400, easing: easing$$1 = easing.cubicOut, start = 0, opacity = 0 }) {
81 const style = getComputedStyle(node);
82 const target_opacity = +style.opacity;
83 const transform = style.transform === 'none' ? '' : style.transform;
84 const sd = 1 - start;
85 const od = target_opacity * (1 - opacity);
86 return {
87 delay,
88 duration,
89 easing: easing$$1,
90 css: (_t, u) => `
91 transform: ${transform} scale(${1 - (sd * u)});
92 opacity: ${target_opacity - (od * u)}
93 `
94 };
95}
96function draw(node, { delay = 0, speed, duration, easing: easing$$1 = easing.cubicInOut }) {
97 const len = node.getTotalLength();
98 if (duration === undefined) {
99 if (speed === undefined) {
100 duration = 800;
101 }
102 else {
103 duration = len / speed;
104 }
105 }
106 else if (typeof duration === 'function') {
107 duration = duration(len);
108 }
109 return {
110 delay,
111 duration,
112 easing: easing$$1,
113 css: (t, u) => `stroke-dasharray: ${t * len} ${u * len}`
114 };
115}
116function crossfade(_a) {
117 var { fallback } = _a, defaults = __rest(_a, ["fallback"]);
118 const to_receive = new Map();
119 const to_send = new Map();
120 function crossfade(from, node, params) {
121 const { delay = 0, duration = d => Math.sqrt(d) * 30, easing: easing$$1 = easing.cubicOut } = internal.assign(internal.assign({}, defaults), params);
122 const to = node.getBoundingClientRect();
123 const dx = from.left - to.left;
124 const dy = from.top - to.top;
125 const d = Math.sqrt(dx * dx + dy * dy);
126 const style = getComputedStyle(node);
127 const transform = style.transform === 'none' ? '' : style.transform;
128 const opacity = +style.opacity;
129 return {
130 delay,
131 duration: internal.is_function(duration) ? duration(d) : duration,
132 easing: easing$$1,
133 css: (t, u) => `
134 opacity: ${t * opacity};
135 transform: ${transform} translate(${u * dx}px,${u * dy}px);
136 `
137 };
138 }
139 function transition(items, counterparts, intro) {
140 return (node, params) => {
141 items.set(params.key, {
142 rect: node.getBoundingClientRect()
143 });
144 return () => {
145 if (counterparts.has(params.key)) {
146 const { rect } = counterparts.get(params.key);
147 counterparts.delete(params.key);
148 return crossfade(rect, node, params);
149 }
150 // if the node is disappearing altogether
151 // (i.e. wasn't claimed by the other list)
152 // then we need to supply an outro
153 items.delete(params.key);
154 return fallback && fallback(node, params, intro);
155 };
156 };
157 }
158 return [
159 transition(to_send, to_receive, false),
160 transition(to_receive, to_send, true)
161 ];
162}
163
164exports.fade = fade;
165exports.fly = fly;
166exports.slide = slide;
167exports.scale = scale;
168exports.draw = draw;
169exports.crossfade = crossfade;