1
+ import { CSSProperties , RefObject } from 'react'
1
2
import {
2
3
SpringConfig ,
3
4
SpringBaseProps ,
@@ -6,6 +7,10 @@ import {
6
7
} from './universal'
7
8
export { SpringConfig , SpringBaseProps , TransitionKeyProps , State }
8
9
10
+ export { config , interpolate } from './universal'
11
+ // hooks are currently web-only
12
+ export { animated } from './web'
13
+
9
14
/** List from `function getForwardProps` in `src/shared/helpers` */
10
15
type ExcludedProps =
11
16
| 'to'
@@ -43,18 +48,34 @@ export type SetUpdateFn<DS extends object> = (ds: DS) => void
43
48
44
49
// The hooks do emulate React's 'ref' by accepting { ref?: React.RefObject<Controller> } and
45
50
// updating it. However, there are no types for Controller, and I assume it is intentionally so.
51
+ // This is a partial interface for Controller that has only the properties needed for useChain to work.
52
+ export interface ReactSpringHook {
53
+ start ( ) : void
54
+ stop ( ) : void
55
+ }
56
+
57
+ export function useChain ( refs : ReadonlyArray < RefObject < ReactSpringHook > > ) : void
58
+ // this looks like it can just be a single overload, but we don't want to allow
59
+ // timeFrame to be specifiable when timeSteps is explicitly "undefined"
60
+ export function useChain (
61
+ refs : ReadonlyArray < RefObject < ReactSpringHook > > ,
62
+ timeSteps : number [ ] ,
63
+ timeFrame ?: number
64
+ ) : void
65
+
46
66
export interface HooksBaseProps
47
67
extends Pick < SpringBaseProps , Exclude < keyof SpringBaseProps , 'config' > > {
48
68
// there is an undocumented onKeyframesHalt which passes the controller instance,
49
69
// so it also cannot be typed unless Controller types are written
70
+ ref ?: React . RefObject < ReactSpringHook >
50
71
}
51
72
52
73
export interface UseSpringBaseProps extends HooksBaseProps {
53
74
config ?: SpringBaseProps [ 'config' ]
54
75
}
55
76
56
- export type UseSpringProps < DS extends UseSpringBaseProps > = Merge <
57
- DS ,
77
+ export type UseSpringProps < DS extends object > = Merge <
78
+ DS & UseSpringBaseProps ,
58
79
{
59
80
from ?: InferFrom < DS >
60
81
/**
@@ -65,6 +86,12 @@ export type UseSpringProps<DS extends UseSpringBaseProps> = Merge<
65
86
>
66
87
67
88
// there's a third value in the tuple but it's not public API (?)
89
+ export function useSpring < DS extends CSSProperties > (
90
+ values : UseSpringProps < DS & CSSProperties >
91
+ ) : ForwardedProps < DS >
92
+ export function useSpring < DS extends CSSProperties > (
93
+ getProps : ( ) => UseSpringProps < DS & CSSProperties >
94
+ ) : [ ForwardedProps < DS > , SetUpdateFn < DS > ]
68
95
export function useSpring < DS extends object > (
69
96
getProps : ( ) => UseSpringProps < DS >
70
97
) : [ ForwardedProps < DS > , SetUpdateFn < DS > ]
@@ -73,6 +100,14 @@ export function useSpring<DS extends object>(
73
100
) : ForwardedProps < DS >
74
101
75
102
// there's a third value in the tuple but it's not public API (?)
103
+ export function useTrail < DS extends CSSProperties > (
104
+ count : number ,
105
+ getProps : ( ) => UseSpringProps < DS & CSSProperties >
106
+ ) : [ ForwardedProps < DS > [ ] , SetUpdateFn < DS > ]
107
+ export function useTrail < DS extends CSSProperties > (
108
+ count : number ,
109
+ values : UseSpringProps < DS & CSSProperties >
110
+ ) : ForwardedProps < DS > [ ] // safe to modify (result of .map)
76
111
export function useTrail < DS extends object > (
77
112
count : number ,
78
113
getProps : ( ) => UseSpringProps < DS >
@@ -132,6 +167,9 @@ export interface UseTransitionResult<TItem, DS extends object> {
132
167
props : ForwardedProps < DS >
133
168
}
134
169
170
+ export function useTransition < TItem , DS extends CSSProperties > (
171
+ values : Merge < DS & CSSProperties , UseTransitionProps < TItem , DS > >
172
+ ) : UseTransitionResult < TItem , ForwardedProps < DS > > [ ] // result array is safe to modify
135
173
export function useTransition < TItem , DS extends object > (
136
174
values : Merge < DS , UseTransitionProps < TItem , DS > >
137
175
) : UseTransitionResult < TItem , ForwardedProps < DS > > [ ] // result array is safe to modify
@@ -179,30 +217,63 @@ export namespace useKeyframes {
179
217
180
218
// fun fact: the state is literally named "undefined" if you're using this overload
181
219
// the docs are vague but it seems this just loops the one animation forever?
220
+ export function spring < DS extends CSSProperties > (
221
+ animation :
222
+ | ReadonlyArray < DS & CSSProperties >
223
+ | ReadonlyArray < KeyframeFn < DS & CSSProperties > >
224
+ | KeyframeFn < DS & CSSProperties > ,
225
+ initialProps ?: UseSpringProps < DS & CSSProperties >
226
+ ) : UseSpringKeyframes < DS & CSSProperties >
182
227
export function spring < DS extends object > (
183
228
animation :
184
229
| ReadonlyArray < DS >
185
230
| ReadonlyArray < KeyframeFn < DS > >
186
231
| KeyframeFn < DS > ,
187
232
initialProps ?: UseSpringProps < DS >
188
233
) : UseSpringKeyframes < DS >
234
+ // this overload does provide CSSProperties autocompletes, but only as long as config, delay, onRest
235
+ // et al are not specified. It's not possible to say TSlotNames can be
236
+ // "string but not keyof SpringKeyframeSlotsConfig".
237
+ export function spring <
238
+ TSlotNames extends string ,
239
+ TSlots extends Record < TSlotNames , CSSProperties >
240
+ > (
241
+ slots : TSlots &
242
+ Record < TSlotNames , CSSProperties > &
243
+ SpringKeyframeSlotsConfig ,
244
+ initialProps ?: UseSpringProps < CSSProperties >
245
+ ) : UseSpringKeyframesWithSlots < TSlots >
189
246
// unfortunately, it's not possible to infer the type of the callback functions (if any are given)
190
- // while also remaining possible to infer the slot names . Callback functions have to be cast with
247
+ // while also remaining possible to infer the slot contents . Callback functions have to be cast with
191
248
// `as useKeyframes.KeyframeFn<{ ... }>`.
192
249
// it's also not possible to specify the types of the values inside TSlots. This is a mess.
193
- export function spring < TSlots extends object > (
250
+ export function spring <
251
+ TSlotNames extends string ,
252
+ TSlots extends Record < TSlotNames , any >
253
+ > (
194
254
slots : TSlots & SpringKeyframeSlotsConfig ,
195
255
// also unfortunately not possible to strongly type this either
196
256
initialProps ?: UseSpringProps < any >
197
257
) : UseSpringKeyframesWithSlots < TSlots >
198
258
259
+ export function trail < DS extends CSSProperties > (
260
+ animation :
261
+ | ReadonlyArray < DS & CSSProperties >
262
+ | ReadonlyArray < KeyframeFn < DS & CSSProperties > >
263
+ | KeyframeFn < DS & CSSProperties > ,
264
+ initialProps ?: UseSpringProps < DS >
265
+ ) : UseTrailKeyframes < DS >
199
266
export function trail < DS extends object > (
200
267
animation :
201
268
| ReadonlyArray < DS >
202
269
| ReadonlyArray < KeyframeFn < DS > >
203
270
| KeyframeFn < DS > ,
204
271
initialProps ?: UseSpringProps < DS >
205
272
) : UseTrailKeyframes < DS >
273
+ // we unfortunately can't use the same trick in trail as we did in spring to have autocomplete for
274
+ // CSS properties, for the same reason the spring trick stops working when a config key is given.
275
+ // It would fail as soon as `items`, a required property, is specified, as its value
276
+ // (necessarily an array-like) would not be compatible with CSSProperties.
206
277
export function trail < TItem , TSlots extends object > (
207
278
slots : TSlots & TrailKeyframeSlotsConfig < TItem > ,
208
279
initialProps ?: UseSpringProps < any >
0 commit comments