|
| 1 | +/* |
| 2 | + * Modal Block – opens fragment links in a dialog instead of navigating. |
| 3 | + */ |
| 4 | + |
| 5 | +import { loadFragment } from '../fragment/fragment.js'; |
| 6 | +import { loadCSS } from '../../scripts/aem.js'; |
| 7 | +import { createTag } from '../../scripts/shared.js'; |
| 8 | +import dynamicBlocks from '../dynamic/index.js'; |
| 9 | + |
| 10 | +const FRAGMENT_PREFIX = '/fragments/'; |
| 11 | + |
| 12 | +function getFragmentPath(href = '') { |
| 13 | + try { |
| 14 | + const url = new URL(href, window.location.origin); |
| 15 | + if (!url.pathname.startsWith(FRAGMENT_PREFIX)) return null; |
| 16 | + return `${url.pathname}${url.search}`; |
| 17 | + } catch { |
| 18 | + return null; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export function setupFragmentModal() { |
| 23 | + if (window.__fragmentModalReady) return; |
| 24 | + window.__fragmentModalReady = true; |
| 25 | + |
| 26 | + loadCSS(`${window.hlx.codeBasePath}/blocks/modal/modal.css`); |
| 27 | + |
| 28 | + const closeBtn = createTag('button', { type: 'button', class: 'modal-close', 'aria-label': 'Close dialog' }, '×'); |
| 29 | + const content = createTag('div', { class: 'modal-content' }); |
| 30 | + const dialog = createTag('div', { |
| 31 | + class: 'modal-dialog', |
| 32 | + role: 'dialog', |
| 33 | + 'aria-modal': 'true', |
| 34 | + 'aria-label': 'Dialog', |
| 35 | + }, [closeBtn, content]); |
| 36 | + const backdrop = createTag('div', { class: 'modal-backdrop', 'aria-hidden': 'true' }); |
| 37 | + const root = createTag('div', { class: 'modal', hidden: 'true' }, [backdrop, dialog]); |
| 38 | + document.body.append(root); |
| 39 | + let previousOverflow = ''; |
| 40 | + let previousFocus = null; |
| 41 | + |
| 42 | + const close = () => { |
| 43 | + root.hidden = true; |
| 44 | + content.replaceChildren(); |
| 45 | + document.body.style.overflow = previousOverflow; |
| 46 | + if (previousFocus?.focus) previousFocus.focus(); |
| 47 | + }; |
| 48 | + |
| 49 | + const open = async (path) => { |
| 50 | + previousFocus = document.activeElement; |
| 51 | + root.hidden = false; |
| 52 | + previousOverflow = document.body.style.overflow; |
| 53 | + document.body.style.overflow = 'hidden'; |
| 54 | + content.textContent = 'Loading...'; |
| 55 | + closeBtn.focus(); |
| 56 | + |
| 57 | + try { |
| 58 | + const fragment = await loadFragment(path); |
| 59 | + if (fragment) { |
| 60 | + const main = createTag('main', { class: 'modal-main' }); |
| 61 | + main.append(...fragment.childNodes); |
| 62 | + content.replaceChildren(main); |
| 63 | + await dynamicBlocks(main); |
| 64 | + } else { |
| 65 | + content.textContent = 'Unable to load this content right now.'; |
| 66 | + } |
| 67 | + dialog.scrollTop = 0; |
| 68 | + } catch { |
| 69 | + content.textContent = 'Unable to load this content right now.'; |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + closeBtn.addEventListener('click', close); |
| 74 | + backdrop.addEventListener('click', close); |
| 75 | + window.addEventListener('keydown', (e) => { |
| 76 | + if (e.key === 'Escape' && !root.hidden) close(); |
| 77 | + }); |
| 78 | + |
| 79 | + document.addEventListener('click', (e) => { |
| 80 | + const link = e.target.closest('main a[href*="/fragments/"]'); |
| 81 | + if (!link) return; |
| 82 | + if (link.closest('header, footer, nav, .modal')) return; |
| 83 | + if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return; |
| 84 | + if (link.target === '_blank') return; |
| 85 | + const path = getFragmentPath(link.href); |
| 86 | + if (!path) return; |
| 87 | + e.preventDefault(); |
| 88 | + open(path); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +export default function decorate(block) { |
| 93 | + setupFragmentModal(); |
| 94 | + block.style.display = 'none'; |
| 95 | +} |
0 commit comments