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: Left scroll JS proper selector #534

Merged
merged 9 commits into from
Jan 12, 2022
Merged

Conversation

choldgraf
Copy link
Collaborator

@choldgraf choldgraf commented Dec 31, 2021

This is an attempt at getting the left scrollbar auto-scroll working again.

It simply adds an extra selector, and applies the offset to the parent div, not the nav div, since the parent div is the one that does the actual scrolling

fixes #535

@choldgraf
Copy link
Collaborator Author

cc @jorisvandenbossche does this look good?

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Thanks for taking a look at this!

(will also try to revive my memory about it tomorrow, when looking with a fresh mind)

var active_pages = sidebarNav.querySelectorAll(".active");
if (active_pages.length > 0) {
// Use the last active page as the offset since it's the page we're on
var offset = active_pages[active_pages.length - 1].offsetTop;
Copy link
Member

Choose a reason for hiding this comment

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

It's far in my memory, but the new code here is only taking the offsetTop from the last active item, while in the old code I am summing the OffsetTop values for all active elements. Was this change deliberate? If the original code was correct, this should give a change in behaviour? (didn't directly notice it though)

Copy link
Collaborator Author

@choldgraf choldgraf Jan 10, 2022

Choose a reason for hiding this comment

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

Your memory is correct! I wasn't sure what the benefit was of calculating the offsetTop for all active items - don't we only ever care about the final active item (aka, the lowest one on the page), since that will be the page that we are on? What's the reason to calculate it for all of them?

Copy link
Member

@jorisvandenbossche jorisvandenbossche Jan 11, 2022

Choose a reason for hiding this comment

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

So I think this was done because offsetTop is relative to the parent div (https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetTop). And so because we have a set of nested divs here, if we want to know the actual, total offset of the page link (the final active item) to the top of the sidebar, we need to sum all those offsets (or find a property that gives this total directly).

Exploring this in the console of the browser for one of the sub-sub pages (https://pydata-sphinx-theme--534.org.readthedocs.build/en/534/demo/subpages/subsubpages/subsubpage5.html):

image

Copy link
Member

Choose a reason for hiding this comment

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

(this might also be solvable with some position mangling which might determine which is the offset parent div .., but not directly understanding this)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ahh good point. I wonder if we could use this to be more efficient in the code:

https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

Copy link
Member

Choose a reason for hiding this comment

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

I think the problem is that that property gives the offset to the "viewport", so distance with the browser visible screen edge, while we need the offset to the actual page, so also what is hidden by scrolling down.

I would have thought that there was also some property to directly get that, but I didn't find anything back when doing the original PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When you say "to the actual page", what do you mean? The just need the right information to set scrollTop equal to the right value, right? What do you think about the implementation here: 8ef7370

That calculates the y value of both the sidebar and the latest active item, and scrolls according to the diff

Co-authored-by: Damian Avila <damianavila82@yahoo.com.ar>
@choldgraf
Copy link
Collaborator Author

I think that this one is good to go?

@jorisvandenbossche when we merge this one in, any objection to making a minor version release?

@choldgraf
Copy link
Collaborator Author

OK I clarified the main question @damianavila had in his latest comment, will merge this one in unless there are objections, since this one is fixing a nasty bug!

@choldgraf choldgraf merged commit f6b514c into pydata:master Jan 12, 2022
@choldgraf choldgraf deleted the left-scroll branch January 12, 2022 20:11
@choldgraf
Copy link
Collaborator Author

thanks all for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Left navbar auto-scroll not working
3 participants