Skip to content

Commit

Permalink
start fixing variable width scroll for css gutters
Browse files Browse the repository at this point in the history
  • Loading branch information
sjstark committed May 13, 2024
1 parent 93839b3 commit 9c463d2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
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

0 comments on commit 9c463d2

Please sign in to comment.