Skip to content

Commit

Permalink
Add dimensionsKnown helper computed prop
Browse files Browse the repository at this point in the history
  • Loading branch information
weotch committed Jan 20, 2024
1 parent 40bc51b commit 2766e52
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/concerns/dimensions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export default
# Check if the drag is currently out bounds
isOutOfBounds: -> @currentX > 0 or @currentX < @endX

# Helper for things that are triggered once dimensions are known so
# they can be more specific about their dependencies
dimensionsKnown: -> @carouselWidth and @viewportWidth

methods:

# Measure the component width for various calculations. Using
Expand Down
6 changes: 3 additions & 3 deletions src/concerns/rtl.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export default
# As an easy way to support rtl, update the index to the final value
# when RTL is enabled. This is change is combined with reversing the order
# of the slides in `ssr-carousel-track`. We're testing for the
# carouselWidth value as way to ensure that the final pages count is known
# dimensionsKnown value as way to ensure that the final pages count is known
# since it depends on knowing the width of the carousel.
mounted: ->
return unless @rtl
if @carouselWidth
if @dimensionsKnown
then @setInitialRtlIndex()
else unwatch = @$watch 'carouselWidth', =>
else unwatch = @$watch 'dimensionsKnown', =>
@setInitialRtlIndex()
unwatch()

Expand Down
6 changes: 3 additions & 3 deletions src/ssr-carousel-track.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default
leftPeekingSlideIndex: Number
rightPeekingSlideIndex: Number
rtl: Boolean
slideWidth: Number
dimensionsKnown: Number

data: ->

Expand Down Expand Up @@ -114,12 +114,12 @@ export default
slides = [...(@$slots.default || []), ...(@$slots.clones || [])]
.filter (vnode) -> !vnode.text

# Reverses the slide if rtl and if the slide slide width is known. This
# Reverses the slide if rtl and if the dimensions are known. This
# second condition exists to prevent the reversal from happening on SSR.
# Which is important because this logic is paired with setting the
# intial index to the last page which can't be known until the slide
# width is known.
if @rtl and @slideWidth then slides = slides.reverse()
if @rtl and @dimensionsKnown then slides = slides.reverse()
return slides

# Makes a clone of the vnode properties we'll be updating so the changes
Expand Down
2 changes: 1 addition & 1 deletion src/ssr-carousel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
leftPeekingSlideIndex,
rightPeekingSlideIndex,
rtl,
slideWidth,
dimensionsKnown,
}`)

//- Render the slotted slides
Expand Down

0 comments on commit 2766e52

Please sign in to comment.