Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for v-model with loop #111

Merged
merged 5 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ For more examples, see the demo: https://vue-ssr-carousel.netlify.app.
| `slides-per-page` | `1` | How many slides are shown per page. Can be set to `null` to allow for flexible widths for slides. See https://vue-ssr-carousel.netlify.app/responsive and note the caveats mentiond within.
| `gutter` | `20` | The size of the space between slides. This can a number or any CSS resolvable string. See https://vue-ssr-carousel.netlify.app/gutters.
| `paginate-by-slide` | `false` | When `false`, dragging the carousel or interacting with the arrows will advance a full page of slides at a time. When `true`, the carousel will come to a rest at each slide.
| `autoplay` | `undefined` | Add a delay in seconds for auto playing the slides. See https://vue-ssr-carousel.netlify.app/misc#autoplay.
| `autoplay-delay | `0` | Add a delay in seconds for auto playing the slides. See https://vue-ssr-carousel.netlify.app/misc#autoplay.
| `loop` | `false` | Boolean to enable looping / infinite scroll. See https://vue-ssr-carousel.netlify.app/looping.
| `center` | `false` | Render the first slide in the middle of the carousel. Should only be used with odd numbers of `slides-per-page`. This results in the slides being rendered visually in a different order than the DOM [which is an accessibility concern](https://developer.mozilla.org/en-US/docs/Web/CSS/order#accessibility_concerns). See https://vue-ssr-carousel.netlify.app/looping.
| `peek` | `0` | A width value for how far adjacent cards should peek into the carousel canvas. This can a number or any CSS resolvable string. See https://vue-ssr-carousel.netlify.app/peeking.
Expand Down
5 changes: 4 additions & 1 deletion demo/components/demos/events/v-model.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<div data-cy='v-model'>

<ssr-carousel v-model='page'>
<ssr-carousel v-model='page' :loop='loop'>
<slide :index='1'></slide>
<slide :index='2'></slide>
<slide :index='3'></slide>
Expand All @@ -18,6 +18,9 @@

<script>
export default {
props: {
loop: Boolean
},
data() {
return {
page: 1,
Expand Down
24 changes: 24 additions & 0 deletions demo/content/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ You can set the initial index with `:value`. Or use `v-model` to track the curre

</div>
</template>
```

This also works with looping:

<demos-events-v-model loop></demos-events-v-model>

```vue
<template>
<div>

<ssr-carousel v-model='page' loop>
<slide :index='1'></slide>
<slide :index='2'></slide>
<slide :index='3'></slide>
</ssr-carousel>

<span class="now">Current Page: {{ page + 1 }}</span>
<button @click='page--'>Back</button>
<button @click='page++'>Next</button>

</div>
</template>



<script>
export default {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
},
"homepage": "https://github.com/BKWLD/vue-ssr-carousel#readme",
"dependencies": {
"bukwild-stylus-library": "^3.1.0",
"lodash": "3 - 4"
"bukwild-stylus-library": "^3.1.0"
},
"devDependencies": {
"@cloak-app/boilerplate": "^1.0.8",
Expand Down
6 changes: 2 additions & 4 deletions src/concerns/dimensions.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
###
Code related to measuring the size of the carousel after mounting
###
import throttle from 'lodash/throttle'
export default

data: ->
Expand All @@ -12,11 +11,10 @@ export default
# Add resize listening
mounted: ->
@onResize()
@onResizeThrottled = throttle @onResize, 200
window.addEventListener 'resize', @onResizeThrottled
window.addEventListener 'resize', @onResize

# Cleanup listeners
beforeDestroy: -> window.removeEventListener 'resize', @onResizeThrottled
beforeDestroy: -> window.removeEventListener 'resize', @onResize

computed:

Expand Down
28 changes: 19 additions & 9 deletions src/concerns/pagination.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,18 @@ export default

watch:

# Treat v-model input as a "goto" request. Immediately fire an input
# event if the index was not changed, like when an edge is reached, to
# update the parent component.
# Treat v-model input as a "goto" request
value: ->
@goto @value
if @value != @boundedIndex

# If the value exceeds the bounds, immediately emit a new input event
# with the corrected value
if @value != @applyIndexBoundaries @value
then @$emit 'input', @boundedIndex

# Else if the incoming value is different than the current value
# then tween to it
else if @value != @boundedIndex then @goto @value

# Emit events on index change
boundedIndex: ->
@$emit 'change', index: @boundedIndex
Expand Down Expand Up @@ -127,6 +131,15 @@ export default

# Tween to a specific index
tweenToIndex: (index) ->
@targetX = @getXForIndex index
@startTweening()

# Jump to an index with no tween
jumpToIndex: (index) ->
@currentX = @targetX = @getXForIndex index

# Calculate the X value given an index
getXForIndex: (index) ->

# Figure out the new x position
x = if @paginateBySlide
Expand All @@ -135,10 +148,7 @@ export default

# Apply adjustments to x value and persist
x += @makeIncompletePageOffset index
@targetX = Math.round @applyXBoundaries x

# Start tweening
@startTweening()
return Math.round @applyXBoundaries x

# Creates a px value to represent adjustments that should be made to
# account for incommplete pages of slides when looping is enabled. Like
Expand Down
2 changes: 1 addition & 1 deletion src/concerns/responsive.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default
watch:

# Fix alignment of slides while resizing
pageWidth: -> @tweenToIndex @index
pageWidth: -> @jumpToIndex @index

# If resizing the browser leads to disabling, reset the slide to the first
# page. Like if a user had switched to the 2nd page on mobile and then
Expand Down
8 changes: 1 addition & 7 deletions src/concerns/tweening.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export default
currentX: 0 # The actual left offset of the slides container
targetX: 0 # Where we may be tweening the slide to
tweening: false # If there is a current RAF based tween running
firstTween: true # Has the first tween been triggered

# Stop any animations that are in flight
beforeDestroy: -> window.cancelAnimationFrame @rafId
Expand All @@ -32,7 +31,6 @@ export default
if @tweening
@$emit 'tween:start', { @index }
@tweenToTarget()
@firstTween = false
else
window.cancelAnimationFrame @rafId
@$emit 'tween:end', { @index }
Expand All @@ -57,12 +55,8 @@ export default
# Tween the currentX to the targetX
tweenToTarget: ->

# If on the first tween and the first page was set to something other
# than 0 by v-model, then instantly jump to the final destination
dampening = if @firstTween and @value != 0 then 1 else @tweenDampening

# Apply tween math
@currentX = @currentX + (@targetX - @currentX) * dampening
@currentX = @currentX + (@targetX - @currentX) * @tweenDampening
if Math.abs(@targetX - @currentX) < 1 # Stops tweening
@currentX = @targetX
@tweening = false
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7142,7 +7142,7 @@ lodash.uniq@^4.5.0:
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
integrity sha512-xfBaXQd9ryd9dlSDvnvI0lvxfLJlYAZzXomUYzLKtUeOQvOP5piqAWuGtrhWeqaXK9hhoM/iyJc5AV+XfsX3HQ==

"lodash@3 - 4", lodash@^4.0.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.17.3, lodash@^4.17.5:
lodash@^4.0.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.17.3, lodash@^4.17.5:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
Expand Down