|
1 | | -# solid-spring |
| 1 | + |
| 2 | +<h1 align="center">solid-spring</h1> |
| 3 | +<h3 align="center">A port of react-spring, for SolidJS</h3> |
| 4 | + |
| 5 | +`solid-spring` is a spring-physics first animation based on react-spring/core for SolidJS. |
| 6 | + |
| 7 | +> This an experimental project that was made to be submitted in hack.solidjs.com, feel free to create github ticket |
| 8 | +
|
| 9 | +The API looks like this: |
| 10 | + |
| 11 | +```jsx |
| 12 | +const styles = createSpring({ |
| 13 | + from: { |
| 14 | + opacity: 0 |
| 15 | + }, |
| 16 | + to: { |
| 17 | + opacity: 1 |
| 18 | + } |
| 19 | +}) |
| 20 | + |
| 21 | +<animated.div style={styles()} /> |
| 22 | +``` |
| 23 | + |
| 24 | +The API is similar to what we have in react-spring, with small differences to make the library compatible with SolidJS |
| 25 | + |
| 26 | + |
| 27 | +## Install |
| 28 | + |
| 29 | +```shell |
| 30 | +npm install solid-spring |
| 31 | +``` |
| 32 | +## API |
| 33 | + |
| 34 | +### `createSpring` |
| 35 | +> Turns values into animated-values. |
| 36 | +
|
| 37 | +```jsx |
| 38 | +import { createSpring, animated } from "solid-spring"; |
| 39 | + |
| 40 | +function ChainExample() { |
| 41 | + const styles = createSpring({ |
| 42 | + loop: true, |
| 43 | + to: [ |
| 44 | + { opacity: 1, color: '#ffaaee' }, |
| 45 | + { opacity: 0, color: 'rgb(14,26,19)' }, |
| 46 | + ], |
| 47 | + from: { opacity: 0, color: 'red' }, |
| 48 | + }) |
| 49 | + |
| 50 | + return <animated.div style={styles()}>I will fade in and out</animated.div> |
| 51 | +} |
| 52 | +``` |
| 53 | +`createSpring` also takes a function in case you want to pass a reactive value as a style! |
| 54 | +```jsx |
| 55 | +const [disabled, setDisabled] = createSignal(false) |
| 56 | + |
| 57 | +const styles = createSpring(() => ({ |
| 58 | + pause: disabled(), |
| 59 | +})) |
| 60 | +``` |
| 61 | +### `createSprings` |
| 62 | +> Creates multiple springs, each with its own config. Use it for static lists, etc. |
| 63 | +
|
| 64 | +Similar to `useSprings` in react-spring, It takes number or a function that returns a number (for reactivity) as the first argument, and a list of springs or a function that returns a spring as the second argument. |
| 65 | + |
| 66 | +```jsx |
| 67 | +function createSprings<Props extends CreateSpringsProps>( |
| 68 | + lengthFn: number | (() => number), |
| 69 | + props: Props[] & CreateSpringsProps<PickAnimated<Props>>[] |
| 70 | +): Accessor<SpringValues<PickAnimated<Props>>[]> & { |
| 71 | + ref: SpringRefType<PickAnimated<Props>>; |
| 72 | +}; |
| 73 | + |
| 74 | +function createSprings<Props extends CreateSpringsProps>( |
| 75 | + lengthFn: number | (() => number), |
| 76 | + props: (i: number, ctrl: Controller) => Props |
| 77 | +): Accessor<SpringValues<PickAnimated<Props>>[]> & { |
| 78 | + ref: SpringRefType<PickAnimated<Props>>; |
| 79 | +}; |
| 80 | +``` |
0 commit comments