fix(SW-3189): Fixing hash navigation when query parameters exists on hotel pages
Approved-by: Hrishikesh Vaipurkar
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
import { cx } from "class-variance-authority"
|
import { cx } from "class-variance-authority"
|
||||||
import NextLink from "next/link"
|
import NextLink from "next/link"
|
||||||
import { useRouter } from "next/navigation"
|
|
||||||
import {
|
import {
|
||||||
useCallback,
|
useCallback,
|
||||||
useEffect,
|
useEffect,
|
||||||
@@ -32,7 +31,6 @@ interface TabNavigationProps {
|
|||||||
|
|
||||||
export default function TabNavigation({ pageSections }: TabNavigationProps) {
|
export default function TabNavigation({ pageSections }: TabNavigationProps) {
|
||||||
const [activeHash, setActiveHash] = useState<string>()
|
const [activeHash, setActiveHash] = useState<string>()
|
||||||
const router = useRouter()
|
|
||||||
const tabNavigationRef = useRef<HTMLDivElement>(null)
|
const tabNavigationRef = useRef<HTMLDivElement>(null)
|
||||||
const tabRefs = useMemo(() => new Map<string, HTMLAnchorElement>(), [])
|
const tabRefs = useMemo(() => new Map<string, HTMLAnchorElement>(), [])
|
||||||
const tabLinks = Object.values(pageSections).map(({ hash }) => hash)
|
const tabLinks = Object.values(pageSections).map(({ hash }) => hash)
|
||||||
@@ -85,10 +83,13 @@ export default function TabNavigation({ pageSections }: TabNavigationProps) {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (activeSectionId) {
|
if (activeSectionId) {
|
||||||
router.replace(`#${activeSectionId}`, { scroll: false })
|
const url = new URL(window.location.href)
|
||||||
|
url.hash = activeSectionId
|
||||||
|
window.history.replaceState(null, "", url.toString())
|
||||||
|
|
||||||
setActiveHash(activeSectionId)
|
setActiveHash(activeSectionId)
|
||||||
}
|
}
|
||||||
}, [activeSectionId, router])
|
}, [activeSectionId])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@@ -122,10 +123,22 @@ export default function TabNavigation({ pageSections }: TabNavigationProps) {
|
|||||||
tabRefs.set(hash, element)
|
tabRefs.set(hash, element)
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onClick={() => {
|
onClick={(e) => {
|
||||||
|
// Prevent default anchor behavior as it doesn't work well with query params
|
||||||
|
// and to ensure smooth scrolling works correctly
|
||||||
|
e.preventDefault()
|
||||||
pauseScrollSpy()
|
pauseScrollSpy()
|
||||||
setActiveHash(hash)
|
setActiveHash(hash)
|
||||||
trackHotelTabClick(heading)
|
trackHotelTabClick(heading)
|
||||||
|
const url = new URL(window.location.href)
|
||||||
|
url.hash = hash
|
||||||
|
window.history.pushState(null, "", url.toString())
|
||||||
|
|
||||||
|
// Manually scroll to the element with that ID
|
||||||
|
const element = document.getElementById(hash)
|
||||||
|
if (element) {
|
||||||
|
element.scrollIntoView({ behavior: "smooth" })
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{heading}
|
{heading}
|
||||||
|
|||||||
Reference in New Issue
Block a user