Skip to content

Commit a6206e4

Browse files
committed
make solid-spring reactive
1 parent bac14fc commit a6206e4

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

demo/src/App.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
import {
22
Component,
3+
createSignal,
34
} from "solid-js";
4-
import { createSpring, animated } from "solid-spring";
5+
import { createSpring, animated, config } from "solid-spring";
56

67
function ChainExample() {
7-
const styles = createSpring({
8-
loop: true,
9-
to: [
10-
{ opacity: 1, color: '#ffaaee' },
11-
{ opacity: 0, color: 'rgb(14,26,19)' },
12-
],
13-
from: { opacity: 0, color: 'red' },
14-
})
8+
const [flip, set] = createSignal(false);
159

16-
return <animated.div style={styles()}>I will fade in and out</animated.div>
10+
const styles = createSpring(() => {
11+
return {
12+
to: { opacity: 1 },
13+
from: { opacity: 0 },
14+
reset: true,
15+
reverse: flip(),
16+
delay: 200,
17+
config: config.molasses,
18+
onRest: () => {
19+
set(!flip());
20+
},
21+
};
22+
});
23+
24+
return <animated.h1 style={styles()}>hello</animated.h1>;
1725
}
1826

1927
const App: Component = () => {
20-
return (
21-
<ChainExample />
22-
);
28+
return <ChainExample />;
2329
};
2430

2531
export default App;

spring/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@
3434
"devDependencies": {
3535
"rollup-plugin-dts": "^4.2.0",
3636
"solid-js": "^1.3.13"
37+
},
38+
"peerDependencies": {
39+
"solid-js": "^1.3.13"
3740
}
3841
}

spring/rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const external = [
1313
...Object.keys(pkg.peerDependencies || []),
1414
"worker_threads",
1515
"esbuild",
16+
"solid-js",
17+
"solid-js/web",
1618
"fs/promises",
1719
];
1820

spring/src/solid/createSpring.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Accessor, createMemo } from "solid-js";
1+
import { Accessor, createEffect, createMemo } from "solid-js";
22
import { SpringRef } from "../SpringRef";
33
import type { SpringRef as SpringRefType } from "../SpringRef";
44
import {
@@ -43,7 +43,7 @@ export function createSpring<Props extends object>(
4343
};
4444

4545
export function createSpring(props: any): any {
46-
const fn = is.fun(props) ? props : () => props;
46+
const fn: Accessor<any> = createMemo(is.fun(props) ? props : () => props);
4747

4848
const springsFn = createSprings(1, fn);
4949
const springMemo = createMemo(() => {

spring/src/solid/createSprings.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
createEffect,
2323
createMemo,
2424
createRenderEffect,
25+
createSignal,
2526
onCleanup,
2627
} from "solid-js";
2728
import { declareUpdate } from "../SpringValue";
@@ -110,13 +111,15 @@ export function createSprings<Props extends CreateSpringsProps>(
110111

111112
// Cache old controllers to dispose in the commit phase.
112113
const prevLength = lengthFn() || 0;
114+
const [update, setUpdate] = createSignal(Symbol())
113115

114116
// Update existing controllers when "deps" are changed.
115117
createRenderEffect(() => {
116118
const length = lengthFn();
117119
declareUpdates(0, Math.min(prevLength, length));
118120
});
119121

122+
120123
/** Fill the `updates` array with declarative updates for the given index range. */
121124
function declareUpdates(startIndex: number, endIndex: number) {
122125
for (let i = startIndex; i < endIndex; i++) {
@@ -130,6 +133,7 @@ export function createSprings<Props extends CreateSpringsProps>(
130133
updates[i] = declareUpdate(update);
131134
}
132135
}
136+
setUpdate(Symbol())
133137
}
134138

135139
// New springs are created during render so users can pass them to
@@ -138,6 +142,8 @@ export function createSprings<Props extends CreateSpringsProps>(
138142
const springs = ctrls.map((ctrl, i) => getSprings(ctrl, updates[i]));
139143

140144
createRenderEffect(() => {
145+
update()
146+
141147
layoutId++;
142148

143149
// Replace the cached controllers.

spring/src/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { AnimatedArray } from "./AnimatedArray";
88
import { AnimatedString } from "./AnimatedString";
99
import { AnimatedValue, getAnimated } from "./animated";
1010
import { InferState } from "./runAsync";
11-
import { Component } from "solid-js";
1211

1312
// types
1413
export class Any {

0 commit comments

Comments
 (0)