Parsed query chips
A search box that echoes back how it read the text: every recognised qualifier becomes its own removable chip, exclusions say "not" in words rather than relying on a leading dash, and anything unrecognised is shown as plain text rather than silently dropped.
Reach for it when building
- a photo or media library search
- an issue tracker with query syntax
- a log or transcript search
- a file browser with filters
- an email client search box
- any search that quietly supports key:value terms
<div class="pqc-demo">
<label class="pqc-field">
<span>Search photos</span>
<input type="search" id="pqc-input" value="camera:fuji before:2024 -screenshot beach cliffs" autocomplete="off" autocapitalize="off" spellcheck="false" aria-describedby="pqc-hint" />
</label>
<ul class="pqc-chips" id="pqc-chips" aria-label="Terms in this search"></ul>
<p class="pqc-note" id="pqc-note" role="status"></p>
<p class="pqc-hint" id="pqc-hint">Recognised keys: camera, before, after, album, lens. Prefix a word with a dash to exclude it.</p>
</div>Colors come from shared theme tokens — --surface, --ink, --border, --accent and friends — so this CSS carries no palette
of its own. Use Runnable file to copy the tokens along with it.
.pqc-demo { display: flex; flex-direction: column; gap: 0.75rem; max-width: 560px; }
.pqc-field { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.9375rem; color: var(--dim); }
.pqc-field input {
font: inherit;
font-family: var(--mono);
min-height: 44px;
padding: 0.45rem 0.6rem;
border-radius: calc(var(--radius) - 4px);
border: 1px solid var(--border-strong);
background: var(--surface);
color: var(--ink);
width: 100%;
}
.pqc-chips { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 0.5rem; }
/* The chip reaches the 34px floor through padding, so the label can stay at
body size instead of shrinking to fit a smaller pill. The radius is half
the one-line height (28px button, padding, border) rather than 999px, so a term long enough to wrap still sits
inside a rounded box instead of a lozenge that clips its corners. */
.pqc-chip {
display: inline-flex;
align-items: center;
gap: 0.35rem;
min-height: 34px;
max-width: 100%;
padding: 0.25rem 0.3rem 0.25rem 0.65rem;
border-radius: 19px;
border: 1px solid var(--border);
background: var(--surface-2);
color: var(--ink);
font-size: 0.9375rem;
}
.pqc-chip[data-kind='qualifier'] { border-color: var(--accent); background: color-mix(in oklab, var(--accent) 14%, var(--surface)); }
/* An exclusion never relies on the dash alone: it says "not", and it takes a
different border so the difference survives a greyscale print. */
.pqc-chip[data-kind='exclusion'] { border-color: var(--signal); border-style: dashed; color: var(--signal); }
/* A recognised key with nothing after the colon is not a filter yet. */
.pqc-chip[data-kind='incomplete'] .pqc-pending { color: var(--dim); font-style: italic; }
/* One unbroken term can be longer than the row: it wraps inside its chip. */
.pqc-term { min-width: 0; overflow-wrap: anywhere; }
.pqc-key { font-weight: 600; }
.pqc-drop {
font: inherit;
line-height: 1;
flex: none;
width: 28px;
height: 28px;
border: none;
border-radius: 999px;
background: transparent;
color: inherit;
cursor: pointer;
font-size: 1.1rem;
}
.pqc-drop:hover { background: color-mix(in oklab, var(--ink) 14%, transparent); }
.pqc-note { margin: 0; font-size: 0.9375rem; color: var(--dim); min-height: 1.6em; }
.pqc-hint { margin: 0; font-size: 0.9375rem; color: var(--dim); font-style: italic; }const QUALIFIERS = ['camera', 'before', 'after', 'album', 'lens'];
const input = document.getElementById('pqc-input');
const chips = document.getElementById('pqc-chips');
const note = document.getElementById('pqc-note');
function parse(text) {
return text
.split(/\s+/)
.filter(Boolean)
.map((raw) => {
if (raw.startsWith('-') && raw.length > 1) {
return { kind: 'exclusion', raw, word: raw.slice(1) };
}
const split = raw.indexOf(':');
const key = split > 0 ? raw.slice(0, split).toLowerCase() : '';
if (QUALIFIERS.includes(key)) {
const value = raw.slice(split + 1);
/* "camera:" is what the box holds halfway through typing a qualifier.
Saying "camera is" with nothing after it would claim a filter exists. */
return value === ''
? { kind: 'incomplete', raw, key }
: { kind: 'qualifier', raw, key, value };
}
return { kind: 'text', raw, word: raw };
});
}
function summarise(terms) {
if (terms.length === 0) return 'Nothing to search yet.';
const count = (kind) => terms.filter((term) => term.kind === kind).length;
const waiting = count('incomplete');
return (
count('qualifier') + ' recognised, ' +
count('exclusion') + ' excluded, ' +
count('text') + ' searched as plain text.' +
(waiting > 0 ? ' ' + waiting + ' waiting for a value.' : '')
);
}
function render(removed) {
const terms = parse(input.value);
chips.innerHTML = '';
chips.hidden = terms.length === 0;
terms.forEach((term, index) => {
const chip = document.createElement('li');
chip.className = 'pqc-chip';
chip.dataset.kind = term.kind;
const body = document.createElement('span');
body.className = 'pqc-term';
if (term.kind === 'incomplete') {
const key = document.createElement('span');
key.className = 'pqc-key';
key.textContent = term.key;
const pending = document.createElement('span');
pending.className = 'pqc-pending';
pending.textContent = ' needs a value';
body.append(key, pending);
} else if (term.kind === 'qualifier') {
const key = document.createElement('span');
key.className = 'pqc-key';
key.textContent = term.key;
body.append(key, document.createTextNode(' is ' + term.value));
} else if (term.kind === 'exclusion') {
const key = document.createElement('span');
key.className = 'pqc-key';
key.textContent = 'not';
body.append(key, document.createTextNode(' ' + term.word));
} else {
body.textContent = term.word;
}
/* Every chip removes its own term. Rendering inert badges above a separate
row of controls is what forces people back into the raw string. */
const drop = document.createElement('button');
drop.type = 'button';
drop.className = 'pqc-drop';
drop.innerHTML = '×';
drop.setAttribute('aria-label', 'Remove ' + term.raw);
drop.addEventListener('click', () => {
input.value = terms
.filter((_, other) => other !== index)
.map((item) => item.raw)
.join(' ');
render(term.raw);
input.focus();
});
chip.append(body, drop);
chips.append(chip);
});
/* The removed chip and its button are gone, so the status line is the only
place a screen reader can hear which term left. */
note.textContent = (removed ? 'Removed ' + removed + '. ' : '') + summarise(terms);
}
input.addEventListener('input', () => render());
render();Paste this into an agent to rebuild the pattern from scratch.
Build a search box that shows its work. As someone types, parse the text and render one chip per term: recognised `key:value` qualifiers, exclusions, and leftover words that will be searched as plain text.
Reach for this the moment a search box quietly supports syntax. People discover `author:` or `before:` from a tooltip and then have no idea whether the thing they typed was understood, whether a typo made it a literal string, or which half of the query is narrowing the results. The chips answer all three without a help page. Walk away from a search box with no syntax behind it, where a row of chips restating the words is noise.
The rule that makes it work: **every chip removes its own term.** The version everyone builds first renders inert badges above a separate row of facet controls, which means dropping one qualifier still sends people back to hand-editing the raw string — and the chips have become decoration. Rebuild the query text from the surviving terms and put focus back in the input.
An exclusion is never signalled by the leading dash alone. Write "not screenshot" in words, and give the chip a second difference that is not colour — a dashed border does it. A minus sign in a pill is the single most misread thing in a query UI, and colour alone fails for anyone who cannot see it.
Show unrecognised words as ordinary text chips rather than hiding them. A misspelt `camrea:fuji` becoming a plain-text chip is how someone finds the typo; silently dropping it is how they conclude the search is broken. Match keys without regard to case, and turn off auto-capitalisation on the input, so a phone keyboard's `Camera:fuji` is not mistaken for a typo.
A recognised key with nothing after the colon is its own state, because the box holds exactly that halfway through typing every qualifier. Render it as "camera needs a value", not "camera is" followed by nothing, and keep it out of the recognised count.
Let the chip reach a 34px minimum through padding, not by shrinking the label. Chips are where small type creeps into an interface, and a query term is something people actually read. Cap a chip at the width of its row and let a long unbroken term wrap inside it, with the remove button held at a fixed size beside it; an album name with no spaces must not push the page sideways or squeeze the button. Use a radius of half the one-line chip height rather than a full pill so a wrapped chip keeps its shape.
Keep the input the source of truth. Parse it on every keystroke and render from the parse — never hold a parallel array of chips that can drift out of step with the text someone can still edit directly.
Report the composition in a `role="status"` line: how many terms were recognised, how many exclude, how many are plain text, and how many are still waiting for a value. When a chip is removed, lead that line with which term went ("Removed beach."), since the chip that had focus no longer exists. With the box empty, hide the chip list and let the line say there is nothing to search yet rather than reporting three zeros. Tie the hint that lists the recognised keys to the input with `aria-describedby`. Every colour comes from theme custom properties, and both chip kinds have to stay distinguishable in both themes.