Search field
A search input with an icon and a clear button that appears when there is text; Escape clears it too.
Installation
bash
npx bootcn add search-fieldbash
pnpm dlx bootcn add search-fieldbash
yarn dlx bootcn add search-fieldbash
bunx --bun bootcn add search-fieldThe CLI also installs the design tokens and these components: label. It prints the <link> and <script> tags to add to your layout.
Usage
Wrap it in a <form role="search"> with a name, so it is a search landmark, and keep a submit button (it can be visually hidden) so Enter submits.
JavaScript API
data-bootcn-search-fieldon the wrapper. The clear button shows when the input has text; clearing (button or Escape) empties it, firesinput, returns focus to the input and emitsbootcn:search-field:clear.Bootcn.searchField.init(root)/destroy().
Accessibility
- A native
type="search"input with a visible label inside a named search landmark. - The clear button is named "Clear search" and returns focus to the input; Escape does the same from the keyboard.
Dependencies
- Bootstrap 5.3+ CSS.
themes/tokens/tokens.css.- The
labelcomponent.
Source
The exact files the CLI installs (search-field@1.0.0). View on GitHub · Report an issue
html
<form class="bootcn-field" role="search" aria-label="Documentation" action="#">
<label class="bootcn-label" for="bootcn-search-field-q">Search the docs</label>
<div class="bootcn-search-field" data-bootcn-search-field>
<svg
class="bootcn-icon bootcn-search-field__icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<circle cx="11" cy="11" r="7" />
<path d="m20 20-3.5-3.5" />
</svg>
<input
class="bootcn-search-field__input"
type="search"
id="bootcn-search-field-q"
name="q"
placeholder="Components, blocks…"
value="date picker"
/>
<button type="button" class="bootcn-search-field__clear" aria-label="Clear search">
<svg
class="bootcn-icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="M18 6 6 18" />
<path d="m6 6 12 12" />
</svg>
</button>
</div>
<button type="submit" class="visually-hidden">Search</button>
</form>css
.bootcn-search-field {
display: flex;
align-items: center;
min-height: var(--bootcn-control-height);
border: var(--bootcn-border-width) solid var(--bootcn-input);
border-radius: var(--bootcn-radius);
background-color: transparent;
box-shadow: var(--bootcn-shadow-sm);
transition:
border-color var(--bootcn-transition),
box-shadow var(--bootcn-transition);
max-width: 20rem;
padding: 0 0.25rem 0 0.75rem;
}
.bootcn-search-field:focus-within {
border-color: var(--bootcn-ring);
box-shadow: 0 0 0 3px var(--bs-focus-ring-color);
}
.bootcn-search-field__icon {
flex: none;
color: var(--bootcn-muted-foreground);
}
.bootcn-search-field__input {
flex: 1 1 auto;
min-width: 0;
height: 100%;
border: 0;
background: transparent;
color: var(--bootcn-foreground);
font-size: 1rem;
outline: 0;
padding: 0 0.5rem;
appearance: none;
}
.bootcn-search-field__input::placeholder {
color: var(--bootcn-muted-foreground);
}
/* The clear button replaces the browser's own. */
.bootcn-search-field__input::-webkit-search-cancel-button {
appearance: none;
}
@media (min-width: 768px) {
.bootcn-search-field__input {
font-size: var(--bootcn-control-font-size);
}
}
.bootcn-search-field__clear {
display: inline-flex;
flex: none;
align-items: center;
justify-content: center;
width: 1.75rem;
height: 1.75rem;
border: 0;
border-radius: var(--bootcn-radius-sm);
background: transparent;
color: var(--bootcn-muted-foreground);
}
.bootcn-search-field__clear:hover {
background-color: var(--bootcn-accent);
color: var(--bootcn-foreground);
}
.bootcn-search-field__clear:focus-visible {
outline: 0;
box-shadow: var(--bootcn-focus-ring);
}
.bootcn-search-field__clear[hidden] {
display: none;
}js
(function () {
'use strict';
const ns = (window.Bootcn = window.Bootcn || {});
if (ns.searchField) return; // already loaded: idempotent
const ROOT = '[data-bootcn-search-field]';
let listening = false;
function sync(root) {
const input = root.querySelector('input');
const clear = root.querySelector('.bootcn-search-field__clear');
if (input && clear) clear.hidden = input.value === '';
}
function clear(root) {
const input = root.querySelector('input');
if (!input) return;
input.value = '';
input.dispatchEvent(new Event('input', { bubbles: true }));
input.focus();
root.dispatchEvent(new CustomEvent('bootcn:search-field:clear', { bubbles: true }));
}
function onClick(event) {
const button = event.target.closest && event.target.closest('.bootcn-search-field__clear');
const root = button && button.closest(ROOT);
if (root) clear(root);
}
function onInput(event) {
const root = event.target.closest && event.target.closest(ROOT);
if (root) sync(root);
}
function onKeydown(event) {
if (event.key !== 'Escape') return;
const root = event.target.closest && event.target.closest(ROOT);
if (!root || event.target.value === '') return;
event.preventDefault();
clear(root);
}
function init(root = document) {
root.querySelectorAll(ROOT).forEach(sync);
if (listening) return;
document.addEventListener('click', onClick);
document.addEventListener('input', onInput);
document.addEventListener('keydown', onKeydown);
listening = true;
}
function destroy() {
if (!listening) return;
document.removeEventListener('click', onClick);
document.removeEventListener('input', onInput);
document.removeEventListener('keydown', onKeydown);
listening = false;
}
ns.searchField = { init: init, destroy: destroy };
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => init());
} else {
init();
}
})();
