Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable SpringRef.set to be called with a function #2069

Merged
merged 3 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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