Skip to content

Commit

Permalink
fix: make dragging up possible without dragging down first
Browse files Browse the repository at this point in the history
  • Loading branch information
94726 committed Jan 4, 2024
1 parent f201c90 commit 577a778
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions aioli/src/Root.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useEventListener, useVModel } from '@vueuse/core'
import { DialogRoot, type DialogRootProps } from 'radix-vue'
import { nextTick, onMounted, ref, toRefs, watch } from 'vue'
import { provideDrawerContext } from './context'
import { dampenValue, getTranslateY, set, isInput } from './helpers'
import { dampenValue, getTranslateY, isInput, set } from './helpers'
const VELOCITY_THRESHOLD = 0.4
Expand Down Expand Up @@ -221,15 +221,14 @@ function resetDrawerStyles() {
})
}
function shouldDrag(el: EventTarget, isDraggingDown: boolean) {
function shouldDrag(el: EventTarget, isDraggingUp: boolean) {
let element = el as HTMLElement
const date = new Date()
const highlightedText = window.getSelection()?.toString()
const swipeAmount = (drawerRef.value ? getTranslateY(drawerRef.value) : null) ?? 0
if (swipeAmount > 0) {
return true
}
// todo this part is bit annoying
// Don't drag if there's highlighted text
if (highlightedText && highlightedText.length > 0) {
Expand All @@ -246,24 +245,16 @@ function shouldDrag(el: EventTarget, isDraggingDown: boolean) {
return false
}
if (isDraggingDown) {
lastTimeDragPrevented = date
// We are dragging down so we should allow scrolling
return false
}
// Keep climbing up the DOM tree as long as there's a parent
while (element) {
// Check if the element is scrollable
if (element.scrollHeight > element.clientHeight) {
// if (element.getAttribute('role') === 'dialog') {
// return true
// }
// if (isDraggingDown && element !== document.body && !swipeAmount && swipeAmount >= 0) {
// lastTimeDragPrevented = new Date()
// return false // Element is scrolled to the top, but we are dragging down so we should allow scrolling
// }
// user is trying to scroll down
if (isDraggingUp && ['scroll', 'auto'].includes(getComputedStyle(element).overflowY)) {
lastTimeDragPrevented = date
// We are dragging down so we should allow scrolling
return false
}
if (element.scrollTop !== 0) {
lastTimeDragPrevented = new Date()
return false // The element is scrollable and not scrolled to the top, so don't drag
Expand Down Expand Up @@ -315,10 +306,10 @@ function onDrag(event: PointerEvent) {
const y = getScreenY(event)
const draggedDistance = pointerStartY.value - y - dragStartY
const isDraggingDown = draggedDistance > 0
const isDraggingUp = draggedDistance > 0
if (!draggedDistance) return // not dragging yet
if (!isAllowedToDrag.value && !shouldDrag(event.target!, isDraggingDown)) return
if (!isAllowedToDrag.value && !shouldDrag(event.target!, isDraggingUp)) return
drawerRef.value?.classList.add(DRAG_CLASS)
// If shouldDrag gave true once after pressing down on the drawer, we set isAllowedToDrag to true and it will remain true until we let go, there's no reason to disable dragging mid way, ever, and that's the solution to it
Expand Down

0 comments on commit 577a778

Please sign in to comment.