Skip to content

Commit 5616878

Browse files
drcmdaaleclarson
authored andcommitted
feat: new hooks api
1 parent ca39e75 commit 5616878

File tree

8 files changed

+45
-41
lines changed

8 files changed

+45
-41
lines changed

packages/core/src/legacy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { useTransition } from './useTransition'
55
import { is } from 'shared'
66

77
export function Spring({ children, ...props }) {
8-
return children(useSpring(props))
8+
return children(useSpring(props)[0])
99
}
1010

1111
export function Trail({ items, children, ...props }) {
12-
const trails = useTrail(items.length, props)
12+
const [trails] = useTrail(items.length, props)
1313
return items.map((item, index) => {
1414
const result = children(item, index)
1515
return is.fun(result) ? result(trails[index]) : result

packages/core/src/useSpring.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { useSprings } from './useSprings'
22
import { is } from 'shared'
33

44
/** API
5-
* const props = useSpring({ ... })
6-
* const [props, set] = useSpring(() => ({ ... }))
5+
* const [props, set, cancel] = useSpring(props, [optionalDeps])
6+
* const [props, set, cancel] = useSpring(() => props, [optionalDeps])
77
*/
88

99
export const useSpring = (props, deps) => {
1010
const isFn = is.fun(props)
1111
const [result, set, stop] = useSprings(1, isFn ? props : [props], deps)
12-
return isFn ? [result[0], set, stop] : result
12+
return [result[0], set, stop]
1313
}

packages/core/src/useSprings.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { useMemoOne } from 'use-memo-one'
55
import { Controller } from './Controller'
66

77
/** API
8-
* const props = useSprings(number, [{ ... }, { ... }, ...])
9-
* const [props, set] = useSprings(number, (i, controller) => ({ ... }))
8+
* const [props, set, cancel] = useSprings(number, props, [optionalDeps])
9+
* const [props, set, cancel] = useSprings(number, [props], [optionalDeps])
10+
* const [props, set, cancel] = useSprings(number, index => props, [optionalDeps])
1011
*/
1112

1213
export const useSprings = (length, propsArg, deps) => {
@@ -90,5 +91,5 @@ export const useSprings = (length, propsArg, deps) => {
9091
})
9192

9293
const values = springs.map(s => ({ ...s.animated }))
93-
return isFn ? [values, update, stop] : values
94+
return [values, update, stop]
9495
}

packages/core/src/useTrail.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { useSprings } from './useSprings'
55
import { callProp } from './helpers'
66

77
/** API
8-
* const trails = useTrail(number, { ... })
9-
* const [trails, set] = useTrail(number, () => ({ ... }))
8+
* const [trails, set, cancel] = useTrail(number, props, [optionalDeps])
9+
* const [trails, set, cancel] = useTrail(number, () => props, [optionalDeps])
1010
*/
1111

12-
export const useTrail = (length, propsArg) => {
12+
export const useTrail = (length, propsArg, deps) => {
1313
const hasNewSprings = length !== usePrev(length)
1414
const isFn = is.fun(propsArg)
1515

@@ -22,22 +22,24 @@ export const useTrail = (length, propsArg) => {
2222
if (hasNewSprings) springs.length = length
2323

2424
// The controllers are recreated whenever `length` changes.
25-
const [values, animate, stop] = useSprings(length, (i, spring) => {
26-
if (isFn && !props) {
27-
props = callProp(propsArg, spring) || {}
28-
}
29-
springs[i] = spring
30-
return {
31-
...props,
32-
...(i > 0 && {
33-
attach: () => springs[i - 1],
25+
const [values, animate, stop] = useSprings(
26+
length,
27+
(i, spring) => {
28+
if (isFn && !props) {
29+
props = callProp(propsArg, spring) || {}
30+
}
31+
springs[i] = spring
32+
return {
33+
...props,
34+
parent: i > 0 ? springs[i - 1] : null,
35+
config: callProp(props.config, i),
3436
onStart: withArgument(props.onStart, i),
3537
onFrame: withArgument(props.onFrame, i),
3638
onRest: withArgument(props.onRest, i),
37-
}),
38-
config: callProp(props.config, i),
39-
}
40-
})
39+
}
40+
},
41+
deps
42+
)
4143

4244
/** For imperative updates to the props of all springs in the trail */
4345
const update = useCallbackOne(
@@ -62,8 +64,7 @@ export const useTrail = (length, propsArg) => {
6264
}
6365
})
6466

65-
// Return the update/stop functions when the `propsArg` is a function.
66-
return isFn ? [values, update, stop] : values
67+
return [values, update, stop]
6768
}
6869

6970
function withArgument(fn, arg) {

packages/core/src/useTransition.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { is, useForceUpdate, useOnce } from 'shared'
44
import { Controller } from './Controller'
55

66
/** API
7-
* const transitions = useTransition(items, itemKeys, { ... })
8-
* const [transitions, update] = useTransition(items, itemKeys, () => ({ ... }))
7+
* const transitions = useTransition(items, itemKeys, props)
8+
* const transitions = useTransition(items, itemKeys, () => props)
99
*/
1010

1111
let guid = 0
@@ -25,6 +25,8 @@ const makeConfig = props => {
2525
}
2626

2727
export function useTransition(input, keyTransform, props) {
28+
// Coerce props to an object
29+
props = useMemo(() => callProp(props), [])
2830
props = makeConfig({
2931
...props,
3032
items: input,

targets/web/src/__tests__/useSpring.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '..';
1212

1313
test('infer return type via forward prop', () => {
14-
const props = useSpring({ width: 0, delay: 1000 });
14+
const [props] = useSpring({ width: 0, delay: 1000 });
1515
assert(props, _ as {
1616
[key: string]: SpringValue<any>;
1717
width: SpringValue<number>;
@@ -24,7 +24,7 @@ test('infer return type via forward prop', () => {
2424
});
2525

2626
test('infer return type via "from" prop', () => {
27-
const props = useSpring({
27+
const [props] = useSpring({
2828
from: { width: 0 },
2929
});
3030
assert(props, _ as {
@@ -34,7 +34,7 @@ test('infer return type via "from" prop', () => {
3434
});
3535

3636
test('infer return type via "to" prop', () => {
37-
const props = useSpring({
37+
const [props] = useSpring({
3838
to: { width: 0 },
3939
});
4040
assert(props, _ as {
@@ -44,7 +44,7 @@ test('infer return type via "to" prop', () => {
4444
});
4545

4646
test('infer return type via "from" and "to" props', () => {
47-
const props = useSpring({
47+
const [props] = useSpring({
4848
from: { width: 0 },
4949
to: { height: '100%' },
5050
});
@@ -56,7 +56,7 @@ test('infer return type via "from" and "to" props', () => {
5656
});
5757

5858
test('infer return type via "from" and forward props', () => {
59-
const props = useSpring({
59+
const [props] = useSpring({
6060
from: { width: 0 },
6161
height: '100%',
6262
});
@@ -68,7 +68,7 @@ test('infer return type via "from" and forward props', () => {
6868
});
6969

7070
test('infer animated array', () => {
71-
const props = useSpring({
71+
const [props] = useSpring({
7272
to: { foo: [0, 0] },
7373
});
7474
assert(props, _ as {
@@ -166,7 +166,7 @@ test('spring refs', () => {
166166
});
167167

168168
test('basic config', () => {
169-
const props = useSpring({
169+
const [props] = useSpring({
170170
from: { width: 0 },
171171
reset: true,
172172
delay: 1000,
@@ -187,7 +187,7 @@ test('basic config', () => {
187187
});
188188

189189
test('function as "to" prop', () => {
190-
const props = useSpring({
190+
const [props] = useSpring({
191191
to: async next => {
192192
assert(next, _ as SpringUpdateFn);
193193

@@ -209,7 +209,7 @@ test('function as "to" prop', () => {
209209
});
210210

211211
test('with "from" prop', () => {
212-
const props = useSpring({
212+
const [props] = useSpring({
213213
from: { foo: 1 },
214214
to: async next => {
215215
assert(next, _ as SpringUpdateFn); // FIXME: should be "SpringUpdateFn<{ foo: number }>"
@@ -229,7 +229,7 @@ test('function as "to" prop', () => {
229229

230230
test('array as "to" prop', () => {
231231
// ⚠️ Animated keys are not inferred when "to" is an array (unless "from" exists)
232-
const props = useSpring({
232+
const [props] = useSpring({
233233
to: [{ opacity: 1 }, { opacity: 0 }],
234234
foo: 0, // ️️⚠️ This key is ignored because "to" exists
235235
});
@@ -239,7 +239,7 @@ test('array as "to" prop', () => {
239239
});
240240

241241
test('with "from" prop', () => {
242-
const props = useSpring({
242+
const [props] = useSpring({
243243
to: [{ opacity: 1 }, { opacity: 0 }],
244244
from: { opacity: 0 },
245245
});

targets/web/src/__tests__/useSprings.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useSprings, SpringValue, SpringUpdateFn, SpringStopFn } from '..';
44
const items: string[] = [];
55

66
test('pass an array', () => {
7-
const springs = useSprings(
7+
const [springs] = useSprings(
88
items.length,
99
items.map(item => {
1010
assert(item, _ as string);

targets/web/src/__tests__/useTrail.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assert, test, _ } from 'spec.ts';
22
import { useTrail, SpringValue, SpringUpdateFn, SpringStopFn } from '..';
33

44
test('basic usage', () => {
5-
const springs = useTrail(3, { opacity: 1 });
5+
const [springs] = useTrail(3, { opacity: 1 });
66
assert(springs, _ as Array<{
77
[key: string]: SpringValue<any>;
88
opacity: SpringValue<number>;

0 commit comments

Comments
 (0)