sp-button

Overview API Changelog

Overview

Section titled Overview

An <sp-button> represents an action a user can take. sp-buttons can be clicked or tapped to perform an action or to navigate to another page. Buttons in Spectrum have several variations for different uses and multiple levels of loudness for various attention-getting needs.

Usage

Section titled Usage

See it on NPM! How big is this package in your project? Try it on webcomponents.dev

yarn add @spectrum-web-components/button

Import the side effectful registration of <sp-button> or <sp-clear-button> as follows:

import '@spectrum-web-components/button/sp-button.js';
import '@spectrum-web-components/button/sp-clear-button.js';
import '@spectrum-web-components/button/sp-close-button.js';

When looking to leverage the Button, ClearButton, or CloseButton base classes as a type and/or for extension purposes, do so via:

import { Button, ClearButton, CloseButton } from '@spectrum-web-components/button';

Anatomy

Section titled Anatomy

Content

Section titled Content

<sp-button> elements can be provided a visible label, a label and an icon, or just an icon.

An icon is provided by placing an icon element in the icon slot.

If the button is icon-only, a non-visible label can be provided via the label attribute on an <sp-button> or on an <sp-icon*> element child to appropriately fulfill the accessibility contract of the button.

Label only
<sp-button variant="primary">Label only</sp-button>
Icon + label SVG Icon + label Icon only

Options

Section titled Options

Sizes

Section titled Sizes
Small Medium
<sp-button size="m">Medium</sp-button>
Large Extra Large

Variants

Section titled Variants

There are many button variants to choose from in Spectrum. The variant attribute defaults to accent, but also accepts the following value: accent, primary, secondary, negative, white, and black. They display as follows:

Accent
<sp-button-group style="min-width: max-content">
    <sp-button variant="accent">Label only</sp-button>
    <sp-button variant="accent">
        <sp-icon-help slot="icon"></sp-icon-help>
        Icon + Label
    </sp-button>
    <sp-button variant="accent" label="Icon only" icon-only>
        <sp-icon-help slot="icon"></sp-icon-help>
    </sp-button>
</sp-button-group>
Primary Secondary Negative Black White

Treatment

Section titled Treatment

The treatment attribute accepts fill and outline as values, and defaults to fill. These display as follows:

Fill
<sp-button-group style="min-width: max-content">
    <sp-button treatment="fill" variant="primary">Primary, Fill</sp-button>
    <sp-button treatment="fill" variant="secondary">Secondary, Fill</sp-button>
    <sp-button treatment="fill" variant="negative">Negative, Fill</sp-button>
</sp-button-group>
Outline Outline, black Outline, white

States

Section titled States

In addition to the variant, <sp-button> elements support two different visual states, disabled and pending, which can be applied by adding the attribute disabled or pending respectively. All <sp-button> variants support these states.

Disabled

Section titled Disabled

While disabled, <sp-button> elements will not respond to click events and will appear faded.

<sp-button-group>
    <sp-button variant="primary">Normal</sp-button>
    <sp-button variant="primary" disabled>Disabled</sp-button>
</sp-button-group>

Pending

Section titled Pending

While in pending state, <sp-button> elements will not respond to click events and will appear faded with an indeterminate <sp-progress-circle>. The <sp-button> element's label and icon will be hidden while in pending state.

Note: The pending state of the <sp-button> element is applied after a 1s delay to avoid flashing the pending state for quick actions. You can override the delay by adding custom css var --pending-delay to your css.

<sp-button-group>
    <sp-button variant="primary">Normal</sp-button>
    <sp-button variant="primary" pending>Pending</sp-button>
</sp-button-group>

Behaviors

Section titled Behaviors

Handling events

Section titled Handling events

Events handlers for clicks and other user actions can be registered on a <sp-button> as one would on a standard HTML <button> element.

<sp-button onclick="spAlert(this, '<sp-button> clicked!')">Click me</sp-button>

In addition to handling events like a native <button> HTML element, one can also use a <sp-button> in place of the <a> HTML element by using the href and optional target attribute.

<sp-button
    href="https://github.com/adobe/spectrum-web-components"
    target="_blank"
>
    Click me
</sp-button>

Autofocus

Section titled Autofocus

The autofocus attribute sets focus on the <sp-button> when the component mounts. This is useful for setting focus to a specific sp-button when a popover or dialog opens.

<sp-button autofocus>Confirm</sp-button>

Accessibility

Section titled Accessibility

Include a label

Section titled Include a label

A button is required to have either a visible text label or a label attribute on either the <sp-button> itself or on an <sp-icon*> element child.

Don't override color

Section titled Don't override color

Do not use custom colors for buttons. The colors of different button variations have been designed to be consistent and accessible.

Don't mix href and non-href buttons in a set of buttons

Section titled Don't mix href and non-href buttons in a set of buttons

A screen reader user will not encounter href buttons when navigating by buttons or form controls. While they can both be used in the same page problems could occur if mixing the types in close proximity to each other.

Use static black or static white to contrast with backgrounds and images

Section titled Use static black or static white to contrast with backgrounds and images

To ensure maximum contrast with the background, use static black for light backgrounds and images, and static white for dark backgrounds and images. Avoid placing static components on top of busy images with a lot of variance in contrast.

Static black on light background
<div style="background-color: #ccffee; padding: 20px">
    <sp-button static="black">Click me</sp-button>
    <sp-button static="black" treatment="outline">Click me</sp-button>
</div>
Static white on dark background

Clearly state the action

Section titled Clearly state the action

Make sure that a button’s label clearly states the outcome of the action. Use the same word or phrase as found elsewhere in the experience.