Skip to content
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

UI elements shifting to right when model opens #2378

Open
phanendraguptha opened this issue Nov 26, 2024 · 0 comments · May be fixed by #2413
Open

UI elements shifting to right when model opens #2378

phanendraguptha opened this issue Nov 26, 2024 · 0 comments · May be fixed by #2413

Comments

@phanendraguptha
Copy link
Contributor

phanendraguptha commented Nov 26, 2024

Description

When the search bar modal opens, elements beneath the popup shift to the right. This issue occurs because the DocSearch--active class is added to the body element, causing the scrollbar to disappear, which in turn shifts the layout.
Code causing this issue

React.useEffect(() => {
    document.body.classList.add('DocSearch--active');

    return (): void => {
      document.body.classList.remove('DocSearch--active');

      // IE11 doesn't support `scrollTo` so we check that the method exists
      // first.
      window.scrollTo?.(0, initialScrollY);
    };

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

instead we can use

  useLayoutEffect(() => {
    // Calculate the scrollbar width to compensate for removed scrollbar
    const scrollBarWidth = window.innerWidth - document.body.clientWidth;
    // Prevent layout shift by adding appropriate margin to the body
    document.body.style.marginRight = `${scrollBarWidth}px`;
     
    return (): void => {
      // Reset body styles
      document.body.style.marginRight = '0px';
    }
  }, []);

Steps to reproduce

For Windows

  1. Visit the ...
  2. Click on search bar
  3. Observe how the elements beneath the popup modal shift to the right.

For Mac

  1. Change the scrollbar behavior to always visible.
  2. Visit the ...
  3. Click on search bar
  4. Observe how the elements beneath the popup modal shift to the right.
image

Live reproduction:

demo.mp4

Expected behavior

The elements beneath the popup modal should not shift to the right

Environment

  • OS: Windows / Linux / macOS
  • Browser: All browsers
  • DocSearch version: 3.8.0
@millotp millotp linked a pull request Dec 17, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant