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

v4: Add defaultOpen option for sidebar visibility control #4042

Merged
merged 14 commits into from
Jan 28, 2025
Merged
1 change: 1 addition & 0 deletions docs/app/docs/docs-theme/built-ins/layout/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ The above is also equivalent to `navigation: true{:js}`.
| `sidebar.autoCollapse` | `boolean{:ts}` | | If true, automatically collapse inactive folders above `defaultMenuCollapseLevel`. |
| `sidebar.defaultMenuCollapseLevel` | `number{:ts}` | `2{:ts}` | Specifies the folder level at which the menu on the left is collapsed by default. |
| `sidebar.toggleButton` | `boolean{:ts}` | `true{:ts}` | Hide/show sidebar toggle button. |
| `sidebar.defaultOpen` | `boolean{:ts}` | `true{:ts}` | Hide/show sidebar by default. |

### Menu Collapse Level

Expand Down
3 changes: 2 additions & 1 deletion examples/swr-site/app/[lang]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ export default async function RootLayout({ children, params }) {
]}
sidebar={{
defaultMenuCollapseLevel: 1,
autoCollapse: true
autoCollapse: true,
defaultOpen: true
}}
toc={{
backToTop: dictionary.backToTop,
Expand Down
4 changes: 2 additions & 2 deletions packages/nextra-theme-docs/src/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ export const MobileNav: FC = () => {

export const Sidebar: FC<{ toc: Heading[] }> = ({ toc }) => {
const { normalizePagesResult, hideSidebar } = useConfig()
const [isExpanded, setIsExpanded] = useState(true)
const themeConfig = useThemeConfig()
const [isExpanded, setIsExpanded] = useState(themeConfig.sidebar.defaultOpen)
const [showToggleAnimation, setToggleAnimation] = useState(false)
const sidebarRef = useRef<HTMLDivElement>(null)
const sidebarControlsId = useId()
Expand All @@ -380,7 +381,6 @@ export const Sidebar: FC<{ toc: Heading[] }> = ({ toc }) => {
}
}, [])

const themeConfig = useThemeConfig()
const anchors =
// When the viewport size is larger than `md`, hide the anchors in
// the sidebar when `floatTOC` is enabled.
Expand Down
3 changes: 2 additions & 1 deletion packages/nextra-theme-docs/src/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ const theme = z.strictObject({
.strictObject({
autoCollapse: z.boolean().optional(),
defaultMenuCollapseLevel: z.number().min(1).int().default(2),
toggleButton: z.boolean().default(true)
toggleButton: z.boolean().default(true),
defaultOpen: z.boolean().default(true),
})
.default({}),
themeSwitch: z
Expand Down
6 changes: 3 additions & 3 deletions packages/nextra/src/client/components/collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export const Collapse: FC<{

if (horizontal) {
// save initial width to avoid word wrapping when container width will be changed
child.style.width = `${child.clientWidth}px`
container.style.width = `${child.clientWidth}px`
// child.style.width = `${child.clientWidth}px`
// container.style.width = `${child.clientWidth}px`
} else {
container.style.height = `${child.clientHeight}px`
}
Expand All @@ -49,7 +49,7 @@ export const Collapse: FC<{
} else {
setTimeout(() => {
if (horizontal) {
container.style.width = '0'
// container.style.width = '0'
} else {
container.style.height = '0'
}
Expand Down