Searchable Select & Switches

Two selects built on the Popover API - a single-choice one with search, and a multi-choice one with tags - plus iOS-style switches. Ready-made showcases: select and switches.

1. Why the Popover API

A native <select> has no search and no multi-select with tags. Instead of rebuilding it entirely in JavaScript, both components rest on the popover attribute: the dropdown menu lands in the top layer - a separate browser layer above everything else on the page.

Effect: the menu is never clipped

A menu in a scrolling table, in a modal, in a card with overflow: hidden - in each of these places a plain position: absolute would get clipped by its parent. The top layer sits outside that hierarchy, so the menu always ends up on top, and anchoring to the button is automatic (implicit anchor).

2. Searchable select

Single choice from a list. The search field is optional - without a text input the component behaves like a plain select, just with its menu in the top layer.

<div class="select-search">
  <button type="button" class="input select-search-trigger"
          popovertarget="client-select">
    <span>Choose from the list…</span>
  </button>

  <div class="select-search-menu" popover id="client-select">
    <input type="text" class="input select-search-input"
           placeholder="Search…">
    <div class="select-search-list">
      <div class="select-search-option" data-value="1">Molique</div>
      <div class="select-search-option" data-value="2">Briko</div>
    </div>
  </div>
</div>
Class / attribute What it does
.select-search The wrapper. The implicit anchor for the menu.
.select-search-trigger The opening button. Requires popovertarget pointing to the menu's id.
.select-search-menu The panel. Requires the popover attribute and a unique id - without them it won't land in the top layer and will vanish like a plain <div>.
.select-search-input The search field. Optional.
.select-search-list The options container.
.select-search-option A single option. Requires data-value - the module reads the value from it on selection.
.is-selected The currently selected option. Set by the script.
.is-hidden An option filtered out by the search field. Also the script.

Event delegation - works with AJAX

The module listens on the container, not on individual options, so it also handles .select-search-option elements added to the DOM after page load - e.g. an AJAX query result. Nothing needs to be re-initialized.

Keyboard and screen readers

Works with no extra code. The arrow keys move the highlight, Home and End jump to the ends, Enter picks, Esc closes, and Enter in the field does not submit the form. Focus stays in the field, and aria-activedescendant tells a screen reader which option is highlighted. In .custom-select the arrow keys move focus between real checkboxes, which stay checkboxes. Search ignores diacritics: “lodz” finds Łódź.

Setting a value from JavaScript

For programmatic filling (e.g. opening an edit form with a record's data), a single global function does the job - it syncs the button label and the highlight exactly like a mouse selection.

const hidden = document.querySelector('#client-id');
MoliqueSelectSearch.setValue(hidden, '2');

3. Multi select

Multiple choice with tags on the button. The option markup is a plain <label> with a checkbox - so selection works natively, with no logic in JS.

<div class="custom-select">
  <button type="button" class="custom-select-trigger"
          popovertarget="skills-select">
    <div class="custom-select-pills">…</div>
  </button>

  <div class="custom-select-dropdown" popover id="skills-select">
    <div class="custom-select-search">
      <input type="text" class="input" placeholder="Search…">
    </div>
    <div class="custom-select-list">
      <div class="custom-select-category">Backend</div>
      <label class="custom-select-option">
        <div class="flex-1">PHP</div>
        <input type="checkbox" class="form-check-input">
      </label>
    </div>
  </div>
</div>
Class What it does
.custom-select The wrapper.
.custom-select-trigger The button. Contains .custom-select-pills instead of a single label.
.custom-select-pills A row of tags for the selected options, wrapped in .badge.
.custom-select-dropdown The panel. Requires popover and id - just like the single select.
.custom-select-search The search field container. The field filters options and hides categories with no match.
.custom-select-list The container for categories and options.
.custom-select-category A heading that groups options.
.custom-select-option On a <label>. Contains a plain input[type="checkbox"].form-check-input.
.icon-chevron The arrow on the button; rotates 180° once open.
.search-icon The magnifying-glass icon in the search field.

4. Switches

A checkbox disguised as an iOS-style slider. The entire mechanism is appearance: none on the checkbox plus a pseudo-element as the floating knob - zero JavaScript, zero extra elements in the DOM.

<label class="form-switch">
  <input type="checkbox" class="form-switch-input" checked>
  <span class="form-switch-label">Email notifications</span>
</label>
Class What it does
.form-switch On a <label>. A click anywhere - including the text - toggles the state.
.form-switch-input The checkbox itself, restyled as a 44×24px slider. The checked color is always --success - see pitfall #3.
.form-switch-label The caption next to the slider.
.form-switch-square A variant with square corners instead of round ones.
.form-switch-outline A variant with just an outline instead of a filled background - visually less dominant.

A switch as a collapse trigger

A .form-switch placed right before .grid-expand (as a sibling) reveals the content underneath once checked - zero JS. Full description of the mechanism and a structural pitfall in the interactive components docs.

5. Pitfalls

1 Missing popover or id on the panel

Without the popover attribute the panel is a plain <div> - the button with popovertarget has nothing to open, so the click does nothing. Without a matching id the button points nowhere. Both look identical: the menu just doesn't appear, with no console error.

<!-- WRONG - no popover, the button won't open anything -->
<div class="select-search-menu" id="x">…</div>

<!-- RIGHT -->
<div class="select-search-menu" popover id="x">…</div>
2 Missing data-value on an option

The module reads an option's value exclusively from the data-value attribute, not from the element's content. An option without it will select visually - it highlights and fills the label - but the hidden field stays empty, so the form submits no value.

3 The switch color is hardcoded to green

A checked .form-switch-input always takes --success - there's no color modifier or local variable to override per instance. If you need a different color (e.g. a red "delete data" switch), the only path is a custom CSS rule targeting the specific class, because changing --success globally would also change every other use of that color in the project.

6. Related pages