Skip to content

Commit

Permalink
feat: enable SpringRef.set to be called with a function (#2069)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaellis committed Feb 6, 2023
1 parent 07b229c commit cd80814
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
16 changes: 16 additions & 0 deletions .changeset/strange-falcons-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@react-spring/core': minor
'@react-spring/animated': minor
'@react-spring/parallax': minor
'@react-spring/rafz': minor
'react-spring': minor
'@react-spring/shared': minor
'@react-spring/types': minor
'@react-spring/konva': minor
'@react-spring/native': minor
'@react-spring/three': minor
'@react-spring/web': minor
'@react-spring/zdog': minor
---

feat: add function to SpringRef.set
4 changes: 3 additions & 1 deletion docs/app/routes/docs/advanced/spring-ref.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ resume(keys?: OneOrMore<string>): this
### Set
Update the state of each controller without animating.
Update the state of each controller without animating. Accepts either a partial state object or a
function that returns a partial state object.
```ts
set(values: Partial<State>): void
set(values: (index: number, ctrl: Controller<State>) => Partial<State>): void
```
### Start
Expand Down
17 changes: 14 additions & 3 deletions packages/core/src/SpringRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface SpringRef<State extends Lookup = Lookup> {

/** Update the state of each controller without animating. */
set(values: Partial<State>): void
/** Update the state of each controller without animating based on their passed state. */
set(values: (index: number, ctrl: Controller<State>) => Partial<State>): void

/** Start the queued animations of each controller. */
start(): AsyncResult<Controller<State>>[]
Expand Down Expand Up @@ -126,8 +128,17 @@ export const SpringRef = <
}

/** Update the state of each controller without animating. */
SpringRef.set = function (values: Partial<State>) {
each(current, ctrl => ctrl.set(values))
SpringRef.set = function (
values:
| Partial<State>
| ((i: number, ctrl: Controller<State>) => Partial<State>)
) {
each(current, (ctrl, i) => {
const update = is.fun(values) ? values(i, ctrl) : values
if (update) {
ctrl.set(update)
}
})
}

SpringRef.start = function (props?: object | ControllerUpdateFn<State>) {
Expand Down Expand Up @@ -163,7 +174,7 @@ export const SpringRef = <
arg: ControllerUpdate<State> | ControllerUpdateFn<State>,
ctrl: Controller<State>,
index: number
): ControllerUpdate<State> | Falsy {
) {
return is.fun(arg) ? arg(index, ctrl) : arg
}

Expand Down

1 comment on commit cd80814

@vercel
Copy link

@vercel vercel bot commented on cd80814 Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.