Skip to content

Commit 1afe79f

Browse files
committed
feat: override source and velocity in snapTo
1 parent 3a6d9ce commit 1afe79f

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

README.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Fires whenever there's been a window resize event, or if the header, footer or c
195195
196196
#### SNAP
197197
198-
Type: `{ source: 'dragging' | 'custom' }`
198+
Type: `{ source: 'dragging' | 'custom' | string }`
199199
200200
Fired after dragging ends, or when calling `ref.snapTo`, and a transition to a valid snap point is happening.
201201
@@ -205,7 +205,7 @@ Fired after dragging ends, or when calling `ref.snapTo`, and a transition to a v
205205
function Example() {
206206
return (
207207
<BottomSheet
208-
onSpringStart={async (event) => {
208+
onSpringStart={(event) => {
209209
if (event.type === 'SNAP' && event.source === 'dragging') {
210210
console.log('Starting a spring animation to user selected snap point')
211211
}
@@ -215,6 +215,33 @@ function Example() {
215215
}
216216
```
217217
218+
When using `snapTo` it's possible to use a different `source` than `'custom'`:
219+
220+
```jsx
221+
function Example() {
222+
const sheetRef = useRef()
223+
return (
224+
<BottomSheet
225+
ref={sheetRef}
226+
snapPoints={({ minHeight, maxHeight }) => [minHeight, maxHeight]}
227+
onSpringEnd={(event) => {
228+
if (event.type === 'SNAP' && event.source === 'snap-to-bottom') {
229+
console.log(
230+
'Just finished an imperativ transition to the bottom snap point'
231+
)
232+
}
233+
}}
234+
>
235+
<button
236+
onClick={() => sheetRef.current.snapTo(0, { source: 'snap-to-bottom' })}
237+
>
238+
Snap to bottom
239+
</button>
240+
</BottomSheet>
241+
)
242+
}
243+
```
244+
218245
### onSpringEnd
219246
220247
Type: `(event: SpringEvent) => void`
@@ -242,7 +269,7 @@ export default function Example() {
242269
243270
### snapTo
244271
245-
Type: `(numberOrCallback: number | (state => number)) => void`
272+
Type: `(numberOrCallback: number | (state => number)) => void, options?: {source?: string, velocity?: number}`
246273
247274
Same signature as the `defaultSnap` prop, calling it will animate the sheet to the new snap point you return. You can either call it with a number, which is the height in px (it'll select the closest snap point that matches your value): `ref.current.snapTo(200)`. Or:
248275
@@ -255,6 +282,16 @@ ref.current.snapTo(({ // Showing all the available props
255282
)
256283
```
257284
285+
There's an optional second argument you can use to override `event.source`, as well as changing the `velocity`:
286+
287+
```js
288+
ref.current.snapTo(({ snapPoints }) => Math.min(...snapPoints), {
289+
// Each property is optional, here showing their default values
290+
source: 'custom',
291+
velocity: 1,
292+
})
293+
```
294+
258295
# Credits
259296
260297
- Play icon used on frame overlays: [font-awesome](https://fontawesome.com/icons/play-circle?style=regular)

src/BottomSheet.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,13 @@ export const BottomSheet = React.forwardRef<
390390
useImperativeHandle(
391391
forwardRef,
392392
() => ({
393-
snapTo: (numberOrCallback) => {
393+
snapTo: (numberOrCallback, { velocity = 1, source = 'custom' } = {}) => {
394394
send('SNAP', {
395-
payload: { y: findSnapRef.current(numberOrCallback), velocity: 1 },
395+
payload: {
396+
y: findSnapRef.current(numberOrCallback),
397+
velocity,
398+
source,
399+
},
396400
})
397401
},
398402
}),

src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,13 @@ export interface RefHandles {
142142
/**
143143
* When given a number it'll find the closest snap point, so you don't need to know the exact value,
144144
* Use the callback method to access what snap points you can choose from.
145+
*
146+
* Use the second argument for advanced settings like:
147+
* `source: string` which is passed to onSpring events, and is 'custom' by default
148+
* `velocity: number` which is 1 by default, adjust it to control the speed of the spring transition to the new snap point
145149
*/
146150
snapTo: (
147-
numberOrCallback: number | ((state: defaultSnapProps) => number)
151+
numberOrCallback: number | ((state: defaultSnapProps) => number),
152+
options?: { source?: string; velocity?: number }
148153
) => void
149154
}

0 commit comments

Comments
 (0)