-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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(www): useActiveHash hook for highlighting links in Docs' table of contents #21762
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking great! I'm seeing one issue in particular that keeps me from saying we should merge this, though I think it's going to be a great improvement 🙂
I'm also interested in the performance aspect of this. Locally it takes a moment for the ToC to update and I guess that won't be there in the actual build, but I fear that if it does, the JavaScript will block the main thread and keep you from being able to click on links (at least that's what I'm seeing when I run this on my machine). I like the potential gains from these changes but I want to make sure they don't cause problems too! |
One thing to consider would be the behavior in the mobile view: We might want to disable this behavior on mobile, since it's kind of silly to update the current location for a component that you can't even see from that location. I do think that there can be improvements in how the table of contents is displayed on mobile (e.g. a menu you can click to expand it) but that's a story for another issue and another PR. |
Made the issue for mobile TOC: #21807. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some minor nits left, otherwise looks good to me.
Just a note: But: |
@muesch The intention is to change the implementation of the tutorial sidebar to use the |
I pulled it locally and it looks good to me, I'll leave it up to @tesseralis's last feedback. Some notes (that aren't blocking) are:
Thanks for the work on this @jlkiri! |
@gillkyle Yes, the reason is that text is not split in sections with their own ids, so there is not anything to tell Intersection Observer to watch |
Hey again @jlkiri! Talked over this with @tesseralis a little more. I think it's really close. To break down the last little pieces: Intersection Observer on content instead of headings - this does seem like a tricky thing to get just right, and it's only a noticeable problem on really big sections of content when you're scrolling vertically (which isn't how most things, if any, are read anyway). So I think that behavior is fine for now ✅ (though the Apollo docs which are a Gatsby site have this implemented) Subheader highlights - I don't think it's suboptimal to have just the child header highlighted, Docusaurus does it that way and it seems okay (https://v2.docusaurus.io/docs/introduction) would that be a hard adjustment? Seems like it might be updating Testing - Probably should have mentioned this sooner but is there something we can add to make sure we don't get regressions with this later as code around it evolves? With the pages that broke as it was being tested I hope it won't encounter other edge cases in |
Subheader highlights - OK. I made it a default in 5f051fd. Now subheaders are also highlighted. Testing - Tbh I don't know how this can be tested meaningfully. I think we could have a unit test for Cloud Tests seem to be failing but I do not know why... |
Sorry it's been hard for me to keep getting back to you on this @jlkiri. Things are busy and we've got a lot we're trying to do all at once! I talked to @marcysutton about testing and these are some things she suggested:
Here are a couple places in code that could help serve as a reference. You can ignore the Cloud tests, this code won't touch that and we can sort those out internally. 🙂 |
…s always highlighted in ToC
Thanks @jlkiri! I noticed one last issue that I updated in c2ed59a, some pages that have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all the hard work on this! (turned out having a lot more to it than we thought 😅) but it's a nice addition to the docs 🎉
Description
Introduces a
useActiveHash
hook which can be used to detect which section of a page one is currently looking at and update URL hash accordingly. Now Table of Contents in Docs highlights links that are currently active (= link's hash is equal to URL hash).Detection is done with
IntersectionObserver
. Not quite sure about which portion of viewport to use to detect intersections. Also I think that refactoring Tutorial to use this hook should be done in other PR.Here is a demo GIF:
Related Issues
Addresses #21640