Skip to content

Your Browser Already Does That

3 min read1 views[JavaScript]
Cover image for "Your Browser Already Does That"

You need an expandable FAQ, a confirmation dialog, and a form that rejects an empty email address.

It is easy to turn that list into three package searches. Each package might be good. Each might also duplicate behavior the browser already provides.

The useful habit is to check the platform before deciding how much infrastructure the feature needs. In my jQuery article, I wrote about how browser improvements change the job of a library. The same question applies to the components we build today.

An expandable section can be HTML

For a simple disclosure, start here:

<details>
  <summary>What does the deployment include?</summary>
  <p>The application, its static assets, and database migrations.</p>
</details>

The browser supplies the open/closed interaction. You can style the elements and use the open attribute for the initial state. You don't need a click handler and a separate JavaScript boolean just to reveal that paragraph. MDN: details

That doesn't mean every component with hidden content is a disclosure. A navigation menu, a tabbed interface, and a searchable tree have different interaction requirements. Choose an element for what it means, then style it for the design.

A useful review question is: if the CSS disappeared, would this still describe the interaction correctly?

A modal doesn't have to start with a div

Here's a small information dialog:

<button type="button" id="show-shortcuts">Keyboard shortcuts</button>

<dialog id="shortcuts" aria-labelledby="shortcuts-title">
  <h2 id="shortcuts-title">Keyboard shortcuts</h2>
  <p>Press / to focus the search field.</p>
  <form method="dialog">
    <button autofocus>Close</button>
  </form>
</dialog>

<script>
  const trigger = document.querySelector('#show-shortcuts');
  const dialog = document.querySelector('#shortcuts');

  trigger.addEventListener('click', () => dialog.showModal());
</script>

Calling showModal() gives you modal behavior, including making the rest of the document inert. A dialog form can close it without submitting to a server. Native dialog behavior also includes keyboard dismissal and focus handling, subject to the element's configuration and browser support. MDN: dialog

You still own the accessible name, suitable initial focus, clear copy, and testing. Check keyboard navigation and focus restoration in the actual page. Putting a complex workflow inside a native element doesn't automatically make that workflow accessible.

For a real destructive action, add a proper submission flow with loading, success, and error states. The example above only displays information.

Let forms express their basic constraints

An email field can declare that it requires a value with email syntax:

<form action="/newsletter" method="post">
  <label for="email">Email address</label>
  <input
    id="email"
    name="email"
    type="email"
    autocomplete="email"
    required
  >
  <button type="submit">Subscribe</button>
</form>

The constraint validation API also provides ways to inspect validity and set custom validation messages. Native email validation checks syntax, not whether a mailbox exists or belongs to the person submitting it. Client-side validation can be bypassed, so the server must validate independently. MDN: constraint validation

For a larger form, I would decide deliberately how errors are presented, associated with fields, and announced. The browser gives you a starting point. Your product still needs an error experience that users can understand.

Less code is useful only when the behavior fits

Consider two proposed implementations of the FAQ. One uses a native disclosure. The other introduces a shared state store, click handlers, custom keyboard behavior, and a component dependency.

The second implementation might be justified if this is part of a larger interface with requirements the first cannot meet. But “we always use a library” is not a requirement.

Write down the interaction before choosing the implementation: can multiple answers stay open? Must an answer be linkable? Does the open state need to survive navigation? What happens without JavaScript?

Those questions give the decision something concrete to rest on.

Check support where your users are

Before adopting a feature, inspect its compatibility information and test your supported browsers, embedded webviews, and assistive technology combinations. New attributes can have a different support story from the element they extend.

The examples here are small enough to evaluate directly. Build a representative page, use only the keyboard, zoom the text, and test what happens when scripts fail.

A library earns its place when it solves requirements you actually have. Knowing the browser's capabilities makes it easier to recognize that moment—and easier to avoid maintaining another implementation of a feature you already had.

# Comments (0)

No comments yet. Be the first.

0/1000