React Scroll Hook with Shadows
Have you ever thought of spicing up your scrollable content with nice-looking shadows? Yes, all browsers have the scroll bar, but we can make our content shine even better and, at the same time, improve usability. I think shadows add a smooth and excellent feel to our content and help by showing the users that “there is more content down here” or “there is more content up here”.
If you’ve had the same thought (I’ll guess so since you’re already reading this) but have not figured out a solution yet, you should read on.
TLDR: You will find the final source code at the bottom of the article
The Scroll Hook
Recently, I made this hook that adds inner shadows when scrolling an element with overflowing content. It is not very complicated and it’s fun to make. We’ll break down the code throughout the post, look into how it works, and finaly use it in our code.https://medium.com/media/154ecf87431923f99747b31da71175d4/href
Handling scroll events
First, let’s go through the roll logic part of the hook. It’s not complicated, and I did not have to create three separate states for each event. But it is done for simplicity instead of holding an object with all the values.https://medium.com/media/360751a2658643353a36a8f419148277/href
element.addEventListener(‘scroll’, (event) => /* do something*/)
The onScrollHandler-method (exported from the hook) we can pass as the value to the onScroll-property of our element. When scrolling the element, the onScroll-property fires and the onScrollHandler-method triggers. Then we can pick the properties scrollTop, scrollHeight and clientHeight from the event target and set them as values for their respective states.
The onScrollHandler-method (exported from the hook) we can pass as the value to the onScroll-property of our element. When scrolling the element, the onScroll-property fires scroll events, and the onScrollHandler-method receives these events. We can then pick the properties scrollTop, scrollHeight and clientHeight from the event target and set them as values for their respective states.
- clientHeight (visible content) is the inner height of an element, including padding. We can calculate it by CSS height + CSS padding. If a horizontal scrollbar is present, the inner height includes the height of the scrollbar.
- scrollHeight (visible + hidden content) is the height of an element’s visible content, including hidden content not visible on the screen for an overflowing element.
- scrollTop is the number of pixels scrolled vertically on the content of an element. Measuring the value takes the distance from the top of an element to the topmost of visible content.
Calculate shadow position
We have to do some calculations to determine if and when we should show shadows on the top, bottom, or both. We want to show shadows concurrently when we’re neither at the top of the scroll nor the bottom.https://medium.com/media/9fcc6ca9c4c03ea3f8bfcfbb8aeb724c/href
- isBottom is true when the inner height of the visible element equals the total height of the visible and hidden content minus (-) pixels scrolled from the top of the element.
- isTop is true when we’ve not scrolled yet, or scrollTop equals 0 pixels.
- isBetween is true when we’ve scrolled more than 0 pixels from the top, and the inner height of the visible element is less (<) than the visible plus (+) hidden content height minus pixels scrolled from the top.
We can show this by example: we could have an element that has 400 pixels inner height [clientHeight], that is scrolled 150 pixels from the top [scrollTop]; we should then get that isBetween is true:https://medium.com/media/b3d88c31a4c5a1d12774709b7da81f28/href
Determine shadow type based on the calculated position
Our getBoxShadow()-function handles the logic for determining and returning the correct CSS box-shadow value as a string. This value we want to set to the target element reactive as we scroll. We’re here using the variables — isBottom, isTop and isBetween —from earlier to determine which shadow to use:https://medium.com/media/dd244c457001664fff144489f9a1b027/href
We want to add shadow to the edge with overflowing content because it makes sense to the eye watching it. So, if we are at the top of the element, we show shadow at the bottom. If we are between (as we calculated in the previous code example), we want to show both top and bottom shadows, or else if we are at the bottom of the element, we want to show the top shadow.
Using the Scroll Hook
Since this is a React hook, we need to create a component that can use it, and it should be straightforward since as there is to do is connect the props to the target element.
In the code block below, we have a component called App. We import the useScrollWithShadow-hook, destruct the onScrollHandler()-method and the boxShadow variable. We pass onScrollHandler() as the value of the div’ onScroll property and set boxShadow as an inline style.https://medium.com/media/d5502fbcba53852feaf3e18921c672a5/href
Without some content that overflows the element, this would not look or work very well. All it is left is to add some content to the div and maybe some static height so we can get an overflow. We could also add CSS transitions for even more smoothening. But from here on, I will leave the rest to your imagination.
Look at the final result for the styled solution with content and source code on Codesandbox.
Here is the final result. Click “toggle shadows” to switch between with and without shadows (source code on Codesandbox):https://medium.com/media/0539ccf83b2c50d23410531b216ea8aa/href
Thanks for reading! (feel free to clap and share if you liked this article)
Code foh shizzle