Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(navigation): fix tab order and layout shift #1793

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 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 Expand Up @@ -133,7 +134,7 @@ nav.m-menu--desktop {
.m-menu--desktop__menu {
background-color: $brand-primary-lightest-contrast;
box-shadow: 0 7px 14px 0 $gray-shadow;
height: 0; // start off closed
display: none; // start off closed
left: 0;
overflow: hidden;
position: absolute;
Expand All @@ -143,7 +144,7 @@ nav.m-menu--desktop {

// opened
.m-menu--desktop__toggle[aria-expanded='true'] + & {
height: initial;
display: initial;
}
}

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