File size: 1,101 Bytes
1e92f2d |
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 |
import * as React from 'react'
import { useContext, PropsWithChildren } from 'react'
/**
* This context affects all new and existing `SpringValue` objects
* created with the hook API or the renderprops API.
*/
export interface ISpringContext {
/** Pause all new and existing animations. */
pause?: boolean
/** Force all new and existing animations to be immediate. */
immediate?: boolean
}
export const SpringContext = React.createContext<ISpringContext>({
pause: false,
immediate: false,
})
export const SpringContextProvider = ({
children,
...props
}: PropsWithChildren<ISpringContext>) => {
const inherited = useContext(SpringContext)
// Inherited values are dominant when truthy.
const pause = props.pause ?? inherited.pause ?? false
const immediate = props.immediate ?? inherited.immediate ?? false
// Memoize the context to avoid unwanted renders.
const contextValue = React.useMemo(
() => ({ pause, immediate }),
[pause, immediate]
)
return (
<SpringContext.Provider value={contextValue}>
{children}
</SpringContext.Provider>
)
}
|