-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add swipeable functionality (#1618)
* feat: add swipeable mixin * feat(MdDrawer): swipeable drawer * docs(MdDrawer): add swipeable prop * feat: allow dynamic swipeElement * feat: reset swiped value after touchend * feat: tweaks of default values * fix: wrong axis for restraints * feat(MdTabs): swipeable tabs * feat(MdTabs): allow swipeable only on tabs content * feat: change naming to mdSwipeElement * feat(MdDrawer): use parent of drawer as swipe element * docs: add api docs for drawer and tabs
- Loading branch information
Showing
6 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
export default { | ||
props: { | ||
mdSwipeable: Boolean, | ||
mdSwipeThreshold: { | ||
type: Number, | ||
default: 150 | ||
}, | ||
mdSwipeRestraint: { | ||
type: Number, | ||
default: 100 | ||
}, | ||
mdSwipeTime: { | ||
type: Number, | ||
default: 300 | ||
} | ||
}, | ||
data: () => ({ | ||
swipeStart: false, | ||
swipeStartTime: null, | ||
swiped: null, | ||
touchPosition: { | ||
startX: 0, | ||
startY: 0 | ||
} | ||
}), | ||
computed: { | ||
getSwipeElement () { | ||
return this.mdSwipeElement || window | ||
} | ||
}, | ||
methods: { | ||
handleTouchStart (event) { | ||
this.touchPosition.startX = event.touches[0].screenX | ||
this.touchPosition.startY = event.touches[0].screenY | ||
this.swipeStartTime = new Date() | ||
|
||
this.swipeStart = true | ||
}, | ||
handleTouchMove (event) { | ||
if (!this.swipeStart) { | ||
return | ||
} | ||
|
||
const touchmoveX = event.touches[0].screenX | ||
const touchmoveY = event.touches[0].screenY | ||
|
||
const actualX = touchmoveX - this.touchPosition.startX | ||
const actualY = touchmoveY - this.touchPosition.startY | ||
|
||
const elapsedTime = new Date() - this.swipeStartTime | ||
|
||
if (elapsedTime <= this.mdSwipeTime) { | ||
if (Math.abs(actualX) >= this.mdSwipeThreshold && Math.abs(actualY) <= this.mdSwipeRestraint) { | ||
this.swiped = actualX < 0 | ||
? 'left' | ||
: 'right' | ||
} else if (Math.abs(actualY) >= this.mdSwipeThreshold && Math.abs(actualX) <= this.mdSwipeRestraint) { | ||
this.swiped = actualY < 0 | ||
? 'up' | ||
: 'down' | ||
} | ||
} | ||
}, | ||
handleTouchEnd () { | ||
this.touchPosition = { | ||
startX: 0, | ||
startY: 0 | ||
} | ||
this.swiped = null | ||
this.swipeStart = false | ||
}, | ||
}, | ||
mounted () { | ||
if (this.mdSwipeable) { | ||
this.getSwipeElement.addEventListener('touchstart', this.handleTouchStart, false) | ||
this.getSwipeElement.addEventListener('touchend', this.handleTouchEnd, false) | ||
this.getSwipeElement.addEventListener('touchmove', this.handleTouchMove, false) | ||
} | ||
}, | ||
beforeDestroy () { | ||
if (this.mdSwipeable) { | ||
this.getSwipeElement.removeEventListener('touchstart', this.handleTouchStart, false) | ||
this.getSwipeElement.removeEventListener('touchend', this.handleTouchEnd, false) | ||
this.getSwipeElement.removeEventListener('touchmove', this.handleTouchMove, false) | ||
} | ||
} | ||
} |