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

Draft: Fix Variable Width Scrolling with CSS Gutters #148

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 5 additions & 9 deletions demo/components/demos/responsive/variable-width.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<template>

<ssr-carousel
data-cy='variable-width'
:slides-per-page='null'>
<slide :index='1' :style='{ width: "65%", height: "30vw"}'></slide>
<slide :index='2' :style='{ width: "50%", height: "30vw"}'></slide>
<slide :index='3' :style='{ width: "30%", height: "30vw"}'></slide>
</ssr-carousel>

<ssr-carousel data-cy="variable-width" :slides-per-page="null" gutter="1rem">
<slide :index="1" :style="{ width: '65%', height: '30vw' }"></slide>
<slide :index="2" :style="{ width: '50%', height: '30vw' }"></slide>
<slide :index="3" :style="{ width: '30%', height: '30vw' }"></slide>
</ssr-carousel>
</template>
4 changes: 2 additions & 2 deletions src/concerns/dimensions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default

# Calculate the width of the whole track from the slideWidth.
trackWidth: ->
if @isVariableWidth
if @isVariableWidthWithPages
then @measuredTrackWidth + @gutterWidth
else @slideWidth * @slidesCount

Expand Down Expand Up @@ -118,6 +118,6 @@ export default

# Get the target X scroll position of a given slide
targetXOfIdx: (idx) ->
if @isVariableWidth
if @isVariableWidthWithPages
then @measuredSlidesWidths[idx].targetXScroll
else @pageWidth / @currentSlidesPerPage
6 changes: 4 additions & 2 deletions src/concerns/dragging.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default
fractionalIndex: ->
return 0 unless @trackWidth

if @isVariableWidth then return @fractionalIndexFromMeasurements
if @isVariableWidthWithPages then return @fractionalIndexFromMeasurements

# Work in positive numbers
x = @currentX * -1
Expand Down Expand Up @@ -153,7 +153,9 @@ export default
else @gotoEnd()

# If rendering variable width slides, don't come to a rest at an index
else if @isVariableWidth then @goto @dragIndex
else if @isVariableWidth
if (!!@showArrows or !!@showDots) then @goto @dragIndex
else @tweenToStop()

# If user was vertically dragging, reset the index
else if @isVerticalDrag then @goto @index
Expand Down
4 changes: 2 additions & 2 deletions src/concerns/pagination.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default
# The current number of pages
pages: -> switch
# When variable width, pages is slide count
when @isVariableWidth then @slidesCount
when @isVariableWidthWithPages then @slidesCount

# When looping and paginating per slide, make a dot per slide
when @paginateBySlide and @shouldLoop then @slidesCount
Expand Down Expand Up @@ -149,7 +149,7 @@ export default

# Figure out the new x position
x = switch
when @isVariableWidth then @targetXOfIdx(@applyIndexBoundaries(index)) * -1
when @isVariableWidthWithPages then @targetXOfIdx(@applyIndexBoundaries(index)) * -1
when @paginateBySlide then index * @slideWidth * -1
else index * @pageWidth * -1

Expand Down
5 changes: 4 additions & 1 deletion src/concerns/variable-width.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export default
# Is the carousel in variable width mode
isVariableWidth: -> @slidesPerPage == null

# Is the carousel in variable width mode and needs pagination
isVariableWidthWithPages: -> @isVariableWidth and (@showArrows or @showDots)

methods:
# Capture all dimensions of carousel
captureCarouselDims: ->
Expand All @@ -31,7 +34,7 @@ export default
...acc
{
width: child.$el.clientWidth
targetXScroll: (acc[idx - 1]?.targetXScroll || 0) + (acc[idx - 1]?.width || 0) + @gutter * (idx > 0)
targetXScroll: (acc[idx - 1]?.targetXScroll || 0) + (acc[idx - 1]?.width || 0) + @gutter * (idx > 0) # This requires gutter to be a number, not a CSS value
}
]
, [] )
Expand Down