Visual Studio Code Button

March 8, 2023 · View on GitHub

The vscode-button is a web component implementation of a button element. The vscode-button also supports several visual appearances––primary, secondary, and icon.

Button hero

Usage

Types

TypeExampleUsage
PrimaryPrimary buttonEmphasizes the highest priority action in a view.
SecondarySecondary buttonUsed for additional actions in a view that already features a primary action.
IconIcon buttonA space-efficient style that renders a single icon to represent a specific action.

Best Practices

❌ Don't✅ Do
Multiple primary buttonsOne primary and multiple secondary buttons
Don't use multiple primary buttons in close proximity.Provide a single primary button with one or more secondary actions
❌ Don't✅ Do
Buttons with incorrect casingButtons with correct casing
Don't use fully capitalized or lowercase text.Use sentence case for all button text.
❌ Don't✅ Do
Button with a vague labelButton with a clear label
Don't use vague action text.Use clear verbs like "Save" or "Cancel" to ensure users feel confident when peforming actions.
❌ Don't✅ Do
Button used as a linkButton clearly associated with the view above
Don't use buttons as navigational elements. Use a vscode-link instead.Use buttons to perform actions relevant to the current view.
❌ Don't✅ Do
Icon button used for primary actionIcon buttons in a group
Don't use an icon button for primary actions.Use icon buttons for supporting actions in space-constrained layouts. Use icons that convey clear outcomes.

Implementation

Interactive component examples

Attributes

AttributeTypeDescription
appearancestringDetermines the visual appearance (primary, secondary, icon) of the button.
aria-labelstringDefines a label for buttons that screen readers can use.
autofocusbooleanDetermines if the element should receive document focus on page load.
disabledbooleanPrevents the user from interacting with the button––it cannot be pressed or focused.
formstringSee MDN.
formactionstringSee MDN.
formenctypestringSee MDN.
formmethodstringSee MDN.
formnovalidatestringSee MDN.
formtargetstringSee MDN.
namestringSee MDN.
typestringSee MDN.
valuestringSee MDN.

Basic Button

<vscode-button>Button Text</vscode-button>

Appearance Attribute

There are a number of visual appearances that the vscode-button can have. The default appearance is primary.

<vscode-button appearance="primary">Button Text</vscode-button>
<vscode-button appearance="secondary">Button Text</vscode-button>
<vscode-button appearance="icon">
  <span class="codicon codicon-check"></span>
</vscode-button>

Autofocus Attribute

<vscode-button autofocus>Button Text</vscode-button>

Disabled Attribute

<vscode-button disabled>Button Text</vscode-button>

Start Icon

An icon can be added to the left of Button text by adding an element with the attribute slot="start".

<!-- Note: Using Visual Studio Code Codicon Library -->

<vscode-button>
  Button Text
  <span slot="start" class="codicon codicon-add"></span>
</vscode-button>

Icon Only

An icon can also fill the default slot of the Button component (instead of text) to create an icon button by using the appearance="icon" attribute and value.

❗️❗️❗️ Important ❗️❗️❗️

Because icon buttons do not have text that can be used by screen readers, they are not meaningfully/semantically accessible by default.

An aria-label of "Icon Button" is automatically defined on all icon buttons so they are still technically accessible out of the box, however, a descriptive and meaningful label that fits the use case or context of the icon button should be defined to replace the default label.

For example, if you're using an icon button to confirm a state change, adding an aria-label with the value "Confirm" or "Confirm Changes" would be appropriate.

<!-- Note: Using Visual Studio Code Codicon Library -->

<vscode-button appearance="icon" aria-label="Confirm">
  <span class="codicon codicon-check"></span>
</vscode-button>