-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
context.js
49 lines (41 loc) · 1.38 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
import { createContext, useMemo } from 'react'
import isString from 'lodash/isString'
import PropTypes from 'prop-types'
import { config as presets } from '@react-spring/web'
export const motionConfigContext = createContext()
export const motionDefaultProps = {
animate: true,
config: 'default',
}
export const MotionConfigProvider = props => {
const { children, animate = true, config = 'default' } = props
const value = useMemo(() => {
const reactSpringConfig = isString(config) ? presets[config] : config
return {
animate,
config: reactSpringConfig,
}
}, [animate, config])
return <motionConfigContext.Provider value={value}>{children}</motionConfigContext.Provider>
}
export const motionPropTypes = {
animate: PropTypes.bool,
motionConfig: 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,
}),
]),
}
MotionConfigProvider.propTypes = {
children: PropTypes.node.isRequired,
animate: motionPropTypes.animate,
config: motionPropTypes.motionConfig,
}