-
Notifications
You must be signed in to change notification settings - Fork 26
Overriding Docusaurus themes
Jerry Lin edited this page May 3, 2021
·
1 revision
To update our custom components when Docusaurus is updated:
- First delete
CodeBlock/,Layout/, andNavbarfromsrc/theme/
Adding Mac OS terminal circles to code blocks:
- Swizzle
CodeBlockwithyarn swizzle @docusaurus/theme-classic CodeBlock - Copy paste
MacOSCircle.jsxinto the newly re-createdsrc/theme/CodeBlock - Import
./MacOSCircle.jsxinsrc/theme/CodeBlock/index.js - Inside the
export defaultcode block component, add the following:
let useMacOsCircles = false;
if (metastring && metastring.includes('terminal=true')) {
useMacOsCircles = true
}- In the code block return, find the
divwhoseclassName={styles.codeBlockLines}and add at the top of thediv:
{useMacOsCircles && (
<div style={{
display: 'flex'
}}>
<MacOSCircle color='#ff5f56' margin={false} />
<MacOSCircle color='#ffbd2e' margin={true}/>
<MacOSCircle color='#27c93f' margin={true}/>
</div>
)}Adding the scroll notifier to the navbar:
- Swizzle
Layoutwithyarn swizzle @docusaurus/theme-classic Layout - Swizzle
Navbarwithyarn swizzle @docusaurus/theme-classic Navbar
In src/theme/Navbar/index.js:
- Add
import ScrollNotifier from ../ScrollNotifier - Change
function Navbar()tofunction Navbar(props) - In
function Navbar(props), add:
let useScrollNotifier = false
try {
useScrollNotifier = window.location.pathname != siteConfig.baseUrl
} catch (e) {}- In the return statement of
function Navbar(props), wrap the entire thing with<> </>, and under the</nav>closing tag, add<ScrollNotifier useScrollNotifier={useScrollNotifier} scrollPercent={props.scrollPercent} />
In src/theme/Layout/index.js:
- Add
import { useScrollPercentage } from 'react-scroll-percentage' - In
function Layout(props), add:
try {
effectiveDocumentHeight = document.body.scrollHeight - window.innerHeight
scrollPercent = Math.min(1, window.scrollY / effectiveDocumentHeight)
} catch (e) {}
const [scrollRef,] = useScrollPercentage() // purpose of this hook is to refresh data on scroll- Change
<Navbar />to<Navbar scrollPercent={scrollPercent} />