Skip to content

Commit db8edeb

Browse files
authored
fix(ui): compensate for scrollbar (#2413)
fixes #2378
1 parent 50de4d9 commit db8edeb

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

bundlesize.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
{
1212
"path": "packages/docsearch-js/dist/umd/index.js",
13-
"maxSize": "35.08 kB"
13+
"maxSize": "35.5 kB"
1414
}
1515
]
1616
}

cypress/e2e/search/actions.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ describe('Start', () => {
99
it('Open modal on search button click', () => {
1010
cy.openModal();
1111
cy.modalIsVisibleAndFocused();
12+
13+
// check that the scrollbar offset is compensated
14+
cy.get('body').should('have.css', 'overflow', 'hidden');
15+
cy.get('body').should('have.css', 'margin-right', '15px');
1216
});
1317

1418
it('Open modal with key shortcut on Windows/Linux', () => {

packages/docsearch-react/src/DocSearchModal.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,17 @@ export function DocSearchModal({
350350
// eslint-disable-next-line react-hooks/exhaustive-deps
351351
}, []);
352352

353+
React.useLayoutEffect(() => {
354+
// Calculate the scrollbar width to compensate for removed scrollbar
355+
const scrollBarWidth = window.innerWidth - document.body.clientWidth;
356+
// Prevent layout shift by adding appropriate margin to the body
357+
document.body.style.marginRight = `${scrollBarWidth}px`;
358+
359+
return (): void => {
360+
document.body.style.marginRight = '0px';
361+
};
362+
}, []);
363+
353364
React.useEffect(() => {
354365
const isMobileMediaQuery = window.matchMedia('(max-width: 768px)');
355366

0 commit comments

Comments
 (0)