OTP Input
A one-time code field: one real input drawn as cells. Digits rise into place, paste and SMS autofill just work, and the cells turn to ink once complete.
View codeA real select, dressed up: the list unrolls from the field, a highlight slides between options and the value rolls to your choice. A pill for toolbars too.
With your AI
Say it to your AI. It gets the exact code below, not its best guess, and knows where it goes.
Swap the country select of my checkout for a Kodza Select.
claude mcp add -t http kodza https://kodza.dev/api/mcp
<label class="kodza-select">
<span class="kodza-select__label">Deploy day</span>
<select class="kodza-select__native" name="day">
<option value="mon">Monday</option>
<option value="tue" selected>Tuesday</option>
<option value="wed">Wednesday</option>
<option value="thu">Thursday</option>
<option value="fri" disabled>Friday (not on our watch)</option>
</select>
</label>
<label class="kodza-select">
<span class="kodza-select__label">Stack</span>
<select class="kodza-select__native" name="stack">
<option value="" selected disabled hidden></option>
<option value="astro">Astro</option>
<option value="vite">Vite</option>
<option value="hugo">Hugo</option>
<option value="html">Plain HTML, like a legend</option>
</select>
</label>
<label class="kodza-select">
<span class="kodza-select__label">Font</span>
<select class="kodza-select__native" name="font">
<optgroup label="Sans">
<option value="geist" selected>Geist</option>
<option value="inter">Inter</option>
<option value="system">System UI</option>
</optgroup>
<optgroup label="Mono">
<option value="geist-mono">Geist Mono</option>
<option value="jetbrains">JetBrains Mono</option>
</optgroup>
</select>
</label>
<label class="kodza-select kodza-select--inline">
<span class="kodza-select__label">Sort</span>
<select class="kodza-select__native" name="sort">
<option value="new" selected>Newest first</option>
<option value="old">Oldest first</option>
<option value="az">A to Z</option>
<option value="stars">Most copied</option>
</select>
</label>
/* Kodza License: free to use, client work included. Not to resell or redistribute, ported or not: https://kodza.dev/docs/#print-license */
/* Kodza — Select
A real select, dressed up: the script builds a listbox on top of it (and keeps the real one
in step, so forms and WordPress get the value). The list unrolls from the field, a highlight
slides from option to option, the chosen one gets a tick, and the field's value rolls to the
new choice. Arrows, Home, End, typing a few letters, Enter, Escape. Without JS, the native
select stays, styled. Same box as Text Field. To start empty, make the first option an empty
one marked selected, disabled and hidden: the label then sits in the field until a choice.
For a toolbar (sort, filters), --inline makes it a 40px pill with the label before the value.
Everything is tuned with the --kz-select-* variables. */
.kodza-select {
--kz-select-bg: #ffffff;
--kz-select-fg: #0a0a0a;
--kz-select-muted: rgba(10, 10, 10, 0.6);
--kz-select-line: rgba(10, 10, 10, 0.14);
--kz-select-line-hover: rgba(10, 10, 10, 0.3);
--kz-select-soft: rgba(10, 10, 10, 0.06);
--kz-select-shadow: 0 18px 40px -16px rgba(10, 10, 10, 0.3);
--kz-select-height: 56px;
--kz-select-radius: 12px;
--kz-select-pad: 16px;
--kz-select-duration: 0.35s;
--kz-select-ease: cubic-bezier(0.22, 1, 0.36, 1);
position: relative;
display: block;
width: 100%;
max-width: 320px;
/* The closed list keeps its size, ready to unroll, but never lengthens the page */
contain: layout;
/* A number, not auto, so it can wait for the list to roll up before dropping back */
z-index: 0;
transition: z-index 0s linear var(--kz-select-duration);
color: var(--kz-select-fg);
}
/* Open, the field rises above what follows it (its list included) */
.kodza-select.is-open {
z-index: 20;
transition-delay: 0s;
}
/* The box: label on top, value below (the native select, or the button the script adds) */
.kodza-select__label {
position: absolute;
top: 10px;
left: var(--kz-select-pad);
z-index: 1;
color: var(--kz-select-muted);
font-size: 12px;
line-height: 1;
pointer-events: none;
transform-origin: left top;
transition: transform var(--kz-select-duration) var(--kz-select-ease);
}
/* Nothing chosen yet (an empty, hidden first option): the label sits in the field at the size
of a value, as in Text Field, and rises when the field takes the focus or opens */
.kodza-select.is-empty:not(.is-open):not(:focus-within) .kodza-select__label {
transform: translateY(calc(var(--kz-select-height) / 2 - 17.5px)) scale(1.25);
}
.kodza-select__native,
.kodza-select__trigger {
display: flex;
align-items: flex-end;
width: 100%;
height: var(--kz-select-height);
margin: 0;
padding: 0 44px 9px var(--kz-select-pad);
border: 0;
border-radius: var(--kz-select-radius);
background: var(--kz-select-bg) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4 6.5 8 10.5l4-4' fill='none' stroke='%23999' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") right 16px center / 16px no-repeat;
box-shadow: inset 0 0 0 1px var(--kz-select-line);
color: inherit;
font: inherit;
font-size: 15px;
line-height: 22px;
text-align: left;
cursor: pointer;
appearance: none;
transition: box-shadow 0.25s ease;
}
.kodza-select__native:hover,
.kodza-select__trigger:hover {
box-shadow: inset 0 0 0 1px var(--kz-select-line-hover);
}
.kodza-select__native:focus-visible,
.kodza-select__trigger:focus-visible,
.kodza-select.is-open .kodza-select__trigger {
outline: none;
box-shadow: inset 0 0 0 1.5px var(--kz-select-fg), 0 0 0 4px var(--kz-select-soft);
}
/* Enhanced: the native select steps back (still in the form), the button takes its place */
.kodza-select.is-ready .kodza-select__native {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
overflow: hidden;
opacity: 0;
pointer-events: none;
clip-path: inset(50%);
}
.kodza-select__trigger {
background-image: none;
}
.kodza-select__value {
display: inline-grid;
min-width: 0;
overflow: hidden;
}
.kodza-select__value > span {
grid-area: 1 / 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* Every option, invisible, under the value: the field keeps the width of the longest one */
.kodza-select__sizes {
display: grid;
height: 0;
visibility: hidden;
}
.kodza-select__sizes > span {
grid-area: 1 / 1;
white-space: nowrap;
}
.kodza-select__chevron {
position: absolute;
top: calc(var(--kz-select-height) / 2 - 8px);
right: 16px;
width: 16px;
height: 16px;
fill: none;
stroke: var(--kz-select-muted);
stroke-width: 1.6;
stroke-linecap: round;
stroke-linejoin: round;
pointer-events: none;
transition: transform var(--kz-select-duration) var(--kz-select-ease);
}
.kodza-select.is-open .kodza-select__chevron {
transform: rotate(180deg);
}
/* The list unrolls from the field */
.kodza-select__list {
position: absolute;
top: calc(var(--kz-select-height) + 8px);
right: 0;
left: 0;
z-index: 20;
max-height: 280px;
margin: 0;
padding: 6px;
overflow-y: auto;
overscroll-behavior: contain;
border-radius: 14px;
background: var(--kz-select-bg);
box-shadow: 0 0 0 1px var(--kz-select-line), var(--kz-select-shadow);
list-style: none;
visibility: hidden;
opacity: 0;
clip-path: inset(0 0 100% 0 round 14px);
transform: translateY(-4px);
transition:
clip-path var(--kz-select-duration) var(--kz-select-ease),
opacity 0.2s ease,
transform var(--kz-select-duration) var(--kz-select-ease),
visibility 0s linear var(--kz-select-duration);
}
.kodza-select.is-open .kodza-select__list {
visibility: visible;
opacity: 1;
clip-path: inset(-40px -40px -60px -40px round 14px);
transform: none;
transition-delay: 0s;
}
.kodza-select.is-above .kodza-select__list {
top: auto;
bottom: calc(var(--kz-select-height) + 8px);
clip-path: inset(100% 0 0 0 round 14px);
transform: translateY(4px);
}
.kodza-select.is-above.is-open .kodza-select__list {
clip-path: inset(-60px -40px -40px -40px round 14px);
transform: none;
}
.kodza-select__highlight {
position: absolute;
top: 0;
right: 6px;
left: 6px;
height: var(--kz-hl-h, 36px);
border-radius: 8px;
background: var(--kz-select-soft);
opacity: var(--kz-hl-on, 0);
transform: translateY(var(--kz-hl-y, 0));
pointer-events: none;
}
.kodza-select.is-open .kodza-select__highlight {
transition: transform 0.22s var(--kz-select-ease), opacity 0.15s ease;
}
.kodza-select__group {
margin: 0;
padding: 8px 10px 4px 32px;
font-family: ui-monospace, "SF Mono", Menlo, monospace;
font-size: 11px;
color: var(--kz-select-muted);
}
.kodza-select__option {
position: relative;
display: flex;
align-items: center;
height: 36px;
padding: 0 10px 0 32px;
border-radius: 8px;
font-size: 14px;
font-weight: 500;
white-space: nowrap;
cursor: pointer;
}
.kodza-select__option[aria-disabled="true"] {
color: var(--kz-select-muted);
cursor: not-allowed;
}
/* The tick of the chosen option draws itself */
.kodza-select__option svg {
position: absolute;
left: 10px;
width: 14px;
height: 14px;
fill: none;
stroke: currentColor;
stroke-width: 1.8;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 1;
stroke-dashoffset: 1;
transition: stroke-dashoffset 0.15s ease-in;
}
.kodza-select__option[aria-selected="true"] svg {
stroke-dashoffset: 0;
transition: stroke-dashoffset 0.35s var(--kz-select-ease) 0.05s;
}
/* Inline: a 40px pill for a toolbar. The label sits before the value, on the same line (the
script measures it), and the field is as wide as its longest option */
.kodza-select--inline {
--kz-select-height: 40px;
--kz-select-radius: 999px;
--kz-select-pad: 14px;
display: inline-block;
width: auto;
max-width: 100%;
vertical-align: middle;
}
.kodza-select--inline .kodza-select__label,
.kodza-select--inline.is-empty:not(.is-open):not(:focus-within) .kodza-select__label {
top: 50%;
font-size: 13px;
transform: translateY(-50%);
}
.kodza-select--inline .kodza-select__native,
.kodza-select--inline .kodza-select__trigger {
align-items: center;
padding: 0 38px 0 calc(var(--kz-select-pad) + var(--kz-select-inset, 3em) + 8px);
font-size: 14px;
font-weight: 500;
line-height: 20px;
}
.kodza-select--inline .kodza-select__native {
width: auto;
background-position: right 14px center;
}
.kodza-select--inline .kodza-select__chevron {
right: 14px;
}
.kodza-select--inline .kodza-select__list {
right: auto;
width: max-content;
min-width: 100%;
max-width: min(320px, calc(100vw - 32px));
}
@media (prefers-reduced-motion: reduce) {
.kodza-select__label,
.kodza-select__native,
.kodza-select__trigger,
.kodza-select__chevron,
.kodza-select__list,
.kodza-select.is-open .kodza-select__highlight,
.kodza-select__option svg {
transition-duration: 0.01ms !important;
transition-delay: 0s !important;
}
}
/* Theming example — on a dark background:
.dark-section .kodza-select {
--kz-select-bg: #161616;
--kz-select-fg: #f2f2f0;
--kz-select-muted: rgba(242, 242, 240, 0.62);
--kz-select-line: rgba(255, 255, 255, 0.14);
--kz-select-line-hover: rgba(255, 255, 255, 0.3);
--kz-select-soft: rgba(255, 255, 255, 0.08);
--kz-select-shadow: 0 18px 40px -16px rgba(0, 0, 0, 0.8);
}
*/
/* kodza-guard 963a4ed2fa1a — 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-select):not(#_) {
vertical-align: revert;
}
:where(.kodza-select__native):not(#_) {
outline: revert;
position: revert;
}
:where(.kodza-select__trigger):not(#_) {
outline: revert;
}
:where(.kodza-select__list):not(#_) {
max-width: revert;
}
:where(.kodza-select__highlight):not(#_) {
transition: revert;
}
:where(.kodza-select__option):not(#_) {
color: revert;
}
/* What the browser gives, on everything the component leaves to it */
.kodza-select:not(#_) {
border: revert;
padding: revert;
border-radius: revert;
outline: revert;
text-decoration: revert;
list-style: revert;
box-sizing: revert;
background-color: revert;
background-image: revert;
box-shadow: revert;
outline-offset: revert;
appearance: 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;
}
.kodza-select__label:not(#_),
.kodza-select__value:not(#_),
.kodza-select__sizes:not(#_),
.kodza-select__sizes span:where(:not([class*="kodza-"])):not(#_),
.kodza-select__value span:where(:not([class*="kodza-"])):not(#_) {
box-sizing: revert;
}
.kodza-select__native:not(#_) {
text-decoration: revert;
list-style: revert;
box-sizing: revert;
float: revert;
outline-offset: revert;
max-width: revert;
max-height: revert;
letter-spacing: revert;
text-transform: revert;
text-underline-offset: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
white-space: revert;
min-height: revert;
}
.kodza-select__trigger:not(#_) {
text-decoration: revert;
list-style: revert;
box-sizing: revert;
float: revert;
position: revert;
outline-offset: revert;
max-width: revert;
max-height: revert;
letter-spacing: revert;
text-transform: revert;
text-underline-offset: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
white-space: revert;
min-height: revert;
}
.kodza-select__chevron:not(#_),
.kodza-select__option svg:where(:not([class*="kodza-"])):not(#_) {
border: revert;
margin: revert;
padding: revert;
border-radius: revert;
outline: revert;
text-decoration: revert;
list-style: revert;
box-sizing: revert;
float: 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-select__list:not(#_) {
border: revert;
outline: revert;
text-decoration: revert;
box-sizing: revert;
float: revert;
outline-offset: revert;
appearance: 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-select__highlight:not(#_) {
border: revert;
margin: revert;
padding: revert;
outline: revert;
text-decoration: revert;
list-style: revert;
box-sizing: revert;
float: 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-select__option:not(#_) {
border: revert;
margin: revert;
outline: revert;
text-decoration: revert;
transition: revert;
list-style: revert;
box-sizing: revert;
float: 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-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-select__group: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-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-select__native option:where(:not([class*="kodza-"])):not(#_),
.kodza-select__native optgroup:where(:not([class*="kodza-"])):not(#_) {
box-sizing: revert;
color: revert;
}
.kodza-select__chevron:not(#_):where(:not([hidden])),
.kodza-select__list:not(#_):where(:not([hidden])),
.kodza-select__highlight:not(#_):where(:not([hidden])),
.kodza-select__group:not(#_):where(:not([hidden])),
.kodza-select__option svg:where(:not([class*="kodza-"])):not(#_):where(:not([hidden])) {
display: revert;
}
.kodza-select__chevron:not(#_):where([hidden]),
.kodza-select__list:not(#_):where([hidden]),
.kodza-select__highlight:not(#_):where([hidden]),
.kodza-select__group:not(#_):where([hidden]),
.kodza-select__option svg:where(:not([class*="kodza-"])):not(#_):where([hidden]) {
display: none;
}
/* 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-select:not(#_) {
position: relative;
display: block;
transition: z-index 0s linear var(--kz-select-duration);
color: var(--kz-select-fg);
}
.kodza-select:not(#_) {
width: min(100%, 320px);
}
.kodza-select.is-open:not(#_) {
transition-delay: 0s;
}
.kodza-select__native:not(#_),
.kodza-select__trigger:not(#_) {
display: flex;
align-items: flex-end;
width: 100%;
height: var(--kz-select-height);
margin: 0;
padding: 0 44px 9px var(--kz-select-pad);
border: 0;
border-radius: var(--kz-select-radius);
background: var(--kz-select-bg) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4 6.5 8 10.5l4-4' fill='none' stroke='%23999' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") right 16px center / 16px no-repeat;
box-shadow: inset 0 0 0 1px var(--kz-select-line);
color: inherit;
font: inherit;
font-size: 15px;
line-height: 22px;
text-align: left;
cursor: pointer;
appearance: none;
transition: box-shadow 0.25s ease;
}
.kodza-select__native:hover:not(#_),
.kodza-select__trigger:hover:not(#_) {
box-shadow: inset 0 0 0 1px var(--kz-select-line-hover);
}
.kodza-select__native:focus-visible:not(#_),
.kodza-select__trigger:focus-visible:not(#_),
.kodza-select.is-open .kodza-select__trigger:not(#_) {
outline: none;
box-shadow: inset 0 0 0 1.5px var(--kz-select-fg), 0 0 0 4px var(--kz-select-soft);
}
.kodza-select.is-ready .kodza-select__native:not(#_) {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
overflow: hidden;
opacity: 0;
pointer-events: none;
clip-path: inset(50%);
}
.kodza-select__trigger:not(#_) {
background-image: none;
}
.kodza-select__chevron:not(#_) {
position: absolute;
transition: transform var(--kz-select-duration) var(--kz-select-ease);
}
.kodza-select__list:not(#_) {
position: absolute;
max-height: 280px;
margin: 0;
padding: 6px;
border-radius: 14px;
background: var(--kz-select-bg);
box-shadow: 0 0 0 1px var(--kz-select-line), var(--kz-select-shadow);
list-style: none;
transition: clip-path var(--kz-select-duration) var(--kz-select-ease),
opacity 0.2s ease,
transform var(--kz-select-duration) var(--kz-select-ease),
visibility 0s linear var(--kz-select-duration);
}
.kodza-select.is-open .kodza-select__list:not(#_) {
transition-delay: 0s;
}
.kodza-select__highlight:not(#_) {
position: absolute;
border-radius: 8px;
background: var(--kz-select-soft);
}
.kodza-select.is-open .kodza-select__highlight:not(#_) {
transition: transform 0.22s var(--kz-select-ease), opacity 0.15s ease;
}
.kodza-select__group:not(#_) {
margin: 0;
padding: 8px 10px 4px 32px;
font-family: ui-monospace, "SF Mono", Menlo, monospace;
font-size: 11px;
color: var(--kz-select-muted);
}
.kodza-select__option:not(#_) {
position: relative;
display: flex;
padding: 0 10px 0 32px;
border-radius: 8px;
font-size: 14px;
font-weight: 500;
}
.kodza-select__option[aria-disabled="true"]:not(#_) {
color: var(--kz-select-muted);
}
.kodza-select__option svg:not(#_) {
position: absolute;
transition: stroke-dashoffset 0.15s ease-in;
}
.kodza-select__option[aria-selected="true"] svg:not(#_) {
transition: stroke-dashoffset 0.35s var(--kz-select-ease) 0.05s;
}
.kodza-select--inline:not(#_) {
display: inline-block;
vertical-align: middle;
}
.kodza-select--inline:not(#_) {
width: auto;
}
.kodza-select--inline .kodza-select__native:not(#_),
.kodza-select--inline .kodza-select__trigger:not(#_) {
align-items: center;
padding: 0 38px 0 calc(var(--kz-select-pad) + var(--kz-select-inset, 3em) + 8px);
font-size: 14px;
font-weight: 500;
line-height: 20px;
}
.kodza-select--inline .kodza-select__native:not(#_) {
width: auto;
background-position: right 14px center;
}
.kodza-select--inline .kodza-select__list:not(#_) {
max-width: min(320px, calc(100vw - 32px));
}
@media (prefers-reduced-motion: reduce) {
.kodza-select__native:not(#_),
.kodza-select__trigger:not(#_) {
transition-duration: 0.01ms !important;
transition-delay: 0s !important;
}
}
@media (prefers-reduced-motion: reduce) {
.kodza-select__chevron:not(#_),
.kodza-select__list:not(#_),
.kodza-select.is-open .kodza-select__highlight:not(#_),
.kodza-select__option svg: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 — Select
// Builds a listbox over the real <select> (options, groups, disabled ones), keeps both in step
// and fires "change" on the real one, so forms and frameworks see an ordinary select. Keyboard
// as in the ARIA "select-only combobox" pattern: the focus stays on the field, arrows move,
// letters jump, Enter picks, Escape closes. Options changed by the page (counts, disabled
// ones, a new list) are read again; after setting .value from a script, fire "change" on it.
(() => {
const SELECTOR = '.kodza-select';
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const tick = '<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M3.5 8.5l3 3 6-7" pathLength="1"/></svg>';
const chevron = '<svg class="kodza-select__chevron" viewBox="0 0 16 16" aria-hidden="true"><path d="M4 6.5 8 10.5l4-4"/></svg>';
let uid = 0;
// One instance. Everything it starts is tied to `signal`, so kodza:destroy stops it all
function mount(root, signal) {
const native = root.querySelector('.kodza-select__native');
const label = root.querySelector('.kodza-select__label');
if (!native) return;
const id = `kodza-select-${++uid}`;
// The field: a button that says the current value
const trigger = document.createElement('button');
trigger.type = 'button';
trigger.className = 'kodza-select__trigger';
trigger.setAttribute('role', 'combobox');
trigger.setAttribute('aria-haspopup', 'listbox');
trigger.setAttribute('aria-expanded', 'false');
trigger.setAttribute('aria-controls', `${id}-list`);
if (label) {
label.id = `${id}-label`;
trigger.setAttribute('aria-labelledby', `${id}-label`);
}
const value = document.createElement('span');
value.className = 'kodza-select__value';
// Every option, invisible, under the value: the field is as wide as the longest one, so it
// never changes width when the value rolls (the inline version sizes itself on it)
const sizes = document.createElement('span');
sizes.className = 'kodza-select__sizes';
sizes.setAttribute('aria-hidden', 'true');
value.append(sizes);
trigger.append(value);
trigger.insertAdjacentHTML('beforeend', chevron);
// The list, from the real options (built again whenever the page changes them)
const list = document.createElement('ul');
list.className = 'kodza-select__list';
list.id = `${id}-list`;
list.setAttribute('role', 'listbox');
if (label) list.setAttribute('aria-labelledby', `${id}-label`);
const highlight = document.createElement('li');
highlight.className = 'kodza-select__highlight';
highlight.setAttribute('role', 'presentation');
list.append(highlight);
let options = [];
const entries = new Map();
const build = () => {
options = [];
entries.clear();
[...list.children].forEach((child) => child !== highlight && child.remove());
sizes.replaceChildren();
[...native.children].forEach((child) => {
const group = child.tagName === 'OPTGROUP' ? child : null;
if (group) {
const head = document.createElement('li');
head.className = 'kodza-select__group';
head.setAttribute('role', 'presentation');
head.textContent = group.label;
list.append(head);
}
(group ? [...group.children] : [child]).forEach((option) => {
if (option.hidden) return; // the placeholder: "nothing chosen yet" is not a choice
const item = document.createElement('li');
item.className = 'kodza-select__option';
item.id = `${id}-option-${options.length}`;
item.setAttribute('role', 'option');
item.innerHTML = tick;
item.append(option.textContent);
if (option.disabled || group?.disabled) item.setAttribute('aria-disabled', 'true');
list.append(item);
const entry = { option, item };
options.push(entry);
entries.set(item, entry);
sizes.append(Object.assign(document.createElement('span'), { textContent: option.textContent }));
});
});
};
build();
native.after(trigger, list);
native.tabIndex = -1;
native.setAttribute('aria-hidden', 'true');
const enabled = () => options.filter(({ item }) => item.getAttribute('aria-disabled') !== 'true');
const isOpen = () => root.classList.contains('is-open');
let active = null;
// The value rolls to the new choice
let shown = null;
const show = (text, animate) => {
const prev = shown;
if (prev && prev.textContent === text) return;
const span = Object.assign(document.createElement('span'), { textContent: text });
value.append(span);
shown = span;
if (!prev) return;
if (!animate || reduceMotion) return prev.remove();
const timing = { duration: 420, easing: 'cubic-bezier(0.22, 1, 0.36, 1)' };
prev.animate([{ transform: 'none' }, { transform: 'translateY(-110%)' }], { ...timing, fill: 'forwards' }).finished.then(() => prev.remove());
span.animate([{ transform: 'translateY(110%)' }, { transform: 'none' }], timing);
};
const sync = (animate) => {
options.forEach(({ option, item }) => item.setAttribute('aria-selected', String(option.selected)));
// An empty, hidden first option leaves the field empty: the label sits inside, as in Text Field
const chosen = native.selectedOptions[0];
const empty = !chosen || chosen.hidden || !chosen.textContent.trim();
root.classList.toggle('is-empty', empty);
show(empty ? '' : chosen.textContent, animate);
};
const light = (entry, scroll = true) => {
active = entry;
if (!entry) {
trigger.removeAttribute('aria-activedescendant');
return list.style.setProperty('--kz-hl-on', 0);
}
trigger.setAttribute('aria-activedescendant', entry.item.id);
list.style.setProperty('--kz-hl-y', `${entry.item.offsetTop}px`);
list.style.setProperty('--kz-hl-h', `${entry.item.offsetHeight}px`);
list.style.setProperty('--kz-hl-on', 1);
// Scroll the list only (scrollIntoView would also nudge the page), with the option's group
// heading when it opens the group, and the list's own padding above the very first row
if (scroll) {
const prev = entry.item.previousElementSibling;
const first = prev?.classList.contains('kodza-select__group') ? prev : entry.item;
const top = first.previousElementSibling === highlight ? 0 : first.offsetTop;
const bottom = entry.item.offsetTop + entry.item.offsetHeight;
if (top < list.scrollTop) list.scrollTop = top;
else if (bottom > list.scrollTop + list.clientHeight) list.scrollTop = bottom - list.clientHeight;
}
};
const open = () => {
if (isOpen()) return;
// Not enough room below and more above: the list opens upwards. A closed list that changes
// side gets there at once, so it always unrolls from the field
const box = trigger.getBoundingClientRect();
const above = box.bottom + Math.min(280, list.scrollHeight) + 16 > innerHeight && box.top > innerHeight / 2;
if (above !== root.classList.contains('is-above')) {
list.style.transition = 'none';
root.classList.toggle('is-above', above);
void list.offsetWidth;
list.style.transition = '';
}
root.classList.add('is-open');
trigger.setAttribute('aria-expanded', 'true');
light(options.find(({ option }) => option.selected) || enabled()[0]);
};
const close = () => {
if (!isOpen()) return;
root.classList.remove('is-open');
trigger.setAttribute('aria-expanded', 'false');
light(null);
};
const pick = (entry) => {
if (!entry || entry.item.getAttribute('aria-disabled') === 'true') return;
const changed = !entry.option.selected;
entry.option.selected = true;
sync(true);
close();
if (changed) native.dispatchEvent(new Event('change', { bubbles: true }));
};
// Typing a few letters jumps to the first matching option
let typed = '';
let typedTimer = 0;
const typeAhead = (char) => {
clearTimeout(typedTimer);
typed += char.toLowerCase();
typedTimer = setTimeout(() => (typed = ''), 600);
const pool = enabled();
const start = active ? pool.indexOf(active) + (typed.length === 1 ? 1 : 0) : 0;
const order = [...pool.slice(start), ...pool.slice(0, start)];
const match = order.find(({ item }) => item.textContent.trim().toLowerCase().startsWith(typed));
if (!match) return;
if (isOpen()) light(match);
else pick(match);
};
trigger.addEventListener('click', () => (isOpen() ? close() : open()), { signal });
trigger.addEventListener('keydown', (event) => {
const pool = enabled();
const index = pool.indexOf(active);
if (!isOpen()) {
if (['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(event.key)) {
event.preventDefault();
open();
} else if (event.key.length === 1 && /\S/.test(event.key) && !event.ctrlKey && !event.metaKey && !event.altKey) {
typeAhead(event.key);
}
return;
}
const moves = { ArrowDown: index + 1, ArrowUp: index - 1, Home: 0, End: pool.length - 1, PageDown: index + 5, PageUp: index - 5 };
if (event.key in moves) {
event.preventDefault();
light(pool[Math.max(0, Math.min(pool.length - 1, moves[event.key]))]);
} else if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
pick(active);
} else if (event.key === 'Tab') {
pick(active);
} else if (event.key.length === 1 && /\S/.test(event.key) && !event.ctrlKey && !event.metaKey && !event.altKey) {
typeAhead(event.key);
}
}, { signal });
const entryAt = (event) => entries.get(event.target.closest?.('.kodza-select__option'));
list.addEventListener('pointermove', (event) => {
const entry = entryAt(event);
if (entry && active !== entry && entry.item.getAttribute('aria-disabled') !== 'true') light(entry, false);
}, { signal });
list.addEventListener('click', (event) => pick(entryAt(event)), { signal });
// Keep the list from stealing the focus from the field, and the wrapping <label> from
// sending clicks to the hidden select: a click on the label text focuses the field
list.addEventListener('mousedown', (event) => event.preventDefault(), { signal });
root.addEventListener('click', (event) => {
if (trigger.contains(event.target)) return;
event.preventDefault();
if (!list.contains(event.target)) trigger.focus();
}, { signal });
document.addEventListener('pointerdown', (event) => !root.contains(event.target) && close(), { signal });
// Escape closes it wherever the focus is: opened with a click, it stays on the field
// (Chrome) or on the page (Safari)
document.addEventListener('keydown', (event) => {
if (event.key !== 'Escape' || !isOpen()) return;
event.preventDefault();
close();
trigger.focus();
}, { signal });
// The real select changed from elsewhere (a form reset, a script): follow it
native.addEventListener('change', () => sync(true), { signal });
native.form?.addEventListener('reset', () => setTimeout(() => sync(true)), { signal });
// The page changed the options (counts, disabled ones, a new list): build the list again,
// keep the highlight on the same option, and show the value without a roll
let queued = false;
const observer = new MutationObserver(() => {
if (queued) return;
queued = true;
queueMicrotask(() => {
queued = false;
if (signal.aborted) return;
const was = active?.option;
build();
sync(false);
if (isOpen()) light(options.find(({ option }) => option === was) || options.find(({ option }) => option.selected) || enabled()[0], false);
});
});
observer.observe(native, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ['disabled', 'hidden', 'label'] });
signal.addEventListener('abort', () => observer.disconnect());
// Inline version: the value starts right after the label, whatever its length
if (label && root.classList.contains('kodza-select--inline')) {
const inset = () => root.style.setProperty('--kz-select-inset', `${Math.ceil(label.offsetWidth)}px`);
inset();
const sizer = new ResizeObserver(inset);
sizer.observe(label);
signal.addEventListener('abort', () => sizer.disconnect());
}
sync(false);
}
// 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.
const instances = new WeakMap();
const find = (scope = document) => [scope, ...scope.querySelectorAll(SELECTOR)].filter((el) => el.matches?.(SELECTOR));
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();
instances.set(root, { controller, html: root.innerHTML, attrs: [...root.attributes].map((a) => [a.name, a.value]) });
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);
instance.controller.abort();
[...root.attributes].forEach((a) => root.removeAttribute(a.name));
instance.attrs.forEach(([name, value]) => root.setAttribute(name, value));
root.innerHTML = instance.html;
});
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 — Select, for React. From https://kodza.dev/components/custom-select/
// 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: custom-select.css and custom-select.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.
import { useRef, useEffect, type ComponentPropsWithoutRef, type ComponentRef, type CSSProperties } from 'react';
import './custom-select.css';
type Variant = "inline";
export type SelectProps = Omit<ComponentPropsWithoutRef<'label'>, "children" | "variant" | "vars"> & {
/** Modifier class(es): .kodza-select--inline */
variant?: Variant | Variant[];
/** Settings, as CSS variables on the root */
vars?: {
/** default: #ffffff */
"--kz-select-bg"?: string | number;
/** default: #0a0a0a */
"--kz-select-fg"?: string | number;
/** default: rgba(10, 10, 10, 0.6) */
"--kz-select-muted"?: string | number;
/** default: rgba(10, 10, 10, 0.14) */
"--kz-select-line"?: string | number;
/** default: rgba(10, 10, 10, 0.3) */
"--kz-select-line-hover"?: string | number;
/** default: rgba(10, 10, 10, 0.06) */
"--kz-select-soft"?: string | number;
/** default: 0 18px 40px -16px rgba(10, 10, 10, 0.3) */
"--kz-select-shadow"?: string | number;
/** default: 56px */
"--kz-select-height"?: string | number;
/** default: 12px */
"--kz-select-radius"?: string | number;
/** default: 16px */
"--kz-select-pad"?: string | number;
/** default: 0.35s */
"--kz-select-duration"?: string | number;
/** default: cubic-bezier(0.22, 1, 0.36, 1) */
"--kz-select-ease"?: string | number;
};
className?: string;
};
export function Select({ variant, vars, className, style, ...rest }: SelectProps) {
const ref = useRef<ComponentRef<'label'>>(null);
const classes = ["kodza-select", ...([] as string[]).concat(variant ?? []).map((v) => `kodza-select--${v}`), 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('./custom-select.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 (
<label key={key} {...rest} className={classes} style={{ ...vars, ...style } as CSSProperties} ref={ref}>
<span className="kodza-select__label">Deploy day</span>
<select className="kodza-select__native" name="day" defaultValue="tue">
<option value="mon">Monday</option>
<option value="tue">Tuesday</option>
<option value="wed">Wednesday</option>
<option value="thu">Thursday</option>
<option value="fri" disabled>Friday (not on our watch)</option>
</select>
</label>
);
}
Checked
Robots poked at it before you did. Every component goes through the same checks, and again each time it changes.