Skip to content

Commit

Permalink
fix(v2): unify anchor behavior (#2162)
Browse files Browse the repository at this point in the history
* fix(v2): unify anchor behavior

* fix(v2): use activeElement

* fix(v2): add support for sticky header

* Fix highlighted anchor if non-hidable navbar enabled
  • Loading branch information
lex111 authored Jan 23, 2020
1 parent c0c0ad6 commit 1555394
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
56 changes: 37 additions & 19 deletions packages/docusaurus-theme-classic/src/theme/Heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,45 @@
/* eslint-disable jsx-a11y/anchor-has-content, jsx-a11y/anchor-is-valid */

import React from 'react';
import classnames from 'classnames';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

import './styles.css';
import styles from './styles.module.css';

const Heading = Tag => ({id, ...props}) => {
if (!id) {
return <Tag {...props} />;
}
return (
<Tag {...props}>
<a aria-hidden="true" tabIndex="-1" className="anchor" id={id} />
<a
aria-hidden="true"
tabIndex="-1"
className="hash-link"
href={`#${id}`}
title="Direct link to heading">
#
</a>
{props.children}
</Tag>
);
};
const Heading = Tag =>
function TargetComponent({id, ...props}) {
const {
siteConfig: {
themeConfig: {navbar: {hideOnScroll = false} = {}} = {},
} = {},
} = useDocusaurusContext();

if (!id) {
return <Tag {...props} />;
}

return (
<Tag {...props}>
<a
aria-hidden="true"
tabIndex="-1"
className={classnames('anchor', {
[styles.enhancedAnchor]: !hideOnScroll,
})}
id={id}
/>
<a
aria-hidden="true"
tabIndex="-1"
className="hash-link"
href={`#${id}`}
title="Direct link to heading">
#
</a>
{props.children}
</Tag>
);
};

export default Heading;
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@
.anchor {
display: block;
position: relative;
top: -5rem;
}

@media only screen and (max-width: 735px) {
.anchor {
top: -10rem;
}
top: -0.5rem;
outline: none;
}

.hash-link {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.enhancedAnchor:target:before {
content: '';
display: block;
height: var(--ifm-navbar-height);
margin: calc(var(--ifm-navbar-height) * -1) 0 0;
visibility: hidden;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ const useHideableNavbar = hideOnScroll => {

const handleScroll = () => {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const documentHeight = document.documentElement.scrollHeight - navbarHeight;
const windowHeight = window.innerHeight;

if (scrollTop < navbarHeight) {
return;
}

const focusedElement = document.activeElement;

if (focusedElement && /^#/.test(window.location.hash)) {
setIsNavbarVisible(false);
focusedElement.blur();
return;
}

const documentHeight = document.documentElement.scrollHeight - navbarHeight;
const windowHeight = window.innerHeight;

if (lastScrollTop && scrollTop > lastScrollTop) {
setIsNavbarVisible(false);
} else if (scrollTop + windowHeight < documentHeight) {
Expand Down

0 comments on commit 1555394

Please sign in to comment.