-
Notifications
You must be signed in to change notification settings - Fork 337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does not work in gatsby projects #154
Comments
@soosap can you please provide more information on this issue? |
Basically this isn't working on gatsby => |
same issue here for my gatsby project... |
Someone made it work on gatsby? |
@AidanRRR Can you try replacing all instances of |
It works, thanks! |
Confirmed this fixes my issue in a gatsby project as well, I have to hide overflow in the root in |
@mdhornet90 by root do you mean the Are you sure you replaced the |
Yes, I'm referring to the
Does the trick for my specific situation (overlay that prevents scrolling of everything except the content of the overlay) |
Well, perhaps we could restrict overflow of both |
I had to do this: // for disabling
React.useEffect(() => {
document.getElementsByTagName("html")[0].style.overflow = "hidden";
disableBodyScroll(document.body);
}, []);
// for enabling
React.useEffect(() => {
enableBodyScroll(document.body);
document.getElementsByTagName("html")[0].style = "";
}, []); |
I am not using gatsby, though I have noticed the same issues when doing SSR where When calling I imagine this issue is the same for gatsby, where scroll is disabled at build time, and enabled on the client. I propose: instead of storing previous values in a variable, store in a |
@WickyNilliams why is @lizy0329 A solution for this might be to change |
@diachedelic excuse the delay, forgot to respond! It's called in server because I may want to show a modal pre-opened on page load. I'm sure there are plenty of other use-cases besides. In any case, whether calling at build-time (SSG) and server-side (SSR) the problem is the same - the assumption that enabling and disabling scrolling will always happen in the same environment Hopefully I am making sense here :) |
@WickyNilliams If I were you I would avoid locking the scroll in anything but a browser environment, as it just does not make sense to do so on the server. Is a way for you to determine which environment you are in? |
@diachedelic shall i create a separate issue to discuss? I feel I am distracting from the original issue here (i misread originally, which is why i commented here, but this issue is not related to my question)... I think there are valid reasons to call on the server, and from a dev perspective the less you have to special-case the server environment, the better. I have a few ideas in mind that could work that would be small changes, and backwards compatible. |
@WickyNilliams Yes, this belongs in a seperate issue. But I think you need to understand that disableScrollLock adds event listeners etc, it does not just modify the DOM. It just does not make sense to call it in a server environment. You should call it immediately after "rehydration" if you need a modal to appear immediately. |
I was also having this problem, in Gatsby v2.32.10, with body-scroll-lock v3.1.5. My requirement was to lock the scroll when a modal side-navigation "drawer" was open. The solution in #154 (comment) improved the situation, but I also had to dynamically set the const [isOpen, setIsOpen] = useState(false); // represents the open state of the navigation drawer
const headerEl = useRef();
useEffect(() => {
if (typeof window === 'undefined') return;
const gatsbyDiv = document.getElementById('gatsby-focus-wrapper');
if (isOpen) {
gatsbyDiv.style.height = '100%';
document.documentElement.style.overflow = 'hidden';
disableBodyScroll(headerEl.current);
} else {
gatsbyDiv.style.height = 'auto';
document.documentElement.style.overflow = 'auto';
enableBodyScroll(headerEl.current);
}
return clearAllBodyScrollLocks();
}, [isOpen]); |
@hellodavecooper It's more likely there could be a potential issue with one part of your solution - you are setting |
In order to lock the scrolling in a gatsby project, the
overflow: hidden
style property must be set on the root<html style="overflow: hidden;">
element.The text was updated successfully, but these errors were encountered: