docs: DX and performance improvements in API reference (#9430)
- Improve scroll behavior between active sections - Improve lag when clicking on a sidebar item - Refactor internal working of the `SidebarProvider` to find active items faster. - Use Next.js's `useRouter` hook for changing the hash (since they added the option to disable scroll) - Change `isBrowser` from a hook to a provider since it's widely used across applications. - Other general improvements and fixes. Closes DOCS-952
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
"use client"
|
||||
|
||||
import React, { useContext, useEffect, useState } from "react"
|
||||
|
||||
type BrowserContextType = {
|
||||
isBrowser: boolean
|
||||
}
|
||||
|
||||
const BrowserContext = React.createContext<BrowserContextType | null>(null)
|
||||
|
||||
type BrowserProviderProps = {
|
||||
children: React.ReactNode
|
||||
}
|
||||
|
||||
export const BrowserProvider = ({ children }: BrowserProviderProps) => {
|
||||
const [isBrowser, setIsBrowser] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
setIsBrowser(typeof window !== "undefined")
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<BrowserContext.Provider
|
||||
value={{
|
||||
isBrowser,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</BrowserContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export const useIsBrowser = () => {
|
||||
const context = useContext(BrowserContext)
|
||||
|
||||
if (!context) {
|
||||
throw new Error("useIsBrowser must be used within a BrowserProvider")
|
||||
}
|
||||
|
||||
return context
|
||||
}
|
||||
Reference in New Issue
Block a user