Skip to content

Commit

Permalink
fix: avoid moving content when blocking scrolling (#3369)
Browse files Browse the repository at this point in the history
With previously introduced changes to scroll block, in certain situation
the content is slightly moving. This happens when there is a scroll bar
visible and some content is aligned next to the scrollbar.
Setting the height to 100dvh ensures that the body is always taking the
full available height which is necessary e.g. for the sidebar.
Setting `position: relative` reverts the previous change where this was
removed. This change mainly avoids moving content.
  • Loading branch information
jeripeierSBB authored and github-actions committed Jan 27, 2025
1 parent 3fca755 commit f7b84ad
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/elements/core/dom/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function pageScrollDisabled(): boolean {
*/
export class SbbScrollHandler {
private _height!: string;
private _position!: string;
private _overflow!: string;
private _marginInlineEnd!: string;

Expand All @@ -18,13 +19,15 @@ export class SbbScrollHandler {

// Save any pre-existing styles to reapply them to the body when enabling the scroll again.
this._height = document.body.style.height;
this._position = document.body.style.position;
this._overflow = document.body.style.overflow;
this._marginInlineEnd = document.body.style.marginInlineEnd;

const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;

document.body.style.overflow = 'hidden';
document.body.style.height = '100%';
document.body.style.height = '100dvh';
document.body.style.position = 'relative';
document.body.style.marginInlineEnd = `${scrollbarWidth}px`;
document.body.style.setProperty('--sbb-scrollbar-width', `${scrollbarWidth}px`);

Expand All @@ -38,6 +41,7 @@ export class SbbScrollHandler {

// Revert body inline styles.
document.body.style.height = this._height || '';
document.body.style.position = this._position || '';
document.body.style.overflow = this._overflow || '';
document.body.style.marginInlineEnd = this._marginInlineEnd || '';
document.body.style.setProperty('--sbb-scrollbar-width', '0');
Expand Down

0 comments on commit f7b84ad

Please sign in to comment.