Skip to content

Commit

Permalink
fix: scrollbar gutters and dialog scrolling on open (#1967)
Browse files Browse the repository at this point in the history
* fix: scrollbar gutters and dialog scrolling on open

* prettier

* fix check for current scrollbarGutter property

* prettier
  • Loading branch information
KonnorRogers authored Apr 11, 2024
1 parent c6da4f5 commit a427433
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/pages/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ New versions of Shoelace are released as-needed and generally occur when a criti

## Next

- Fixed a bug in `<dialog>` where when it showed it would create a layout shift. [#1967]
- Fixed a bug in `<sl-tooltip>` that allowed unwanted text properties to leak in [#1947]

## 2.15.0
Expand Down
13 changes: 13 additions & 0 deletions src/internal/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ export function lockBodyScrolling(lockingEl: HTMLElement) {
if (!document.documentElement.classList.contains('sl-scroll-lock')) {
/** Scrollbar width + body padding calculation can go away once Safari has scrollbar-gutter support. */
const scrollbarWidth = getScrollbarWidth() + getExistingBodyPadding(); // must be measured before the `sl-scroll-lock` class is applied

let scrollbarGutterProperty = getComputedStyle(document.documentElement).scrollbarGutter;

// default is auto, unsupported browsers is "undefined"
if (!scrollbarGutterProperty || scrollbarGutterProperty === 'auto') {
scrollbarGutterProperty = 'stable';
}

if (scrollbarWidth <= 0) {
// if there's no scrollbar, just set it to "revert" so whatever the user has set gets used. This is useful is the page is not overflowing and showing a scrollbar, or if the user has overflow: hidden, or any other reason a scrollbar may not be showing.
scrollbarGutterProperty = 'revert';
}
document.documentElement.style.setProperty('--sl-scroll-lock-gutter', scrollbarGutterProperty);
document.documentElement.classList.add('sl-scroll-lock');
document.documentElement.style.setProperty('--sl-scroll-lock-size', `${scrollbarWidth}px`);
}
Expand Down
5 changes: 4 additions & 1 deletion src/themes/_utility.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

@supports (scrollbar-gutter: stable) {
.sl-scroll-lock {
scrollbar-gutter: stable !important;
scrollbar-gutter: var(--sl-scroll-lock-gutter) !important;
}

.sl-scroll-lock body {
overflow: hidden !important;
}
}
Expand Down

0 comments on commit a427433

Please sign in to comment.