Burger Button
A menu button: the lines meet, then cross, and Menu rolls to Close at a fixed width. It follows aria-expanded and fires kodza:burger, so it drives the menu you already have.
View codeA Menu button that grows into the menu around itself: the card widens, then unfolds, Close stays on the button, and each link rises as the edge uncovers it. Grows upward near the bottom.
With your AI
Say it to your AI. It gets the exact code below, not its best guess, and knows where it goes.
Add the Kodza Expanding Pill Menu to my page.
claude mcp add -t http kodza https://kodza.dev/api/mcp
<div class="kodza-expanding-pill">
<button class="kodza-expanding-pill__toggle" type="button" aria-haspopup="dialog" aria-expanded="false"><span class="kodza-expanding-pill__lines" aria-hidden="true"></span><span class="kodza-expanding-pill__say"><span>Menu</span><span aria-hidden="true">Close</span></span></button>
<div class="kodza-expanding-pill__panel" role="dialog" aria-modal="true" aria-label="Menu" tabindex="-1" hidden>
<span class="kodza-expanding-pill__shade" aria-hidden="true"></span>
<div class="kodza-expanding-pill__float">
<div class="kodza-expanding-pill__card">
<button class="kodza-expanding-pill__close" type="button"><span class="kodza-expanding-pill__cross" aria-hidden="true"></span><span class="kodza-expanding-pill__say"><span aria-hidden="true">Menu</span><span>Close</span></span></button>
<p class="kodza-expanding-pill__label">Five links. That's the site.</p>
<nav class="kodza-expanding-pill__nav" aria-label="Main">
<a class="kodza-expanding-pill__link" href="#"><span class="kodza-expanding-pill__mask"><span class="kodza-expanding-pill__word">Work</span></span><span class="kodza-expanding-pill__index">01</span></a>
<a class="kodza-expanding-pill__link" href="#"><span class="kodza-expanding-pill__mask"><span class="kodza-expanding-pill__word">Studio</span></span><span class="kodza-expanding-pill__index">02</span></a>
<a class="kodza-expanding-pill__link" href="#"><span class="kodza-expanding-pill__mask"><span class="kodza-expanding-pill__word">Process</span></span><span class="kodza-expanding-pill__index">03</span></a>
<a class="kodza-expanding-pill__link" href="#"><span class="kodza-expanding-pill__mask"><span class="kodza-expanding-pill__word">Notes</span></span><span class="kodza-expanding-pill__index">04</span></a>
<a class="kodza-expanding-pill__link" href="#"><span class="kodza-expanding-pill__mask"><span class="kodza-expanding-pill__word">Contact</span></span><span class="kodza-expanding-pill__index">05</span></a>
</nav>
<div class="kodza-expanding-pill__foot">
<a href="#">studio@example.com</a>
<p class="kodza-expanding-pill__socials"><a href="#">GitHub</a><a href="#">X</a><a href="#">Bluesky</a></p>
</div>
<span class="kodza-expanding-pill__dock" aria-hidden="true"></span>
</div>
</div>
</div>
</div>
/* Kodza License: free to use, client work included. Not to resell or redistribute, ported or not: https://kodza.dev/docs/#print-license */
/* Kodza — Expanding Pill Menu
The Menu button grows into a card around itself: it widens first, then unfolds towards the
middle of the screen, its corners staying concentric with the pill. Close never moves, it is
the button, its word rolling from Menu to Close. Each link rises from its mask as the card's
edge uncovers it, and closing folds it all back into the button. Near the bottom of the screen
the card grows upward; near an edge, inward. No page to mark: it opens over the page.
Everything is tuned with the --kz-expanding-pill-* variables. */
.kodza-expanding-pill {
--kz-expanding-pill-bg: #0a0a0a; /* the button, and the card it becomes */
--kz-expanding-pill-fg: #f2f2f0;
--kz-expanding-pill-muted: rgba(242, 242, 240, 0.62);
--kz-expanding-pill-shade: rgba(10, 10, 10, 0.2); /* over the page while it is open */
--kz-expanding-pill-width: 360px; /* the card, never wider than the screen minus 12px a side */
--kz-expanding-pill-size: 34px; /* the links */
--kz-expanding-pill-inset: 6px; /* from the card's edge to Close: the corners stay concentric */
--kz-expanding-pill-duration: 0.8s;
--kz-expanding-pill-ease: cubic-bezier(0.62, 0, 0.22, 1);
display: inline-block;
}
.kodza-expanding-pill__toggle,
.kodza-expanding-pill__close {
display: inline-flex;
align-items: center;
gap: 10px;
height: 40px;
margin: 0;
padding: 0 18px 0 16px;
border: 0;
border-radius: 999px;
font: inherit;
font-size: 15px;
font-weight: 500;
line-height: 1;
letter-spacing: -0.01em;
white-space: nowrap;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
}
.kodza-expanding-pill__toggle {
background: var(--kz-expanding-pill-bg);
color: var(--kz-expanding-pill-fg);
}
/* On the card, Close is the button: same place, same size, a lighter ground once open */
.kodza-expanding-pill__close {
position: absolute;
top: var(--kz-expanding-pill-inset);
right: var(--kz-expanding-pill-inset);
z-index: 1;
box-sizing: border-box;
background: color-mix(in srgb, var(--kz-expanding-pill-fg) 12%, transparent);
color: var(--kz-expanding-pill-fg);
transition: background-color 0.25s;
}
.kodza-expanding-pill__close:hover {
background: color-mix(in srgb, var(--kz-expanding-pill-fg) 18%, transparent);
}
.kodza-expanding-pill__toggle:focus-visible {
outline: 2px solid var(--kz-expanding-pill-bg);
outline-offset: 3px;
}
.kodza-expanding-pill__close:focus-visible,
.kodza-expanding-pill__foot a:focus-visible {
outline: 2px solid var(--kz-expanding-pill-fg);
outline-offset: 3px;
}
/* A link's focus is drawn on its word, so it rises with it */
.kodza-expanding-pill__link:focus-visible {
outline: none;
}
.kodza-expanding-pill__link:focus-visible .kodza-expanding-pill__word {
text-decoration: underline;
text-decoration-thickness: 2px;
text-underline-offset: 0.12em;
}
/* Two lines on the button; Close shows them crossed, turned on the card's clock */
.kodza-expanding-pill__lines,
.kodza-expanding-pill__cross {
position: relative;
flex: none;
width: 14px;
height: 8px;
}
.kodza-expanding-pill__lines::before,
.kodza-expanding-pill__lines::after,
.kodza-expanding-pill__cross::before,
.kodza-expanding-pill__cross::after {
content: "";
position: absolute;
left: 0;
width: 100%;
height: 1.5px;
border-radius: 1px;
background: currentColor;
/* On its own layer: drawn the same way at rest and while turning, so no snap at the end */
will-change: transform;
}
.kodza-expanding-pill__lines::before,
.kodza-expanding-pill__cross::before {
top: 0;
}
.kodza-expanding-pill__lines::after,
.kodza-expanding-pill__cross::after {
top: 6px; /* whole pixels: both lines sit on the pixel grid the same way */
}
/* Whole at rest, as an inset: the pointer slides it to a shorter one (from none it would jump) */
.kodza-expanding-pill__lines::after {
clip-path: inset(0 0 0 0);
transition: clip-path 0.3s cubic-bezier(0.22, 1, 0.36, 1);
}
.kodza-expanding-pill__toggle:hover .kodza-expanding-pill__lines::after {
clip-path: inset(0 45% 0 0);
}
.kodza-expanding-pill__toggle[aria-expanded="true"] .kodza-expanding-pill__lines::after {
clip-path: inset(0 0 0 0);
}
.kodza-expanding-pill__cross::before {
translate: 0 3px;
rotate: 45deg;
}
.kodza-expanding-pill__cross::after {
translate: 0 -3px;
rotate: -45deg;
}
/* Menu and Close share one cell: the button keeps the width of the longer word, and on the
card the word rolls from one to the other */
.kodza-expanding-pill__say {
display: grid;
margin: -3px 0;
padding: 3px 0;
overflow: hidden;
}
.kodza-expanding-pill__say > span {
grid-area: 1 / 1;
display: block;
}
.kodza-expanding-pill__toggle .kodza-expanding-pill__say > span + span {
visibility: hidden;
}
.kodza-expanding-pill__close .kodza-expanding-pill__say > span:first-child {
transform: translateY(-110%);
}
/* The menu: a layer over the page while it is open, the card placed on the button by the script */
.kodza-expanding-pill__panel {
position: fixed;
inset: 0;
z-index: 99990;
display: block;
box-sizing: border-box;
width: 100%;
height: 100%;
max-width: none;
max-height: none;
margin: 0;
padding: 0;
border: 0;
overflow: hidden;
background: transparent;
color: var(--kz-expanding-pill-fg);
font: inherit;
font-size: 15px;
line-height: 1.4;
text-align: left;
-webkit-font-smoothing: antialiased;
}
.kodza-expanding-pill__panel[hidden] {
display: none;
}
.kodza-expanding-pill__panel:focus {
outline: none;
}
/* A click beside the card closes it */
.kodza-expanding-pill__shade {
position: absolute;
inset: 0;
background: var(--kz-expanding-pill-shade);
}
/* The shadow lives outside the card, so it follows the card's growing shape */
.kodza-expanding-pill__float {
position: absolute;
top: 12px;
right: 12px;
width: min(var(--kz-expanding-pill-width), calc(100% - 24px));
filter: drop-shadow(0 24px 48px rgba(0, 0, 0, 0.22));
}
.kodza-expanding-pill__card {
position: relative;
display: flex;
flex-direction: column;
box-sizing: border-box;
padding: var(--kz-expanding-pill-inset) 0;
border-radius: calc(20px + var(--kz-expanding-pill-inset));
background: var(--kz-expanding-pill-bg);
color: var(--kz-expanding-pill-fg);
overflow: hidden;
}
.kodza-expanding-pill__label,
.kodza-expanding-pill__index {
font-family: "Geist Mono", ui-monospace, "SF Mono", Menlo, monospace;
font-size: 12px;
font-weight: 400;
line-height: 1.4;
letter-spacing: 0;
color: var(--kz-expanding-pill-muted);
}
/* The label shares Close's row, on the other side of it */
.kodza-expanding-pill__label {
display: flex;
align-items: center;
flex: none;
min-height: 40px;
margin: 0;
padding: 0 20px;
}
.kodza-expanding-pill__card.is-start .kodza-expanding-pill__label {
justify-content: flex-end;
}
.kodza-expanding-pill__card.is-mid:not(.is-up) .kodza-expanding-pill__label {
visibility: hidden;
}
/* Growing upward: Close at the bottom, on the button, the label up top on its own */
.kodza-expanding-pill__dock {
display: none;
flex: none;
height: 40px;
}
.kodza-expanding-pill__card.is-up .kodza-expanding-pill__label {
justify-content: flex-start;
}
.kodza-expanding-pill__card.is-up .kodza-expanding-pill__dock {
display: block;
}
.kodza-expanding-pill__nav {
display: grid;
min-height: 0;
padding: 10px 20px 18px;
overflow: hidden auto;
overscroll-behavior: contain;
}
.kodza-expanding-pill__link {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
align-items: center;
gap: 16px;
padding: 3px 0;
color: inherit;
text-decoration: none;
border-radius: 4px;
transition: opacity 0.35s cubic-bezier(0.22, 1, 0.36, 1);
}
/* The mask has the size of its word, room for descenders included */
.kodza-expanding-pill__mask {
display: block;
margin: -0.06em 0 -0.22em;
padding: 0.06em 0 0.22em;
overflow: hidden;
font-size: var(--kz-expanding-pill-size);
}
.kodza-expanding-pill__word {
display: block;
font-size: var(--kz-expanding-pill-size);
font-weight: 500;
line-height: 1;
letter-spacing: -0.04em;
white-space: nowrap;
}
.kodza-expanding-pill__index {
transition: color 0.25s;
}
/* Pointed at: the other links step back, its number lights up */
.kodza-expanding-pill__nav:hover .kodza-expanding-pill__link:not(:hover),
.kodza-expanding-pill__nav:has(.kodza-expanding-pill__link:focus-visible) .kodza-expanding-pill__link:not(:focus-visible) {
opacity: 0.35;
}
.kodza-expanding-pill__link:hover .kodza-expanding-pill__index,
.kodza-expanding-pill__link:focus-visible .kodza-expanding-pill__index {
color: var(--kz-expanding-pill-fg);
}
.kodza-expanding-pill__foot {
display: flex;
flex: none;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 8px 16px;
margin: 0 20px;
padding: 14px 0 12px;
border-top: 1px solid color-mix(in srgb, var(--kz-expanding-pill-fg) 14%, transparent);
font-size: 13px;
color: var(--kz-expanding-pill-muted);
}
.kodza-expanding-pill__foot a {
color: var(--kz-expanding-pill-fg);
text-decoration: none;
border-radius: 2px;
}
.kodza-expanding-pill__foot a:hover {
text-decoration: underline;
text-decoration-thickness: 1px;
text-underline-offset: 3px;
}
.kodza-expanding-pill__socials {
display: flex;
gap: 14px;
margin: 0;
}
@media (prefers-reduced-motion: reduce) {
.kodza-expanding-pill__toggle *,
.kodza-expanding-pill__toggle *::before,
.kodza-expanding-pill__toggle *::after,
.kodza-expanding-pill__close,
.kodza-expanding-pill__link,
.kodza-expanding-pill__index {
transition-duration: 0.01ms !important;
transition-delay: 0s !important;
}
}
/* Dark site: a paper pill, and a paper card
.kodza-expanding-pill {
--kz-expanding-pill-bg: #f2f2f0;
--kz-expanding-pill-fg: #0a0a0a;
--kz-expanding-pill-muted: rgba(10, 10, 10, 0.62);
--kz-expanding-pill-shade: rgba(0, 0, 0, 0.5);
}
*/
/* kodza-guard 92c53261613b — generated by Kodza, do not edit.
Theme guard: keeps a site's own CSS (WordPress themes, Bootstrap, CSS resets…) off this
component. :not(#_) is always true and weighs as much as an id, so these lines win over
theme rules made of classes and tags. Customise the component through its --kz-* variables. */
/* What the browser gives, where the component sets it only in some states: at the lowest weight, so its own rules below put it back */
:where(.kodza-expanding-pill__toggle):not(#_),
:where(.kodza-expanding-pill__close):not(#_) {
outline: revert;
outline-offset: revert;
}
:where(.kodza-expanding-pill__panel):not(#_),
:where(.kodza-expanding-pill__link):not(#_) {
outline: revert;
}
:where(.kodza-expanding-pill__foot a:where(:not([class*="kodza-"]))):not(#_),
:where(.kodza-expanding-pill__socials a:where(:not([class*="kodza-"]))):not(#_) {
outline: revert;
outline-offset: revert;
text-underline-offset: revert;
}
/* What the browser gives, on everything the component leaves to it */
.kodza-expanding-pill:not(#_),
.kodza-expanding-pill__lines:not(#_),
.kodza-expanding-pill__say:not(#_),
.kodza-expanding-pill__shade:not(#_),
.kodza-expanding-pill__float:not(#_),
.kodza-expanding-pill__cross:not(#_),
.kodza-expanding-pill__mask:not(#_),
.kodza-expanding-pill__word:not(#_),
.kodza-expanding-pill__index:not(#_),
.kodza-expanding-pill__foot:not(#_),
.kodza-expanding-pill__dock:not(#_),
.kodza-expanding-pill__say span:where(:not([class*="kodza-"])):not(#_) {
box-sizing: revert;
}
.kodza-expanding-pill__toggle:not(#_) {
text-decoration: revert;
transition: revert;
list-style: revert;
box-sizing: revert;
float: revert;
position: revert;
box-shadow: revert;
appearance: revert;
max-width: revert;
max-height: revert;
text-transform: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
min-height: revert;
}
.kodza-expanding-pill__panel:not(#_) {
box-shadow: revert;
outline-offset: revert;
}
.kodza-expanding-pill__close:not(#_) {
text-decoration: revert;
list-style: revert;
float: revert;
box-shadow: revert;
appearance: revert;
max-width: revert;
max-height: revert;
text-transform: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
min-height: revert;
}
.kodza-expanding-pill__label:not(#_) {
border: revert;
border-radius: revert;
outline: revert;
text-decoration: revert;
transition: revert;
list-style: revert;
box-sizing: revert;
float: revert;
position: revert;
background-color: revert;
background-image: revert;
box-shadow: revert;
outline-offset: revert;
appearance: revert;
max-width: revert;
max-height: revert;
font-style: revert;
text-transform: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
}
.kodza-expanding-pill__nav:not(#_) {
border: revert;
margin: revert;
border-radius: revert;
outline: revert;
text-decoration: revert;
transition: revert;
list-style: revert;
box-sizing: revert;
float: revert;
position: revert;
background-color: revert;
background-image: revert;
box-shadow: revert;
outline-offset: revert;
appearance: revert;
max-width: revert;
max-height: revert;
color: revert;
font-family: revert;
font-size: revert;
font-weight: revert;
font-style: revert;
line-height: revert;
letter-spacing: revert;
text-transform: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
}
.kodza-expanding-pill__link:not(#_) {
border: revert;
margin: revert;
list-style: revert;
box-sizing: revert;
float: revert;
position: revert;
background-color: revert;
background-image: revert;
box-shadow: revert;
outline-offset: revert;
appearance: revert;
max-width: revert;
max-height: revert;
font-family: revert;
font-size: revert;
font-weight: revert;
font-style: revert;
line-height: revert;
letter-spacing: revert;
text-transform: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
}
.kodza-expanding-pill__socials:not(#_) {
border: revert;
padding: revert;
border-radius: revert;
outline: revert;
text-decoration: revert;
transition: revert;
list-style: revert;
box-sizing: revert;
float: revert;
position: revert;
background-color: revert;
background-image: revert;
box-shadow: revert;
outline-offset: revert;
appearance: revert;
max-width: revert;
max-height: revert;
color: revert;
font-family: revert;
font-size: revert;
font-weight: revert;
font-style: revert;
line-height: revert;
letter-spacing: revert;
text-transform: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
}
.kodza-expanding-pill__foot a:where(:not([class*="kodza-"])):not(#_),
.kodza-expanding-pill__socials a:where(:not([class*="kodza-"])):not(#_) {
border: revert;
margin: revert;
padding: revert;
transition: revert;
list-style: revert;
box-sizing: revert;
float: revert;
position: revert;
background-color: revert;
background-image: revert;
box-shadow: revert;
appearance: revert;
max-width: revert;
max-height: revert;
font-family: revert;
font-size: revert;
font-weight: revert;
font-style: revert;
line-height: revert;
letter-spacing: revert;
text-transform: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
}
.kodza-expanding-pill__foot a:where(:not([class*="kodza-"])):not(#_):where(:not([hidden])),
.kodza-expanding-pill__socials a:where(:not([class*="kodza-"])):not(#_):where(:not([hidden])) {
display: revert;
}
.kodza-expanding-pill__foot a:where(:not([class*="kodza-"])):not(#_):where([hidden]),
.kodza-expanding-pill__socials a:where(:not([class*="kodza-"])):not(#_):where([hidden]) {
display: none;
}
.kodza-expanding-pill__lines:not(#_)::before,
.kodza-expanding-pill__lines:not(#_)::after,
.kodza-expanding-pill__cross:not(#_)::before,
.kodza-expanding-pill__cross:not(#_)::after {
box-sizing: revert;
}
/* The component's own rules, one step stronger: real buttons and fields, what it sets on tags themes style (links, lists, headings, images…), and the root width (a theme may narrow it, never widen it) */
.kodza-expanding-pill__toggle:not(#_),
.kodza-expanding-pill__close:not(#_) {
display: inline-flex;
align-items: center;
gap: 10px;
height: 40px;
margin: 0;
padding: 0 18px 0 16px;
border: 0;
border-radius: 999px;
font: inherit;
font-size: 15px;
font-weight: 500;
line-height: 1;
letter-spacing: -0.01em;
white-space: nowrap;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
}
.kodza-expanding-pill__toggle:not(#_) {
background: var(--kz-expanding-pill-bg);
color: var(--kz-expanding-pill-fg);
}
.kodza-expanding-pill__close:not(#_) {
position: absolute;
top: var(--kz-expanding-pill-inset);
right: var(--kz-expanding-pill-inset);
z-index: 1;
box-sizing: border-box;
background: color-mix(in srgb, var(--kz-expanding-pill-fg) 12%, transparent);
color: var(--kz-expanding-pill-fg);
transition: background-color 0.25s;
}
.kodza-expanding-pill__close:hover:not(#_) {
background: color-mix(in srgb, var(--kz-expanding-pill-fg) 18%, transparent);
}
.kodza-expanding-pill__toggle:focus-visible:not(#_) {
outline: 2px solid var(--kz-expanding-pill-bg);
outline-offset: 3px;
}
.kodza-expanding-pill__close:focus-visible:not(#_) {
outline: 2px solid var(--kz-expanding-pill-fg);
outline-offset: 3px;
}
.kodza-expanding-pill__foot a:focus-visible:not(#_) {
outline: 2px solid var(--kz-expanding-pill-fg);
outline-offset: 3px;
}
.kodza-expanding-pill__link:focus-visible:not(#_) {
outline: none;
}
.kodza-expanding-pill__panel:focus:not(#_) {
outline: none;
}
.kodza-expanding-pill__label:not(#_) {
font-family: "Geist Mono", ui-monospace, "SF Mono", Menlo, monospace;
font-size: 12px;
font-weight: 400;
line-height: 1.4;
letter-spacing: 0;
color: var(--kz-expanding-pill-muted);
}
.kodza-expanding-pill__label:not(#_) {
display: flex;
margin: 0;
padding: 0 20px;
}
.kodza-expanding-pill__nav:not(#_) {
display: grid;
padding: 10px 20px 18px;
}
.kodza-expanding-pill__link:not(#_) {
display: grid;
padding: 3px 0;
color: inherit;
text-decoration: none;
border-radius: 4px;
transition: opacity 0.35s cubic-bezier(0.22, 1, 0.36, 1);
}
.kodza-expanding-pill__foot a:not(#_) {
color: var(--kz-expanding-pill-fg);
text-decoration: none;
border-radius: 2px;
}
.kodza-expanding-pill__foot a:hover:not(#_) {
text-decoration: underline;
text-decoration-thickness: 1px;
text-underline-offset: 3px;
}
.kodza-expanding-pill__socials:not(#_) {
display: flex;
margin: 0;
}
@media (prefers-reduced-motion: reduce) {
.kodza-expanding-pill__close:not(#_) {
transition-duration: 0.01ms !important;
transition-delay: 0s !important;
}
}
@media (prefers-reduced-motion: reduce) {
.kodza-expanding-pill__link:not(#_) {
transition-duration: 0.01ms !important;
transition-delay: 0s !important;
}
}
// Kodza License: free to use, client work included. Not to resell or redistribute, ported or not: https://kodza.dev/docs/#print-license
// Kodza — Expanding Pill Menu
// The Menu button grows into a card around itself: it widens first, then unfolds towards the
// middle of the screen, and its corners go from the pill's to the card's without ever stretching
// the text. Close is the button, in the same place, its word rolling from Menu to Close; each
// link rises from its mask as the card's edge uncovers it, a click beside the card closes it, and
// closing folds it all back into the button, from wherever it is.
(() => {
// The two lines that change from one menu of the family to the next
const NAME = 'expanding-pill';
const VARS = ['bg', 'fg', 'muted', 'shade', 'width', 'size', 'inset'];
// ---------- Menu engine (begin) ----------
// The same block in every burger menu of the family (Trapdoor, Fullscreen, Side Reveal...),
// copied as is. .kodza-NAME__toggle opens .kodza-NAME__panel and the menu's opening plays as
// one timeline, backwards to close, from wherever it is. While open the panel leaves the menu
// with its variables (VARS), the page stays put (wheel, swipe, keys), Tab stays inside, Escape
// closes and the focus goes back to the button. A page that takes part (data-NAME-page,
// data-page="…" on the menu, or a theme's wrapper) takes its fixed elements along and is put
// back exactly as it was. Each menu only writes how it moves: see `menu`.
const SELECTOR = `.kodza-${NAME}`;
const motion = window.matchMedia('(prefers-reduced-motion: reduce)');
const RISE = 'cubic-bezier(0.22, 1, 0.36, 1)';
const SWING = 'cubic-bezier(0.65, 0, 0.35, 1)';
const BACK = 1.6; // closing replays the opening backwards, this much faster
const FOCUSABLE = 'a[href], button:not([disabled]), input, select, textarea, [tabindex]:not([tabindex="-1"])';
const taken = new WeakMap(); // page → how to put it back at once (one menu moves a page at a time)
const transparent = (color) => !color || color === 'transparent' || /^rgba\(.*,\s*0\)$/.test(color);
// [time, progress] pairs along a cubic-bezier(): when does the edge pass a given row?
const curve = (easing) => {
const n = (easing.match(/-?\d*\.?\d+/g) || []).map(Number);
const [x1, y1, x2, y2] = n.length === 4 ? n : [0.76, 0, 0.24, 1];
const at = (s, a, b) => 3 * (1 - s) * (1 - s) * s * a + 3 * (1 - s) * s * s * b + s * s * s;
return Array.from({ length: 101 }, (_, i) => [at(i / 100, x1, x2), at(i / 100, y1, y2)]);
};
// A transform makes the page the containing block of its fixed elements: they are found
// first, then moved back to where they were on screen (see `rideAlong`)
const fixedInside = (page) => {
const found = [];
const walker = document.createTreeWalker(page, NodeFilter.SHOW_ELEMENT, {
acceptNode: (el) => {
const style = getComputedStyle(el);
if (style.display === 'none') return NodeFilter.FILTER_REJECT;
if (style.position !== 'fixed') return NodeFilter.FILTER_SKIP;
found.push({ el, box: el.getBoundingClientRect(), shift: style.translate, inline: el.style.translate, attr: el.hasAttribute('style') });
return NodeFilter.FILTER_REJECT;
},
});
while (walker.nextNode()) {
// acceptNode collects them
}
return found;
};
const rideAlong = (fixed, k) => fixed.forEach((item) => {
const now = item.el.getBoundingClientRect();
const [x = 0, y = 0] = item.shift === 'none' ? [] : item.shift.split(' ').map(parseFloat);
item.el.style.translate = `${x + (item.box.left - now.left) / k}px ${y + (item.box.top - now.top) / k}px`;
});
const letGo = (fixed) => fixed.forEach(({ el, inline, attr }) => {
el.style.translate = inline;
if (!attr && !el.getAttribute('style')) el.removeAttribute('style');
});
// Where the page is, in screen pixels: its box, its scale (a scaled parent changes nothing
// once divided by `k`), and the part of it that is on screen
const view = (page) => {
const r = page.getBoundingClientRect();
const { clientWidth: vw, clientHeight: vh } = document.documentElement;
return {
r,
k: page.offsetWidth ? r.width / page.offsetWidth : 1,
top: Math.max(r.top, 0),
bottom: Math.min(r.bottom, vh),
left: Math.max(r.left, 0),
right: Math.min(r.right, vw),
};
};
// One menu. `setup(m)` receives its parts (toggle, panel, closer, links, one, all, css, cut, and
// while open page, reduced) and returns what the menu does itself:
// page 'under' when the menu lies under the page that moves, 'over' when it covers the
// page that moves under it; without it the page stays as it is
// rig(page) sets the flat page up for the ride, returns its geometry with `k` (its scale);
// again on resize
// frames(g) the page's keyframes, set again on resize
// build(add, mode, g) the opening, add(el, keyframes, delay, duration, easing, options) per
// animation; mode is 'page', 'flat' (no page found) or 'reduced'
// ready(), entry(), key(event), resize(), reset() optional: before it opens, where the
// keyboard enters, a key of its own (true once handled), after a resize, once closed
const menu = (root, signal, setup) => {
const toggle = root.querySelector(`.kodza-${NAME}__toggle`);
const panel = root.querySelector(`.kodza-${NAME}__panel`);
if (!toggle || !panel) return;
const one = (s) => panel.querySelector(s);
const all = (s) => [...panel.querySelectorAll(s)];
const closer = one(`.kodza-${NAME}__close`);
const links = all(`.kodza-${NAME}__link`);
const home = [panel.parentNode, panel.nextSibling];
const css = (name) => getComputedStyle(root).getPropertyValue(`--kz-${NAME}-${name}`).trim();
let state = 'closed'; // closed, opening, open, closing
let sequence = []; // the opening: animations on one clock, so it can run backwards
let session = null; // what only lives while the menu is open
let moving = null; // the page taking part, and what it looked like before
let reduced = false;
let run = 0;
let then = null;
// The page: named on the menu (data-page="…"), or the marked wrapper around it, or else
// the wrapper of a WordPress theme (block themes, Astra, GeneratePress, Kadence, OceanWP,
// Blocksy, Divi…). Without one, the menu plays its sequence without the page
const findPage = () => {
let el = null;
try {
el = root.dataset.page ? document.querySelector(root.dataset.page) : null;
} catch {
el = null;
}
el = el || root.closest(`[data-${NAME}-page]`) || document.querySelector(`[data-${NAME}-page]`) || root.closest('.wp-site-blocks, #page, #outer-wrap, #main-container, #page-container');
if (!el || el === document.body || el === document.documentElement) return null;
// It has to fill the screen (below the WordPress admin bar): otherwise part of the site, a
// header outside it or the space under a short page, would be left out of the ride
const r = el.getBoundingClientRect();
const bar = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--wp-admin--admin-bar--height')) || 0;
const { clientWidth: vw, clientHeight: vh } = document.documentElement;
return r.top <= bar + 1 && r.left <= 1 && r.right >= vw - 1 && r.bottom >= vh - 1 ? el : null;
};
// The colour under the page's content, so the menu never shows through it
const ground = (page) => {
const own = css('page');
if (own && own !== 'auto') return own;
for (let el = page.parentElement; el; el = el.parentElement) {
const color = getComputedStyle(el).backgroundColor;
if (!transparent(color)) return color;
}
return '#ffffff';
};
// A flex or grid page shares its height out among its children: for the ride their sizes
// are held as they are, so a shorter page box changes nothing inside
const hold = (page, style) => {
if (style.display.includes('grid')) {
page.style.gridTemplateRows = style.gridTemplateRows;
page.style.gridTemplateColumns = style.gridTemplateColumns;
} else if (style.display.includes('flex')) {
const column = style.flexDirection.startsWith('column');
moving.held = [...page.children].map((el) => {
const own = getComputedStyle(el);
const item = { el, flex: el.style.flex, attr: el.hasAttribute('style') };
el.style.flex = `0 0 ${column ? own.height : own.width}`;
return item;
});
}
};
const release = () => {
moving.held.forEach(({ el, flex, attr }) => {
el.style.flex = flex;
if (!attr && !el.getAttribute('style')) el.removeAttribute('style');
});
moving.held = [];
Object.assign(moving.page.style, moving.cut);
};
// The page's box ends at `at` (its own pixels from its top) for the ride, the space it
// leaves kept as margin: what moves below it would stretch the scroll area. Checked on the
// spot: if that moves anything inside, even with the children held, the page is left as it
// was. Returns where the box ends
const cut = (at) => {
const { page } = moving;
const full = page.offsetHeight;
if (at >= full) return full;
const style = getComputedStyle(page);
const edges = style.boxSizing === 'border-box' ? 0 : ['paddingTop', 'paddingBottom', 'borderTopWidth', 'borderBottomWidth'].reduce((sum, side) => sum + parseFloat(style[side]), 0);
const margin = parseFloat(style.marginBottom) || 0;
const boxes = (els) => els.map((el) => el.getBoundingClientRect()).map((r) => [r.top, r.left, r.width, r.height]);
const kids = [...page.children];
const before = boxes(kids);
const shorten = () => {
page.style.height = `${Math.max(at - edges, 0)}px`;
page.style.overflowY = 'clip';
page.style.marginBottom = `${margin + full - at}px`;
return boxes(kids).some((box, i) => box.some((n, j) => Math.abs(n - before[i][j]) > 0.5));
};
let moved = shorten();
if (moved && /flex|grid/.test(style.display)) {
release();
hold(page, style);
moved = shorten();
}
if (moved) release();
return moved ? full : at;
};
const m = {
root, toggle, panel, closer, links, one, all, css, cut,
get page() {
return moving ? moving.page : null;
},
get reduced() {
return reduced;
},
};
const own = setup(m);
// With the page untransformed: where its fixed elements are, then the menu's own geometry
const rig = () => {
letGo(moving.fixed);
release();
moving.fixed = fixedInside(moving.page);
return own.rig(moving.page);
};
// The opening, as one timeline. Every animation gets an end delay that makes it as long as
// the longest, so running them all backwards is the exact reverse, even halfway through.
const timeline = (mode, g) => {
const steps = [];
const add = (el, frames, delay, duration, easing = RISE, extra = {}) => {
if (el) steps.push([el, frames, { delay, duration, easing, ...extra }]);
};
own.build(add, mode, g);
const total = Math.max(...steps.map(([, , o]) => o.delay + o.duration));
return steps.map(([el, frames, o]) => el.animate(frames, { ...o, fill: 'both', endDelay: total - o.delay - o.duration }));
};
const play = (forward) => {
const token = ++run;
sequence.forEach((animation) => {
animation.updatePlaybackRate(forward ? 1 : -BACK);
animation.play();
});
Promise.all(sequence.map((animation) => animation.finished)).then(() => {
if (token !== run) return;
if (forward) state = 'open';
else land();
}, () => {});
};
// Closed again: the animations, the borrowed styles and the listeners go, the panel goes home
const land = () => {
run++;
sequence.forEach((animation) => animation.cancel());
panel.getAnimations({ subtree: true }).forEach((animation) => animation.cancel());
sequence = [];
session?.abort();
session = null;
if (moving) {
const { page, saved, attr } = moving;
letGo(moving.fixed);
release();
Object.assign(page.style, saved);
if (!attr && !page.getAttribute('style')) page.removeAttribute('style');
if (taken.get(page) === land) taken.delete(page);
moving = null;
}
try {
if (panel.popover && panel.matches(':popover-open')) panel.hidePopover();
} catch {
// no popover support: it was a plain fixed layer
}
panel.removeAttribute('popover');
panel.removeAttribute('style');
panel.hidden = true;
if (panel.parentNode !== home[0]) home[0].insertBefore(panel, home[1]?.parentNode === home[0] ? home[1] : null);
own.reset?.();
state = 'closed';
const next = then;
then = null;
next?.();
};
// While open the page stays put: a wheel or a swipe only scrolls what scrolls in the menu
const scrollsInside = (target, x, y, dy) => {
for (let el = target; el instanceof Element && panel.contains(el); el = el.parentElement) {
const box = el.getBoundingClientRect();
if (x < box.left || x > box.right || y < box.top || y > box.bottom) continue;
if (!/(auto|scroll)/.test(getComputedStyle(el).overflowY) || el.scrollHeight <= el.clientHeight + 1) continue;
if (dy > 0 ? el.scrollTop + el.clientHeight < el.scrollHeight - 1 : el.scrollTop > 0) return true;
}
return false;
};
// The links you can reach now (a menu with levels hides the others)
const rows = () => links.filter((link) => link.getClientRects().length && !link.closest('[inert]'));
const keydown = (event) => {
if (state === 'closing' || event.defaultPrevented) return;
if (own.key?.(event)) return;
if (event.key === 'Escape') {
event.preventDefault();
shut();
return;
}
const current = document.activeElement;
if (event.key === 'Tab') {
const stops = all(FOCUSABLE).filter((el) => el.getClientRects().length && !el.closest('[inert]'));
const first = stops[0];
const last = stops[stops.length - 1];
const outside = !panel.contains(current) || current === panel;
if (outside || (event.shiftKey && current === first) || (!event.shiftKey && current === last)) {
event.preventDefault();
(event.shiftKey ? last : first)?.focus();
}
return;
}
const list = rows();
const i = list.indexOf(current);
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
event.preventDefault();
const step = event.key === 'ArrowDown' ? 1 : -1;
list[i < 0 ? (step > 0 ? 0 : list.length - 1) : (i + step + list.length) % list.length]?.focus();
} else if (event.key === 'Home' || event.key === 'End') {
event.preventDefault();
list[event.key === 'Home' ? 0 : list.length - 1]?.focus();
} else if ([' ', 'PageUp', 'PageDown'].includes(event.key) && !current?.closest?.('button, input, select, textarea')) {
event.preventDefault();
}
};
const listen = (s) => {
let last = 0;
const at = [scrollX, scrollY];
document.addEventListener('wheel', (event) => {
if (!scrollsInside(event.target, event.clientX, event.clientY, event.deltaY)) event.preventDefault();
}, { passive: false, signal: s });
document.addEventListener('touchstart', (event) => (last = event.touches[0].clientY), { passive: true, signal: s });
document.addEventListener('touchmove', (event) => {
const touch = event.touches[0];
const dy = last - touch.clientY;
last = touch.clientY;
if (!scrollsInside(event.target, touch.clientX, touch.clientY, dy)) event.preventDefault();
}, { passive: false, signal: s });
document.addEventListener('keydown', keydown, { signal: s });
// A scrollbar dragged anyway: the page goes back to where it was
window.addEventListener('scroll', () => {
if (scrollX !== at[0] || scrollY !== at[1]) window.scrollTo({ left: at[0], top: at[1], behavior: 'instant' });
}, { signal: s });
let queued = 0;
window.addEventListener('resize', () => {
if (queued) return;
queued = requestAnimationFrame(() => {
queued = 0;
if (s.aborted) return;
if (moving) {
const effect = sequence.find((animation) => animation.effect.target === moving.page)?.effect;
effect?.setKeyframes([]);
const g = rig();
effect?.setKeyframes(own.frames(g));
rideAlong(moving.fixed, g.k);
}
own.resize?.();
});
}, { signal: s });
s.addEventListener('abort', () => cancelAnimationFrame(queued));
// A click on the page that moved aside brings it back
moving?.page.addEventListener('click', (event) => {
if (state === 'closing') return;
event.preventDefault();
event.stopPropagation();
shut();
}, { capture: true, signal: s });
};
const enter = (keyboard) => {
const target = keyboard ? own.entry?.() || links.find((link) => link.getAttribute('aria-current')) || rows()[0] || closer : panel;
target?.focus({ preventScroll: true });
};
const open = (keyboard) => {
if (state === 'opening' || state === 'open') return;
toggle.setAttribute('aria-expanded', 'true');
if (state === 'closing') {
state = 'opening';
then = null;
panel.style.pointerEvents = '';
play(true);
enter(keyboard);
return;
}
reduced = motion.matches;
const page = reduced || !own.page ? null : findPage();
const under = page && own.page === 'under';
let g = null;
if (page) {
taken.get(page)?.();
const { transformOrigin, backgroundColor, position, zIndex, height, overflowY, marginBottom, gridTemplateRows, gridTemplateColumns } = page.style;
moving = { page, saved: { transformOrigin, backgroundColor, position, zIndex }, cut: { height, overflowY, marginBottom, gridTemplateRows, gridTemplateColumns }, attr: page.hasAttribute('style'), fixed: [], held: [] };
taken.set(page, land);
g = rig();
const shown = getComputedStyle(page);
if (transparent(shown.backgroundColor)) page.style.backgroundColor = ground(page);
// Under the page: page over panel over the rest of the theme (a header, a sticky bar),
// under the admin bar
const still = shown.position === 'static';
if (under && (!still || ['top', 'right', 'bottom', 'left'].every((side) => shown[side] === 'auto'))) {
if (still) page.style.position = 'relative';
page.style.zIndex = '99991';
panel.style.zIndex = '99990';
}
}
// The panel leaves the menu while open, so the styles of a theme's content (links,
// paragraphs) never reach it: right under the page, or on top of everything
VARS.forEach((name) => panel.style.setProperty(`--kz-${NAME}-${name}`, css(name)));
panel.style.fontFamily = getComputedStyle(root).fontFamily;
if (under) page.before(panel);
else document.body.append(panel);
panel.hidden = false;
if (!under) {
try {
panel.popover = 'manual';
panel.showPopover();
} catch {
// no popover support: a fixed layer over the page
}
}
own.ready?.();
sequence = timeline(page ? 'page' : reduced ? 'reduced' : 'flat', g);
if (moving) rideAlong(moving.fixed, g.k);
session = new AbortController();
listen(session.signal);
state = 'opening';
play(true);
enter(keyboard);
};
const shut = (after) => {
if (state === 'closed' || state === 'closing') return;
then = after || null;
state = 'closing';
toggle.setAttribute('aria-expanded', 'false');
toggle.focus({ preventScroll: true });
// On its way out it lets clicks through: Menu, under it, opens it again from where it is
panel.style.pointerEvents = 'none';
play(false);
};
// Keyboard (Enter, Space) clicks have no detail: the focus then goes into the menu
toggle.addEventListener('click', (event) => open(event.detail === 0), { signal });
closer?.addEventListener('click', () => shut(), { signal });
// A link to this very page (#section, or just #) closes the menu first, then goes there
links.forEach((link) => link.addEventListener('click', (event) => {
const here = link.origin === location.origin && link.pathname === location.pathname && link.search === location.search;
if (!here || event.defaultPrevented || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
event.preventDefault();
const hash = link.hash;
shut(hash ? () => (location.hash = hash) : null);
}, { signal }));
// Back from another page (bfcache): the menu is closed, the page as it was
window.addEventListener('pageshow', (event) => event.persisted && state !== 'closed' && land(), { signal });
// Stopped while open: the page is put back at once and the panel goes back in place
signal.addEventListener('abort', () => {
then = null;
if (state !== 'closed') land();
});
};
// ---------- Menu engine (end) ----------
function mount(root, signal) {
menu(root, signal, (m) => {
const { panel, toggle, closer, links, one, all, css } = m;
const shade = one('.kodza-expanding-pill__shade');
const float = one('.kodza-expanding-pill__float');
const card = one('.kodza-expanding-pill__card');
const cross = one('.kodza-expanding-pill__cross');
const label = one('.kodza-expanding-pill__label');
const foot = all('.kodza-expanding-pill__foot > *');
const lines = toggle.querySelector('.kodza-expanding-pill__lines');
const [was, now] = closer ? closer.querySelectorAll('.kodza-expanding-pill__say > span') : [];
const EDGE = 12; // the card keeps this far from the edges of the screen
const WIDE = [0, 0.7]; // the part of the opening the card widens in
const TALL = [0.14, 1]; // and the part it unfolds in
let box = null; // the card, and the button inside it
// The card on the button: it grows from the button towards the middle of the screen
// (downward from the top half, upward from the bottom half), and Close lands exactly on it
const place = () => {
const { clientWidth: vw, clientHeight: vh } = document.documentElement;
const b = toggle.getBoundingClientRect();
const inset = parseFloat(css('inset')) || 0;
const w = Math.min(parseFloat(css('width')) || 360, vw - 2 * EDGE);
const middle = b.left + b.width / 2;
const side = middle < vw * 0.4 ? 'start' : middle > vw * 0.6 ? 'end' : 'mid';
const up = b.top + b.height / 2 > vh / 2;
card.classList.toggle('is-up', up);
card.classList.toggle('is-start', side === 'start');
card.classList.toggle('is-mid', side === 'mid');
const x = side === 'start' ? b.left - inset : side === 'end' ? b.right + inset - w : middle - w / 2;
const left = Math.min(Math.max(x, EDGE), vw - EDGE - w);
Object.assign(float.style, { width: `${w}px`, right: 'auto' });
card.style.maxHeight = `${Math.max(up ? b.bottom + inset - EDGE : vh - EDGE - b.top + inset, b.height + 2 * inset)}px`;
const h = card.offsetHeight;
const top = up ? b.bottom + inset - h : b.top - inset;
Object.assign(float.style, { left: `${left}px`, top: `${top}px` });
if (closer) Object.assign(closer.style, { left: `${b.left - left}px`, top: `${b.top - top}px`, right: 'auto', width: `${b.width}px`, height: `${b.height}px` });
box = { w, h, x: b.left - left, y: b.top - top, bw: b.width, bh: b.height, round: parseFloat(getComputedStyle(card).borderTopLeftRadius) || 0 };
};
// Where the card's edges are at a moment of the opening (0 to 1): it widens first, then
// unfolds, and its corners go from the pill's to the card's. Never a scale: the text inside
// is never stretched
const ease = () => curve(css('ease') || 'cubic-bezier(0.62, 0, 0.22, 1)');
const along = (samples, t) => {
if (t <= 0 || t >= 1) return t <= 0 ? 0 : 1;
const i = samples.findIndex(([s]) => s >= t);
const [[t0, p0], [t1, p1]] = [samples[i - 1], samples[i]];
return p0 + ((p1 - p0) * (t - t0)) / (t1 - t0 || 1);
};
const share = ([a, b], t) => Math.min(Math.max((t - a) / (b - a), 0), 1);
const edges = (samples, t) => {
const { w, h, x, y, bw, bh, round } = box;
const across = along(samples, share(WIDE, t));
const down = along(samples, share(TALL, t));
return {
top: y * (1 - down),
right: (w - x - bw) * (1 - across),
bottom: (h - y - bh) * (1 - down),
left: x * (1 - across),
round: bh / 2 + (round - bh / 2) * down,
};
};
const shape = (samples) => Array.from({ length: 41 }, (_, i) => {
const e = edges(samples, i / 40);
return { offset: i / 40, clipPath: `inset(${e.top}px ${e.right}px ${e.bottom}px ${e.left}px round ${e.round}px)` };
});
// When the edges have gone past a piece of the card (its text, not its whole row)
const rect = (el) => {
if (el !== label) return el.getBoundingClientRect();
const range = document.createRange();
range.selectNodeContents(el);
return range.getBoundingClientRect();
};
const reach = (el, samples, time) => {
const c = card.getBoundingClientRect();
const r = rect(el);
for (let i = 0; i <= 100; i++) {
const e = edges(samples, i / 100);
if (e.top <= r.top - c.top + 1 && e.left <= r.left - c.left + 1 && box.h - e.bottom >= r.bottom - c.top - 1 && box.w - e.right >= r.right - c.left - 1) return (i / 100) * time;
}
return time;
};
// Close shows the button's lines, then crosses them
const turn = (el, sign) => {
const pseudoElement = sign > 0 ? '::before' : '::after';
return (add) => {
add(el, [{ translate: '0 0' }, { translate: `0 ${3 * sign}px` }], 0, 200, SWING, { pseudoElement });
add(el, [{ rotate: '0deg' }, { rotate: `${45 * sign}deg` }], 170, 300, RISE, { pseudoElement });
};
};
const build = (add, mode) => {
if (mode === 'reduced') {
add(panel, [{ opacity: 0 }, { opacity: 1 }], 0, 180, 'ease');
return;
}
const time = (parseFloat(css('duration')) || 0.8) * 1000;
const samples = ease();
add(card, shape(samples), 0, time, 'linear');
const shadow = getComputedStyle(float).filter;
if (shadow && shadow !== 'none') add(float, [{ filter: 'drop-shadow(rgba(0, 0, 0, 0) 0px 0px 0px)' }, { filter: shadow }], 0, time, css('ease') || SWING);
add(shade, [{ opacity: 0 }, { opacity: 1 }], 0, time * 0.6);
// Close starts as the button, as it was under the pointer, then takes its own ground
if (closer) {
add(closer, [{ backgroundColor: 'transparent' }, { backgroundColor: getComputedStyle(closer).backgroundColor }], time * 0.45, time * 0.55, 'ease');
[turn(cross, 1), turn(cross, -1)].forEach((go) => go(add));
const short = lines ? getComputedStyle(lines, '::after').clipPath : 'none';
if (short && short !== 'none') add(cross, [{ clipPath: short }, { clipPath: 'inset(0px)' }], 0, 200, RISE, { pseudoElement: '::after' });
add(was, [{ transform: 'translateY(0%)' }, { transform: 'translateY(-110%)' }], 60, 420, SWING);
add(now, [{ transform: 'translateY(110%)' }, { transform: 'translateY(0%)' }], 160, 600);
}
if (label) add(label, [{ opacity: 0, transform: 'translateY(6px)' }, { opacity: 1, transform: 'translateY(0px)' }], reach(label, samples, time) + 40, 420);
// Each link rises from its mask once the edges are past it, in the order they get there
// (bottom first when the card grows upward). Short tails: closing plays them first, backwards
let at = -Infinity;
links
.map((link) => ({ link, mask: link.querySelector('.kodza-expanding-pill__mask') }))
.map((item) => ({ ...item, when: reach(item.mask || item.link, samples, time) }))
.sort((a, b) => a.when - b.when)
.forEach(({ link, when }) => {
at = Math.max(at + 60, when - 140, 0);
add(link.querySelector('.kodza-expanding-pill__word'), [{ transform: 'translateY(110%)' }, { transform: 'translateY(0%)' }], at, 600);
const index = link.querySelector('.kodza-expanding-pill__index');
if (index) add(index, [{ opacity: 0, transform: 'translateY(6px)' }, { opacity: 1, transform: 'translateY(0px)' }], Math.max(at + 160, reach(index, samples, time)), 420);
});
const up = card.classList.contains('is-up');
foot.forEach((el, i) => {
add(el, [{ opacity: 0, transform: 'translateY(8px)' }, { opacity: 1, transform: 'translateY(0px)' }], Math.max(reach(el, samples, time) + 40, up ? 0 : at + 100 + i * 50), 420);
});
};
// The screen changed size while open: the card goes back on the button
const resize = () => {
if (!box) return;
const effect = card.getAnimations().find((animation) => animation.effect?.getKeyframes()[0]?.clipPath)?.effect;
place();
effect?.setKeyframes(shape(ease()));
};
const reset = () => {
box = null;
[float, card, closer].forEach((el) => el?.removeAttribute('style'));
card.classList.remove('is-up', 'is-start', 'is-mid');
};
// A click beside the card closes it
shade?.addEventListener('click', () => closer?.click(), { signal });
return { build, ready: place, resize, reset };
});
}
// Kodza lifecycle, the same in every component: `kodza:init` starts the instances inside
// event.detail.root (the whole page by default), `kodza:destroy` stops them and puts their
// markup back. For themes and apps that swap content without a page load. Each running root
// keeps its markup as written under KODZA, shared by every Kodza script on the page: a Kodza
// component inside another one is saved as written, not half-started, and stopped with it.
const KODZA = Symbol.for('kodza');
const instances = new WeakMap();
const find = (scope = document) => [scope, ...scope.querySelectorAll(SELECTOR)].filter((el) => el.matches?.(SELECTOR));
const attrsOf = (el) => [...el.attributes].map((a) => [a.name, a.value]);
const put = (el, saved) => {
[...el.attributes].forEach((a) => el.removeAttribute(a.name));
saved.attrs.forEach(([name, value]) => el.setAttribute(name, value));
el.innerHTML = saved.html;
};
// The markup inside a root as written: Kodza components already running in it give back their own
const written = (root) => {
const all = [...root.querySelectorAll('*')];
if (!all.some((el) => el[KODZA])) return root.innerHTML;
const copy = root.cloneNode(true);
const twins = copy.querySelectorAll('*');
let last = null;
all.forEach((el, i) => {
if (!el[KODZA] || last?.contains(el)) return;
last = el;
put(twins[i], el[KODZA]);
});
return copy.innerHTML;
};
const init = (scope) => find(scope).forEach((root) => {
if (root.classList.contains('is-ready')) return; // already running, maybe from another copy of this script
const controller = new AbortController();
const saved = { html: written(root), attrs: attrsOf(root), stop: () => destroy(root) };
instances.set(root, { controller, saved });
root[KODZA] = saved;
mount(root, controller.signal);
root.classList.add('is-ready');
});
const destroy = (scope) => find(scope).forEach((root) => {
const instance = instances.get(root);
if (!instance) return;
instances.delete(root);
// The Kodza components running inside it stop first: once its markup is put back, their own
// script could not find them any more
root.querySelectorAll('*').forEach((el) => el[KODZA]?.stop());
instance.controller.abort();
delete root[KODZA];
put(root, instance.saved);
});
document.addEventListener('kodza:init', (event) => init(event.detail?.root));
document.addEventListener('kodza:destroy', (event) => destroy(event.detail?.root));
init();
// Loaded in <head>, the script runs before the markup exists: once more when it is parsed
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', () => init(), { once: true });
})();
'use client';
// Kodza — Expanding Pill Menu, for React. From https://kodza.dev/components/expanding-pill-menu/
// Kodza License: free to use, client work included. Not to resell or redistribute, ported or not: https://kodza.dev/docs/#print-license
// Next to this file: expanding-pill-menu.css and expanding-pill-menu.js, the component's CSS and JS from Kodza, as they are.
// Yours to edit: the markup is plain JSX below.
// The script changes this markup once React has rendered it (it adds layers, splits text, moves
// nodes), so React must not patch it afterwards. When a prop changes, the key gives React a fresh
// element: kodza:destroy stops the script on the old one, kodza:init starts it on the new one.
// Built from your data (tabs, rows, options)? Put what the markup is made of in the key, as plain
// values (ids, text, numbers): a change starts the script again on fresh markup. What the visitor
// changes (the open tab, the sort, what they type) is the script's: keep it out of the key, follow
// it with onClick or onInput on the root, and change it from your code as a visitor would (click
// the tab; set the field's value, then dispatch a bubbling input event).
import { useRef, useEffect, type ComponentPropsWithoutRef, type ComponentRef, type CSSProperties } from 'react';
import './expanding-pill-menu.css';
export type ExpandingPillMenuProps = Omit<ComponentPropsWithoutRef<'div'>, "children" | "vars"> & {
/** Settings, as CSS variables on the root */
vars?: {
/** the button, and the card it becomes — default: #0a0a0a */
"--kz-expanding-pill-bg"?: string | number;
/** default: #f2f2f0 */
"--kz-expanding-pill-fg"?: string | number;
/** default: rgba(242, 242, 240, 0.62) */
"--kz-expanding-pill-muted"?: string | number;
/** over the page while it is open — default: rgba(10, 10, 10, 0.2) */
"--kz-expanding-pill-shade"?: string | number;
/** the card, never wider than the screen minus 12px a side — default: 360px */
"--kz-expanding-pill-width"?: string | number;
/** the links — default: 34px */
"--kz-expanding-pill-size"?: string | number;
/** from the card's edge to Close: the corners stay concentric — default: 6px */
"--kz-expanding-pill-inset"?: string | number;
/** default: 0.8s */
"--kz-expanding-pill-duration"?: string | number;
/** default: cubic-bezier(0.62, 0, 0.22, 1) */
"--kz-expanding-pill-ease"?: string | number;
};
className?: string;
};
export function ExpandingPillMenu({ vars, className, style, ...rest }: ExpandingPillMenuProps) {
const ref = useRef<ComponentRef<'div'>>(null);
const classes = ["kodza-expanding-pill", className].filter(Boolean).join(' ');
// Everything the markup is made of: when it changes, React gets a fresh element (see the top)
const key = JSON.stringify([classes, vars, style, rest]);
// Starts the script on this element once React has rendered it, stops it when it goes
useEffect(() => {
const root = ref.current;
let live = true;
// @ts-ignore: a plain script, no types to find
import('./expanding-pill-menu.js').then(() => {
if (live) document.dispatchEvent(new CustomEvent('kodza:init', { detail: { root } }));
});
return () => {
live = false;
document.dispatchEvent(new CustomEvent('kodza:destroy', { detail: { root } }));
};
}, [key]);
return (
<div key={key} {...rest} className={classes} style={{ ...vars, ...style } as CSSProperties} ref={ref}>
<button className="kodza-expanding-pill__toggle" type="button" aria-haspopup="dialog" aria-expanded="false">
<span className="kodza-expanding-pill__lines" aria-hidden="true" />
<span className="kodza-expanding-pill__say">
<span>Menu</span>
<span aria-hidden="true">Close</span>
</span>
</button>
<div className="kodza-expanding-pill__panel" role="dialog" aria-modal="true" aria-label="Menu" tabIndex={-1} hidden>
<span className="kodza-expanding-pill__shade" aria-hidden="true" />
<div className="kodza-expanding-pill__float">
<div className="kodza-expanding-pill__card">
<button className="kodza-expanding-pill__close" type="button">
<span className="kodza-expanding-pill__cross" aria-hidden="true" />
<span className="kodza-expanding-pill__say">
<span aria-hidden="true">Menu</span>
<span>Close</span>
</span>
</button>
<p className="kodza-expanding-pill__label">Five links. That's the site.</p>
<nav className="kodza-expanding-pill__nav" aria-label="Main">
<a className="kodza-expanding-pill__link" href="#">
<span className="kodza-expanding-pill__mask">
<span className="kodza-expanding-pill__word">Work</span>
</span>
<span className="kodza-expanding-pill__index">01</span>
</a>
{' '}
<a className="kodza-expanding-pill__link" href="#">
<span className="kodza-expanding-pill__mask">
<span className="kodza-expanding-pill__word">Studio</span>
</span>
<span className="kodza-expanding-pill__index">02</span>
</a>
{' '}
<a className="kodza-expanding-pill__link" href="#">
<span className="kodza-expanding-pill__mask">
<span className="kodza-expanding-pill__word">Process</span>
</span>
<span className="kodza-expanding-pill__index">03</span>
</a>
{' '}
<a className="kodza-expanding-pill__link" href="#">
<span className="kodza-expanding-pill__mask">
<span className="kodza-expanding-pill__word">Notes</span>
</span>
<span className="kodza-expanding-pill__index">04</span>
</a>
{' '}
<a className="kodza-expanding-pill__link" href="#">
<span className="kodza-expanding-pill__mask">
<span className="kodza-expanding-pill__word">Contact</span>
</span>
<span className="kodza-expanding-pill__index">05</span>
</a>
</nav>
<div className="kodza-expanding-pill__foot">
<a href="#">studio@example.com</a>
<p className="kodza-expanding-pill__socials">
<a href="#">GitHub</a>
<a href="#">X</a>
<a href="#">Bluesky</a>
</p>
</div>
<span className="kodza-expanding-pill__dock" aria-hidden="true" />
</div>
</div>
</div>
</div>
);
}
Checked
Robots poked at it before you did. Every component goes through the same checks, and again each time it changes.