DEV Community

Create section navigation with React and Intersection Observer

Maciek Grzybek on July 05, 2019

Create section navigation with React and Intersection Observer Introduction For one of the last project at work, I had to c...
Collapse
 
thatfemicode profile image
thatFemicode

Hi Maceik, came across this article and i must say it is really helpful but i am trying to create something like this website olaolu.dev/ where those boxes by the sides are the navigators but i keep getting error, do you by any means have a solution to something like that, i'd be really grateful, tried using static data and i am still getting the errors

Collapse
 
maciekgrzybek profile image
Maciek Grzybek

Hey, I'd need more details, a repo or a code sandbox, it's really hard to help without more information :)

Collapse
 
thatfemicode profile image
thatFemicode

github.com/thatFemicode/Landing
Here is a link to the repo, i am trying to recreate what you did with the intersection observer api but for navigation like the link i sent to you where each section that comes in makes the circles background turn black

Thread Thread
 
thatfemicode profile image
thatFemicode • Edited

I am not sure i am explaining well, instead of looping over several characters, the sections will have their own ref, i keep getting errors, been on it since yesterday night, if you notice from the link i sent earlier olaolu.dev/, there are boxes that keeps track of the section. do you get me now?, i'd be really grateful if you can assist me in making this work, thanks a lot

Thread Thread
 
thatfemicode profile image
thatFemicode

I think i have a better explanation now, yes
so with refernce to that website i sent olaolu.dev/ what i want to achieve with your approach using the IO API is to have a navigator (the boxes or circles) that can be clicked on and goes directly to a section either be it home or gallery and once a particular section is in view, the circle or box now has a style it gives like as shown in the website i sent. Just like yours using static data where you click on the names which serves as navigator and the particular character comes into the viewport then both the navigator and character get the yellow background, I keep getting errors since i am not looping over. i have done somethings on the github repo i sent. I'd be really greatful if you can assist me.

Collapse
 
thatfemicode profile image
thatFemicode

I'd make a repo and share now

Collapse
 
alanominator profile image
Alanominator • Edited

I found bag. When you click navigation button and stop scroll by touching screen, button, that has been clicked, has active class until another button is clicked.

I created own section-navigation. The same idea, but other decisions.

Collapse
 
sebastiangraz profile image
Sebastian Grąz

Hi Maciek, thanks for the write-up! It helped me a lot in getting started with IO + hooks. In my own implementation I found a way to make the rootMargin configuration simpler than using a 1px pixel window height listener. While achieving the same thing.

{
  rootMargin: "-50% 0px -50% 0px",
  threshold: 0,
}
Enter fullscreen mode Exit fullscreen mode

Hope this helps someone else stumbling upon this post.

Collapse
 
maciekgrzybek profile image
Maciek Grzybek

Oh wow, that's way better than my approach :) Awesome stuff, thanks :) BTW - great designs mate :)

Collapse
 
sirluis profile image
Luis Henrique

Unnecessarily complicated, mixing graphql and a lot of non related stuff on an article about Intersection Observer + React. I'm sorry for the newbie people on web development who are reading this and have no idea on what is graphql and gql etc.

Collapse
 
maciekgrzybek profile image
Maciek Grzybek

Well, that's your opinion :)

Collapse
 
maciekgrzybek profile image
Maciek Grzybek

I can explain it to you if you need.

Collapse
 
takhine profile image
Aniketh Nair

I believe this gives beginners the opportunity to upgrade themselves so thanks for that!

Collapse
 
sandeshsapkota profile image
sandeshsapkota • Edited

HI Maciek ! I get so confuesd to implement on my project because of gql things. do you have same kind of more simplified examples ?

Collapse
 
maciekgrzybek profile image
Maciek Grzybek

Hey yeah I can create something. I'll post a link later today OK?

Collapse
 
sandeshsapkota profile image
sandeshsapkota

Okay. thanks.

Thread Thread
 
maciekgrzybek profile image
Maciek Grzybek

Hey, sorry for the delay, I had a busy weekend :) Here's an example without the GraphQL -> github.com/maciekgrzybek/react-sec...

It's using static data in data.json file. Hope that helps :) Let me know if you need anything else.

Thread Thread
 
sandeshsapkota profile image
sandeshsapkota

A big thank you! I really appreciate this.

Collapse
 
maciekgrzybek profile image
Maciek Grzybek

Hi Akmal, thanks for the comment. Good spot, I totally forgot about it :) I've already updated example code and the demo. Not sure what do you mean about that Codesandbox example though. Looks like the code it's spot on. Could you explain? Thanks :)

Collapse
 
eliasbutting profile image
Elias Butting

Thanks! Helped a lot!!

Collapse
 
maciekgrzybek profile image
Maciek Grzybek

Glad it did :)

Collapse
 
emmaccen profile image
Lucius Emmanuel Emmaccen

Apt!