feat: Custom Menu Structure (#1605)
This commit is contained in:
1 parent
ba5e1497bf
commit
f8bbee22b7
25 files changed
+202
-75
No files matched your search
@@ -15,12 +15,13 @@ export const Layout = ({ children }) => {
|
||||
const nav = useRef();
|
||||
|
||||
const handleSidebarToggle = () => {
|
||||
setSidebarOpen(s => !s);
|
||||
setSidebarOpen((s) => !s);
|
||||
};
|
||||
|
||||
return (
|
||||
<BaseLayout sx={{ '& > div': { flex: '1 1 auto' } }} data-testid="layout">
|
||||
<Main
|
||||
id="main-layout"
|
||||
sx={styles.main}
|
||||
className={classnames({
|
||||
'overflow-hidden h-screen lg:overflow-auto': sidebarOpen,
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/** @jsx jsx */
|
||||
import { jsx } from 'theme-ui';
|
||||
import React from 'react';
|
||||
import { useCurrentDoc } from 'docz';
|
||||
|
||||
import * as styles from 'gatsby-theme-docz/src/components/NavGroup/styles';
|
||||
import { NavLink } from 'gatsby-theme-docz/src/components/NavLink';
|
||||
import { NavGroup as DoczNavGroup } from 'gatsby-theme-docz/src/components/NavGroup';
|
||||
import { ChevronDown } from 'gatsby-theme-docz/src/components/Icons';
|
||||
|
||||
export const NavGroup = ({ item, sidebarRef }) => {
|
||||
const currentDoc = useCurrentDoc();
|
||||
const currentDocRef = React.useRef();
|
||||
const { name, menu } = item;
|
||||
const [subheadingsVisible, setShowsubheadings] = React.useState(
|
||||
currentDoc.menu === name
|
||||
);
|
||||
const toggleSubheadings = () => setShowsubheadings(!subheadingsVisible);
|
||||
React.useEffect(() => {
|
||||
if (sidebarRef.current && currentDocRef.current) {
|
||||
sidebarRef.current.scrollTo(0, currentDocRef.current.offsetTop);
|
||||
}
|
||||
}, [sidebarRef]);
|
||||
return (
|
||||
<div sx={styles.wrapper} data-testid="nav-group">
|
||||
<div sx={styles.title} onClick={toggleSubheadings}>
|
||||
{item.name}
|
||||
<ChevronDown sx={styles.chevron({ active: subheadingsVisible })} />
|
||||
</div>
|
||||
<div sx={styles.sublinkWrapper} data-testid="nav-group-links">
|
||||
{menu &&
|
||||
subheadingsVisible &&
|
||||
menu.map((menu) => {
|
||||
if (currentDoc.route === menu.route) {
|
||||
return (
|
||||
<NavLink key={menu.id} item={menu} ref={currentDocRef}>
|
||||
{menu.name}
|
||||
</NavLink>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<NavLink key={menu.id} item={menu}>
|
||||
{menu.name}
|
||||
</NavLink>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -15,7 +15,7 @@ export const Sidebar = React.forwardRef((props, ref) => {
|
||||
const menus = useMenus({ query });
|
||||
const currentDoc = useCurrentDoc();
|
||||
const currentDocRef = useRef();
|
||||
const handleChange = ev => {
|
||||
const handleChange = (ev) => {
|
||||
setQuery(ev.target.value);
|
||||
};
|
||||
useEffect(() => {
|
||||
@@ -24,6 +24,47 @@ export const Sidebar = React.forwardRef((props, ref) => {
|
||||
}
|
||||
}, [ref]);
|
||||
|
||||
const customMenus = {
|
||||
Components: [],
|
||||
System: [],
|
||||
Examples: [],
|
||||
};
|
||||
|
||||
const MENU_CATEGORIES = {
|
||||
Components: [
|
||||
'General',
|
||||
'Form',
|
||||
'Navigation',
|
||||
'Feedback',
|
||||
'Data Display',
|
||||
'Other',
|
||||
],
|
||||
Examples: ['Views'],
|
||||
System: ['Colors'],
|
||||
};
|
||||
|
||||
const renderMenuCategories = () => {
|
||||
return Object.keys(customMenus).map((menuName) => {
|
||||
return (
|
||||
<div key={menuName}>
|
||||
<h2 className="pl-2 border-l-8 -ml-4 border-secondary-active">
|
||||
{menuName}
|
||||
</h2>
|
||||
{customMenus[menuName].map((item) => item)}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const getMenuCategory = (menuName) => {
|
||||
return Object.keys(MENU_CATEGORIES).find((category) => {
|
||||
if (MENU_CATEGORIES[category].includes(menuName)) {
|
||||
return category;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{props.sidebarOpen && (
|
||||
@@ -56,11 +97,26 @@ export const Sidebar = React.forwardRef((props, ref) => {
|
||||
onChange={handleChange}
|
||||
/>
|
||||
{menus &&
|
||||
menus.map(menu => {
|
||||
if (!menu.route)
|
||||
menus.map((menu) => {
|
||||
const isGroup = !!menu.menu;
|
||||
const Component = isGroup ? NavGroup : NavLink;
|
||||
const menuCategory = getMenuCategory(menu.name) || null;
|
||||
|
||||
if (menuCategory) {
|
||||
customMenus[menuCategory].push(
|
||||
<Component key={menu.id} item={menu} sidebarRef={ref}>
|
||||
{!isGroup && menu.name}
|
||||
</Component>
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!menu.route) {
|
||||
return (
|
||||
<NavGroup key={menu.id} item={menu} sidebarRef={ref} />
|
||||
);
|
||||
}
|
||||
|
||||
if (menu.route === currentDoc.route) {
|
||||
return (
|
||||
<NavLink key={menu.id} item={menu} ref={currentDocRef}>
|
||||
@@ -74,6 +130,7 @@ export const Sidebar = React.forwardRef((props, ref) => {
|
||||
</NavLink>
|
||||
);
|
||||
})}
|
||||
{renderMenuCategories()}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user