1
0
Fork 0

fix(ui): compensate for scrollbar (#2413)

fixes #2378
This commit is contained in:
Pierre Millot 2025-01-21 09:41:27 +01:00 committed by GitHub
parent 50de4d9de0
commit db8edeb818
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 1 deletions

View file

@ -10,7 +10,7 @@
},
{
"path": "packages/docsearch-js/dist/umd/index.js",
"maxSize": "35.08 kB"
"maxSize": "35.5 kB"
}
]
}

View file

@ -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', () => {

View file

@ -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)');