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

feat(theme-classic): reintroduce autoscrolling TOC #6666

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

mjvbz
Copy link

@mjvbz mjvbz commented Feb 13, 2022

#6317 Reimagined. Reverts #6748

#6317 breaks pages using scroll-behavior: smooth as both JS and CSS are trying to smoothly scroll the page. For sites like about.supabase.com which uses scroll-behavior: smooth (reasonable thing to add IMO), clicking on the TOCs doesn't bring you to the right section of the page. ( the issue on #6620 )

This is Pull Request in order to fix this issue and bring the website a better user experience.

This PR:

  • Delays the auto-scroll of TOC after the page has finished scrolling, to prevent concurrent scrolls
  • Makes the scroll only happen if a vertical scroll can bring the link into view
  • Adds an autoScrollTOC config option
  • Adds scroll-behavior: smooth to the website, both for dogfooding and better UX

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Feb 13, 2022
@Josh-Cena Josh-Cena changed the title Fix Smooth Scrolling fix: allow auto-scrolling TOC with smooth scrolling Feb 13, 2022
@Josh-Cena Josh-Cena added the pr: bug fix This PR fixes a bug in a past release. label Feb 13, 2022
@netlify
Copy link

netlify bot commented Feb 13, 2022

[V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit d7ece94
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/6242d9f9c4e8dd00084f65f4
😎 Deploy Preview https://deploy-preview-6666--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link

github-actions bot commented Feb 13, 2022

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 50
🟢 Accessibility 100
🟠 Best practices 83
🟢 SEO 100
🟠 PWA 80

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

@Josh-Cena Josh-Cena requested a review from yangshun February 13, 2022 08:41
@Josh-Cena
Copy link
Collaborator

@yangshun I think this fixes it. You can test here: https://deploy-preview-6666--docusaurus-2.netlify.app/tests/docs/more-test/ or on any other page

@Josh-Cena Josh-Cena changed the title fix: allow auto-scrolling TOC with smooth scrolling fix: various auto-scrolling TOC fixes Feb 13, 2022
@@ -46,6 +46,7 @@ const DEFAULT_CONFIG = {
minHeadingLevel: 2,
maxHeadingLevel: 3,
},
autoScrollTOC: true,
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can keep this undocumented for now, probably not useful for most people, as their sites don't work because of broken CSS in the first place...

@Josh-Cena Josh-Cena mentioned this pull request Feb 13, 2022
7 tasks
Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

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

Looks great!

@Josh-Cena
Copy link
Collaborator

Oops, not entirely good. The back-to-top button doesn't work

@Josh-Cena
Copy link
Collaborator

Ah, it's caused by our smooth scrolling polyfill, which supabase gets around by removing the BTT button altogether.

@slorber What do you think about here? Should we use scroll-behavior: smooth for our own website? At least I don't think the BTT button should break when the user has scroll-behavior: smooth in userland.

@mjvbz
Copy link
Author

mjvbz commented Feb 13, 2022

Looks Great on the new changes!

@Josh-Cena
Copy link
Collaborator

Okay, BTT button fixed with some hacks...

@mjvbz
Copy link
Author

mjvbz commented Feb 14, 2022

Okay, BTT button fixed with some hacks...

Great! Everything is working fine and the documents have no bugs.

@duanwilliam
Copy link
Contributor

smooth scrolling is still sometimes interrupted on lower performance machines (dropped frames?). using a higher value on the timeout e.g. 100, 250 doesn't seem to visibly affect the perceived display of the active toc item (at least in my opinion), while significantly lowering the chance of interruption on slower machines

@Josh-Cena
Copy link
Collaborator

@duanwilliam Good point. Turned the timeout up to 200, and the behavior is fine.

@Josh-Cena Josh-Cena changed the title fix: various auto-scrolling TOC fixes fix: various auto-scrolling TOC fixes; better support for smooth scroll-behavior Feb 15, 2022
@mjvbz
Copy link
Author

mjvbz commented Feb 16, 2022

Is it sure that this will be working on all browsers and systems, Does any system or browser will find bugs? @Josh-Cena

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

this looks to me quite complex, hard to review/test and with a lot of moving parts for such a tiny toc highlighting feature 🤷‍♂️

I'd rather plainly disable this feature (or make it opt-in, default to false) until we can find a good implementation for it, as implementing and reviewing this properly prevents us to focus on 2.0 which is way more important.


This new implementation breaks TOC navigation.

This is what I get when clicking on "Crowdin Overview" in the TOC while at the bottom of this page: https://deploy-preview-6666--docusaurus-2.netlify.app/docs/i18n/crowdin/#example-configuration

The clicked TOC heading now appears under the navbar and is not visible anymore

image

@@ -37,18 +37,29 @@ function smoothScrollTopNative(): CancelScrollTop {

function smoothScrollTopPolyfill(): CancelScrollTop {
let raf: number | null = null;
// Coercing to auto scroll to prevent conflicts with scroll-behavior.
// This is a polyfill for browsers without smooth scrolling anyways.
document.documentElement.style.scrollBehavior = 'auto';
Copy link
Collaborator

Choose a reason for hiding this comment

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

if a previous value is already set, will it be properly restored?

What's the impact of not restoring it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

There should be no other case where a style is inlined in the DOM. I don't believe any userland code would do that instead of using :root selector. We need to force using scroll-behavior: auto, because otherwise CSS and JS would scroll the page simultaneously and cancel each other, as is the entire purpose of sending this PR.

window.scrollTo(0, 0);
document.documentElement.style.scrollBehavior = '';
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here, restore previous value?

Copy link
Collaborator

Choose a reason for hiding this comment

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

There should be no previous value.

) {
link.scrollIntoView({block: 'nearest', behavior: 'smooth'});
}
}, 200);
Copy link
Collaborator

