Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix sidebar highlight sync in the reference docs (#42249)

Illia Basalaiev53b076091866
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenuRefListItems.tsx b/apps/docs/components/Navigation/NavigationMenu/NavigationMenuRefListItems.tsx
index fa84e200b06..328e91d112a 100644
--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenuRefListItems.tsx
+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenuRefListItems.tsx
@@ -58,6 +58,7 @@ const FunctionLink = memo(function FunctionLink({
*/
onClick={(e) => {
e.preventDefault()
+ menuState.setMenuActiveRefId(id)
history.pushState({}, '', url)
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches
document.getElementById(slug)?.scrollIntoView({
diff --git a/apps/docs/features/docs/Reference.navigation.client.tsx b/apps/docs/features/docs/Reference.navigation.client.tsx
index 485f6542c78..5df531fc063 100644
--- a/apps/docs/features/docs/Reference.navigation.client.tsx
+++ b/apps/docs/features/docs/Reference.navigation.client.tsx
@@ -7,7 +7,16 @@ import { ChevronUp } from 'lucide-react'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import type { HTMLAttributes, MouseEvent, PropsWithChildren } from 'react'
-import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'
+import {
+ createContext,
+ useCallback,
+ useContext,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+ useSyncExternalStore,
+} from 'react'
import { cn } from 'ui'
@@ -17,6 +26,63 @@ import { BASE_PATH } from '~/lib/constants'
export const ReferenceContentInitiallyScrolledContext = createContext<boolean>(false)
+let patchCount = 0
+let originalPushState: typeof history.pushState | null = null
+let originalReplaceState: typeof history.replaceState | null = null
+const pathnameListeners = new Set<() => void>()
+
+function notifyPathnameListeners() {
+ pathnameListeners.forEach((callback) => callback())
+}
+
+function subscribeToPathname(callback: () => void) {
+ pathnameListeners.add(callback)
+
+ if (patchCount === 0) {
+ window.addEventListener('popstate', notifyPathnameListeners)
+
+ originalPushState = history.pushState.bind(history)
+ history.pushState = (...args) => {
+ originalPushState!(...args)
+ notifyPathnameListeners()
+ }
+
+ originalReplaceState = history.replaceState.bind(history)
+ history.replaceState = (...args) => {
+ originalReplaceState!(...args)
+ notifyPathnameListeners()
+ }
+ }
+ patchCount++
+
+ return () => {
+ pathnameListeners.delete(callback)
+ patchCount--
+
+ if (patchCount === 0) {
+ window.removeEventListener('popstate', notifyPathnameListeners)
+ history.pushState = originalPushState!
+ history.replaceState = originalReplaceState!
+ originalPushState = null
+ originalReplaceState = null
+ }
+ }
+}
+
+function getPathname() {
+ if (typeof window === 'undefined') return ''
+ const pathname = window.location.pathname
+ return pathname.startsWith(BASE_PATH) ? pathname.slice(BASE_PATH.length) : pathname
+}
+
+function getServerPathname() {
+ return ''
+}
+
+function useCurrentPathname() {
+ return useSyncExternalStore(subscribeToPathname, getPathname, getServerPathname)
+}
+
export function ReferenceContentScrollHandler({
libPath,
version,
@@ -184,7 +250,7 @@ export function RefLink({
}) {
const ref = useRef<HTMLAnchorElement>(null)
- const pathname = usePathname()
+ const pathname = useCurrentPathname()
const href = deriveHref(basePath, section)
const isActive =
pathname === href || (pathname === basePath && href.replace(basePath, '') === '/introduction')
@@ -230,7 +296,7 @@ export function RefLink({
function useCompoundRefLinkActive(basePath: string, section: AbbrevApiReferenceSection) {
const [open, _setOpen] = useState(false)
- const pathname = usePathname()
+ const pathname = useCurrentPathname()
const parentHref = deriveHref(basePath, section)
const isParentActive = pathname === parentHref