Skip to content

Commit

Permalink
feat(stack): add onMove handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Rod Leviton committed Mar 10, 2020
1 parent c04a015 commit 0435c6d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# vue-card-stack

![CI](https://github.com/rodleviton/vue-card-stack/workflows/CI/badge.svg)
[![GitHub license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/rodleviton/vue-card-stack/blob/master/LICENCE)
[![GitHub license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/rodleviton/vue-card-stack/blob/master/LICENSE)


## Install
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-card-stack",
"version": "1.0.3",
"version": "1.1.0",
"license": "MIT",
"author": "Rod Leviton <[email protected]>",
"description": "Stackable, swipe-able, tweak-able Vue card component.",
Expand Down
32 changes: 12 additions & 20 deletions src/serve-dev.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@
<div style="width: 100%; display: flex; align-items: center; flex-direction: column;">
<VueCardStack
:cards="cards"
:cardWidth="300"
:cardHeight="460"
stackWidth="100%"
:maxVisibleCards="parseInt(maxVisibleCards)"
:scaleMultiplier="parseFloat(scaleMultiplier)"
:card-width="300"
:card-height="460"
:stack-width="stackWidth"
:max-visible-cards="parseInt(maxVisibleCards)"
:scale-multiplier="parseFloat(scaleMultiplier)"
:on-move="onMove"
ref="stack"
>
<template v-slot:card="{ card }">
<div
class="card"
:style="{
background: card.background,
boxShadow: cardShadows[card.$index]
}"
></div>
<div class="card" :style="{ background: card.background }"></div>
</template>
</VueCardStack>
<div>
Expand Down Expand Up @@ -92,14 +87,11 @@ export default Vue.extend({
this.containerWidth = parseInt(val);
this.$refs.stack.rebuild();
}, 100)
},
cardShadows() {
return this.cards.map((_, index) => {
const y = 10 - 10 * index * (1 / this.maxVisibleCards);
const x = 10 - 10 * index * (1 / this.maxVisibleCards);
return `-1px ${x}px ${y}px 0px rgba(0, 0, 0, 0.05)`;
});
}
},
methods: {
onMove(val) {
console.log(val)
}
}
});
Expand Down
13 changes: 12 additions & 1 deletion src/vue-card-stack.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export default {
paddingVertical: {
type: Number,
default: () => 20
},
onMove: {
type: Function,
default: null
}
},
data() {
Expand Down Expand Up @@ -178,6 +182,10 @@ export default {
const minDistanceToTravel =
(this.cardWidth + this.paddingHorizontal) / this.sensitivity;
if (this.onMove) {
this.onMove(0)
}
if (this.isDraggingRight) {
if (distanceTravelled > minDistanceToTravel) {
const cardToMoveToBottomOfStack = this.stack.shift();
Expand All @@ -200,6 +208,10 @@ export default {
moveStack(dragXPos) {
const activeCardOffset = dragXPos - this.dragStartX;
if (this.onMove) {
this.onMove(activeCardOffset / (this.cardWidth + this.paddingHorizontal))
}
if (this.isDraggingRight) {
this.activeCardIndex = 1;
} else {
Expand All @@ -208,7 +220,6 @@ export default {
this.stack = this.stack.map((card, index) => {
const isActiveCard = index === this.activeCardIndex;
const xPos = isActiveCard
? this.cardDefaults[index].xPos + activeCardOffset
: this.cardDefaults[index].xPos +
Expand Down

0 comments on commit 0435c6d

Please sign in to comment.