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.
Top comments (2)
If you want to learn more about Intersection Observer check out this article and for React Context check out this article.
Some comments may only be visible to logged-in visitors. Sign in to view all comments.