feat(SW-1960): Improved scrolling and tabnavigation on hotel pages

Approved-by: Matilda Landström
This commit is contained in:
Erik Tiekstra
2025-05-26 12:32:18 +00:00
parent a8e52c6c8a
commit d73f8d844e
8 changed files with 205 additions and 69 deletions

View File

@@ -18,6 +18,7 @@
--main-menu-mobile-height: 75px; --main-menu-mobile-height: 75px;
--main-menu-desktop-height: 125px; --main-menu-desktop-height: 125px;
--booking-widget-mobile-height: 75px; --booking-widget-mobile-height: 75px;
--booking-widget-tablet-height: 150px;
--booking-widget-desktop-height: 77px; --booking-widget-desktop-height: 77px;
--hotel-page-map-desktop-width: 23.75rem; --hotel-page-map-desktop-width: 23.75rem;

View File

@@ -1,3 +1,7 @@
.accordionSection {
scroll-margin-top: var(--hotel-page-scroll-margin-top);
}
.accordion:not(.allVisible) :nth-child(n + 6) { .accordion:not(.allVisible) :nth-child(n + 6) {
display: none; display: none;
} }

View File

@@ -24,7 +24,10 @@ export default function AccordionSection({ accordion, title }: AccordionProps) {
} }
return ( return (
<SectionContainer id={HotelHashValues.faq}> <SectionContainer
className={styles.accordionSection}
id={HotelHashValues.faq}
>
<SectionHeader textTransform="uppercase" title={title} /> <SectionHeader textTransform="uppercase" title={title} />
<Accordion <Accordion
className={`${styles.accordion} ${allAccordionsVisible ? styles.allVisible : ""}`} className={`${styles.accordion} ${allAccordionsVisible ? styles.allVisible : ""}`}

View File

@@ -1,13 +1,17 @@
"use client" "use client"
import { cx } from "class-variance-authority"
import NextLink from "next/link"
import { useRouter } from "next/navigation" import { useRouter } from "next/navigation"
import { useEffect, useRef } from "react" import { useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react"
import { useIntl } from "react-intl" import { useIntl } from "react-intl"
import { Typography } from "@scandic-hotels/design-system/Typography"
import { StickyElementNameEnum } from "@/stores/sticky-position" import { StickyElementNameEnum } from "@/stores/sticky-position"
import Link from "@/components/TempDesignSystem/Link"
import useHash from "@/hooks/useHash" import useHash from "@/hooks/useHash"
import useScrollShadows from "@/hooks/useScrollShadows"
import useScrollSpy from "@/hooks/useScrollSpy" import useScrollSpy from "@/hooks/useScrollSpy"
import useStickyPosition from "@/hooks/useStickyPosition" import useStickyPosition from "@/hooks/useStickyPosition"
import { trackHotelTabClick } from "@/utils/tracking" import { trackHotelTabClick } from "@/utils/tracking"
@@ -31,12 +35,17 @@ export default function TabNavigation({
const intl = useIntl() const intl = useIntl()
const router = useRouter() const router = useRouter()
const tabNavigationRef = useRef<HTMLDivElement>(null) const tabNavigationRef = useRef<HTMLDivElement>(null)
const tabRefs = useMemo(() => new Map<string, HTMLAnchorElement>(), [])
useStickyPosition({ useStickyPosition({
ref: tabNavigationRef, ref: tabNavigationRef,
name: StickyElementNameEnum.HOTEL_TAB_NAVIGATION, name: StickyElementNameEnum.HOTEL_TAB_NAVIGATION,
group: "hotelPage", group: "hotelPage",
}) })
const { containerRef, showLeftShadow, showRightShadow } =
useScrollShadows<HTMLDivElement>()
const tabLinks: { hash: HotelHashValues; text: string }[] = [ const tabLinks: { hash: HotelHashValues; text: string }[] = [
{ {
hash: HotelHashValues.overview, hash: HotelHashValues.overview,
@@ -120,6 +129,41 @@ export default function TabNavigation({
tabLinks.map(({ hash }) => hash) tabLinks.map(({ hash }) => hash)
) )
const scrollLinkToCenter = useCallback(
(hash: string) => {
requestAnimationFrame(() => {
const activeTab = tabRefs.get(hash)
if (!containerRef.current || !activeTab) {
return
}
const container = containerRef.current
const containerWidth = container.offsetWidth
const tabWidth = activeTab.offsetWidth
const tabLeft = activeTab.offsetLeft
const scrollLeft = tabLeft - containerWidth / 2 + tabWidth / 2
const maxScrollLeft = container.scrollWidth - containerWidth
// Ensure scrollLeft is inside the viewport
const boundedScrollLeft = Math.max(
0,
Math.min(scrollLeft, maxScrollLeft)
)
container.scrollTo({
left: boundedScrollLeft,
behavior: "smooth",
})
})
},
[containerRef, tabRefs]
)
useLayoutEffect(() => {
const activeHash = hash || HotelHashValues.overview
scrollLinkToCenter(activeHash)
}, [hash, scrollLinkToCenter])
useEffect(() => { useEffect(() => {
if (activeSectionId) { if (activeSectionId) {
router.replace(`#${activeSectionId}`, { scroll: false }) router.replace(`#${activeSectionId}`, { scroll: false })
@@ -127,28 +171,45 @@ export default function TabNavigation({
}, [activeSectionId, router]) }, [activeSectionId, router])
return ( return (
<div ref={tabNavigationRef} className={styles.stickyWrapper}> <div
<nav className={styles.tabsContainer}> ref={tabNavigationRef}
className={cx(styles.containerWrapper, {
[styles.showLeftShadow]: showLeftShadow,
[styles.showRightShadow]: showRightShadow,
})}
>
<nav className={styles.tabsContainer} ref={containerRef}>
{tabLinks.map((link) => { {tabLinks.map((link) => {
const isActive = const isActive =
hash === link.hash || hash === link.hash ||
(!hash && link.hash === HotelHashValues.overview) (!hash && link.hash === HotelHashValues.overview)
return ( return (
<Link <Typography
key={link.hash} key={link.hash}
variant={
isActive ? "Body/Paragraph/mdBold" : "Body/Paragraph/mdRegular"
}
>
<NextLink
href={`#${link.hash}`} href={`#${link.hash}`}
active={isActive} className={cx(styles.link, {
variant="tab" [styles.active]: isActive,
color="burgundy" })}
textDecoration="none" aria-current={isActive}
scroll={true} scroll={true}
ref={(element) => {
if (element) {
tabRefs.set(link.hash, element)
}
}}
onClick={() => { onClick={() => {
pauseScrollSpy() pauseScrollSpy()
trackHotelTabClick(link.text) trackHotelTabClick(link.text)
}} }}
> >
{link.text} {link.text}
</Link> </NextLink>
</Typography>
) )
})} })}
</nav> </nav>

View File

@@ -1,33 +1,77 @@
.stickyWrapper { .containerWrapper {
position: sticky; position: sticky;
top: var(--booking-widget-mobile-height); top: 0;
z-index: var(--hotel-tab-navigation-z-index); z-index: var(--hotel-tab-navigation-z-index);
background-color: var(--Base-Surface-Subtle-Normal); background-color: var(--Surface-Secondary-Default);
border-bottom: 1px solid var(--Base-Border-Subtle); border-bottom: 1px solid var(--Border-Default);
overflow-x: auto; width: 100%;
white-space: nowrap;
&::before,
&::after {
content: "";
position: absolute;
top: 0;
bottom: 0;
width: 76px;
pointer-events: none;
z-index: 1;
opacity: 0;
transition: opacity 0.3s ease;
}
&::before {
left: 0;
background: linear-gradient(
-90deg,
rgba(242, 236, 230, 0.1) 6%,
rgba(242, 236, 230, 1) 90%
);
}
&::after {
right: 0;
background: linear-gradient(
90deg,
rgba(242, 236, 230, 0.1) 6%,
rgba(242, 236, 230, 1) 90%
);
}
&.showLeftShadow::before,
&.showRightShadow::after {
opacity: 1;
}
} }
.tabsContainer { .tabsContainer {
display: flex; display: flex;
gap: var(--Spacing-x4); gap: var(--Space-x4);
justify-content: flex-start; justify-content: flex-start;
width: 100%; width: var(--max-width-content);
overflow-x: auto;
max-width: var(--max-width-page);
margin: 0 auto; margin: 0 auto;
overflow-y: hidden;
overflow-x: auto;
scroll-snap-type: x mandatory;
scrollbar-width: none;
white-space: nowrap;
margin-bottom: -1px; /* Adjust for border overlap */
} }
@media screen and (min-width: 768px) { .link {
.stickyWrapper { color: var(--Text-Interactive-Default);
top: var(--booking-widget-desktop-height); padding: var(--Space-x2) var(--Space-x0);
text-decoration: none;
border-bottom: 2px solid transparent;
transition: border-bottom-color 0.3s ease-in-out;
&:hover,
&.active {
border-bottom-color: var(--Text-Interactive-Default);
} }
} }
@media screen and (min-width: 1367px) { @media screen and (min-width: 1367px) {
.tabsContainer { .tabsContainer {
padding: 0 var(--Spacing-x5); padding: 0 var(--Space-x5);
max-width: calc(100% - var(--hotel-page-map-desktop-width));
overflow-x: visible; overflow-x: visible;
margin: 0; margin: 0;
} }

View File

@@ -59,6 +59,10 @@
@media screen and (min-width: 768px) { @media screen and (min-width: 768px) {
.pageContainer { .pageContainer {
--hotel-page-scroll-margin-top: calc(
var(--hotel-page-navigation-height) +
var(--booking-widget-tablet-height) + var(--Space-x2)
);
margin: 0 auto; margin: 0 auto;
} }

View File

@@ -1,6 +1,33 @@
.containerWrapper { .containerWrapper {
position: relative; position: relative;
overflow: hidden; overflow: hidden;
&::before,
&::after {
content: "";
position: absolute;
top: 0;
bottom: 0;
width: var(--Space-x3);
pointer-events: none;
z-index: 1;
opacity: 0;
transition: opacity 0.3s ease;
&.showLeftShadow::before,
&.showRightShadow::after {
opacity: 1;
}
}
&::before {
left: 0;
background: linear-gradient(-90deg, rgba(255, 255, 255, 0.1) 6%, #fff 90%);
}
&::after {
right: 0;
background: linear-gradient(90deg, rgba(255, 255, 255, 0.1) 6%, #fff 90%);
}
} }
.container { .container {
@@ -13,39 +40,6 @@
width: 100%; width: 100%;
} }
.containerWrapper::after {
content: "";
position: absolute;
top: 0;
bottom: 0;
width: var(--Space-x3);
pointer-events: none;
z-index: 1;
opacity: 0;
transition: opacity 0.3s ease;
right: 0;
background: linear-gradient(90deg, rgba(255, 255, 255, 0.1) 6%, #fff 90%);
}
.containerWrapper::before {
content: "";
position: absolute;
top: 0;
bottom: 0;
width: var(--Space-x3);
pointer-events: none;
z-index: 1;
opacity: 0;
transition: opacity 0.3s ease;
left: 0;
background: linear-gradient(-90deg, rgba(255, 255, 255, 0.1) 6%, #fff 90%);
}
.showLeftShadow::before,
.showRightShadow::after {
opacity: 1;
}
.filter { .filter {
display: flex; display: flex;
align-items: center; align-items: center;

View File

@@ -1,5 +1,7 @@
import { useCallback, useEffect, useMemo, useRef, useState } from "react" import { useCallback, useEffect, useMemo, useRef, useState } from "react"
import { debounce } from "../utils/debounce"
export default function useScrollSpy( export default function useScrollSpy(
sectionIds: string[], sectionIds: string[],
options: IntersectionObserverInit = {} options: IntersectionObserverInit = {}
@@ -9,6 +11,7 @@ export default function useScrollSpy(
} { } {
const [activeSectionId, setActiveSectionId] = useState("") const [activeSectionId, setActiveSectionId] = useState("")
const observerIsInactive = useRef(false) const observerIsInactive = useRef(false)
const safetyTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
const mergedOptions = useMemo( const mergedOptions = useMemo(
() => ({ () => ({
@@ -42,6 +45,22 @@ export default function useScrollSpy(
[] []
) )
// Scroll event listener to reset the observerIsInactive state
// This is to support the pauseScrollSpy function to prevent the observer from firing when scrolling.
useEffect(() => {
const handleScroll = debounce(() => {
if (observerIsInactive.current) {
observerIsInactive.current = false
}
}, 200)
window.addEventListener("scroll", handleScroll, { passive: true })
return () => {
window.removeEventListener("scroll", handleScroll)
}
}, [])
useEffect(() => { useEffect(() => {
const observer = new IntersectionObserver(handleIntersection, mergedOptions) const observer = new IntersectionObserver(handleIntersection, mergedOptions)
const elements = sectionIds const elements = sectionIds
@@ -55,12 +74,18 @@ export default function useScrollSpy(
return () => elements.forEach((el) => el && observer.unobserve(el)) return () => elements.forEach((el) => el && observer.unobserve(el))
}, [sectionIds, mergedOptions, handleIntersection]) }, [sectionIds, mergedOptions, handleIntersection])
const pauseScrollSpy = () => { function pauseScrollSpy() {
observerIsInactive.current = true observerIsInactive.current = true
setTimeout(() => { if (safetyTimeoutRef.current) {
clearTimeout(safetyTimeoutRef.current)
}
// Safety timeout - maximum time to wait before re-enabling
safetyTimeoutRef.current = setTimeout(() => {
observerIsInactive.current = false observerIsInactive.current = false
}, 500) safetyTimeoutRef.current = null
}, 1500)
} }
return { activeSectionId, pauseScrollSpy } return { activeSectionId, pauseScrollSpy }