Skip to content

Commit

Permalink
fix(navigation): maintain scrollbar space
Browse files Browse the repository at this point in the history
when toggling the menu, don't collapse the space occupied by the scrollbar
  • Loading branch information
thecristen committed Nov 8, 2023
1 parent 1e0ce3f commit 308f68c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 3 additions & 2 deletions apps/site/assets/css/_global-navigation.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ header {
}

[data-nav-open] {
body {
overflow: hidden; // prevent scrolling
.body-wrapper {
overflow: inherit; // same value as on body
position: relative; // so children elements using `position: absolute` are in the right place
}

.m-menu--cover {
Expand Down
22 changes: 21 additions & 1 deletion apps/site/assets/ts/app/global-navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ export function setup(rootElement: HTMLElement): void {
if (aMenuIsBeingExpanded) {
// eslint-disable-next-line no-param-reassign
rootElement.dataset.navOpen = "true";
disableBodyScroll(header);
if (observedDataAttributes.includes(TOGGLE_NAMES.mobile)) {
// eslint-disable-next-line no-param-reassign
header.dataset.navOpen = "true";
Expand All @@ -164,6 +163,7 @@ export function setup(rootElement: HTMLElement): void {
} else if (observedDataAttributes.includes(TOGGLE_NAMES.search)) {
// eslint-disable-next-line no-param-reassign
header.dataset.searchOpen = "true";
disableBodyScroll(header);
}
} else {
// only do this if no other menu is expanded
Expand All @@ -190,6 +190,26 @@ export function setup(rootElement: HTMLElement): void {
aMenuIsBeingExpanded &&
observedDataAttributes.includes(TOGGLE_NAMES.desktop)
) {
// Disable scrolling the page, but accomodate any visible scrollbars in
// order to avoid horizontal shift in the layout when scrolling becomes
// disabled. This additionally requires adjusting the width of the veil,
// to maintain a pleasing appearance.
disableBodyScroll(header, { reserveScrollBarGap: true });
const cover = rootElement.querySelector<HTMLElement>("[data-nav='veil']");
if (
cover &&
!cover.style.paddingRight &&
rootElement.dataset.navOpen === "true"
) {
const body = rootElement.querySelector("body");
// this was added by { reserveScrollBarGap: true }
const paddingRight = body?.style.paddingRight;
if (paddingRight && paddingRight !== "") {
// add same 'padding' for veil by substracting from width
cover.style.width = `calc(100% - ${paddingRight})`;
}
}

const thisMenu = mutations.map(({ target }) =>
(target as Element).getAttribute("aria-controls")
)[0];
Expand Down

0 comments on commit 308f68c

Please sign in to comment.