Skip to content

Commit

Permalink
site: fix toc active menu (ant-design#25978)
Browse files Browse the repository at this point in the history
afc163 authored Aug 3, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 1efbc08 commit 8c7f7b4
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions site/theme/template/Content/MainContent.jsx
Original file line number Diff line number Diff line change
@@ -59,11 +59,11 @@ function getSideBarOpenKeys(nextProps) {
}

function updateActiveToc(id) {
[].forEach.call(document.querySelectorAll('.toc-affix li a'), node => {
node.className = '';
});
const currentNode = document.querySelectorAll(`.toc-affix li a[href="#${id}"]`)[0];
if (currentNode) {
[].forEach.call(document.querySelectorAll('.toc-affix li a'), node => {
node.className = '';
});
currentNode.className = 'current';
}
}
@@ -76,8 +76,7 @@ class MainContent extends Component {
};

componentDidMount() {
this.componentDidUpdate();
window.addEventListener('load', this.handleInitialHashOnLoad);
window.addEventListener('load', this.handleLoad);
window.addEventListener('hashchange', this.handleHashChange);
}

@@ -188,18 +187,11 @@ class MainContent extends Component {
this.setState({ openKeys });
};

handleInitialHashOnLoad = () => {
setTimeout(() => {
if (!window.location.hash) {
return;
}
const element = document.getElementById(
decodeURIComponent(window.location.hash.replace('#', '')),
);
if (element && document.documentElement.scrollTop === 0) {
element.scrollIntoView();
}
}, 0);
handleLoad = () => {
if (window.location.hash) {
updateActiveToc(window.location.hash.replace(/^#/, ''));
}
this.bindScroller();
};

handleHashChange = () => {
@@ -223,7 +215,7 @@ class MainContent extends Component {
this.scroller
.setup({
step: '.markdown > h2, .code-box', // required
offset: 0,
offset: '10px',
})
.onStepEnter(({ element }) => {
updateActiveToc(element.id);

0 comments on commit 8c7f7b4

Please sign in to comment.