-
Notifications
You must be signed in to change notification settings - Fork 194
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
152 rhs toc #167
152 rhs toc #167
Conversation
✅ Deploy Preview for boring-fermat-22cb64 ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
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.
hey @olayway please see following comments for this PR.
-
some observations:
- a left and right padding has been added to the homepage layout
- there is a
Sidebar.js
component not being used - a
package.json
outside the site folder
-
suggestions:
- hooks like
getObserver
would be best in maybelib
or ahooks
folder
- hooks like
I feel there are a few moving parts to the code and was thinking maybe it's best if we keep everything in a single Toc file with the getObserver stuff so that it can also be reused as a drop in component since we have both left and right. eg. <TocComponent right .... />
Overall I like your approach 👍, I can work further on this if you dont mind.
@khalilcodes, thank you for your review! I'd like to continue working on this feature 😉 Let me check your suggestions and update the code accordingly. I'll take care of it probably tomorrow. As for these two comments of yours:
Right... I have created a basic left-hand side component at the beginning to test how the layout will work when we have both LHS and RHS ToCs. But it shouldn't go into this PR!
Again, my bad. Thanks for spotting that :) |
Hi @khalilcodes! Once again, thank you for your review 👏 🎊 Here is the full response to your comments.
Hmm... I haven't touched the
I've removed it. Thank you for your vigilance! 👏
Removed. Thank you! 👏
I totally agree. There was too much going on in the As for creating a separate component for this: I tried this approach in the first go when I was trying to extract headings from the markdown and create a
You're right about that this approach prevents us from having a re-usable ToC component, but I don't think it is necessary. I don't think we should show site contents both on the RHS and in the LHS sidebar at the same time. I think on the LHS sidebar we should display different pages of the site, but not necessarily contents of each. However there is a situation when we could have the ToC on the LHS -> for smaller screens (atm the ToC only visible for large screens). In this case, we could move it above the content of the Sidebar. Here is the example of this behavior: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API. I think it is pretty nice and it would only require us to work on CSS a bit. I'd love to hear your opinion on that! Additional changes: |
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.
@olayway really thanking you for this detailed explanation. 🙏
I've moved the logic related to keeping track of the current active heading to the custom hook getHeadingsObserver and placed it in the new site/hooks folder.
Yes agree it looks much better now 👍
I've found a rehype plugin rehype-toc that generates the ToC. ...
I don't see any drawbacks for using this plugin. On the contrary, it makes things easier rather than creating a toc logic from scratch and I too would prefer to use this as well. +1 for adding that 👏
Re the component I was actually thinking about doing something along these lines:
export default function TableOfContents () {
// ... states here
useEffect(() => {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
// not sure if we use entry.intersectionRatio > 0 we can store in state ?
// right now when we go backwards it has to reach the heading to trigger active
// .. something like netlfy docs
if (entry.isIntersecting) {
setActiveId(entry.target.id);
}
});
// ...rest
}
// ...rest for active class, etc.
const tocLinks = document.querySelectorAll(".toc-link")
tocLinks.forEach(item => {
const id = item.href.split("#").pop()
observer.observe(document.getElementById(id)
})
return () => {
tocLinks.forEach(item => {
const id = item.href.split("#").pop()
observer.unobserve(document.getElementById(id)
})
// ...rest
}
},[])
return
}
The pros about above is that it eliminates the need for having the Heading.js
component where we do createElement
and no modifications in MDXContent.js
to the heading. Also, the observer would not be required in <MDXContent observer={observer} ... />
.
But I do not know how the performance for the above would stack up compared to your approach, so I think let's just go with yours. 👍
You're right about that this approach prevents us from having a re-usable ToC component, but I don't think it is necessary.
I think so too now. It's best the Sidebar and Toc are separated.
However there is a situation when we could have the ToC on the LHS -> for smaller screens
Yes I agree this would be great a feature to add. Maybe if it's too much for now we can have it in the pipeline.
Concluding, there are a few merge conflicts now since I updated the repo for the SEO issue 😅. I guess that would be all and we can merge this.
EDIT: personally I feel the border looked nice as a separator but that's minor.
Thanks a lot for this !
Hi @khalilcodes
I really like this idea! But I couldn't make it work as expected 😞 I'll give it another try again sometime in the future but for now I've moved using the observer hook to the
Cool! I'll implement it while working on the LHS ToC (Sidebar).
Fixed!
I think let's leave it as-is for now. We can test some options in the future and leave the best one. Additional notes:
|
site/pages/[...slug].js
Outdated
const frontMatter = { | ||
...meta, | ||
date: meta.date === "Invalid Date" ? null : meta.date, | ||
created: meta.created === "Invalid Date" ? null : meta.created |
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.
I have moved the dates logic to contentlayer config, so these can be safely removed now.
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.
Removed.
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.
Hi @olayway thanks for trying out the component and with the changes.
on Additional notes
- I've removed dynamic imports for Anchor and Paragraph ...
This was only done for code-splitting for page performance as recommended in lighthouse. In my testing adding dynamic to these had an impact, whereas when I tried this with the Tooltip
component there wasn't any difference. Also if we look at the build times (after npm run build
) there is a major difference there too. Do have a look at the deploy metrics in #164. But let's test again, and if it affects I will change them back to dynamic.
- There are a lot of changes in the
package.json
...
You are right, I am using npm v6. I'll update it to v8 and hopefully that fixes it.
Other than the small change above, everything looks good to go and we can merge this. 👍
I see... I've added the dynamic imports back for now but I have enabled back ssr rendering for I suspect the metrics improvement had to do with the |
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.
@olayway yes, good idea to keep performance testing as a separate issue. Merging this for now. Thanks a lot for the great work !!
Closes #152
Changes
lg
Also:
Preview