Choose a reason for hiding this comment

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

magic timeout values like that are a recipe for troubles 😅 I'd like to avoid it at all cost

What about waiting for the end of scroll events using debounce + trailing events?

Copy link
Collaborator

@Josh-Cena Josh-Cena Feb 17, 2022

Choose a reason for hiding this comment

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

Well, even if you are debouncing, you have to debounce within a certain time range, right? This is essentially a debounce.

}
const handle = setTimeout(() => {
const linkRect = link.getBoundingClientRect();
Copy link
Collaborator

Choose a reason for hiding this comment

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

all this code is quite low-level, please extract it under a meaningful function name that convey intent, and allowing us to more easily migrate to the official "if-needed" feature later?

scrollIntoView(node, {
  scrollMode: 'if-needed',
  block: 'nearest', 
  behavior: 'smooth'
})

Maybe using this package? https://github.com/stipsan/scroll-into-view-if-needed

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not if-needed. It's the exact opposite: we don't scroll if we have to do a horizontal scroll. We don't care if we do a moot scroll, but we don't want the scroll to be horizontal.

@Josh-Cena
Copy link
Collaborator

This new implementation breaks TOC navigation.

It has nothing to do with this fix. It's because of the scroll-behavior: smooth CSS. Our implementation assumes that TOC navigation would always hide the navbar, but now, an anchor link navigation can actually scroll up and make the navbar visible. If we revert to scroll-behavior: auto on our website, we won't observe this anymore.

We are mainly just interested in fixing the JS and empowering users to use scroll-behavior: smooth in userland. (And in such case they probably won't use the auto-hiding navbar)

@Josh-Cena
Copy link
Collaborator

Josh-Cena commented Feb 17, 2022

It started as a quick fix for the autoscrolling TOC with scroll-behavior: smooth CSS, but we quickly noticed much more incompatibilities, such as the back-to-top button. The ultimate aim of this PR is to make the user freely enable smooth scroll behavior without doing other work to maintain a good UX.

@slorber
Copy link
Collaborator

slorber commented Feb 17, 2022

Can we split this into separate PRs so that we can understand exactly what is the purpose and impact of each piece of code?

It will help me review it because in current state 🤷‍♂️ Remember I want to focus on 2.0 so I can't spend too much time on this PR

Can we have the most important fix (#6620) merged immediately, and figure it out later for the smooth scroll which is useful but less important?

@mjvbz
Copy link
Author

mjvbz commented Mar 1, 2022

@slorber, @Josh-Cena
Should I resolve the conflicts errors?
packages/docusaurus-theme-common/src/utils/useTOCHighlight.ts

@Josh-Cena Josh-Cena changed the title fix: various auto-scrolling TOC fixes; better support for smooth scroll-behavior feat(theme-classic): reintroduce autoscrolling TOC Mar 29, 2022
@Josh-Cena Josh-Cena added pr: new feature This PR adds a new API or behavior. and removed pr: bug fix This PR fixes a bug in a past release. labels Apr 9, 2022
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: new feature This PR adds a new API or behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants