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: add useSpring hook #16

Merged
merged 3 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export * from "./use-motion";
export * from "./use-mount-effect";
export * from "./use-mouse";
export * from "./use-previous";
export * from "./use-spring";
export * from "./use-tagged";
export * from "./use-throttle-callback";
export * from "./use-throttle-effect";
Expand Down
38 changes: 38 additions & 0 deletions src/use-spring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## 🪝 `useSpring`

```ts
function useSpring<T extends MotionGoal>(goal: T | Binding<T>, options?: SpringOptions): Binding<T>
```

Applies spring animations to the given value, and updates the goal with the latest value on every re-render. Returns a binding that updates with the Motion.

### 📕 Parameters

- `goal` - The goal of the motor.
- `options` - Options for the spring (or a spring config).

### 📗 Returns

- A binding of the motor's value.

### 📘 Example

A button that fades in and out when hovered.

```tsx
function Button() {
const [springValue, setSpringValue] = useBinding(0)
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved
const hover = useSpring(springValue, config.spring.stiff);
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved

return (
<textbutton
Event={{
MouseEnter: () => setSpringValue(1),
MouseLeave: () => setSpringValue(0),
}}
Size={new UDim2(0, 100, 0, 100)}
BackgroundTransparency={hover.map((t) => lerp(0.8, 0.5, t))}
/>
);
}
```
1 change: 1 addition & 0 deletions src/use-spring/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./use-spring";
15 changes: 15 additions & 0 deletions src/use-spring/use-spring.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference types="@rbxts/testez/globals" />

import { renderHook } from "../utils/testez";
import { useSpring } from "./use-spring";

export = () => {
it("should return a binding", () => {
const { result, unmount } = renderHook(() => useSpring(0));

expect(result.current.getValue()).to.be.a("number");
expect(result.current.getValue()).to.equal(0);

unmount();
});
};
25 changes: 25 additions & 0 deletions src/use-spring/use-spring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Binding } from "@rbxts/react";
import { useRef } from "@rbxts/react";
import type { MotionGoal, SpringOptions } from "@rbxts/ripple";
import { useMotion } from "../use-motion";
import { getBindingValue } from "../utils/binding";
import { useEventListener } from "../use-event-listener";
import { RunService } from "@rbxts/services";

export function useSpring(goal: number | Binding<number>, options?: SpringOptions): Binding<number>;
export function useSpring<T extends MotionGoal>(goal: T | Binding<T>, options?: SpringOptions): Binding<T>;
export function useSpring(goal: MotionGoal | Binding<MotionGoal>, options?: SpringOptions) {
const [binding, motion] = useMotion(getBindingValue(goal));
const previousValue = useRef(getBindingValue(goal));

useEventListener(RunService.Heartbeat, () => {
const currentValue = getBindingValue(goal);

if (currentValue !== previousValue.current) {
previousValue.current = currentValue;
motion.spring(currentValue, options);
}
});

return binding;
}