Fix Chromium scrollbar-induced layout shifts

This commit addresses an issue in Chromium on Linux and Windows where
the appearance of a vertical scrollbar causes unexpected horizontal
layout shifts. This behavior typically occurs when the window is
resized, a card is opened or a script is selected, resulting in content
being pushed to the left.

The solution implemented involves using `scrollbar-gutter: stable` to
ensure space is always allocated for the scrollbar, thus preventing any
shift in the page layout. This fix primarily affects Chromium-based
browsers on Linux and Windows. It has no impact on Firefox on any
platform, or any browser on macOS (including Chromium). Because these
render the scrollbar as an overlay, and do not suffer from this issue.

Steps to reproduce the issue using Chromium browser on Linux/Windows:

1. Open the app with a height large enough where a vertical scrollbar is
   not visible.
2. Resize the window to a height that triggers a vertical scrollbar.
3. Notice the layout shift as the body content moves to the right.

Changes:

- Add a CSS mixin to handle scrollbar gutter allocation with a fallback.
- Add support for modal dialog background lock to handle
  `scrollbar-gutter: stable;` in calculations to avoid layout shift when
  a modal is open.
- Add E2E test to avoid regression.
- Update DevToolkit to accommodate new scrollbar spacing.
This commit is contained in:
undergroundwires
2024-05-09 18:35:02 +02:00
parent dd71536316
commit bc4879cfe9
12 changed files with 377 additions and 180 deletions

View File

