Skip to content

Tweak sidebar to update navigation highlight on scroll #48

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

Merged
merged 5 commits into from
Oct 8, 2017

Conversation

jxom
Copy link
Contributor

@jxom jxom commented Oct 7, 2017

This intends to fix #21.

Basically, as the page scrolls or the hash is changed, it will update the 'active' sidebar nav item accordingly.

@facebook-github-bot
Copy link
Collaborator

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@reactjs-bot
Copy link

reactjs-bot commented Oct 7, 2017

Deploy preview ready!

Built with commit 39def4b

https://deploy-preview-48--reactjs.netlify.com

@facebook-github-bot
Copy link
Collaborator

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

const itemIds = _getItemIds(section.items);
this.setState({
itemTopOffsets: _getElementTopOffsetsById(itemIds),
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Offsets can change when the window is resized. This approach would require recomputing offsets after a "resize" event.

{section.items.map(item => (
<li
key={item.id}
class Section extends Component {
Copy link
Contributor

Choose a reason for hiding this comment

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

How do you feel about leaving the Section component as the simple, functional component it was before and creating a new component that decorates it and adds this highlighting behavior? I think this might provide a couple of benefits:

  • Section stays simple and focused on markup.
  • Pages like Tutorial could opt into using this scroll-sync decorator while other pages like Docs can skip it and avoid the unnecessary performance cost.

isActive,
}) => {
const active =
typeof isActive === 'boolean' ? isActive : isItemActive(location, item);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice if we didn't have to do this in all of the createLink* util methods.

What do you think about just making isActive a boolean parameter that's always expected, and:

  • In the case of the scroll-sync decorator component, we could do a smarter check and pass along as a prop to Section.
  • If no such prop is passed then Section can just call the default isItemActive util itself

@jxom
Copy link
Contributor Author

jxom commented Oct 7, 2017

Thanks for the feedback @bvaughn! 🙏 Will make this changes later today!

@jxom
Copy link
Contributor Author

jxom commented Oct 8, 2017

@bvaughn - what do you reckon?

Copy link
Contributor

@bvaughn bvaughn 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! Thanks for the contribution! 😁

@@ -18,9 +18,7 @@ import isItemActive from 'utils/isItemActive';
import slugify from 'utils/slugify';
import {colors, media} from 'theme';
Copy link
Contributor

Choose a reason for hiding this comment

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

The isItemActive import can be removed now that it's no longer being used.

@bvaughn bvaughn merged commit be1f9a6 into reactjs:master Oct 8, 2017
@jxom jxom deleted the update-sidebar-highlight-on-scroll branch October 8, 2017 22:31
jhonmike pushed a commit to jhonmike/reactjs.org that referenced this pull request Jul 1, 2020
* ReactDOM page translation

* Fixing translation

Co-Authored-By: michellocana <[email protected]>

* Fixing hydrate section translation

Co-Authored-By: michellocana <[email protected]>

* Fixing browser support section translation

* Component -> Componente
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tutorial navigation highlight should update as you scroll
4 participants