Search Input
A search field with a clear button, keyboard shortcut and expanding focus state.
AI prompt
Build a search input inside a form with role="search". It contains a magnifier SVG on the left, an input of type="search" with a visible-hidden label "Search components", a clear button (×) on the right that is hidden until the input has text, and a small "/" key hint chip at the right when empty. The field is 44px tall, 12px radius; at rest 240px wide and on focus it grows to 320px over 220ms with cubic-bezier(0.4,0,0.2,1) and shows an accent ring. Pressing the "/" key anywhere on the page (when not typing in a field) focuses the input. Escape clears the text if any, otherwise blurs. The clear button fades and scales in over 140ms, refocuses the input after clearing and has aria-label "Clear search". Under prefers-reduced-motion skip animation. Hide the hint on pointer: coarse devices. Hide the browser's native cancel button.About this pattern
Definition
A text field specialised for queries: a leading magnifier icon, a clear button that appears only when there is text, and a keyboard shortcut hint. On focus the field widens slightly to give the query room.
Purpose & UX principles
- Search is a primary navigation method; it should be reachable from anywhere with a shortcut (/ or Cmd/Ctrl+K).
- The clear button saves selecting and deleting text, especially on touch.
- Use
type="search"androle="search"on the form so assistive technology finds it.
Interaction states
- Idle, focused (wider, accent ring), has text (clear button visible), cleared.
- Escape clears the field if it has text; otherwise it blurs.
Motion specification
- Width: 220ms cubic-bezier(0.4, 0, 0.2, 1).
- Clear button: fade and scale from 0.8, 140ms.
- Reduced motion: width change is instant.
Responsive behaviour
- Full width on phones; on desktop the field grows from 240px to 320px on focus.
- Hide the shortcut hint on touch devices.
Code
<form class="sf" role="search" onsubmit="return false">
<svg class="mag" viewBox="0 0 24 24" aria-hidden="true"><path d="M10 4a6 6 0 104.5 10l5 5 1.5-1.5-5-5A6 6 0 0010 4z"/></svg>
<label for="q" class="sr">Search components</label>
<input id="q" type="search" placeholder="Search components" autocomplete="off">
<kbd id="kbd">/</kbd>
<button type="button" class="clr" id="clr" aria-label="Clear search" hidden>×</button>
</form>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.sf{position:relative;width:240px;max-width:100%;transition:width .22s cubic-bezier(.4,0,.2,1)}
.sf:focus-within{width:320px}
.sf input{width:100%;height:44px;padding:0 42px 0 40px;border-radius:12px;border:1px solid var(--line);background:var(--surface);color:var(--text);font:inherit;font-size:16px;outline:none;-webkit-appearance:none}
.sf input::-webkit-search-cancel-button{display:none}
.sf input:focus{border-color:var(--accent);box-shadow:0 0 0 3px rgba(23,23,23,.12)}
.mag{position:absolute;left:13px;top:12px;width:20px;height:20px;fill:var(--muted)}
kbd{position:absolute;right:12px;top:11px;font:600 12px var(--mono,monospace);color:var(--muted);border:1px solid var(--line);border-radius:6px;padding:1px 7px}
.sf input:not(:placeholder-shown)~kbd{display:none}
.clr{position:absolute;right:8px;top:8px;width:28px;height:28px;border-radius:50%;border:0;background:var(--surface-2);color:var(--text);cursor:pointer;animation:pop .14s ease-out}
@keyframes pop{from{opacity:0;transform:scale(.8)}}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
@media (hover:none){kbd{display:none}}
@media (prefers-reduced-motion:reduce){.sf{transition:none}.clr{animation:none}}const q=document.getElementById('q'),clr=document.getElementById('clr');
q.addEventListener('input',()=>{clr.hidden=!q.value;});
clr.addEventListener('click',()=>{q.value='';clr.hidden=true;q.focus();});
q.addEventListener('keydown',e=>{if(e.key==='Escape'){if(q.value){q.value='';clr.hidden=true;}else q.blur();}});
document.addEventListener('keydown',e=>{if(e.key==='/'&&!/INPUT|TEXTAREA/.test(document.activeElement.tagName)){e.preventDefault();q.focus();}});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Search Input — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.sf{position:relative;width:240px;max-width:100%;transition:width .22s cubic-bezier(.4,0,.2,1)}
.sf:focus-within{width:320px}
.sf input{width:100%;height:44px;padding:0 42px 0 40px;border-radius:12px;border:1px solid var(--line);background:var(--surface);color:var(--text);font:inherit;font-size:16px;outline:none;-webkit-appearance:none}
.sf input::-webkit-search-cancel-button{display:none}
.sf input:focus{border-color:var(--accent);box-shadow:0 0 0 3px rgba(23,23,23,.12)}
.mag{position:absolute;left:13px;top:12px;width:20px;height:20px;fill:var(--muted)}
kbd{position:absolute;right:12px;top:11px;font:600 12px var(--mono,monospace);color:var(--muted);border:1px solid var(--line);border-radius:6px;padding:1px 7px}
.sf input:not(:placeholder-shown)~kbd{display:none}
.clr{position:absolute;right:8px;top:8px;width:28px;height:28px;border-radius:50%;border:0;background:var(--surface-2);color:var(--text);cursor:pointer;animation:pop .14s ease-out}
@keyframes pop{from{opacity:0;transform:scale(.8)}}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
@media (hover:none){kbd{display:none}}
@media (prefers-reduced-motion:reduce){.sf{transition:none}.clr{animation:none}}
</style></head>
<body><div class="stage">
<form class="sf" role="search" onsubmit="return false">
<svg class="mag" viewBox="0 0 24 24" aria-hidden="true"><path d="M10 4a6 6 0 104.5 10l5 5 1.5-1.5-5-5A6 6 0 0010 4z"/></svg>
<label for="q" class="sr">Search components</label>
<input id="q" type="search" placeholder="Search components" autocomplete="off">
<kbd id="kbd">/</kbd>
<button type="button" class="clr" id="clr" aria-label="Clear search" hidden>×</button>
</form>
</div>
<script>
const q=document.getElementById('q'),clr=document.getElementById('clr');
q.addEventListener('input',()=>{clr.hidden=!q.value;});
clr.addEventListener('click',()=>{q.value='';clr.hidden=true;q.focus();});
q.addEventListener('keydown',e=>{if(e.key==='Escape'){if(q.value){q.value='';clr.hidden=true;}else q.blur();}});
document.addEventListener('keydown',e=>{if(e.key==='/'&&!/INPUT|TEXTAREA/.test(document.activeElement.tagName)){e.preventDefault();q.focus();}});
</script>
</body></html>