Skip to content

Content shift when collapsing or expanding the sidebar #7918

Closed as not planned

Description

@yzhe819

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

The displayed portion of the content changes visually when switching between the collapsed and expanded states of the sidebar. When I collapsed and then expanded sidebar at the bottom, the page display jumps from the bottom footer to the content section.

Reproducible demo

https://docusaurus.io/docs/deployment

Steps to reproduce

  1. Navigate to: https://docusaurus.io/docs/deployment
  2. Scroll to the bottom
  3. Collapse the sidebar
  4. Expand the sidebar
  5. Try 3 and 4 multiple times (You can notice the vertical shift of web page content)

Demo

video1197996455.mp4

Expected behavior

No content shift when collapsing or expanding the sidebar

Need more expected details from the development team
cc @Josh-Cena

Actual behavior

Before the sidebar is collapsed, the page is at the bottom, but after the sidebar is collapsed and expanded, the page will jump to the Deploying to Layer0 section.

Your environment

Self-service

  • I'd be willing to fix this bug myself.

Activity

  1. added
    bug
    status: needs triage
    on Aug 8, 2022
  2. added
    domain: theme
    and removed
    status: needs triage
    on Aug 8, 2022
  3. slorber commented on Aug 9, 2022

    @slorber
  4. added
    closed: duplicate
    on Aug 9, 2022
  5. Josh-Cena commented on Aug 9, 2022

    @Josh-Cena
  6. removed
    closed: duplicate
    on Aug 9, 2022
  7. slorber commented on Aug 9, 2022

    @slorber
  8. Josh-Cena commented on Aug 9, 2022

    @Josh-Cena
  9. slorber commented on Aug 9, 2022

    @slorber
    Collaborator

    I see now 🤪 thanks for clarifying. The markdown page content has changed from smaller to taller.

    So I'm going to move my comment here and remove it from the older issue. 👇👇👇


    This behavior is because we toggle from max-width: 1320px to max-width: 1440px

    Changing the dimension of the content container leads to some lines wrapping, leading to the layout shift. It's not the sidebar that changes height, but rather the scroll container that changes dimensions as we toggle the sidebar.

    CleanShot 2022-08-09 at 16 59 46@2x

    CleanShot 2022-08-09 at 17 00 12@2x

    You'll notice that the layout shift is more important on larger docs, and at the bottom: this is just because there are more lines wrapping above :)


    In the end => larger container VS no layout shift: you can only pick one of those.

    I'd say the layout shift is a minor annoyance and usually we want to expand a bit the content container when more space is available.

    Technically we could keep the same container size for large desktop screens, but for mobile we absolutely want to increase that container width anyway.

    CleanShot 2022-08-09 at 17 02 14

    It wouldn't make sense to collapse the sidebar and still keep a small content container (< 500px in the screenshot above).

    What I mean is: layout shifts are expected to happen, on purpose, because we want to expand the length of the lines

    If you want 0 layout shift, I'd say it's your responsibility to ensure the content container keeps the exact same size. You can do that with CSS. I don't think we'd want Docusaurus to do that by default.


    Now, a possible solution to mitigate this layout shift, particularly on very large documents where the layout shift can be significant (like the bottom of our deployment page)

    • take some content node in the middle of the screen
    • ensure it keeps the exact same viewport position after we have resized the container

    Note we already do something similar when switching tabs (cf #5618), that can also produce layout shifts, and have some util already:

    const {blockElementScrollPositionUntilNextRender} = useScrollPositionBlocker();

    This won't prevent new line wraps in the visible viewport. Not even sure it's worth implementing this.

    Is this layout shift a big deal in the first place and annoying some real users?

  10. slorber commented on Oct 16, 2026

    @slorber
    Collaborator

    Closing as working as designed. When the docs sidebar is hidden, the doc content gets the freed space on purpose. Lines re-wrap, the page gets shorter, and the browser clamps the scroll position. Expanding the sidebar makes the page taller again, but the scroll position stays where it was clamped.

    Still the same on 4.0.0-canary-6972. With a 1280px viewport, scrolled to the bottom of a long doc, the content container goes from 965px to 1235px when the sidebar is hidden, the page height from 22269px to 18732px, and after expanding the sidebar you end up 3537px above the bottom.

    Repro: https://github.com/slorber/docusaurus-repros/tree/issue-7918

    • A minimal v4 site with themeConfig.docs.sidebar.hideable: true and a long doc
    • The README has the steps and measurements at 1280px and 1920px wide
    • A CSS workaround in src/css/custom.css, off by default, enabled with one line in docusaurus.config.js

    If you prefer no shift over a wider content area, this CSS keeps the content exactly as wide as with the sidebar visible, centered in the freed space:

    @media (width >= 997px) {
      [class*='docSidebarContainerHidden'] + main {
        max-width: min(
          calc(100% - var(--doc-sidebar-width)),
          var(--ifm-container-width-xl)
        );
        margin-inline: auto;
      }
    }

    Verified in the repro: the content width, the page height, and the scroll position at the bottom don't change when toggling the sidebar.

    (AI-assisted)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdomain: theme

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions