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

SITE-23502 - resolving SSR issues with carousel. #1512

Merged
merged 27 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c2746ad
adding SSR check & default Width
Bsoong Sep 9, 2024
8c24ced
change file
Bsoong Sep 9, 2024
a1a3fc5
adding lockedWidth prop and SSR handling logic
Bsoong Sep 9, 2024
527cbfe
reverting changes to helpers
Bsoong Sep 9, 2024
837ca26
adding storyfix
Bsoong Sep 9, 2024
14bd6e9
exporting getVisibleSlides
Bsoong Sep 9, 2024
38ebce8
lockedWidth and SSR test
Bsoong Sep 9, 2024
1dd2e01
adjusted to use lockedWidth and add controls to storybook
Bsoong Sep 9, 2024
2e5fa2e
removing console log
Bsoong Sep 9, 2024
37c8e7e
Update SITE-23502_2024-09-09-14-02.json
Bsoong Sep 9, 2024
77138e6
set isBrowser in a useEffect for hydration issues
Bsoong Sep 9, 2024
c8c3f37
Merge branch 'SITE-23502' of https://github.com/priceline/design-syst…
Bsoong Sep 9, 2024
493ebbf
adding helper useIsBrowser
Bsoong Sep 9, 2024
8fb0d12
space
Bsoong Sep 9, 2024
9ebdde9
refactoring useResponsiveSlides
Bsoong Sep 9, 2024
9d21824
removing fn call
Bsoong Sep 9, 2024
3491215
reworking isBrowser usage
Bsoong Sep 9, 2024
82efc13
removing untouched file
Bsoong Sep 9, 2024
339bbf6
removing lockedWidth prop & fixing up useResponsive
Bsoong Sep 9, 2024
4e35202
removing unused fn & trest
Bsoong Sep 9, 2024
efde20e
rush change
Bsoong Sep 9, 2024
3e4e220
fixing exports
Bsoong Sep 9, 2024
49dbb62
hopefully fixing the issue
Bsoong Sep 9, 2024
27f9d67
default
Bsoong Sep 9, 2024
95f4c13
comments
Bsoong Sep 9, 2024
98b0226
Delete packages/carousel/src/use
Bsoong Sep 9, 2024
8fd54ec
Update common/changes/pcln-carousel/SITE-23502_2024-09-09-14-02.json
Bsoong Sep 10, 2024
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
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,
}
Loading