UNPKG

3.47 kBJavaScriptView Raw
1// Follow https://material.google.com/motion/duration-easing.html#duration-easing-natural-easing-curves
2// to learn the context in which each easing should be used.
3export const easing = {
4 // This is the most common easing curve.
5 easeInOut: 'cubic-bezier(0.4, 0, 0.2, 1)',
6 // Objects enter the screen at full velocity from off-screen and
7 // slowly decelerate to a resting point.
8 easeOut: 'cubic-bezier(0.0, 0, 0.2, 1)',
9 // Objects leave the screen at full velocity. They do not decelerate when off-screen.
10 easeIn: 'cubic-bezier(0.4, 0, 1, 1)',
11 // The sharp curve is used by objects that may return to the screen at any time.
12 sharp: 'cubic-bezier(0.4, 0, 0.6, 1)'
13};
14
15// Follow https://m2.material.io/guidelines/motion/duration-easing.html#duration-easing-common-durations
16// to learn when use what timing
17export const duration = {
18 shortest: 150,
19 shorter: 200,
20 short: 250,
21 // most basic recommended timing
22 standard: 300,
23 // this is to be used in complex animations
24 complex: 375,
25 // recommended when something is entering screen
26 enteringScreen: 225,
27 // recommended when something is leaving screen
28 leavingScreen: 195
29};
30function formatMs(milliseconds) {
31 return `${Math.round(milliseconds)}ms`;
32}
33function getAutoHeightDuration(height) {
34 if (!height) {
35 return 0;
36 }
37 const constant = height / 36;
38
39 // https://www.desmos.com/calculator/vbrp3ggqet
40 return Math.min(Math.round((4 + 15 * constant ** 0.25 + constant / 5) * 10), 3000);
41}
42export default function createTransitions(inputTransitions) {
43 const mergedEasing = {
44 ...easing,
45 ...inputTransitions.easing
46 };
47 const mergedDuration = {
48 ...duration,
49 ...inputTransitions.duration
50 };
51 const create = (props = ['all'], options = {}) => {
52 const {
53 duration: durationOption = mergedDuration.standard,
54 easing: easingOption = mergedEasing.easeInOut,
55 delay = 0,
56 ...other
57 } = options;
58 if (process.env.NODE_ENV !== 'production') {
59 const isString = value => typeof value === 'string';
60 const isNumber = value => !Number.isNaN(parseFloat(value));
61 if (!isString(props) && !Array.isArray(props)) {
62 console.error('MUI: Argument "props" must be a string or Array.');
63 }
64 if (!isNumber(durationOption) && !isString(durationOption)) {
65 console.error(`MUI: Argument "duration" must be a number or a string but found ${durationOption}.`);
66 }
67 if (!isString(easingOption)) {
68 console.error('MUI: Argument "easing" must be a string.');
69 }
70 if (!isNumber(delay) && !isString(delay)) {
71 console.error('MUI: Argument "delay" must be a number or a string.');
72 }
73 if (typeof options !== 'object') {
74 console.error(['MUI: Secong argument of transition.create must be an object.', "Arguments should be either `create('prop1', options)` or `create(['prop1', 'prop2'], options)`"].join('\n'));
75 }
76 if (Object.keys(other).length !== 0) {
77 console.error(`MUI: Unrecognized argument(s) [${Object.keys(other).join(',')}].`);
78 }
79 }
80 return (Array.isArray(props) ? props : [props]).map(animatedProp => `${animatedProp} ${typeof durationOption === 'string' ? durationOption : formatMs(durationOption)} ${easingOption} ${typeof delay === 'string' ? delay : formatMs(delay)}`).join(',');
81 };
82 return {
83 getAutoHeightDuration,
84 create,
85 ...inputTransitions,
86 easing: mergedEasing,
87 duration: mergedDuration
88 };
89}
\No newline at end of file