0.42.2 • Published 13 days ago

@spectrum-web-components/button v0.42.2

Weekly downloads
837
License
Apache-2.0
Repository
github
Last release
13 days ago

Description

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. sp-buttons in Spectrum have several variations for different uses and multiple levels of loudness for various attention-getting needs.

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';

Sizes

<sp-button size="s">Small</sp-button>
<sp-button size="m">Medium</sp-button>
<sp-button size="l">Large</sp-button>
<sp-button size="xl">Extra Large</sp-button>

Content

<sp-button> elements can be provided a visible label, a label with an icon, or just an icon (a non-visible label can be prived via the label attribute on an <sp-button> or on an <sp-icon*> element child to appropriately fulfill the accessibility contract of the button). An icon is provided by placing an icon element to the icon slot.

<sp-button-group>
    <sp-button variant="primary">Label only</sp-button>
    <sp-button variant="primary">
        <sp-icon-help slot="icon"></sp-icon-help>
        Icon + Label
    </sp-button>
    <sp-button variant="primary">
        <svg
            slot="icon"
            viewBox="0 0 36 36"
            focusable="false"
            aria-hidden="true"
            role="img"
        >
            <path
                d="M16 36a4.407 4.407 0 0 0 4-4h-8a4.407 4.407 0 0 0 4 4zm9.143-24.615c0-3.437-3.206-4.891-7.143-5.268V3a1.079 1.079 0 0 0-1.143-1h-1.714A1.079 1.079 0 0 0 14 3v3.117c-3.937.377-7.143 1.831-7.143 5.268C6.857 26.8 2 26.111 2 28.154V30h28v-1.846C30 26 25.143 26.8 25.143 11.385z"
            ></path>
        </svg>
        SVG Icon + Label
    </sp-button>
    <sp-button variant="primary" label="Icon only" icon-only>
        <sp-icon-help slot="icon"></sp-icon-help>
    </sp-button>
</sp-button-group>

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:

<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>
<sp-button-group style="min-width: max-content">
    <sp-button variant="primary">Label only</sp-button>
    <sp-button variant="primary">
        <sp-icon-help slot="icon"></sp-icon-help>
        Icon + Label
    </sp-button>
    <sp-button variant="primary" label="Icon only" icon-only>
        <sp-icon-help slot="icon"></sp-icon-help>
    </sp-button>
</sp-button-group>
<sp-button-group style="min-width: max-content">
    <sp-button variant="secondary">Label only</sp-button>
    <sp-button variant="secondary">
        <sp-icon-help slot="icon"></sp-icon-help>
        Icon + Label
    </sp-button>
    <sp-button variant="secondary" label="Icon only" icon-only>
        <sp-icon-help slot="icon"></sp-icon-help>
    </sp-button>
</sp-button-group>
<sp-button-group style="min-width: max-content">
    <sp-button variant="negative">Label only</sp-button>
    <sp-button variant="negative">
        <sp-icon-help slot="icon"></sp-icon-help>
        Icon + Label
    </sp-button>
    <sp-button variant="negative" label="Icon only" icon-only>
        <sp-icon-help slot="icon"></sp-icon-help>
    </sp-button>
</sp-button-group>
<sp-button-group style="min-width: max-content">
    <sp-button static="black">Label only</sp-button>
    <sp-button static="black">
        <sp-icon-help slot="icon"></sp-icon-help>
        Icon + Label
    </sp-button>
    <sp-button static="black" label="Icon only" icon-only>
        <sp-icon-help slot="icon"></sp-icon-help>
    </sp-button>
</sp-button-group>
<sp-button-group style="min-width: max-content">
    <sp-button static="white">Label only</sp-button>
    <sp-button static="white">
        <sp-icon-help slot="icon"></sp-icon-help>
        Icon + Label
    </sp-button>
    <sp-button static="white" label="Icon only" icon-only>
        <sp-icon-help slot="icon"></sp-icon-help>
    </sp-button>
</sp-button-group>

Treatment

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

