Download react-spring/packages/animated/src/createHost.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 2.16 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-spring/packages/animated/src/createHost.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-spring/packages/animated/src/createHost.ts
-
curl -L -o createHost.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-spring/packages/animated/src/createHost.ts
2.16 kB
| import { Lookup } from '@react-spring/types' | |
| import { is, eachProp } from '@react-spring/shared' | |
| import { AnimatableComponent, withAnimated } from './withAnimated' | |
| import { Animated } from './Animated' | |
| import { AnimatedObject } from './AnimatedObject' | |
| export interface HostConfig { | |
| /** Provide custom logic for native updates */ | |
| applyAnimatedValues: (node: any, props: Lookup) => boolean | void | |
| /** Wrap the `style` prop with an animated node */ | |
| createAnimatedStyle: (style: Lookup) => Animated | |
| /** Intercept props before they're passed to an animated component */ | |
| getComponentProps: (props: Lookup) => typeof props | |
| } | |
| // A stub type that gets replaced by @react-spring/web and others. | |
| type WithAnimated = { | |
| (Component: AnimatableComponent): any | |
| [key: string]: any | |
| } | |
| // For storing the animated version on the original component | |
| const cacheKey = Symbol.for('AnimatedComponent') | |
| export const createHost = ( | |
| components: AnimatableComponent[] | { [key: string]: AnimatableComponent }, | |
| { | |
| applyAnimatedValues = () => false, | |
| createAnimatedStyle = style => new AnimatedObject(style), | |
| getComponentProps = props => props, | |
| }: Partial<HostConfig> = {} | |
| ) => { | |
| const hostConfig: HostConfig = { | |
| applyAnimatedValues, | |
| createAnimatedStyle, | |
| getComponentProps, | |
| } | |
| const animated: WithAnimated = (Component: any) => { | |
| const displayName = getDisplayName(Component) || 'Anonymous' | |
| if (is.str(Component)) { | |
| Component = | |
| animated[Component] || | |
| (animated[Component] = withAnimated(Component, hostConfig)) | |
| } else { | |
| Component = | |
| Component[cacheKey] || | |
| (Component[cacheKey] = withAnimated(Component, hostConfig)) | |
| } | |
| Component.displayName = `Animated(${displayName})` | |
| return Component | |
| } | |
| eachProp(components, (Component, key) => { | |
| if (is.arr(components)) { | |
| key = getDisplayName(Component)! | |
| } | |
| animated[key] = animated(Component) | |
| }) | |
| return { | |
| animated, | |
| } | |
| } | |
| const getDisplayName = (arg: AnimatableComponent) => | |
| is.str(arg) | |
| ? arg | |
| : arg && is.str(arg.displayName) | |
| ? arg.displayName | |
| : (is.fun(arg) && arg.name) || null | |