01Start with the task
Someone opens search because they have a destination in mind. The interface should shorten the distance to it. A decorative entrance, an unexplained icon, or a field that needs a second click all spend attention before the search has begun.
For a small writing site, the useful sequence is simple: open the dialog, put focus in the field, show the available posts, and narrow the results as the person types. Keep the article title prominent. Use the category and summary to distinguish similar results, rather than giving every piece of metadata equal visual weight.
02Give the dialog a name
A native dialog opened with showModal() provides modal behavior, including making the rest of the page inert. Give the dialog an accessible name and keep an explicit close button. Escape is useful, but a visible exit also helps people using touch.
<button id="open-search" aria-haspopup="dialog" aria-controls="search">Search posts</button>
<dialog id="search" aria-labelledby="search-title">
<h2 id="search-title">Find a post</h2>
<label for="query">Search by title or topic</label>
<input id="query" type="search" autocomplete="off" autofocus>
<p role="status" id="search-status"></p>
<ul id="results"></ul>
<form method="dialog"><button>Close search</button></form>
</dialog>const trigger = document.querySelector('#open-search')
const dialog = document.querySelector('#search')
const query = document.querySelector('#query')
trigger.addEventListener('click', () => {
dialog.showModal()
query.focus()
})
dialog.addEventListener('close', () => trigger.focus())This example covers opening, closing, and focus. The result list still needs your filtering and rendering logic. Use real links for results so opening a new tab and copying an address continue to work.
03Design the empty state
An empty query and an unsuccessful query are different states. Before typing, show recent posts or all posts if the collection is small. After a query finds nothing, keep the query visible and say what happened: “No posts match shader lighting.” Offer a clear-search action instead of an unrelated recommendation.
For local filtering, update results immediately. Announce a short result count in the status region after a small pause in typing; repeatedly announcing the entire list can interrupt a screen-reader user. If results come from a server, distinguish loading, no matches, and request failure. An unavailable service is not evidence that the post does not exist.
04Test the return trip
Walk through the interface using only a keyboard. Open search, type a query, tab through the results, close it with Escape, and confirm that focus returns to the trigger. Repeat with a long title, no results, browser zoom, and a narrow screen. The dialog should scroll internally when its contents outgrow the viewport.
Keep motion brief and honor reduced-motion preferences. A dialog feels polished when it gives people a predictable place to start and a reliable way back. That matters more than an elaborate entrance.
Reference: MDN — the dialog element.