<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>
<sp-button-group style="min-width: max-content">
    <sp-button treatment="outline" variant="primary">
        Primary, Outline
    </sp-button>
    <sp-button treatment="outline" variant="secondary">
        Secondary, Outline
    </sp-button>
    <sp-button treatment="outline" variant="negative">
        Negative, Outline
    </sp-button>
</sp-button-group>
<sp-button-group
    style="background: var(--spectrum-seafoam-600); padding: 0.5em; min-width: max-content"
>
    <sp-button treatment="outline" static="black">Label only</sp-button>
    <sp-button treatment="outline" static="black">
        <sp-icon-help slot="icon"></sp-icon-help>
        Icon + Label
    </sp-button>
    <sp-button treatment="outline" static="black" label="Icon only" icon-only>
        <sp-icon-help slot="icon"></sp-icon-help>
    </sp-button>
</sp-button-group>
<sp-button-group
    style="background: var(--spectrum-seafoam-600); padding: 0.5em; min-width: max-content"
>
    <sp-button treatment="outline" static="white">Label only</sp-button>
    <sp-button treatment="outline" static="white">
        <sp-icon-help slot="icon"></sp-icon-help>
        Icon + Label
    </sp-button>
    <sp-button treatment="outline" static="white" label="Icon only" icon-only>
        <sp-icon-help slot="icon"></sp-icon-help>
    </sp-button>
</sp-button-group>

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

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

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

Note: pending state of the <sp-button> element is applied after 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>

Handling events

Events handlers for clicks and other user actions can be registered on a <sp-button> as 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> 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

The autofocus attribute sets focus to 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>
0.42.2

13 days ago

0.42.1

14 days ago

0.42.0

28 days ago

0.41.2

1 month ago

0.41.1

2 months ago

0.41.0

2 months ago

0.40.5

2 months ago

0.40.4

3 months ago

0.40.3

3 months ago

0.40.2

4 months ago

0.40.1

4 months ago

0.35.1-rc.15

9 months ago

0.35.1-rc.25

9 months ago

0.35.1-rc.26

9 months ago

0.35.1-rc.24

9 months ago

0.38.0

7 months ago

0.35.1-rc.34

8 months ago

0.35.1-rc.43

8 months ago

0.35.1-rc.41

8 months ago

0.39.1

6 months ago

0.39.0

7 months ago

0.39.4

6 months ago

0.39.3

6 months ago

0.39.2

6 months ago

0.36.0

8 months ago

0.40.0

5 months ago

0.37.0

8 months ago

0.34.1-rc.0

9 months ago

0.34.0

9 months ago

0.35.0

9 months ago

0.33.3-overlay.66

10 months ago

0.33.3-overlay.65

10 months ago

0.33.3-overlay.61

10 months ago

0.31.1-react.21

11 months ago

0.32.0

11 months ago

0.31.1-react.3

11 months ago

0.31.1-react.2

11 months ago

0.30.1-overlay.30

11 months ago

0.33.1-overlay.39

10 months ago

0.30.1-overlay.31

11 months ago

0.33.2

10 months ago

0.33.0

10 months ago

0.30.1-overlay.38

11 months ago

0.30.1-overlay.37

11 months ago

0.30.1-overlay.41

11 months ago

0.30.1-overlay.42

11 months ago

0.30.1-overlay.40

11 months ago

0.30.0

12 months ago

0.33.1-overlay.41

10 months ago

0.32.1-overlay.33

11 months ago

0.32.1-overlay.41

11 months ago

0.31.0

11 months ago

0.31.1-overlay.29

11 months ago

0.20.6-overlay.18

12 months ago

0.20.6-overlay.17

12 months ago

0.20.6-overlay.14

12 months ago

0.20.6-overlay.15

12 months ago

0.20.6-overlay.12

12 months ago

0.20.6-overlay.13

12 months ago

0.20.6-overlay.10

12 months ago

0.20.6-overlay.11

12 months ago

0.20.5

12 months ago

0.20.4

1 year ago

0.20.3

1 year ago

0.20.2

1 year ago

0.20.1

