Skip to content

Search

An lt-input with room for a magnifying glass. Both classes are set, so every state and variable from Input applies here too.

type="search" gives the browser’s own cross for clearing the field, and “Search” instead of “Enter” on the mobile keyboard. Safari and Chrome draw the cross differently, Firefox does not draw it at all, and whether Escape clears the field varies.

role="search" on the form lets screen readers jump straight to the search on the page.

The placeholder disappears as soon as the user types, and several screen readers do not read it out. If the field is so obviously a search that a visible label becomes noise, hide it with .lt-sr-only rather than leaving it out.

<label class="lt-label lt-sr-only" for="search">Search the cases</label>
<input class="lt-input lt-search" type="search" id="search" name="q" />
<p class="lt-sr-only" role="status" id="matches"></p>
<script type="module">
const field = document.getElementById("search")
const status = document.getElementById("matches")
let timeout
field.addEventListener("input", () => {
clearTimeout(timeout)
timeout = setTimeout(async () => {
const response = await fetch(`/api/search?q=${encodeURIComponent(field.value)}`)
const cases = await response.json()
renderResults(cases)
status.textContent = `${cases.length} matches`
}, 300)
})
</script>

The 300 millisecond delay is not only for the server. Without it the number of matches is read out on every keystroke.

ClassWhat it is
.lt-searchThe search field. Used together with .lt-input.
.lt-search-rowThe row with the field and the button
VariableDefault
--lt-search-icon-spacevar(--lt-spacing-10)
--lt-search-icon-sizevar(--lt-spacing-5)

The icon comes from --lt-icon-search and follows the theme. See Customising.

  • Announce the number of matches in an element with role="status". Without it, someone listening to the page does not know whether the search found anything.
  • Do not remove the browser’s cross without offering something else. Clearing the field by hand is awkward on a phone.
  • If search is the main function of the page, let the field have focus from the start. If it is one of several tools, do not: focus that jumps is confusing.