chore: uiv2 docz custom page (#1511)
* feat: Customized Docz Layout * manually apply padding into playground * fix playground bug -- sticky items * fix window * remove specifc tailwind style for docz
This commit is contained in:
1 parent
dd18de86f8
commit
2049147021
14 files changed
+426
-82
No files matched your search
@@ -0,0 +1,69 @@
|
||||
/** @jsx jsx */
|
||||
import { jsx, Box, Flex, useColorMode } from 'theme-ui';
|
||||
import { useConfig, useCurrentDoc } from 'docz';
|
||||
|
||||
import * as styles from 'gatsby-theme-docz/src/components/Header/styles';
|
||||
import {
|
||||
Edit,
|
||||
Menu,
|
||||
Sun,
|
||||
Github,
|
||||
} from 'gatsby-theme-docz/src/components/Icons';
|
||||
import { Logo } from 'gatsby-theme-docz/src/components/Logo';
|
||||
|
||||
export const Header = props => {
|
||||
const { onOpen } = props;
|
||||
const {
|
||||
repository,
|
||||
themeConfig: { showDarkModeSwitch, showMarkdownEditButton },
|
||||
} = useConfig();
|
||||
const { edit = true, ...doc } = useCurrentDoc();
|
||||
const [colorMode, setColorMode] = useColorMode();
|
||||
|
||||
const toggleColorMode = () => {
|
||||
setColorMode(colorMode === 'light' ? 'dark' : 'light');
|
||||
};
|
||||
|
||||
return (
|
||||
<div sx={styles.wrapper} data-testid="header">
|
||||
<div sx={styles.innerContainer}>
|
||||
<div className="flex">
|
||||
<button className="mr-4" sx={styles.menuButton} onClick={onOpen}>
|
||||
<Menu size={25} />
|
||||
</button>
|
||||
<Logo />
|
||||
</div>
|
||||
<Flex>
|
||||
{repository && (
|
||||
<Box sx={{ mr: 2 }}>
|
||||
<a
|
||||
href={repository}
|
||||
sx={styles.headerButton}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<Github size={15} />
|
||||
</a>
|
||||
</Box>
|
||||
)}
|
||||
{showDarkModeSwitch && (
|
||||
<button sx={styles.headerButton} onClick={toggleColorMode}>
|
||||
<Sun size={15} />
|
||||
</button>
|
||||
)}
|
||||
</Flex>
|
||||
{showMarkdownEditButton && edit && doc.link && (
|
||||
<a
|
||||
sx={styles.editButton}
|
||||
href={doc.link}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<Edit width={14} />
|
||||
<Box sx={{ pl: 2 }}>Edit page</Box>
|
||||
</a>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,50 @@
|
||||
/** @jsx jsx */
|
||||
import { useRef, useState } from 'react';
|
||||
import { jsx, Layout as BaseLayout, Main } from 'theme-ui';
|
||||
import classnames from 'classnames';
|
||||
|
||||
import { Header } from '../Header';
|
||||
import { Sidebar } from '../Sidebar';
|
||||
import * as styles from 'gatsby-theme-docz/src/components/Layout/styles';
|
||||
|
||||
const getSidebarStatus = () =>
|
||||
typeof window !== 'undefined' && window.location.pathname === '/';
|
||||
|
||||
export const Layout = ({ children }) => {
|
||||
const [sidebarOpen, setSidebarOpen] = useState(getSidebarStatus());
|
||||
const nav = useRef();
|
||||
|
||||
const handleSidebarToggle = () => {
|
||||
setSidebarOpen(s => !s);
|
||||
};
|
||||
|
||||
return (
|
||||
<BaseLayout sx={{ '& > div': { flex: '1 1 auto' } }} data-testid="layout">
|
||||
<Main
|
||||
sx={styles.main}
|
||||
className={classnames({
|
||||
'overflow-hidden h-screen lg:overflow-auto': sidebarOpen,
|
||||
})}
|
||||
>
|
||||
<Header onOpen={handleSidebarToggle} />
|
||||
<div className={classnames('flex', {})}>
|
||||
<Sidebar
|
||||
ref={nav}
|
||||
sidebarOpen={sidebarOpen}
|
||||
onFocus={() => setSidebarOpen(true)}
|
||||
onBlur={() => setSidebarOpen(false)}
|
||||
onClick={() => setSidebarOpen(false)}
|
||||
/>
|
||||
<div
|
||||
className={classnames('py-10 w-full', {
|
||||
'px-4 lg:px-20': sidebarOpen,
|
||||
'px-4 lg:px-8': !sidebarOpen,
|
||||
})}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
</Main>
|
||||
</BaseLayout>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,79 @@
|
||||
/** @jsx jsx */
|
||||
/** @jsxFrag React.Fragment */
|
||||
import React, { useState, useRef, useEffect } from 'react';
|
||||
import classnames from 'classnames';
|
||||
|
||||
import { jsx } from 'theme-ui';
|
||||
import { useMenus, useCurrentDoc } from 'docz';
|
||||
|
||||
import { NavSearch } from 'gatsby-theme-docz/src/components/NavSearch';
|
||||
import { NavLink } from 'gatsby-theme-docz/src/components/NavLink';
|
||||
import { NavGroup } from 'gatsby-theme-docz/src/components/NavGroup';
|
||||
|
||||
export const Sidebar = React.forwardRef((props, ref) => {
|
||||
const [query, setQuery] = useState('');
|
||||
const menus = useMenus({ query });
|
||||
const currentDoc = useCurrentDoc();
|
||||
const currentDocRef = useRef();
|
||||
const handleChange = ev => {
|
||||
setQuery(ev.target.value);
|
||||
};
|
||||
useEffect(() => {
|
||||
if (ref.current && currentDocRef.current) {
|
||||
ref.current.scrollTo(0, currentDocRef.current.offsetTop);
|
||||
}
|
||||
}, [ref]);
|
||||
|
||||
return (
|
||||
<>
|
||||
{props.sidebarOpen && (
|
||||
<>
|
||||
<div className="block lg:hidden">
|
||||
<div
|
||||
onClick={() => props.onClick()}
|
||||
className="fixed z-10 left-0 w-full h-full bg-black opacity-80"
|
||||
style={{ top: '81px' }}
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
ref={ref}
|
||||
data-testid="sidebar"
|
||||
className={classnames(
|
||||
'p-8 flex-col bg-white top-0 overflow-auto z-10 border-r border-gray-400 hidden fixed left-0 bottom-0 lg:block lg:sticky lg:h-screen lg:top-0'
|
||||
)}
|
||||
style={{
|
||||
display: props.sidebarOpen ? 'block' : 'none',
|
||||
top: '81px',
|
||||
minWidth: 250,
|
||||
}}
|
||||
>
|
||||
<NavSearch
|
||||
placeholder="Type to search..."
|
||||
value={query}
|
||||
onChange={handleChange}
|
||||
/>
|
||||
{menus &&
|
||||
menus.map(menu => {
|
||||
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}>
|
||||
{menu.name}
|
||||
</NavLink>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<NavLink key={menu.id} item={menu}>
|
||||
{menu.name}
|
||||
</NavLink>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user