Skip to content

Commit

Permalink
feat: support to hide sidebar via page frontmatter (#1504)
Browse files Browse the repository at this point in the history
* feat: add type

* feat: control the display of the sidebar menu feature

* docs: update default-theme docs

* Update src/client/theme-default/layouts/DocLayout/index.tsx

* chore: rename

* feat: themeConfig.sidebar support Boolean

* Revert "feat: themeConfig.sidebar support Boolean"

This reverts commit 7ae61a2.
  • Loading branch information
Wxh16144 authored Feb 24, 2023
1 parent 2d39c02 commit 09db565
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
8 changes: 8 additions & 0 deletions docs/theme/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,12 @@ hero:
link: /getting-started
```

### sidebar

- 类型:`Boolean`
- 默认值:`true`
- 详细:

控制侧边栏菜单的显示或隐藏。

<!-- md config end -->
5 changes: 5 additions & 0 deletions src/client/theme-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ export interface IRouteMeta {
atomId?: string;
filename?: string;
debug?: boolean;
/**
* Control the display of the sidebar menu.
* @default true
*/
sidebar?: boolean;
[key: string]: any;
};
// route toc
Expand Down
14 changes: 8 additions & 6 deletions src/client/theme-default/layouts/DocLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ const DocLayout: FC = () => {
const sidebar = useSidebarData();
const { hash } = useLocation();
const { loading } = useSiteData();
const [showSidebar, setShowSidebar] = useState(false);
const [activateSidebar, updateActivateSidebar] = useState(false);
const { frontmatter: fm } = useRouteMeta();

const showSidebar = fm.sidebar !== false && sidebar?.length > 0;

// handle hash change or visit page hash after async chunk loaded
useEffect(() => {
const id = hash.replace('#', '');
Expand All @@ -49,8 +51,8 @@ const DocLayout: FC = () => {
return (
<div
className="dumi-default-doc-layout"
data-mobile-sidebar-active={showSidebar || undefined}
onClick={() => setShowSidebar(false)}
data-mobile-sidebar-active={activateSidebar || undefined}
onClick={() => updateActivateSidebar(false)}
>
<Helmet>
<html lang={intl.locale.replace(/-.+$/, '')} />
Expand All @@ -70,14 +72,14 @@ const DocLayout: FC = () => {
<Header />
<Hero />
<Features />
{sidebar && (
{showSidebar && (
<div className="dumi-default-doc-layout-mobile-bar">
<button
type="button"
className="dumi-default-sidebar-btn"
onClick={(ev) => {
ev.stopPropagation();
setShowSidebar((v) => !v);
updateActivateSidebar((v) => !v);
}}
>
<IconSidebar />
Expand All @@ -86,7 +88,7 @@ const DocLayout: FC = () => {
</div>
)}
<main>
<Sidebar />
{showSidebar && <Sidebar />}
<Content>
{outlet}
<Footer />
Expand Down

0 comments on commit 09db565

Please sign in to comment.