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: refactor TOC highlighting + handle edge cases #5361

Merged
merged 1 commit into from Aug 14, 2021

Conversation

slorber
Copy link
Collaborator

@slorber slorber commented Aug 14, 2021

Motivation

The TOC highlighting hook was quite messy and hard to understand

This is a total refactor with logic that is easier to reason about, and also solving some edge cases

Fix #5318 where the first anchor can highlighted even if it's way below the viewport.

Fix highlighting bugs due to not ignoring h4/h5/h6 headings

Fix unnecessary usage of state, leading to useless React re-renders on scroll

Have you read the Contributing Guidelines on pull requests?

yes

Test Plan

preview

@slorber slorber added the pr: bug fix This PR fixes a bug in a past release. label Aug 14, 2021
@slorber slorber requested a review from lex111 as a code owner August 14, 2021 16:08
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Aug 14, 2021
@netlify
Copy link

netlify bot commented Aug 14, 2021

✔️ [V2]

🔨 Explore the source changes: 203652a

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/6117ea95fea15f0007acc3d3

😎 Browse the preview: https://deploy-preview-5361--docusaurus-2.netlify.app

@github-actions
Copy link

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 69
🟢 Accessibility 98
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-5361--docusaurus-2.netlify.app/

@github-actions
Copy link

Size Change: +308 B (0%)

Total Size: 803 kB

Filename Size Change
website/build/blog/2017/12/14/introducing-docusaurus/index.html 65.3 kB +66 B (0%)
website/build/docs/index.html 43.1 kB +154 B (0%)
website/build/docs/installation/index.html 50.7 kB +88 B (0%)
ℹ️ View Unchanged
Filename Size
website/.docusaurus/globalData.json 36.2 kB
website/build/assets/css/styles.********.css 93.9 kB
website/build/assets/js/main.********.js 405 kB
website/build/blog/index.html 34.2 kB
website/build/index.html 29.8 kB
website/build/tests/docs/index.html 23.6 kB
website/build/tests/docs/standalone/index.html 21.6 kB

compressed-size-action

@slorber slorber merged commit b8841de into master Aug 14, 2021
@slorber slorber deleted the slorber/anchor-highlighting-issues branch August 14, 2021 16:53
}

function getAnchors() {
// For toc highlighting, we only consider h2/h3 anchors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, we'll make headings levels displayed in TOC customizable, so maybe now we can avoid hard-coded h2/h3 only?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too late :D

Agree that this should be configurable, but let's see how to add this in another PR like #4310

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think anchors created in TOC should have an extra class like anchor__toc or something, because otherwise we'd still get weird behaviors if user render headings with JSX/React and they don't appear in the TOC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TOC item should not be highlighted if below the viewport
3 participants