Skip to content

Commit 3d553b5

Browse files
authored
feat: swipe close threshold (#35)
* Add swipeCloseThreshold * Update README.md
1 parent df084d8 commit 3d553b5

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,21 @@ For Nuxt 3, just wrap component in `<ClientOnly>`
132132

133133
### Prop Definitions
134134

135-
| Prop | Type | Default | Description |
136-
| ------------------- | ------------------------- | ---------------- | ---------------------------------------------- |
137-
| duration | Number | 250 | Animation duration in milliseconds |
138-
| snapPoints | Array<number\|string> | [instinctHeight] | Custom snapping positions |
139-
| initialSnapPoint | Number | minHeight | Initial snap point index |
140-
| blocking | Boolean | true | Block interactions with underlying content |
141-
| canSwipeClose | Boolean | true | Enable swipe-to-close gesture |
142-
| canBackdropClose | Boolean | true | Allow closing by tapping backdrop |
143-
| expandOnContentDrag | Boolean | true | Enable expanding by dragging content |
144-
| teleprtTo | String \| RendererElement | body | Teleport to a specific element |
145-
| teleportDefer | Boolean | false | Defer teleporting until opened (Vue 3.5+ only) |
146-
| headerClass | String | '' | Set header element class |
147-
| contentClass | String | '' | Set content element class |
148-
| footerClass | String | '' | Set footer element class |
135+
| Prop | Type | Default | Description |
136+
| ------------------- | ------------------------- | ---------------- | ------------------------------------------------------------------------- |
137+
| duration | Number | 250 | Animation duration in milliseconds |
138+
| snapPoints | Array<number\|string> | [instinctHeight] | Custom snapping positions |
139+
| initialSnapPoint | Number | minHeight | Initial snap point index |
140+
| blocking | Boolean | true | Block interactions with underlying content |
141+
| canSwipeClose | Boolean | true | Enable swipe-to-close gesture |
142+
| swipeCloseThreshold | Number\|String | "50%" | The amount of translation (in px or %) after which the element will close |
143+
| canBackdropClose | Boolean | true | Allow closing by tapping backdrop |
144+
| expandOnContentDrag | Boolean | true | Enable expanding by dragging content |
145+
| teleportTo | String \| RendererElement | body | Teleport to a specific element |
146+
| teleportDefer | Boolean | false | Defer teleporting until opened (Vue 3.5+ only) |
147+
| headerClass | String | '' | Set header element class |
148+
| contentClass | String | '' | Set content element class |
149+
| footerClass | String | '' | Set footer element class |
149150

150151
## Exposed methods
151152

@@ -160,7 +161,7 @@ Assuming there is `const bottomSheet = ref()`
160161
## Events
161162

162163
| Event | Description | Payload |
163-
| -------------- | -------------------------------------- | ----------------------- |
164+
| --------------- | -------------------------------------- | ----------------------- |
164165
| opened | Emitted when sheet finishes opening | - |
165166
| opening-started | Emitted when sheet starts opening | - |
166167
| closed | Emitted when sheet finishes closing | - |

packages/vue-spring-bottom-sheet/src/BottomSheet.vue

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,27 @@ const handlePan = async (_: PointerEvent, info: PanInfo) => {
316316
}
317317
318318
const handlePanEnd = () => {
319-
translateY.value = props.canSwipeClose
320-
? [0, height.value].reduce((prev, curr) =>
321-
Math.abs(curr - translateY.value) < Math.abs(prev - translateY.value) ? curr : prev,
322-
)
323-
: 0
319+
if (props.canSwipeClose) {
320+
let threshold = height.value / 2
321+
322+
if (props.swipeCloseThreshold && typeof props.swipeCloseThreshold === 'number') {
323+
threshold = props.swipeCloseThreshold
324+
}
325+
326+
if (
327+
props.swipeCloseThreshold &&
328+
typeof props.swipeCloseThreshold === 'string' &&
329+
props.swipeCloseThreshold.includes('%')
330+
) {
331+
threshold = height.value * (Number(props.swipeCloseThreshold.replace('%', '')) / 100)
332+
}
333+
334+
if (translateY.value > threshold) {
335+
translateY.value = height.value
336+
}
337+
} else {
338+
translateY.value = 0
339+
}
324340
325341
controls = animate(translateYValue, translateY.value, {
326342
duration: props.duration / 1000,

packages/vue-spring-bottom-sheet/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface BottomSheetProps {
66
initialSnapPoint?: number
77
blocking?: boolean
88
canSwipeClose?: boolean
9+
swipeCloseThreshold?: string | number
910
canBackdropClose?: boolean
1011
expandOnContentDrag?: boolean
1112
modelValue?: boolean

0 commit comments

Comments
 (0)