Skip to content

Commit

Permalink
SITE-23502 - resolving SSR issues with carousel. (#1512)
Browse files Browse the repository at this point in the history
* adding SSR check & default Width

* change file

* adding lockedWidth prop and SSR handling logic

* reverting changes to helpers

* adding storyfix

* exporting getVisibleSlides

* lockedWidth and SSR test

* adjusted to use lockedWidth and add controls to storybook

* removing console log

* Update SITE-23502_2024-09-09-14-02.json

* set isBrowser in a useEffect for hydration issues

* adding helper useIsBrowser

* space

* refactoring useResponsiveSlides

* removing fn call

* reworking isBrowser usage

* removing untouched file

* removing lockedWidth prop & fixing up useResponsive

* removing unused fn & trest

* rush change

* fixing exports

* hopefully fixing the issue

* default

* comments

* Delete packages/carousel/src/use

* Update common/changes/pcln-carousel/SITE-23502_2024-09-09-14-02.json

Co-authored-by: Craig Palermo <[email protected]>

---------

Co-authored-by: Craig Palermo <[email protected]>
  • Loading branch information
Bsoong and craigpalermo authored Sep 10, 2024
1 parent c3b1059 commit bb6340b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
10 changes: 10 additions & 0 deletions common/changes/pcln-carousel/SITE-23502_2024-09-09-14-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "pcln-carousel",
"comment": "Adding SSR check to no prevent crashes",
"type": "minor"
}
],
"packageName": "pcln-carousel"
}
47 changes: 29 additions & 18 deletions packages/carousel/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
VISIBLE_SLIDES_BREAKPOINT_2,
CAROUSEL_BREAKPOINT_1,
CAROUSEL_BREAKPOINT_2,
MEDIA_QUERY_MATCH,
MEDIA_QUERY_MATCH
} from './constants'
import debounce from 'lodash.debounce'

Expand All @@ -21,48 +21,59 @@ const getVisibleSlides = (visibleSlides, windowWidth) =>
windowWidth < CAROUSEL_BREAKPOINT_1
? visibleSlides[0]
: windowWidth < CAROUSEL_BREAKPOINT_2
? visibleSlides[1]
: visibleSlides[2]
? visibleSlides[1]
: visibleSlides[2]

const useResponsiveVisibleSlides = (visibleSlides) => {
const [width, setWidth] = useState(window.innerWidth)
const [width, setWidth] = useState(CAROUSEL_BREAKPOINT_2)

const handleResize = () => {
setWidth(window.innerWidth)
}
useEffect(() => {
// If window is undefined return default desktop breakpoint
if (typeof window === 'undefined') return {
responsiveVisibleSlides: getVisibleSlides(visibleSlides, width),
browserWidth: width,
}

// Debounce to avoid the function fire multiple times
const handleResizeDebounced = debounce(handleResize, 250)
// Client is initialized so we can access window, resize immediately with new context, and set up matchMedia listeners
const handleResize = () => {
setWidth(window.innerWidth)
}

useEffect(() => {
let media
handleResize()
const handleResizeDebounced = debounce(handleResize, 250)
let mediaQueryList
try {
media = window.matchMedia(MEDIA_QUERY_MATCH)
media.addEventListener('change', handleResizeDebounced)
mediaQueryList = window.matchMedia(MEDIA_QUERY_MATCH)
mediaQueryList.addEventListener('change', handleResizeDebounced)
} catch {
window.addEventListener('resize', handleResizeDebounced)
}

return () => {
if (media?.removeEventListener) {
media.removeEventListener('change', handleResizeDebounced)
if (mediaQueryList?.removeEventListener) {
mediaQueryList.removeEventListener('change', handleResizeDebounced)
} else {
window.removeEventListener('resize', handleResizeDebounced)
}
}
})
}, [])

return {
responsiveVisibleSlides: getVisibleSlides(visibleSlides, width),
browserWidth: width,
}
}


//This is to keep consistant with previous version.
//We should make a major version release that will allow more breakpoints
const getMobileVisibleSlidesArray = (visibleSlides) => [visibleSlides[0], null, visibleSlides[1]]

const getMobileVisibleSlides = (visibleSlides) =>
Array.isArray(visibleSlides) ? getMobileVisibleSlidesArray(visibleSlides) : visibleSlides

export { getSlideKey, getVisibleSlidesArray, useResponsiveVisibleSlides, getMobileVisibleSlides }
export {
getSlideKey,
getVisibleSlidesArray,
useResponsiveVisibleSlides,
getMobileVisibleSlides,
}

0 comments on commit bb6340b

Please sign in to comment.