From f06b56bee5c7fae34c75abd30aba526252552d6e Mon Sep 17 00:00:00 2001 From: JoelCDL Date: Tue, 5 Nov 2024 17:09:25 -0800 Subject: [PATCH] Replace header nav toggle logic with popover api --- css/components/headernav.css | 113 ++++++++++----------------- css/components/mobilenav.css | 4 + elements/3-components/headernav.hbs | 3 +- elements/_template-default.hbs | 2 +- elements/_template-page.hbs | 2 +- js/anchor-positioning.js | 19 +++++ js/headernav.js | 115 ++++++++++++++-------------- js/main.js | 2 + js/popover-support.js | 5 ++ package-lock.json | 80 ++++++++++++++++++- package.json | 3 + 11 files changed, 216 insertions(+), 132 deletions(-) create mode 100644 js/anchor-positioning.js create mode 100644 js/popover-support.js diff --git a/css/components/headernav.css b/css/components/headernav.css index f3dd3ec4..c089a567 100644 --- a/css/components/headernav.css +++ b/css/components/headernav.css @@ -1,15 +1,18 @@ /***** Header Nav Component *****/ -/* Read notes in component config */ - .c-headernav { --headernav-section-padding: var(--space3); --headernav-item-margin: var(--space3); - position: relative; + .no-popover & { + ul ul { + display: none; + } + } ul { - all: unset; + margin: unset; + padding: unset; list-style-type: none; li { @@ -18,39 +21,32 @@ } } - a { - display: block; - } - - /* Sections: */ - > ul { - @media (--min-width2) { + @media (--min-width2) { + a { + display: block; + } + + /* Sections: */ + > ul { display: flex; - - > li { - flex: 1 1 auto; - } } - } + + > ul > li { + flex: 1 1 auto; - > ul > li { - @media (--min-width2) { - /* Nav bar main items: */ &:not(:last-child) { border-inline-end: 1px solid oklch(55% 0 0deg); } } - } - - /* Nav bar links: */ - > ul > li > a { - @media (--min-width2) { + + /* Nav bar links: */ + > ul > li > a { padding: var(--space1); background: linear-gradient(oklch(95% 0 0deg), oklch(88% 0 0deg)); color: oklch(36% 0 0deg); text-align: center; text-decoration: none; - + &:focus, &:hover { color: var(--color-blue); @@ -60,9 +56,28 @@ } /* Panels: */ - > ul > li > ul { - display: none; + + [popover] { position: absolute; + top: anchor(bottom); + left: anchor(left); + margin: 0; + transition-property: opacity, display, overlay; + transition-duration: .3s; + transition-behavior: allow-discrete; + border: unset; + opacity: 0; + + &:popover-open { + opacity: 1; + + @starting-style { + opacity: 0; + } + } + } + + > ul > li > ul { padding: var(--headernav-section-padding); column-gap: calc(var(--headernav-section-padding) * 2); column-rule: 1px solid oklch(88% 0 0deg); @@ -126,48 +141,4 @@ > ul > li li { margin-block-end: var(--headernav-item-margin); } - - /**** Panel Toggle ****/ - - /* If c-headernav JS not supported: */ - - &.c-headernav-no-js { - > ul > li:hover > ul { - @media (--min-width2) { - display: block; - z-index: 10; - } - } - - /* If :focus-within not supported. Only primary links accessible via tabkey: */ - - > ul > li a:focus { - @media (--min-width2) { - display: block; - z-index: 10; - } - } - - /* If :focus-within supported. All links accessbile via focus. No toggle state on primary links, so user must tab through every secondary link in each section to reach the next section: */ - - > ul > li:focus-within > ul { - @media (--min-width2) { - display: block; - z-index: 10; - } - } - } - - /* If c-headernav JS supported: */ - - &.c-headernav-js { - /* Primary links disabled via JS and used as a toggle for each section. Hover and focus states handled by JS and set via 'open' class: */ - - li.open ul { - @media (--min-width2) { - display: block; - z-index: 10; - } - } - } } diff --git a/css/components/mobilenav.css b/css/components/mobilenav.css index 53e4ea47..ae6d945a 100644 --- a/css/components/mobilenav.css +++ b/css/components/mobilenav.css @@ -42,6 +42,10 @@ mask: url('data-url:npm:fa-light/angle-right.svg') center / 0.7rem no-repeat; } } + + ul { + display: none; + } } } } diff --git a/elements/3-components/headernav.hbs b/elements/3-components/headernav.hbs index c8a89143..4f289e3c 100644 --- a/elements/3-components/headernav.hbs +++ b/elements/3-components/headernav.hbs @@ -1,5 +1,4 @@ -{{!-- Read notes in component config --}} -