Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no page reload when pointing to relative path in docs #1559

Merged
merged 3 commits into from
Jun 29, 2022
Merged
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
15 changes: 13 additions & 2 deletions packages/website/modules/docs-theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React, { useEffect } from 'react';
import { MDXProvider } from '@mdx-js/react';

import Head from 'next/head';
import { useRouter } from 'next/router';
import hljs from 'highlight.js/lib/core';
import javascript from 'highlight.js/lib/languages/javascript';
import shell from 'highlight.js/lib/languages/shell';
import go from 'highlight.js/lib/languages/go';
import json from 'highlight.js/lib/languages/json';

import Sidebar from './sidebar/sidebar';
import Feedback from './feedback/feedback';
import Toc from './toc/toc';
Expand All @@ -20,10 +20,21 @@ hljs.registerLanguage('json', json);

export default function Docs(props) {
const { meta, route, ...rest } = props;
const router = useRouter();

useEffect(() => {
hljs.highlightAll();
});

// no reload on local links
const localLinks = document.querySelectorAll('.docs-body a[href^="/docs/"]');
localLinks?.forEach(link => {
link.addEventListener('click', e => {
e.preventDefault();
// @ts-ignore
router.push(e.currentTarget.href);
});
});
}, [router]);

const sharedHead = (
<Head>
Expand Down
27 changes: 13 additions & 14 deletions packages/website/modules/docs-theme/toc/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ if (typeof window !== 'undefined') {
export default function Toc() {
const [isOpen, setOpen] = useState(false);
const [nestedHeadings, setNestedHeadings] = useState([]);
const [updateKey, setUpdateKey] = useState(0);
const activeHeadings = useRef({});
let controller;

Expand All @@ -35,15 +34,14 @@ export default function Toc() {
return str;
};

const updateHeadings = (id) => {
const updateHeadings = id => {
for (const element in activeHeadings.current) {
if (activeHeadings.current[element]) {
activeHeadings.current[element] = false;
}
}
activeHeadings.current[id] = true;
setUpdateKey(key => key + 1);
}
};

const getNestedHeadings = headingElements => {
const nestedHeadings = [];
Expand All @@ -63,17 +61,21 @@ export default function Toc() {
}

// scroll effects / add active class
const exitScene = (e) => {
const exitScene = e => {
if (e.scrollDirection === 'REVERSE' && index !== 0) {
updateHeadings(headingIds[index - 1]);
}
}
};

const scene = new ScrollMagic.Scene({
triggerElement: `#${id}`,
triggerHook: 0.25,
duration: 200
}).on('enter', () => { updateHeadings(id) }).addTo(controller);
duration: 200,
})
.on('enter', () => {
updateHeadings(id);
})
.addTo(controller);
scene.on('leave', exitScene).addTo(controller);
});
return nestedHeadings;
Expand All @@ -83,18 +85,14 @@ export default function Toc() {
<ul>
{headings.map((heading, i) => (
<li key={heading.id}>
<a
href={`#${heading.id}`}
className={clsx(activeHeadings.current[heading.id] ? 'active' : '')}>
<a href={`#${heading.id}`} className={clsx(activeHeadings.current[heading.id] ? 'active' : '')}>
{heading.title}
</a>
{heading.items.length > 0 && (
<ul>
{heading.items.map((child, j) => (
<li key={child.id}>
<a
href={`#${child.id}`}
className={clsx(activeHeadings.current[child.id] ? 'active' : '')}>
<a href={`#${child.id}`} className={clsx(activeHeadings.current[child.id] ? 'active' : '')}>
{child.title}
</a>
</li>
Expand All @@ -107,6 +105,7 @@ export default function Toc() {
);

useEffect(() => {
// eslint-disable-next-line react-hooks/exhaustive-deps
controller = new ScrollMagic.Controller();
const headingElements = Array.from(document.querySelectorAll('.docs-body h2, .docs-body h3'));
const newNestedHeadings = getNestedHeadings(headingElements);
Expand Down