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

fix(ui): compensate for scrollbar #2413

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
{
"path": "packages/docsearch-js/dist/umd/index.js",
"maxSize": "35.08 kB"
"maxSize": "35.5 kB"
}
]
}
4 changes: 4 additions & 0 deletions cypress/e2e/search/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('Start', () => {
it('Open modal on search button click', () => {
cy.openModal();
cy.modalIsVisibleAndFocused();

// check that the scrollbar offset is compensated
cy.get('body').should('have.css', 'overflow', 'hidden');
cy.get('body').should('have.css', 'margin-right', '15px');
});

it('Open modal with key shortcut on Windows/Linux', () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/docsearch-react/src/DocSearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,17 @@ export function DocSearchModal({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

React.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 => {
document.body.style.marginRight = '0px';
};
}, []);

React.useEffect(() => {
const isMobileMediaQuery = window.matchMedia('(max-width: 768px)');

Expand Down