Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/theme-default/src/assets/panel-left-close.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/theme-default/src/assets/panel-left-open.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 35 additions & 3 deletions packages/theme-default/src/components/Nav/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { useLocation, usePageData, useWindowSize } from '@rspress/runtime';
import type { NavItem } from '@rspress/shared';
import { Search } from '@theme';
import PanelLeftClose from '@theme-assets/panel-left-close';
import PanelLeftOpen from '@theme-assets/panel-left-open';
import { useHiddenNav } from '../../logic/useHiddenNav';
import { useMediaQuery } from '../../logic/useMediaQuery';
import { useNavData } from '../../logic/useNav';
import { useUISwitch } from '../../logic/useUISwitch';
import { NavHamburger } from '../NavHamburger';
import { SocialLinks } from '../SocialLinks';
import { SvgWrapper } from '../SvgWrapper';
import { SwitchAppearance } from '../SwitchAppearance';
import { NavBarTitle } from './NavBarTitle';
import { NavMenuGroup } from './NavMenuGroup';
Expand All @@ -19,13 +24,23 @@ export interface NavProps {
navTitle?: React.ReactNode;
afterNavTitle?: React.ReactNode;
afterNavMenu?: React.ReactNode;
showSidebar?: boolean;
toggleShowSidebar?: () => void;
}

const DEFAULT_NAV_POSITION = 'right';

export function Nav(props: NavProps) {
const { beforeNavTitle, afterNavTitle, beforeNav, afterNavMenu, navTitle } =
props;
const {
beforeNavTitle,
afterNavTitle,
beforeNav,
afterNavMenu,
navTitle,
showSidebar,
toggleShowSidebar,
} = props;
const uiSwitch = useUISwitch();
const { siteData, page } = usePageData();
const { base } = siteData;
const { pathname } = useLocation();
Expand Down Expand Up @@ -122,6 +137,11 @@ export function Nav(props: NavProps) {
}
return styles.relative;
};
// sync opacity with sidebar
const is960 = useMediaQuery('(min-width: 960px)');
const showToggleBtn =
uiSwitch.showSidebar && page.pageType === 'doc' && is960;
const toggleSidebarIcon = showSidebar ? PanelLeftClose : PanelLeftOpen;

return (
<>
Expand All @@ -133,11 +153,23 @@ export function Nav(props: NavProps) {
} ${computeNavPosition()}`}
>
<div
className={`${styles.container} rp-flex rp-justify-between rp-items-center rp-h-full`}
className={`${styles.container} rp-flex rp-justify-between rp-items-center rp-h-full rp-gap-x-4`}
>
{beforeNavTitle}
{navTitle || <NavBarTitle />}
{afterNavTitle}

{showToggleBtn ? (
<SvgWrapper
icon={toggleSidebarIcon}
style={{
width: '18px',
height: '18px',
}}
onClick={toggleShowSidebar}
/>
) : null}

<div className="rp-flex rp-flex-1 rp-justify-end rp-items-center">
{leftNav()}
{rightNav()}
Expand Down
23 changes: 19 additions & 4 deletions packages/theme-default/src/layout/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { useHead } from '@unhead/react';
import { Head } from '@unhead/react';
import type React from 'react';
import { useMemo, useState } from 'react';
import type { NavProps } from '../../components/Nav';
import { useSetup } from '../../logic/sideEffects';
import { useLocaleSiteData } from '../../logic/useLocaleSiteData';
Expand Down Expand Up @@ -129,12 +130,24 @@ export function Layout(props: LayoutProps) {
siteData.description ||
localesData.description;

const [showSidebar, setShowSidebar] = useState(
page.frontmatter.sidebar !== false,
);

const toggleShowSideBar = () => setShowSidebar(prev => !prev);

// Control whether or not to display the navbar, sidebar, outline and footer
// `props.uiSwitch` has higher priority and allows user to override the default value
const uiSwitch = {
...useUISwitch(),
...props.uiSwitch,
};
const defaultUiSwitch = useUISwitch();

const uiSwitch = useMemo(
() => ({
...defaultUiSwitch,
showSidebar: defaultUiSwitch.showSidebar !== false && showSidebar,
...props.uiSwitch,
}),
[defaultUiSwitch, showSidebar, props.uiSwitch],
);

// Use doc layout by default
const getContentLayout = () => {
Expand Down Expand Up @@ -177,6 +190,8 @@ export function Layout(props: LayoutProps) {
navTitle={navTitle}
beforeNav={beforeNav}
afterNavMenu={afterNavMenu}
showSidebar={uiSwitch.showSidebar}
toggleShowSidebar={toggleShowSideBar}
/>
)}

Expand Down
9 changes: 8 additions & 1 deletion packages/theme-default/src/logic/useUISwitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ export function useUISwitch(): UISwitchResult {
// 1. frontmatter.sidebar
// 2. themeConfig.locales.sidebar
// 3. themeConfig.sidebar
const showSidebar =
const calcShowSidebar =
frontmatter?.sidebar !== false && Object.keys(sidebar).length > 0;

const [showSidebar, setShowSidebar] = useState(calcShowSidebar);

useEffect(() => {
setShowSidebar(calcShowSidebar);
}, [calcShowSidebar]);

const { width } = useWindowSize();

const showSidebarMenu =
Expand Down Expand Up @@ -80,6 +86,7 @@ export function useUISwitch(): UISwitchResult {

if (sidebar === QueryStatus.Hide) {
document.documentElement.style.setProperty('--rp-sidebar-width', '0px');
setShowSidebar(false);
}

if (aside === QueryStatus.Hide) {
Expand Down