From 1afe79f317086d0ab24869a3e312cb766a992c86 Mon Sep 17 00:00:00 2001 From: Cody Olsen Date: Thu, 21 Jan 2021 21:40:42 +0100 Subject: [PATCH] feat: override source and velocity in snapTo --- README.md | 43 ++++++++++++++++++++++++++++++++++++++++--- src/BottomSheet.tsx | 8 ++++++-- src/types.ts | 7 ++++++- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 17181dae..846ab4ee 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ Fires whenever there's been a window resize event, or if the header, footer or c #### SNAP -Type: `{ source: 'dragging' | 'custom' }` +Type: `{ source: 'dragging' | 'custom' | string }` Fired after dragging ends, or when calling `ref.snapTo`, and a transition to a valid snap point is happening. @@ -205,7 +205,7 @@ Fired after dragging ends, or when calling `ref.snapTo`, and a transition to a v function Example() { return ( { + onSpringStart={(event) => { if (event.type === 'SNAP' && event.source === 'dragging') { console.log('Starting a spring animation to user selected snap point') } @@ -215,6 +215,33 @@ function Example() { } ``` +When using `snapTo` it's possible to use a different `source` than `'custom'`: + +```jsx +function Example() { + const sheetRef = useRef() + return ( + [minHeight, maxHeight]} + onSpringEnd={(event) => { + if (event.type === 'SNAP' && event.source === 'snap-to-bottom') { + console.log( + 'Just finished an imperativ transition to the bottom snap point' + ) + } + }} + > + + + ) +} +``` + ### onSpringEnd Type: `(event: SpringEvent) => void` @@ -242,7 +269,7 @@ export default function Example() { ### snapTo -Type: `(numberOrCallback: number | (state => number)) => void` +Type: `(numberOrCallback: number | (state => number)) => void, options?: {source?: string, velocity?: number}` 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: @@ -255,6 +282,16 @@ ref.current.snapTo(({ // Showing all the available props ) ``` +There's an optional second argument you can use to override `event.source`, as well as changing the `velocity`: + +```js +ref.current.snapTo(({ snapPoints }) => Math.min(...snapPoints), { + // Each property is optional, here showing their default values + source: 'custom', + velocity: 1, +}) +``` + # Credits - Play icon used on frame overlays: [font-awesome](https://fontawesome.com/icons/play-circle?style=regular) diff --git a/src/BottomSheet.tsx b/src/BottomSheet.tsx index b5d62989..4926d9c3 100644 --- a/src/BottomSheet.tsx +++ b/src/BottomSheet.tsx @@ -390,9 +390,13 @@ export const BottomSheet = React.forwardRef< useImperativeHandle( forwardRef, () => ({ - snapTo: (numberOrCallback) => { + snapTo: (numberOrCallback, { velocity = 1, source = 'custom' } = {}) => { send('SNAP', { - payload: { y: findSnapRef.current(numberOrCallback), velocity: 1 }, + payload: { + y: findSnapRef.current(numberOrCallback), + velocity, + source, + }, }) }, }), diff --git a/src/types.ts b/src/types.ts index aae506a5..c3c73141 100644 --- a/src/types.ts +++ b/src/types.ts @@ -142,8 +142,13 @@ export interface RefHandles { /** * When given a number it'll find the closest snap point, so you don't need to know the exact value, * Use the callback method to access what snap points you can choose from. + * + * Use the second argument for advanced settings like: + * `source: string` which is passed to onSpring events, and is 'custom' by default + * `velocity: number` which is 1 by default, adjust it to control the speed of the spring transition to the new snap point */ snapTo: ( - numberOrCallback: number | ((state: defaultSnapProps) => number) + numberOrCallback: number | ((state: defaultSnapProps) => number), + options?: { source?: string; velocity?: number } ) => void }