Skip to content

feat: add springConfig prop to customize animation #251

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ Type: `boolean`

Disabled by default. By default, a user can expand the bottom sheet only by dragging a header or the overlay. This option enables expanding the bottom sheet on the content dragging.

### springConfig

Type: `{ mass: number; tension: number; friction: number }`

Helps you to customize the movement and speed of the animations.

```jsx
<BottomSheet
// Animation faster than the default
springConfig={{mass: 0.1, tension: 370, friction: 26}}
/>
```

## Events

All events receive `SpringEvent` as their argument. The payload varies, but `type` is always present, which can be `'OPEN' | 'RESIZE' | 'SNAP' | 'CLOSE'` depending on the scenario.
Expand Down
2 changes: 2 additions & 0 deletions src/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const BottomSheet = React.forwardRef<
blocking = true,
scrollLocking = true,
style,
springConfig,
onSpringStart,
onSpringCancel,
onSpringEnd,
Expand Down Expand Up @@ -173,6 +174,7 @@ export const BottomSheet = React.forwardRef<
friction,
friction + (friction - friction * velocity)
),
...springConfig,
},
onRest: (...args) => {
resolve(...args)
Expand Down
15 changes: 15 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ export type SpringEvent =
| { type: 'RESIZE'; source: ResizeSource }
| { type: 'SNAP'; source: 'dragging' | 'custom' | string }

/**
* Properties that can be used to customize the animation.
* see https://react-spring.dev/docs/advanced/config#config-visualizer
*/
export type SpringConfig = {
mass: number
tension: number
friction: number
}

export type Props = {
/**
* Ensure that whatever you put in here have at least 1px height, or else the bottom sheet won't open
Expand All @@ -55,6 +65,11 @@ export type Props = {
*/
sibling?: React.ReactNode

/**
* Pass the spring configurations (to change animation) in this format: { mass, tension, friction }.
*/
springConfig?: SpringConfig

/**
* Start a transition from closed to open, open to closed, or snap to snap.
* Return a promise or async to delay the start of the transition, just remember it can be cancelled.
Expand Down