Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent scrolling using CSS on React rendered components

Tags:

So I render a component via React within my html like so:

 <html>   <body>     <div id=app>${appHtml}</div>     <script src="/bundle.js"></script>   </body> </html> 

Within my app I have a burger button, that onClick goes full screen.

However, the body is scrollable. I'd normally add a class to the body tag and make it overflow: hidden to prevent this. However, my react component is rendered within the body tag so I have no control over toggling classes based on a click within a react component.

Has anyone got any ideas/advice on how i'd achieve what i'm looking for.

Thanks!

like image 414
pourmesomecode Avatar asked Oct 10 '16 16:10

pourmesomecode


People also ask

How do I stop my CSS from scrolling?

Disabling scroll with only CSS. There's another way to disable scrolling that is commonly used when opening modals or scrollable floating elements. And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.

How do you control scroll in React?

U need to set window. scrollTo(0, 0) when u click on link so it would start on top of page.

How do I stop scrolling when scrolling a div element CSS?

mouseleave(function() { $('body'). bind('mousewheel DOMMouseScroll', function() { return true; }); }); This is stopping all scrolling where as I want scrolling to still be possible inside the container.


2 Answers

"I have no control over toggling classes based on a click within a react component."

Not necessarily true!

It's good that you're thinking in a "React-ful" way and wary about modifying the DOM. The main reason you want to avoid doing DOM manipulation is because it causes conflicts between what React is trying to render and the unknown changes you might be trying to make. But in this case you're not manipulating the DOM that React is rendering, you're manipulating its parent. In that case you would be totally fine doing something like this:

document.body.style.overflow = "hidden" 

Or

document.body.classList.add("no-scroll") 

Or whatever works. You're totally safe because React only renders the DOM within #app and doesn't care about what happens in its parent. In fact many apps and websites use React in only a small part of the page, to render a single component or widget, instead of an entire app.

That aside, there is an even better, more "React-ful" way to do what you want. Simply restructure your app in such a way that the scrolling container is within your React app instead of body. The structure might look something like this:

<html>   <body>     <div id="app">       <div id="scroll-container">         <!-- the rest of your app -->       </div>     </div>   </body> </html> 

Make body overflow hidden, and body and #app fill the entire screen, and you can control whether #scroll-container allows scrolling or not entirely within React.

like image 89
jered Avatar answered Oct 21 '22 06:10

jered


The above doesn't work for iOS mobile.

body-scroll-lock uses a combination of CSS and JS to make it work for all devices, whilst maintaining scrollability of a target element (eg. modal).

ie. for iOS, need to detect when the bottom or top of a target element is reached, and then stop scrolling completely

like image 36
Will Po Avatar answered Oct 21 '22 06:10

Will Po