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: SSR hydration issue #453

Merged
merged 2 commits into from
Dec 11, 2024
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
17 changes: 6 additions & 11 deletions src/components/Carousel/Carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
ComponentInternalInstance,
watchEffect,
shallowReactive,
pushScopeId,
getCurrentInstance,
} from 'vue'

import { ARIA as ARIAComponent } from '@/components/ARIA'
Expand Down Expand Up @@ -122,6 +120,9 @@
const clonedSlidesCount = computed(() => Math.ceil(config.itemsToShow) + 1)

function updateBreakpointsConfig(): void {
if (!mounted.value) {
return
}

Check warning on line 125 in src/components/Carousel/Carousel.ts

View workflow job for this annotation

GitHub Actions / coverage-report

These lines are not covered by a test
// Determine the width source based on the 'breakpointMode' config
const widthSource =
(fallbackConfig.value.breakpointMode === 'carousel'
Expand Down Expand Up @@ -265,12 +266,9 @@
})
}

updateBreakpointsConfig()
onMounted((): void => {
mounted.value = true
if (fallbackConfig.value.breakpointMode === 'carousel') {
updateBreakpointsConfig()
}
updateBreakpointsConfig()
initAutoplay()

if (root.value) {
Expand Down Expand Up @@ -565,6 +563,7 @@
slidesCount,
viewport,
slides,
clonedSlidesCount,
scrolledIndex,
currentSlide: currentSlideIndex,
maxSlide: maxSlideIndex,
Expand Down Expand Up @@ -663,7 +662,7 @@
*/
const trackTransform: ComputedRef<string> = computed(() => {
// Calculate the scrolled index with wrapping offset if applicable
const cloneOffset = config.wrapAround ? clonedSlidesCount.value : 0
const cloneOffset = config.wrapAround && mounted.value ? clonedSlidesCount.value : 0

// Determine direction multiplier for orientation
const directionMultiplier = isReversed.value ? -1 : 1
Expand Down Expand Up @@ -702,13 +701,9 @@
const addonsElements = slotAddons?.(data) || []

if (config.wrapAround) {
// Ensure scoped CSS tracks properly
const scopeId = output.length > 0 ? output[0].scopeId : null
pushScopeId(scopeId)
const toShow = clonedSlidesCount.value
const slidesBefore = createCloneSlides({ slides, position: 'before', toShow })
const slidesAfter = createCloneSlides({ slides, position: 'after', toShow })
pushScopeId(getCurrentInstance()!.vnode.scopeId)
output = [...slidesBefore, ...output, ...slidesAfter]
}

Expand Down
1 change: 1 addition & 0 deletions src/components/Carousel/Carousel.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type InjectedCarousel = Reactive<{
viewport: Ref<Element | null>
slides: ShallowReactive<Array<ComponentInternalInstance>>
slidesCount: ComputedRef<number>
clonedSlidesCount: ComputedRef<number>
currentSlide: Ref<number>
scrolledIndex: Ref<number>
maxSlide: ComputedRef<number>
Expand Down
21 changes: 11 additions & 10 deletions src/utils/createCloneSlides.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { cloneVNode, ComponentInternalInstance, h } from 'vue'

import { Slide } from '@/components/Slide'
import { cloneVNode, ComponentInternalInstance, VNode } from 'vue'

type CreateCloneSlidesArgs = {
slides: Array<ComponentInternalInstance>
Expand All @@ -9,24 +7,27 @@ type CreateCloneSlidesArgs = {
}

export function createCloneSlides({ slides, position, toShow }: CreateCloneSlidesArgs) {
const clones = []
const clones: VNode[] = []
const isBefore = position === 'before'
const start = isBefore ? -toShow : 0
const end = isBefore ? 0 : toShow

if (slides.length <= 0) {
return clones
}

for (let i = start; i < end; i++) {
const index = isBefore ? i : slides.length > 0 ? i + slides.length : i + 99999
const index = isBefore ? i : i + slides.length
const props = {
index,
isClone: true,
id: undefined, // Make sure we don't duplicate the id which would be invalid html
key: `clone-${position}-${i}`,
}
clones.push(
slides.length > 0
? cloneVNode(slides[(i % slides.length + slides.length) % slides.length].vnode, props)
: h(Slide, props)
)
const vnode = slides[((i % slides.length) + slides.length) % slides.length].vnode
const clone = cloneVNode(vnode, props)
clone.el = null
clones.push(clone)
}

return clones
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/__snapshots__/carousel.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exports[`SSR Carousel > renders server side properly 1`] = `
</div>"
`;

exports[`SSR Carousel > renders server side properly 2`] = `"<div id="app"><section class="carousel is-ltr" style="" dir="ltr" aria-label="Gallery" tabindex="0"><div class="carousel__viewport"><ol class="carousel__track" style="transform:translateX(0px);"><li style="width:50%;" class="carousel__slide carousel__slide--clone" aria-hidden="true"></li><li style="width:50%;" class="carousel__slide carousel__slide--clone" aria-hidden="true"></li><li style="width:50%;" class="carousel__slide carousel__slide--clone" aria-hidden="true"></li><!--[--><li style="width:50%;" class="carousel__slide carousel__slide--visible carousel__slide--prev" id="v-0">1 <input type="text"></li><li style="width:50%;" class="carousel__slide carousel__slide--visible carousel__slide--active" id="v-1">2 <input type="text"></li><li style="width:50%;" class="carousel__slide carousel__slide--visible carousel__slide--next" id="v-2">3 <input type="text"></li><li style="width:50%;" class="carousel__slide" id="v-3">4 <input type="text"></li><li style="width:50%;" class="carousel__slide" id="v-4">5 <input type="text"></li><!--]--><li style="width:50%;" class="carousel__slide carousel__slide--clone" aria-hidden="true"></li><li style="width:50%;" class="carousel__slide carousel__slide--clone" aria-hidden="true"></li><li style="width:50%;" class="carousel__slide carousel__slide--clone" aria-hidden="true"></li></ol></div><!--[--><!--[--><button type="button" aria-label="Navigate to previous slide" title="Navigate to previous slide" class="carousel__prev"><svg class="carousel__icon" viewBox="0 0 24 24" role="img" aria-label="Arrow pointing to the left"><title>Arrow pointing to the left</title><path d="M15.41 16.59L10.83 12l4.58-4.59L14 6l-6 6 6 6 1.41-1.41z"></path></svg></button><button type="button" aria-label="Navigate to next slide" title="Navigate to next slide" class="carousel__next"><svg class="carousel__icon" viewBox="0 0 24 24" role="img" aria-label="Arrow pointing to the right"><title>Arrow pointing to the right</title><path d="M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z"></path></svg></button><!--]--><ol class="carousel__pagination"><li class="carousel__pagination-item"><button type="button" class="carousel__pagination-button" aria-label="Navigate to slide 1" aria-pressed="false" aria-controls="v-0" title="Navigate to slide 1"></button></li><li class="carousel__pagination-item"><button type="button" class="carousel__pagination-button carousel__pagination-button--active" aria-label="Navigate to slide 2" aria-pressed="true" aria-controls="v-1" title="Navigate to slide 2"></button></li><li class="carousel__pagination-item"><button type="button" class="carousel__pagination-button" aria-label="Navigate to slide 3" aria-pressed="false" aria-controls="v-2" title="Navigate to slide 3"></button></li><li class="carousel__pagination-item"><button type="button" class="carousel__pagination-button" aria-label="Navigate to slide 4" aria-pressed="false" aria-controls="v-3" title="Navigate to slide 4"></button></li><li class="carousel__pagination-item"><button type="button" class="carousel__pagination-button" aria-label="Navigate to slide 5" aria-pressed="false" aria-controls="v-4" title="Navigate to slide 5"></button></li></ol><!--]--><div class="carousel__liveregion carousel__sr-only" aria-live="polite" aria-atomic="true">Item 2 of 5</div></section></div>"`;
exports[`SSR Carousel > renders server side properly 2`] = `"<div id="app"><section class="carousel is-ltr" style="" dir="ltr" aria-label="Gallery" tabindex="0"><div class="carousel__viewport"><ol class="carousel__track" style="transform:translateX(0px);"><!--[--><li style="width:50%;" class="carousel__slide carousel__slide--visible carousel__slide--prev" id="v-0">1 <input type="text"></li><li style="width:50%;" class="carousel__slide carousel__slide--visible carousel__slide--active" id="v-1">2 <input type="text"></li><li style="width:50%;" class="carousel__slide carousel__slide--visible carousel__slide--next" id="v-2">3 <input type="text"></li><li style="width:50%;" class="carousel__slide" id="v-3">4 <input type="text"></li><li style="width:50%;" class="carousel__slide" id="v-4">5 <input type="text"></li><!--]--></ol></div><!--[--><!--[--><button type="button" aria-label="Navigate to previous slide" title="Navigate to previous slide" class="carousel__prev"><svg class="carousel__icon" viewBox="0 0 24 24" role="img" aria-label="Arrow pointing to the left"><title>Arrow pointing to the left</title><path d="M15.41 16.59L10.83 12l4.58-4.59L14 6l-6 6 6 6 1.41-1.41z"></path></svg></button><button type="button" aria-label="Navigate to next slide" title="Navigate to next slide" class="carousel__next"><svg class="carousel__icon" viewBox="0 0 24 24" role="img" aria-label="Arrow pointing to the right"><title>Arrow pointing to the right</title><path d="M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z"></path></svg></button><!--]--><ol class="carousel__pagination"><li class="carousel__pagination-item"><button type="button" class="carousel__pagination-button" aria-label="Navigate to slide 1" aria-pressed="false" aria-controls="v-0" title="Navigate to slide 1"></button></li><li class="carousel__pagination-item"><button type="button" class="carousel__pagination-button carousel__pagination-button--active" aria-label="Navigate to slide 2" aria-pressed="true" aria-controls="v-1" title="Navigate to slide 2"></button></li><li class="carousel__pagination-item"><button type="button" class="carousel__pagination-button" aria-label="Navigate to slide 3" aria-pressed="false" aria-controls="v-2" title="Navigate to slide 3"></button></li><li class="carousel__pagination-item"><button type="button" class="carousel__pagination-button" aria-label="Navigate to slide 4" aria-pressed="false" aria-controls="v-3" title="Navigate to slide 4"></button></li><li class="carousel__pagination-item"><button type="button" class="carousel__pagination-button" aria-label="Navigate to slide 5" aria-pressed="false" aria-controls="v-4" title="Navigate to slide 5"></button></li></ol><!--]--><div class="carousel__liveregion carousel__sr-only" aria-live="polite" aria-atomic="true">Item 2 of 5</div></section></div>"`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the change, I can see here the cloned slides will no be rendered on SSR.


exports[`SSR Carousel > renders slotted server side properly 1`] = `
"<div id="app">
Expand All @@ -59,7 +59,7 @@ exports[`SSR Carousel > renders slotted server side properly 1`] = `
</div>"
`;

exports[`SSR Carousel > renders slotted server side properly 2`] = `"<div id="app"><section class="carousel is-ltr" style="" dir="ltr" aria-label="Gallery" tabindex="0"><div class="carousel__viewport"><ol class="carousel__track" style="transform:translateX(0px);"><li style="width:100%;" class="carousel__slide carousel__slide--clone" aria-hidden="true"></li><li style="width:100%;" class="carousel__slide carousel__slide--clone carousel__slide--prev" aria-hidden="true"></li><!--[--><!--[--><li style="width:100%;" class="carousel__slide carousel__slide--visible carousel__slide--active" id="v-0">1</li><li style="width:100%;" class="carousel__slide carousel__slide--next" id="v-1">2</li><li style="width:100%;" class="carousel__slide" id="v-2">3</li><li style="width:100%;" class="carousel__slide" id="v-3">4</li><li style="width:100%;" class="carousel__slide" id="v-4">5</li><!--]--><!--]--><li style="width:100%;" class="carousel__slide carousel__slide--clone" aria-hidden="true"></li><li style="width:100%;" class="carousel__slide carousel__slide--clone" aria-hidden="true"></li></ol></div><!--[--><!--]--><div class="carousel__liveregion carousel__sr-only" aria-live="polite" aria-atomic="true">Item 1 of 5</div></section></div>"`;
exports[`SSR Carousel > renders slotted server side properly 2`] = `"<div id="app"><section class="carousel is-ltr" style="" dir="ltr" aria-label="Gallery" tabindex="0"><div class="carousel__viewport"><ol class="carousel__track" style="transform:translateX(0px);"><!--[--><!--[--><li style="width:100%;" class="carousel__slide carousel__slide--visible carousel__slide--active" id="v-0">1</li><li style="width:100%;" class="carousel__slide carousel__slide--next" id="v-1">2</li><li style="width:100%;" class="carousel__slide" id="v-2">3</li><li style="width:100%;" class="carousel__slide" id="v-3">4</li><li style="width:100%;" class="carousel__slide" id="v-4">5</li><!--]--><!--]--></ol></div><!--[--><!--]--><div class="carousel__liveregion carousel__sr-only" aria-live="polite" aria-atomic="true">Item 1 of 5</div></section></div>"`;

exports[`Wrap around Carousel.ts > renders wrapAround correctly 1`] = `
"<section class="carousel is-ltr" dir="ltr" aria-label="Gallery" tabindex="0">
Expand Down
Loading