1 year ago

0.20.0

1 year ago

0.19.8-react.54

1 year ago

0.19.8

1 year ago

0.19.9

1 year ago

0.19.10

1 year ago

0.19.3

2 years ago

0.19.4

1 year ago

0.19.5

1 year ago

0.19.6

1 year ago

0.19.7

1 year ago

0.19.1

2 years ago

0.19.2

2 years ago

0.18.1-devmode.0

2 years ago

0.17.7

2 years ago

0.18.1-devmode.7

2 years ago

0.18.1

2 years ago

0.18.0

2 years ago

0.19.0

2 years ago

0.19.0-devmode.0

2 years ago

0.18.2-devmode.0

2 years ago

0.17.4

2 years ago

0.17.5

2 years ago

0.17.6

2 years ago

0.17.3

2 years ago

0.17.2

2 years ago

0.17.0

2 years ago

0.17.1

2 years ago

0.16.3-express.0

2 years ago

0.16.5-express.9

2 years ago

0.16.3

2 years ago

0.16.4

2 years ago

0.16.2

2 years ago

0.15.1

2 years ago

0.16.0

2 years ago

0.16.1

2 years ago

0.15.0

2 years ago

0.14.9

3 years ago

0.14.8

3 years ago

0.14.7

3 years ago

0.14.6

3 years ago

0.14.6-beta.32

3 years ago

0.14.6-alpha.30

3 years ago

0.14.5

3 years ago

0.14.6-alpha.1

3 years ago

0.14.4

3 years ago

0.14.5-alpha.1

3 years ago

0.14.4-alpha.1

3 years ago

0.14.3

3 years ago

0.14.2

3 years ago

0.14.3-alpha.1

3 years ago

0.14.2-alpha.14

3 years ago

0.14.2-alpha.13

3 years ago

0.14.2-alpha.12

3 years ago

0.14.2-alpha.1

3 years ago

0.14.1

3 years ago

0.14.1-alpha.0

3 years ago

0.14.0

3 years ago

0.13.8-alpha.0

3 years ago

0.13.7

3 years ago

0.13.6

3 years ago

0.13.5

3 years ago

0.13.5-alpha.26

3 years ago

0.13.5-alpha.23

3 years ago

0.13.5-alpha.21

3 years ago

0.13.5-beta.10

3 years ago

0.13.5-beta.11

3 years ago

0.13.5-beta.12

3 years ago

0.13.5-beta.13

3 years ago

0.13.5-beta.14

3 years ago

0.13.5-beta.15

3 years ago

0.13.5-beta.16

3 years ago

0.13.5-beta.17

3 years ago

0.13.5-alpha.17

3 years ago

0.13.5-alpha.10

3 years ago

0.13.5-alpha.12

3 years ago

0.13.5-alpha.11

3 years ago

0.13.5-alpha.9

3 years ago

0.13.4

3 years ago

0.13.3

3 years ago

0.13.2

3 years ago

0.13.1

3 years ago

0.13.0

3 years ago

0.11.3-alpha.33

3 years ago

0.12.0

3 years ago

0.11.2

3 years ago

0.11.1

3 years ago

0.11.0

3 years ago

0.9.5-alpha.178

3 years ago

0.10.0-alpha.40

3 years ago

0.10.0

3 years ago

0.9.5-alpha.169

3 years ago

0.9.5-alpha.84

3 years ago

0.9.5-alpha.85

3 years ago

0.9.5-alpha.60

3 years ago

0.9.4

4 years ago

0.9.4-alpha.0

4 years ago

0.9.3

4 years ago

0.9.3-beta.17

4 years ago

0.9.3-alpha.17

4 years ago

0.9.2

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.8.4-alpha.71

4 years ago

0.8.4

4 years ago

0.8.4-alpha.28

4 years ago

0.8.3

4 years ago

0.8.2

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.1-alpha.38

4 years ago

0.7.1-alpha.6

4 years ago

0.7.0

4 years ago

0.6.0

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.9-alpha.1

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.2

4 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.1

5 years ago