On Buttons and Links
Tags: webcomponents, designsystems, design, css
Published: 2021-10-06 14:32:00

I posted a tweet yesterday that was met with some controversy, so I thought it would be a good idea to unravel it.

{% twitter 1445479402532261892 %}

At face value, I understand why people are upset with this. It's hard to provide context in 280 characters, though, so [I made it a thread](https://twitter.com/claviska/status/1445479402532261892). That was a mistake, because people don't always read the thread so the intent gets lost and assumptions get made. Twitter is a wonderful place.

## Semantically Different

Let's start by stating the obvious. A button and a link are [semantically different HTML elements](https://www.webaxe.org/proper-use-buttons-links/), so no need to bust out the keyboard to tell me that. The idea of merging the two into a single component comes from real world experience working on multiple design system teams for large companies.

The fact is that designers always, always ask for links that look like buttons and buttons that look like links. I know, [designers shouldn't do this](https://twitter.com/oliverturner/status/1445724464860127244?s=20). But this isn't a fantasy, it's our reality, and [when it costs your organization measurable value](https://twitter.com/claviska/status/1445722314889437184) in terms of conversions, sales, etc., you're not going to win that argument.

Emotions aside, this often leads to developers producing two separate [button](https://explore.fast.design/components/fast-button) and [link](https://explore.fast.design/components/fast-anchor) components with the same styles and logic, and consumers end up misusing them just like `<button>` and `<a>`.

To alleviate this, [I propose that a custom element](https://twitter.com/claviska/status/1445480631832436747) such as `<my-button>` should render the same "button" visually, but based on the presense of an optional `href` attribute, it will use a `<button>` or an `<a>` under the hood.

```html
<my-button>Save</my-button>
<!-- 👉 <button class="button button--primary">Save</button> -->

<my-button href="/new">New</my-button>
<!-- 👉 <a class="button button--primary" href="/new">New</a> -->
```

This makes it more foolproof — if it goes somewhere, it becomes a link. If not, it's remains a button. So far, we're more or less aligned with the Twittersphere&hellip;[except for one detail](https://twitter.com/EmmaJ_PR/status/1445664177821925387?s=20):

_They should look different because they should do different things._

## Visually Different?

As much as I want to agree with this statement, it falls apart in practice. _Links will inevitably need to look like buttons from time to time._ No, buttons and links are not _semantically_ the same, but there are times when they need to be _visually_ the same.

In web apps, the function of buttons and links is often blended. Some actions, such as "new," might simply open a new page. Others aren't as obvious. "Save" might fire off an [XHR](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) and redirect you to another page if the response is successful — or go nowhere and show an error if the response fails.

Suddenly, the line between "button" and "link" is more blurry. Should it still be a link if it takes me somewhere only _after_ an XHR is successful? What if the request errors out and doesn't redirect? What if you click a link and the app prevents navigation because you have unsaved changes? There are so many scenarios and "what ifs" that, in practice, it's hard to settle on a 100% agreeable definition.

And then comes the design team with a Figma for the app's toolbar.

![Button group with four actions: new, edit, settings, download](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tj4buvuhvzp5vjiel8wd.png)

We'll assume that "New" takes you to `/new` and "Edit" takes you to `/edit/:id`. Those should definitely be links! But wait — "Settings" doesn't go anywhere. It just opens up a dialog, so it needs to be a button. And instead of creating an endpoint on the server that downloads the file, we'll use the [`download`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-download) attribute so that also needs to be a link.

Let's adjust the design to account for the idea that buttons and links _must_ be visually different.

![A button group with a new link, edit link, settings button, and a download link](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztqg0xv5j1t388tbzyzg.png)

Do you think the design team is going to be thrilled with these changes to the toolbar? Do you think your customers will prefer this toolbar over the previous one?

## What Users Expect

If the user is presented with a set of actions, _they don't care if you use buttons or links under the hood_. They care that it's consistent and that each control does what it's supposed to do. Remember that in all cases, each control's action is clearly inferred from its label.

Whether clicking "Delete" takes the user to a new page to confirm or opens a dialog to confirm is irrelevant. To the user, they're all just "buttons" that do something.*

It is true that links and buttons have different behaviors and the decision to make them look visually similar should be carefully considered. For example, <kbd>CMD</kbd> or <kbd>CTRL</kbd> + click typically opens a link in a new window. When buttons and links are identical, it can put users at a disadvantage.

I would recommend _not_ making buttons and links look the same if you can reasonably expect the user will desire such behaviors. Some environments are more predictable than others. But accessibility isn't binary, so thought and consideration is still required from both design and development regardless of your approach.

## Enforcement via API

The suggestion I'm making enforces the semantic definition of "button" and "link" by leaning on API design to ensure <abbr title="Don't repeat yourself">DRY</abbr> principles and remove the burden of thought from consumers. This solves the problem of when to use a link or button under the hood.

It does not answer the question of "_should buttons and links be visually similar_?" That's a topic I'd love to have a conversation about because there are plenty of use cases that suggest at least sometimes they should.

Just know that, if you're working on a design system, it's extremely likely you will come across this requirement!

---

<small>

*This doesn't mean you should throw the baby out with the bathwater. It just means there might be situations where you need to use a link as a button, and the user doesn't care as long as it works. The download example comes to mind.

</small>