Sub topic 3
Designing the search field
This chapter covers the search input itself: where it goes, how it looks, what it does on submit, and how it behaves on edge cases. It assumes you know basic HTML forms and can read TypeScript. Suggestions, typo tolerance, and result layout have their own chapters. Here the focus is the field and the contract it makes with the user.
What the search field promises
A search field tells the user: "type what you want, and the app will find it." If the field is hard to spot, too small, or behaves in surprising ways, users either skip it or fail with it. Most design decisions in this chapter come down to keeping that promise.
Three questions shape the design:
- Can users find the field without hunting?
- Does it accept the kind of input users actually type?
- Is it clear what happens after they type?
Visibility and placement
Users look for search in predictable places. On most sites and apps that is the top of the page, often at the top center or top right. Follow that convention unless you have evidence against it.
How prominent the field should be depends on how central search is to your app:
| App type | Example | Search treatment |
|---|---|---|
| Search is the main task | Online store, documentation site, mail client | Always-visible, wide field in the header |
| Search is one of several ways to find things | Settings, a media app with strong browsing | Visible field or an icon that expands into a field |
| Search is rarely needed | Small app with a short list | An icon, or no search at all |
An icon-only entry point saves space but costs discoverability. A magnifying-glass icon is widely understood, but a visible field with a text label or placeholder is easier to notice and easier to use. If the user would otherwise have to tap an icon before typing, you add a step to the most common action in the app.
Width
A field should be wide enough for a typical query. Queries are often short, but a field that shows only 10 characters makes users scroll within it and lose sight of what they typed. As a rule of thumb, aim for the field to show a typical query without clipping. Wider fields also signal that longer, more specific queries are welcome.
Labels, placeholders, and icons
A search field needs an accessible name. A placeholder is not a label. Placeholder text disappears when the user types, has low contrast in many default styles, and is not reliably announced by every screen reader.
You have two good options:
- A visible
<label>next to or above the field. This is the most robust choice. - A visually hidden label, plus a visible submit button that says "Search" or shows a clear icon. This works when the layout is tight and the field's purpose is obvious from context.
Use the placeholder only for a short hint, such as an example query. Do not put the only instruction there.
The example below shows a search landmark with a visible label, a submit button, and a status message that announces the result count to screen readers.
<form role="search" action="/search" method="get">
<label for="q">Search products</label>
<input id="q" name="q" type="search" autocomplete="off">
<button type="submit">Search</button>
</form>
<p role="status">24 results for "boots"</p>Points worth noting in this markup:
role="search"marks the form as a search landmark. Screen reader users can jump straight to it.type="search"gives the browser a hint. Many browsers add a clear control and offer better keyboard behavior on mobile.method="get"puts the query in the URL (/search?q=boots). That makes the results page shareable and lets the back button work. The chapter on filter state covers this in more depth.role="status"creates a polite live region. When its text changes, screen readers announce it without moving focus.autocomplete="off"turns off the browser's own history dropdown. Use it only when you provide your own suggestions. Otherwise leave browser autofill alone, because it helps users.
Scope hints
If search covers only part of the app, say so. "Search products" is clearer than "Search". When the scope can change, such as searching inside a folder or across the whole workspace, show the current scope near the field and let users widen it. A user who searches a folder and gets no results may conclude the item does not exist, when the app only looked in the wrong place.
Submit behavior
There are two common models:
- Submit-driven. Results change only when the user presses Enter or the search button. This is predictable and cheap for the server.
- Live (search-as-you-type). Results update as the user types, usually after a short pause. This feels fast when the data set is small and the response is quick.
Choose based on cost and risk, not fashion.
| Factor | Submit-driven | Live |
|---|---|---|
| Server load | One request per search | Many requests per search |
| Layout stability | Page changes only on user action | Results shift while the user types |
| Screen reader noise | Low | Can be high if each update is announced |
| Best for | Large catalogs, expensive queries, full results pages | Small lists, local filtering, command palettes |
Whichever model you pick, support both Enter and a visible button. Some users never press Enter, and some never click. A form element gives you both for free.
If you do use live results, delay the request until the user pauses. This is called debouncing. Waiting 150 to 300 ms after the last keystroke is a common range. The code below shows a small debounce helper. Latency and feedback gets a full treatment in a later chapter.
function debounce<A extends unknown[]>(fn: (...args: A) => void, waitMs: number) {
let timer: ReturnType<typeof setTimeout> | undefined;
return (...args: A) => {
if (timer !== undefined) clearTimeout(timer);
timer = setTimeout(() => fn(...args), waitMs);
};
}
const onInput = debounce((value: string) => {
console.log("fetch suggestions for", value);
}, 200);Each call resets the timer, so the function runs only after 200 ms with no new input.
Input handling
The field should accept whatever users type without complaint. A few rules prevent most problems.
- Trim whitespace. Leading and trailing spaces are almost always accidental. Trim them before searching, but do not trim the field's visible value while the user is typing, or the cursor will jump.
- Do not restrict characters. Users search for product codes like
A-100/B, email addresses, and names with accents or apostrophes. Reject nothing. Escape the input properly on the server instead. - Allow paste and long input. Do not set a tight
maxlength. If you must cap length for performance, set a generous limit and tell the user when you cut a query. - Handle empty submits deliberately. Pressing Search with an empty field should do something sensible: show all items, show recent searches, or show a short message. Doing nothing leaves users unsure whether the button works.
- Preserve the query. After results load, the field should still show what the user typed. Clearing it forces users to retype to refine.
The clear button and keyboard behavior
A clear control (usually an "x") lets users reset the field in one action, which matters most on touch devices where selecting and deleting text is awkward. With type="search", many browsers provide one, but its look varies. If you build your own, make it a real <button> with an accessible name such as "Clear search", and return focus to the input after clearing.
Keyboard behavior to support:
- Enter submits the search.
- Escape clears the field or closes an open suggestion list. Pick one behavior and keep it consistent. A common pattern is: first Escape closes suggestions, second Escape clears the field.
- A shortcut to focus the field, such as
/, is a helpful addition for apps used all day. Do not trigger it while the user is typing in another input, and do not override browser shortcuts.
On mobile, set enterkeyhint="search" on the input so the on-screen keyboard shows a search key instead of a generic return key.
Common errors
- Placeholder as the only label. Fails for screen readers and disappears while typing.
- Search icon with no button behavior. A decorative magnifying glass that does nothing on click frustrates users who expect it to submit.
- Clearing the field after search. Users lose their query and cannot refine it.
- Firing a request per keystroke. Wastes server resources and can show stale results when responses arrive out of order.
- Moving focus on every result update. Screen reader users lose their place. Update a status region instead.
- Hiding scope. The user does not know what is being searched.
Putting it together
A solid default search field has these properties:
- It sits in a conventional place and is wide enough for a typical query.
- It has a visible or properly hidden label and a submit button.
- It uses a
searchlandmark andtype="search". - It submits on Enter and on button press.
- It keeps the user's query visible after results load.
- It offers a clear button and a consistent Escape behavior.
- It reports the result count in a polite status region.
- It states its scope when the scope is not obvious.
Exercise
A teammate builds a search field with only a placeholder reading "Search..." and an icon that does not respond to clicks. The field clears itself after each search and sends a request on every keystroke. List four problems and the fix for each.
Show answer
(1) Placeholder only: add a visible label or a visually hidden label, because placeholders vanish while typing and are not reliable accessible names. (2) Icon that does nothing: make it a real submit button, or add a button labeled "Search", so mouse and touch users can submit. (3) Clearing after search: keep the query in the field so users can refine it, and provide a separate clear button. (4) Request per keystroke: debounce input by about 150 to 300 ms, or switch to submit-driven search, and discard out-of-date responses.
Exercise
Your app lets users search inside a single project, but users keep reporting "search is broken" when they cannot find files from other projects. What design change addresses this without changing the search engine?
Show answer
Make the scope visible, for example with the label "Search in Project Alpha" near the field, and offer a clear way to widen the search to all projects. Also consider showing the scope in the no-results message.