@@ -31,6 +31,7 @@ import { defineComponent, ref } from 'vue';
import { injectKey } from '@/presentation/injectionSymbols';
import FlatButton from '@/presentation/components/Shared/FlatButton.vue';
import { dumpNames } from './DumpNames';
import { useScrollbarGutterWidth } from './UseScrollbarGutterWidth';
export default defineComponent({
components: {
@@ -39,6 +40,7 @@ export default defineComponent({
setup() {
const { log } = injectKey((keys) => keys.useLogger);
const isOpen = ref(true);
const scrollbarGutterWidth = useScrollbarGutterWidth();
const devActions: readonly DevAction[] = [
{
@@ -58,6 +60,7 @@ export default defineComponent({
devActions,
isOpen,
close,
scrollbarGutterWidth,
};
},
});
@@ -71,10 +74,14 @@ interface DevAction {
<style scoped lang="scss">
@use "@/presentation/assets/styles/main" as *;
$viewport-edge-offset: $spacing-absolute-large; // close to Chromium gutter width (15px)
.dev-toolkit-container {
position: fixed;
top: 0;
right: 0;
top: $viewport-edge-offset;
right: max(v-bind(scrollbarGutterWidth), $viewport-edge-offset);
background-color: rgba($color-on-surface, 0.5);
color: $color-on-primary;
padding: $spacing-absolute-medium;

View File

@@ -0,0 +1,39 @@
import {
computed, readonly, ref, watch,
} from 'vue';
import { throttle } from '@/application/Common/Timing/Throttle';
import { useAutoUnsubscribedEventListener } from '../Shared/Hooks/UseAutoUnsubscribedEventListener';
const RESIZE_EVENT_THROTTLE_MS = 200;
export function useScrollbarGutterWidth() {
const scrollbarWidthInPx = ref(getScrollbarGutterWidth());
const { startListening } = useAutoUnsubscribedEventListener();
startListening(window, 'resize', throttle(() => {
scrollbarWidthInPx.value = getScrollbarGutterWidth();
}, RESIZE_EVENT_THROTTLE_MS));
const bodyWidth = useBodyWidth();
watch(() => bodyWidth.value, () => {
scrollbarWidthInPx.value = getScrollbarGutterWidth();
}, { immediate: false });
const scrollbarWidthStyle = computed(() => `${scrollbarWidthInPx.value}px`);
return readonly(scrollbarWidthStyle);
}
function getScrollbarGutterWidth(): number {
return document.documentElement.clientWidth - document.documentElement.offsetWidth;
}
function useBodyWidth() {
const width = ref(document.body.offsetWidth);
const observer = new ResizeObserver((entries) => throttle(() => {
for (const entry of entries) {
width.value = entry.borderBoxSize[0].inlineSize;
}
}, RESIZE_EVENT_THROTTLE_MS));
observer.observe(document.body, { box: 'border-box' });
return readonly(width);
}

View File

@@ -16,4 +16,6 @@ export interface ScrollDomStateAccessor {
readonly htmlScrollHeight: number;
readonly htmlClientWidth: number;
readonly htmlClientHeight: number;
readonly htmlOffsetWidth: number;
readonly htmlOffsetHeight: number;
}

View File

@@ -103,7 +103,6 @@ const BodyStyleLeft: DomPropertyMutator<{
storeInitialState: (dom) => ({
htmlScrollLeft: dom.htmlScrollLeft,
bodyStyleLeft: dom.bodyStyleLeft,
bodyMarginLeft: dom.bodyComputedMarginLeft,
}),
onBlock: (initialState, dom) => {
if (initialState.htmlScrollLeft === 0) {
@@ -206,13 +205,21 @@ const BodyPositionFixed: DomPropertyMutator<{
const BodyWidth100Percent: DomPropertyMutator<{
readonly bodyStyleWidth: string;
readonly htmlOffsetWidth: number;
readonly htmlClientWidth: number;
}> = {
storeInitialState: (dom) => ({
bodyStyleWidth: dom.bodyStyleWidth,
htmlOffsetWidth: dom.htmlOffsetWidth,
htmlClientWidth: dom.htmlClientWidth,
}),
onBlock: (_, dom) => {
dom.bodyStyleWidth = calculateBodyViewportStyleWithMargins(
[dom.bodyComputedMarginLeft, dom.bodyComputedMarginRight],
onBlock: (initialState, dom) => {
dom.bodyStyleWidth = calculateAdjustedStyle(
[
dom.bodyComputedMarginLeft,
dom.bodyComputedMarginRight,
calculateScrollbarGutterStyle(initialState.htmlClientWidth, initialState.htmlOffsetWidth),
],
);
return ScrollRevertAction.RestoreRequired;
},
@@ -223,13 +230,21 @@ const BodyWidth100Percent: DomPropertyMutator<{
const BodyHeight100Percent: DomPropertyMutator<{
readonly bodyStyleHeight: string;
readonly htmlOffsetHeight: number;
readonly htmlClientHeight: number;
}> = {
storeInitialState: (dom) => ({
bodyStyleHeight: dom.bodyStyleHeight,
htmlOffsetHeight: dom.htmlOffsetHeight,
htmlClientHeight: dom.htmlClientHeight,
}),
onBlock: (_, dom) => {
dom.bodyStyleHeight = calculateBodyViewportStyleWithMargins(
[dom.bodyComputedMarginTop, dom.bodyComputedMarginBottom],
onBlock: (initialState, dom) => {
dom.bodyStyleHeight = calculateAdjustedStyle(
[
dom.bodyComputedMarginTop,
dom.bodyComputedMarginBottom,
calculateScrollbarGutterStyle(initialState.htmlClientHeight, initialState.htmlOffsetHeight),
],
);
return ScrollRevertAction.RestoreRequired;
},
@@ -280,11 +295,20 @@ interface DomPropertyMutator<TInitialStateValue> {
restoreStateOnUnblock(storedState: TInitialStateValue, dom: ScrollDomStateAccessor): void;
}
function calculateBodyViewportStyleWithMargins(
margins: readonly string[],
/** Calculates allocated scrollbar gutter, adjusting for `scrollbar-gutter: stable` */
function calculateScrollbarGutterStyle(
clientSize: number,
offsetSize: number,
): string {
const scrollbarGutterSize = clientSize - offsetSize;
return scrollbarGutterSize !== 0 ? `${scrollbarGutterSize}px` : '';
}
function calculateAdjustedStyle(
spaceOffsets: readonly string[],
): string {
let value = '100%';
const calculatedMargin = margins
const calculatedMargin = spaceOffsets
.filter((marginText) => marginText.length > 0)
.join(' + '); // without setting margins, it leads to layout shift if body has margin
if (calculatedMargin) {

View File

@@ -61,4 +61,8 @@ class WindowScrollDomState implements ScrollDomStateAccessor {
get htmlClientWidth(): number { return HtmlElement.clientWidth; }
get htmlClientHeight(): number { return HtmlElement.clientHeight; }
get htmlOffsetWidth(): number { return HtmlElement.offsetWidth; }
get htmlOffsetHeight(): number { return HtmlElement.offsetHeight; }
}