/
context.js
61 lines (54 loc) · 1.82 KB
/
context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import React, { createContext, useMemo } from 'react'
import { isString } from 'lodash'
import PropTypes from 'prop-types'
import { config as presets } from 'react-spring'
export const motionConfigContext = createContext()
/**
* For now we're supporting both react-motion and react-spring,
* however, react-motion will be gradually replaced by react-spring.
*/
export const MotionConfigProvider = ({ children, animate, stiffness, damping, config }) => {
const value = useMemo(() => {
const reactSpringConfig = isString(config) ? presets[config] : config
return {
animate,
springConfig: { stiffness, damping },
config: reactSpringConfig,
}
}, [animate, stiffness, damping, config])
return <motionConfigContext.Provider value={value}>{children}</motionConfigContext.Provider>
}
MotionConfigProvider.propTypes = {
children: PropTypes.node.isRequired,
animate: PropTypes.bool,
stiffness: PropTypes.number,
damping: PropTypes.number,
config: PropTypes.oneOfType([
PropTypes.oneOf(Object.keys(presets)),
PropTypes.shape({
mass: PropTypes.number,
tension: PropTypes.number,
friction: PropTypes.number,
clamp: PropTypes.bool,
precision: PropTypes.number,
velocity: PropTypes.number,
duration: PropTypes.number,
easing: PropTypes.func,
}),
]),
}
export const MotionDefaultProps = {
animate: true,
stiffness: 90,
damping: 15,
config: 'default',
}
MotionConfigProvider.defaultProps = MotionDefaultProps