Skip to content

Commit

Permalink
feat: SNAP events now have source
Browse files Browse the repository at this point in the history
Fixes #87
  • Loading branch information
stipsan committed Jan 21, 2021
1 parent f67d1bd commit 240c212
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ iOS Safari, and some other mobile culprits, can be tricky if you're on a page th
## Events
All events receive `SprinngEvent` as their argument. It has a single property, `type`, which can be `'OPEN' | 'RESIZE' | 'SNAP' | 'CLOSE'` depending on the scenario.
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.
### onSpringStart
Expand Down Expand Up @@ -195,8 +195,26 @@ Fires whenever there's been a window resize event, or if the header, footer or c
#### SNAP
Type: `{ source: 'dragging' | 'custom' }`
Fired after dragging ends, or when calling `ref.snapTo`, and a transition to a valid snap point is happening.
`source` is `'dragging'` if the snapping is responding to a drag gesture that just ended. And it's set to `'custom'` when using `ref.snapTo`.
```jsx
function Example() {
return (
<BottomSheet
onSpringStart={async (event) => {
if (event.type === 'SNAP' && event.source === 'dragging') {
console.log('Starting a spring animation to user selected snap point')
}
}}
/>
)
}
```
### onSpringEnd
Type: `(event: SpringEvent) => void`
Expand Down
26 changes: 21 additions & 5 deletions src/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ export const BottomSheet = React.forwardRef<
[]
),
onSnapCancel: useCallback(
() => onSpringCancelRef.current?.({ type: 'SNAP' }),
(context) =>
onSpringCancelRef.current?.({
type: 'SNAP',
source: context.snapSource,
}),
[]
),
onCloseCancel: useCallback(
Expand All @@ -184,7 +188,11 @@ export const BottomSheet = React.forwardRef<
[]
),
onSnapEnd: useCallback(
() => onSpringEndRef.current?.({ type: 'SNAP' }),
(context, event) =>
onSpringEndRef.current?.({
type: 'SNAP',
source: context.snapSource,
}),
[]
),
onResizeEnd: useCallback(
Expand All @@ -195,7 +203,11 @@ export const BottomSheet = React.forwardRef<
context: { initialState },
services: {
onSnapStart: useCallback(
async () => onSpringStartRef.current?.({ type: 'SNAP' }),
async (context, event) =>
onSpringStartRef.current?.({
type: 'SNAP',
source: event.payload.source || 'custom',
}),
[]
),
onOpenStart: useCallback(
Expand All @@ -211,7 +223,11 @@ export const BottomSheet = React.forwardRef<
[]
),
onSnapEnd: useCallback(
async () => onSpringEndRef.current?.({ type: 'SNAP' }),
async (context, event) =>
onSpringEndRef.current?.({
type: 'SNAP',
source: context.snapSource,
}),
[]
),
onOpenEnd: useCallback(
Expand Down Expand Up @@ -463,7 +479,7 @@ export const BottomSheet = React.forwardRef<
}

if (last) {
send('SNAP', { payload: { y: newY, velocity } })
send('SNAP', { payload: { y: newY, velocity, source: 'dragging' } })

return memo
}
Expand Down
10 changes: 9 additions & 1 deletion src/machines/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ interface OverlayStateSchema {

type OverlayEvent =
| { type: 'OPEN' }
| { type: 'SNAP'; payload: { y: number; velocity: number } }
| {
type: 'SNAP'
payload: {
y: number
velocity: number
source: 'dragging' | 'custom' | string
}
}
| { type: 'CLOSE' }
| { type: 'DRAG' }
| { type: 'RESIZE' }
Expand Down Expand Up @@ -191,6 +198,7 @@ export const overlayMachine = Machine<
// @ts-expect-error
y: (_, { payload: { y } }) => y,
velocity: (_, { payload: { velocity } }) => velocity,
snapSource: (_, { payload: { source = 'custom' } }) => source,
}),
],
},
Expand Down
8 changes: 5 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ export type defaultSnapProps = {
} & SnapPointProps

/* Might make sense to expose a preventDefault method here */
export type SpringEvent = {
type: 'OPEN' | 'CLOSE' | 'RESIZE' | 'SNAP'
}
export type SpringEvent =
| { type: 'OPEN' }
| { type: 'CLOSE' }
| { type: 'RESIZE' }
| { type: 'SNAP'; source: 'dragging' | 'custom' | string }

export type Props = {
/**
Expand Down

0 comments on commit 240c212

Please sign in to comment.