Skip to content

Commit

Permalink
Handle CLS issue on localnav: mobile redesign (#3253)
Browse files Browse the repository at this point in the history
* Check for localnav

* Moving getGnanSource to utils

* Awaiting gnav source

* Adding lanalog when there is mismatch with content and name

* Lint fixes

* Changing height of localnav
  • Loading branch information
bandana147 committed Jan 7, 2025
1 parent 7f97145 commit 9ad027a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion libs/blocks/global-navigation/global-navigation.css
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ header + .feds-localnav {

.feds-localnav-title {
width: 100%;
height: 53px;
height: 40px;
font-size: 16px;
font-weight: 700;
border: 0;
Expand Down
20 changes: 8 additions & 12 deletions libs/blocks/global-navigation/global-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
loadIms,
decorateLinks,
loadScript,
getGnavSource,
} from '../../utils/utils.js';
import {
closeAllDropdowns,
Expand Down Expand Up @@ -385,7 +386,12 @@ class Gnav {
if (this.elements.localNav || !this.newMobileNav || isDesktop.matches) {
localNavItems[0].querySelector('a').textContent = title.trim();
} else {
const localNav = document.querySelector('.feds-localnav');
let localNav = document.querySelector('.feds-localnav');
if (!localNav) {
lanaLog({ message: 'GNAV: Localnav does not include \'localnav\' in its name.', tags: 'errorType=info,module=gnav' });
localNav = toFragment`<div class="feds-localnav"/>`;
this.block.after(localNav);
}
localNav.append(toFragment`<button class="feds-navLink--hoverCaret feds-localnav-title"></button>`, toFragment` <div class="feds-localnav-items"></div>`);

const itemWrapper = localNav.querySelector('.feds-localnav-items');
Expand Down Expand Up @@ -1181,19 +1187,9 @@ class Gnav {
};
}

const getSource = async () => {
const { locale, dynamicNavKey } = getConfig();
let url = getMetadata('gnav-source') || `${locale.contentRoot}/gnav`;
if (dynamicNavKey) {
const { default: dynamicNav } = await import('../../features/dynamic-navigation/dynamic-navigation.js');
url = dynamicNav(url, dynamicNavKey);
}
return url;
};

export default async function init(block) {
const { mep } = getConfig();
const sourceUrl = await getSource();
const sourceUrl = await getGnavSource();
const newMobileNav = getMetadata('mobile-gnav-v2') !== 'false';
const [url, hash = ''] = sourceUrl.split('#');
if (hash === '_noActiveItem') {
Expand Down
2 changes: 1 addition & 1 deletion libs/styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ header.global-navigation a {
}

.feds-localnav {
height: 53px;
height: 40px;
}

@media (min-width: 900px) {
Expand Down
15 changes: 13 additions & 2 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,17 @@ function decorateDefaults(el) {
});
}

function decorateHeader() {
export async function getGnavSource() {
const { locale, dynamicNavKey } = getConfig();
let url = getMetadata('gnav-source') || `${locale.contentRoot}/gnav`;
if (dynamicNavKey) {
const { default: dynamicNav } = await import('../features/dynamic-navigation/dynamic-navigation.js');
url = dynamicNav(url, dynamicNavKey);
}
return url;
}

async function decorateHeader() {
const breadcrumbs = document.querySelector('.breadcrumbs');
breadcrumbs?.remove();
const header = document.querySelector('header');
Expand All @@ -794,7 +804,8 @@ function decorateHeader() {
const dynamicNavActive = getMetadata('dynamic-nav') === 'on'
&& window.sessionStorage.getItem('gnavSource') !== null;
if (!dynamicNavActive && (baseBreadcrumbs || breadcrumbs || autoBreadcrumbs)) header.classList.add('has-breadcrumbs');
if (getMetadata('is-localnav') === 'true' && getMetadata('mobile-gnav-v2') !== 'false') {
const gnavSource = await getGnavSource();
if (gnavSource.split('/').pop().match('localnav') && getMetadata('mobile-gnav-v2') !== 'false') {
// Preserving space to avoid CLS issue
const localNavWrapper = createTag('div', { class: 'feds-localnav' });
header.after(localNavWrapper);
Expand Down

0 comments on commit 9ad027a

Please sign in to comment.