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

Fix: Selected framework in Code component #12496

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
53 changes: 44 additions & 9 deletions docs/components/Code/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useSearchParams } from "next/navigation"
import { useRouter } from "next/router"
import { useThemeConfig } from "nextra-theme-docs"
import { Tabs } from "nextra/components"
import React, { Children, useEffect, MouseEvent } from "react"
import React, { Children, useEffect, MouseEvent, useState } from "react"

interface ChildrenProps {
children: React.ReactNode
Expand Down Expand Up @@ -58,28 +58,61 @@ export function Code({ children }: ChildrenProps) {

const renderedFrameworks = withNextJsPages ? allFrameworks : baseFrameworks

const [selectedFramework, setSelectedFramework] = useState<string>(
Object.values(renderedFrameworks)[0]
)

const updateFrameworkStorage = (value: string): void => {
const params = new URLSearchParams(searchParams?.toString())
params.set("framework", value)
router.push(`${router.pathname}?${params.toString()}`)
}

const handleFrameworkChange = (framework: string) => {
window.localStorage.setItem(AUTHJS_TAB_KEY, framework)
window.dispatchEvent(
new StorageEvent("storage", {
key: AUTHJS_TAB_KEY,
newValue: framework,
})
)
updateFrameworkStorage(parseParams(framework))
}

const handleClickFramework = (event: MouseEvent<HTMLDivElement>) => {
if (!(event.target instanceof HTMLButtonElement)) return
const { textContent } = event.target as unknown as HTMLDivElement
updateFrameworkStorage(parseParams(textContent!))
if (textContent) {
handleFrameworkChange(textContent)
}
}

useEffect(() => {
const length = Object.keys(renderedFrameworks).length
function handleStorage(event: StorageEvent) {
if (
event.key === AUTHJS_TAB_KEY &&
event.newValue &&
Object.values(renderedFrameworks).includes(event.newValue)
) {
setSelectedFramework(event.newValue)
}
}
window.addEventListener("storage", handleStorage)
return () => {
window.removeEventListener("storage", handleStorage)
}
}, [])

useEffect(() => {
const getFrameworkStorage = window.localStorage.getItem(AUTHJS_TAB_KEY)
const indexFramework = parseInt(getFrameworkStorage ?? "0") % length
if (!getFrameworkStorage) {
window.localStorage.setItem(AUTHJS_TAB_KEY, "0")
handleFrameworkChange(Object.values(renderedFrameworks)[0])
} else if (
Object.values(renderedFrameworks).includes(getFrameworkStorage)
) {
setSelectedFramework(getFrameworkStorage)
updateFrameworkStorage(parseParams(getFrameworkStorage))
}
updateFrameworkStorage(
parseParams(Object.values(renderedFrameworks)[indexFramework])
)
}, [router.pathname, renderedFrameworks])

return (
Expand All @@ -88,7 +121,9 @@ export function Code({ children }: ChildrenProps) {
onClick={handleClickFramework}
>
<Tabs
storageKey={AUTHJS_TAB_KEY}
selectedIndex={Object.values(renderedFrameworks).indexOf(
selectedFramework
)}
items={Object.values(renderedFrameworks)}
>
{Object.keys(renderedFrameworks).map((f) => {
Expand Down
Loading