First Attempt at Intersection Observer

Thanks to Pedro, I learned a cool API this week and applied it in our favorite project telescope.

I was assigned an issue to accomplish the new banner design, which is to make the navbar appear only when the user scroll to the timeline. In other word,…


This content originally appeared on DEV Community and was authored by Yuan-Hsi Lee

Thanks to Pedro, I learned a cool API this week and applied it in our favorite project telescope.

I was assigned an issue to accomplish the new banner design, which is to make the navbar appear only when the user scroll to the timeline. In other word, the navbar should not be showed in the banner, but, it should be showed when the first post is at the top of the screen.

To accomplish this feature, I need to track the element in the current screen. The API that Pedro introduced to me is Intersection Observer. The basic idea of this API is to observe the element in the viewport. The function isIntersecting() tells you if the element that you're observing is still in the viewport. It doesn't have to 100% in the screen or 100% out of the screen, the observed percentage can be configured as well.

Therefore, since our goal is to show the navbar when the user scrolls to the timeline, and the first post is at the top of the screen. In other word, the navbar should be showed when the banner is totally gone. We put the observer to our banner.

In this PR, I move <navbar> into <main>, so that I can separate it from the <Banner>. Moreover, to add props for <Banner> and <navbar> in order to share the result of intersection observer. After moving the position of navbar, I also need to add navbar to about page and search page, because all pages used to share the navbar when it was in the banner header. The other changes that needed to make is the CSS styling of navbar in about page. In about page, <a> uses a certain color from our palettes. Once we move navbar to about page, the navbar will be applied with these color because navbar icons all have <a> in their links. By adding another styling setting to <svg>, the color can be overridden. I'm still thinking if there is a better way to do so, since the original styling for navbar should not be overridden, and the solution should not be overridden to correct.

It was an amazing experience to work with something new. Thanks to Pedro and Dave's help and suggestion in this PR! The initial thought was actually using React context. However, it is too complicated to use, and since we only need to share the state with one component, we can do it without using context. But, I'm definitely gonna check out more practices of React context.


This content originally appeared on DEV Community and was authored by Yuan-Hsi Lee


Print Share Comment Cite Upload Translate Updates
APA

Yuan-Hsi Lee | Sciencx (2021-04-12T01:15:16+00:00) First Attempt at Intersection Observer. Retrieved from https://www.scien.cx/2021/04/12/first-attempt-at-intersection-observer/

MLA
" » First Attempt at Intersection Observer." Yuan-Hsi Lee | Sciencx - Monday April 12, 2021, https://www.scien.cx/2021/04/12/first-attempt-at-intersection-observer/
HARVARD
Yuan-Hsi Lee | Sciencx Monday April 12, 2021 » First Attempt at Intersection Observer., viewed ,<https://www.scien.cx/2021/04/12/first-attempt-at-intersection-observer/>
VANCOUVER
Yuan-Hsi Lee | Sciencx - » First Attempt at Intersection Observer. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/12/first-attempt-at-intersection-observer/
CHICAGO
" » First Attempt at Intersection Observer." Yuan-Hsi Lee | Sciencx - Accessed . https://www.scien.cx/2021/04/12/first-attempt-at-intersection-observer/
IEEE
" » First Attempt at Intersection Observer." Yuan-Hsi Lee | Sciencx [Online]. Available: https://www.scien.cx/2021/04/12/first-attempt-at-intersection-observer/. [Accessed: ]
rf:citation
» First Attempt at Intersection Observer | Yuan-Hsi Lee | Sciencx | https://www.scien.cx/2021/04/12/first-attempt-at-intersection-observer/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.