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

Feature-16-sidebar-test #58

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
extends: 'airbnb-base',
env: {
browser: true,
'jest/globals': true,
},
parser: '@babel/eslint-parser',
parserOptions: {
Expand All @@ -19,4 +20,5 @@ module.exports = {
}],
'function-paren-newline': 'off',
},
plugins: ['jest'],
};
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ helix-importer-ui
.DS_Store
*.bak
.idea

.vscode/
.htmlvalidate.json
.htmlvalidateignore
1 change: 1 addition & 0 deletions blocks/fragment/fragment.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* nothing here */
45 changes: 45 additions & 0 deletions blocks/fragment/fragment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Fragment Block
* Include content from one Helix page in another.
* https://www.hlx.live/developer/block-collection/fragment
*/

import {
decorateMain,
} from '../../scripts/scripts.js';

import {
loadBlocks,
} from '../../scripts/aem.js';

/**
* Loads a fragment.
* @param {string} path The path to the fragment
* @returns {HTMLElement} The root element of the fragment
*/
async function loadFragment(path) {
if (path && path.startsWith('/')) {
const resp = await fetch(`${path}.plain.html`);
if (resp.ok) {
const main = document.createElement('main');
main.innerHTML = await resp.text();
decorateMain(main);
await loadBlocks(main);
return main;
}
}
return null;
}

export default async function decorate(block) {
const link = block.querySelector('a');
const path = link ? link.getAttribute('href') : block.textContent.trim();
const fragment = await loadFragment(path);
if (fragment) {
const fragmentSection = fragment.querySelector(':scope .section');
if (fragmentSection) {
block.closest('.section').classList.add(...fragmentSection.classList);
block.closest('.fragment-wrapper').replaceWith(...fragmentSection.childNodes);
}
}
}
6 changes: 4 additions & 2 deletions blocks/header/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ export default async function decorate(block) {
nav.prepend(hamburger);
nav.setAttribute('aria-expanded', 'false');
// prevent mobile nav behavior on window resize
toggleMenu(nav, navSections, isDesktop.matches);
isDesktop.addEventListener('change', () => toggleMenu(nav, navSections, isDesktop.matches));
if (navSections) {
toggleMenu(nav, navSections, isDesktop.matches);
isDesktop.addEventListener('change', () => toggleMenu(nav, navSections, isDesktop.matches));
}

decorateIcons(nav);
const navWrapper = document.createElement('div');
Expand Down
Loading