Skip to content

Commit b045cc3

Browse files
author
Dimitri POSTOLOV
authoredOct 8, 2023
[v3] fix invisible mobile menu after click on hamburger (#2400)

File tree

6 files changed

+92
-65
lines changed

6 files changed

+92
-65
lines changed
 

‎.changeset/beige-moose-learn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'nextra-theme-docs': patch
3+
---
4+
5+
fix invisible mobile menu after click on hamburger

‎packages/nextra-theme-docs/src/components/sidebar.tsx

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import cn from 'clsx'
2-
import { useRouter } from 'next/router'
32
import type { Heading } from 'nextra'
43
import { useFSRoute, useMounted } from 'nextra/hooks'
54
import { ArrowRightIcon, ExpandIcon } from 'nextra/icons'
@@ -321,7 +320,6 @@ export function Sidebar({
321320
}: SideBarProps): ReactElement {
322321
const config = useConfig()
323322
const { menu, setMenu } = useMenu()
324-
const router = useRouter()
325323
const [focused, setFocused] = useState<null | string>(null)
326324
const [showSidebar, setSidebar] = useState(true)
327325
const [showToggleAnimation, setToggleAnimation] = useState(false)
@@ -359,19 +357,14 @@ export function Sidebar({
359357
}
360358
}, [menu])
361359

362-
// Always close mobile nav when route was changed (e.g. logo click)
363-
useEffect(() => {
364-
setMenu(false)
365-
}, [router.asPath, setMenu])
366-
367360
const hasI18n = config.i18n.length > 0
368361
const hasMenu = config.darkMode || hasI18n || config.sidebar.toggleButton
369362

370363
return (
371364
<>
372-
{includePlaceholder && asPopover ? (
365+
{includePlaceholder && asPopover && (
373366
<div className="max-xl:_hidden _h-0 _w-64 _shrink-0" />
374-
) : null}
367+
)}
375368
<div
376369
className={cn(
377370
'motion-reduce:_transition-none [transition:background-color_1.5s_ease]',
@@ -460,7 +453,7 @@ export function Sidebar({
460453
>
461454
<LocaleSwitch
462455
lite={!showSidebar}
463-
className={cn(showSidebar ? '_grow' : 'max-md:_grow')}
456+
className={showSidebar ? '_grow' : 'max-md:_grow'}
464457
/>
465458
{config.darkMode && (
466459
<div

‎packages/nextra-theme-docs/src/contexts/config.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { ThemeProvider } from 'next-themes'
2+
import { useRouter } from 'next/router'
23
import type { FrontMatter, PageMapItem, PageOpts } from 'nextra'
34
import { metaSchema } from 'nextra/schemas'
45
import type { ReactElement, ReactNode } from 'react'
5-
import { createContext, useContext, useState } from 'react'
6+
import { createContext, useContext, useEffect, useState } from 'react'
67
import type { ZodError } from 'zod'
78
import type { DocsThemeConfig } from '../constants'
89
import { DEEP_OBJECT_KEYS, DEFAULT_THEME } from '../constants'
@@ -71,6 +72,7 @@ export function ConfigProvider({
7172
value: Context
7273
}): ReactElement {
7374
const [menu, setMenu] = useState(false)
75+
const { asPath } = useRouter()
7476
// Merge only on first load
7577
theme ||= {
7678
...DEFAULT_THEME,
@@ -106,6 +108,11 @@ export function ConfigProvider({
106108

107109
const { nextThemes } = extendedConfig
108110

111+
// Always close mobile nav when route was changed (e.g. logo click)
112+
useEffect(() => {
113+
setMenu(false)
114+
}, [asPath])
115+
109116
return (
110117
<ThemeProvider
111118
attribute="class"

‎packages/nextra-theme-docs/src/index.tsx

+26-7
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,9 @@ function InnerLayout({
136136
const { direction } = config.i18n.find(l => l.locale === locale) || config
137137
const dir = direction === 'rtl' ? 'rtl' : 'ltr'
138138

139-
const components = getComponents({
140-
isRawLayout: themeContext.layout === 'raw',
141-
components: {
142-
...config.components,
143-
// @ts-expect-error fixme
144-
wrapper: function NextraWrapper({ toc, children }) {
139+
const wrapper: NextraMDXContent = useMemo(
140+
() =>
141+
function NextraWrapper({ toc, children }) {
145142
const tocEl =
146143
activeType === 'page' ||
147144
!themeContext.toc ||
@@ -198,7 +195,29 @@ function InnerLayout({
198195
</Body>
199196
</div>
200197
)
201-
} satisfies NextraMDXContent
198+
},
199+
[
200+
activeIndex,
201+
activePath,
202+
activeType,
203+
config.toc.component,
204+
config.toc.float,
205+
directories,
206+
docsDirectories,
207+
filePath,
208+
flatDocsDirectories,
209+
hideSidebar,
210+
themeContext,
211+
timestamp
212+
]
213+
)
214+
215+
const components = getComponents({
216+
isRawLayout: themeContext.layout === 'raw',
217+
components: {
218+
...config.components,
219+
// @ts-expect-error fixme
220+
wrapper
202221
}
203222
})
204223

‎packages/nextra-theme-docs/src/mdx-components.tsx

+50-46
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,55 @@ const A = ({ href = '', ...props }) => (
175175
<Anchor href={href} newWindow={EXTERNAL_HREF_REGEX.test(href)} {...props} />
176176
)
177177

178+
const DEFAULT_COMPONENTS: Components = {
179+
h1: props => (
180+
<h1
181+
className="_mt-2 _text-4xl _font-bold _tracking-tight _text-slate-900 dark:_text-slate-100"
182+
{...props}
183+
/>
184+
),
185+
ul: props => (
186+
<ul
187+
className="_mt-6 _list-disc first:_mt-0 ltr:_ml-6 rtl:_mr-6"
188+
{...props}
189+
/>
190+
),
191+
ol: props => (
192+
<ol
193+
className="_mt-6 _list-decimal first:_mt-0 ltr:_ml-6 rtl:_mr-6"
194+
{...props}
195+
/>
196+
),
197+
li: props => <li className="_my-2" {...props} />,
198+
blockquote: props => (
199+
<blockquote
200+
className={cn(
201+
'_mt-6 _border-gray-300 _italic _text-gray-700 dark:_border-gray-700 dark:_text-gray-400',
202+
'first:_mt-0 ltr:_border-l-2 ltr:_pl-6 rtl:_border-r-2 rtl:_pr-6'
203+
)}
204+
{...props}
205+
/>
206+
),
207+
hr: props => (
208+
<hr
209+
className="_my-8 _border-neutral-200/70 contrast-more:_border-neutral-400 dark:_border-primary-100/10 contrast-more:dark:_border-neutral-400"
210+
{...props}
211+
/>
212+
),
213+
a: Link,
214+
table: props => (
215+
<Table className="nextra-scrollbar _mt-6 _p-0 first:_mt-0" {...props} />
216+
),
217+
p: props => <p className="_mt-6 _leading-7 first:_mt-0" {...props} />,
218+
tr: Tr,
219+
th: Th,
220+
td: Td,
221+
details: Details,
222+
summary: Summary,
223+
pre: Pre,
224+
code: Code
225+
}
226+
178227
export function getComponents({
179228
isRawLayout,
180229
components
@@ -188,57 +237,12 @@ export function getComponents({
188237

189238
const context = { index: 0 }
190239
return {
191-
h1: props => (
192-
<h1
193-
className="_mt-2 _text-4xl _font-bold _tracking-tight _text-slate-900 dark:_text-slate-100"
194-
{...props}
195-
/>
196-
),
240+
...DEFAULT_COMPONENTS,
197241
h2: createHeading('h2', context),
198242
h3: createHeading('h3', context),
199243
h4: createHeading('h4', context),
200244
h5: createHeading('h5', context),
201245
h6: createHeading('h6', context),
202-
ul: props => (
203-
<ul
204-
className="_mt-6 _list-disc first:_mt-0 ltr:_ml-6 rtl:_mr-6"
205-
{...props}
206-
/>
207-
),
208-
ol: props => (
209-
<ol
210-
className="_mt-6 _list-decimal first:_mt-0 ltr:_ml-6 rtl:_mr-6"
211-
{...props}
212-
/>
213-
),
214-
li: props => <li className="_my-2" {...props} />,
215-
blockquote: props => (
216-
<blockquote
217-
className={cn(
218-
'_mt-6 _border-gray-300 _italic _text-gray-700 dark:_border-gray-700 dark:_text-gray-400',
219-
'first:_mt-0 ltr:_border-l-2 ltr:_pl-6 rtl:_border-r-2 rtl:_pr-6'
220-
)}
221-
{...props}
222-
/>
223-
),
224-
hr: props => (
225-
<hr
226-
className="_my-8 _border-neutral-200/70 contrast-more:_border-neutral-400 dark:_border-primary-100/10 contrast-more:dark:_border-neutral-400"
227-
{...props}
228-
/>
229-
),
230-
a: Link,
231-
table: props => (
232-
<Table className="nextra-scrollbar _mt-6 _p-0 first:_mt-0" {...props} />
233-
),
234-
p: props => <p className="_mt-6 _leading-7 first:_mt-0" {...props} />,
235-
tr: Tr,
236-
th: Th,
237-
td: Td,
238-
details: Details,
239-
summary: Summary,
240-
pre: Pre,
241-
code: Code,
242246
...components
243247
}
244248
}

‎packages/nextra/src/client/setup-page.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ export function HOC_MDXWrapper(
153153
]
154154
const { Layout, themeConfig } = __nextra_internal__
155155
const { route } = useRouter()
156-
console.log({ route })
157156

158157
const pageContext = __nextra_internal__.context[route]
159158

0 commit comments

Comments
 (0)
Please sign in to comment.