Import#
import { Button } from '@dnb/eufemia'
Description#
The Button component should be used as the primary call-to-action in a form, or as a user interaction mechanism.
Relevant links#
Accessibility#
Buttons are keyboard accessible and support standard interaction patterns. Use meaningful text or aria-label when using icon-only buttons. Avoid using disabled buttons when possible, as they don't communicate why an action is unavailable.
Variants and sizes#
There should never be more than one primary button in a given context. The secondary and tertiary button variants do not have this constraint. A button should not be used when a link would suffice.
The Button component comes in different sizes.
Primary button sizes#
For the primary variant, the recommended sizes are default and large.
Secondary button sizes#
For the secondary variant, the recommended sizes are default and large.
Tertiary button sizes#
For tertiary buttons with text, use default or large, with default as the recommended size. A variant with iconPosition="top" is also available.
It is not recommended to use the tertiary button without an icon. Looking for a similar variant without an icon? Check out Anchor instead.
Icon button sizes#
Icon-only buttons support small, medium, default and large, including the tertiary variant. When size is omitted, an icon-only button uses medium.
size controls the button dimensions, while iconSize controls the icon dimensions. For example, this small tertiary button is 1.5rem square (24 px at the default root font size), with a 1rem (16 px) icon:
<Buttonvariant="tertiary"size="small"icon="add"iconSize="default"title="Add attachment"/>
Related components#
Button is part of the Actions category. Other components for similar needs:
- Anchor — to take people to another page, section, or website.
- HelpButton — when a short explanation should be available next to a label or control.
- Menu — when people need to choose from a small list of actions or links.
Demos#
Choose a variant#
Use a button to perform an action. Use Anchor for ordinary navigation.
- Primary: the main action. Use at most one in a given context.
- Secondary: other actions alongside the primary action.
- Tertiary: a less prominent action with an icon.
Expand Code below each example to edit it or copy the implementation.
Text buttons#
Use a short label that describes the action. Set variant="secondary" for a secondary action.
Primary button#
Secondary button#
Buttons with icons#
Set icon to add an icon. Icons appear on the right by default. Use iconPosition="left" to place one before the label.
Primary button with icon#
Primary button with icon on left#
Tertiary button#
Use variant="tertiary" with an icon for a less prominent action. The label can be plain text or a React element.
Icon above the label#
Use iconPosition="top" to place the icon above the label.
Wrapping long labels#
Set wrap to allow the label to span multiple lines. The icon stays aligned with the top of the text.
Icon button#
Use buttons with visible text by default. Only use an icon-only button when the action and what it affects are immediately clear to your users, without a tooltip. If there is any doubt, keep the text label.
Choose an icon that fits the context#
Use an existing Eufemia icon and keep its meaning consistent throughout your application. Even in the contexts below, only omit the text label if the action and its scope are unambiguous to your users.
| Icon | Action | Context to check |
|---|---|---|
| Close | In the dialog or panel it closes. | |
| Delete | Beside the attachment it deletes. | |
| Search | Beside a labeled search field. | |
| Edit | In the row of the item it edits. | |
| Add | In a list that shows what is added. |
Use visible text when#
- The action is important or difficult to undo, such as "Send payment" or "Delete account". A trash icon does not explain the scope of a deletion.
- The icon could mean several things. A plus could mean add an item, create an account, or zoom in. Use a label such as "Add account".
- People need a tooltip to understand the action. Tooltips can help, but should not be the only explanation, especially on touch screens.
Limited space, including on smaller screens, is not enough reason to remove a text label.
Name the action#
Every icon-only button needs an accessible name. Describe the action, such as "Delete invoice.pdf", rather than the shape, such as "Trash icon".
Set aria-label and add a matching tooltip for hover and keyboard focus. A tooltip alone does not provide an accessible name in Eufemia.
<Button icon="close" aria-label="Close" tooltip="Close" />
Alternatively, title supplies the accessible name automatically for icon-only buttons.
The labels above the buttons identify the sizes and states.
Sizes#
Use default or large for primary and secondary buttons. Icon-only buttons also support small and medium. See Variants and sizes for the size comparisons.
Anchor button#
Set href when navigation needs the visual emphasis of a button. Button renders an anchor element in this case. Prefer Anchor for ordinary links.
Disabled buttons#
Set disabled to make an action unavailable. Prefer keeping actions available where possible. A disabled button alone does not explain what the user needs to do next.
Error state#
Set status="error" to indicate an error state. When an action fails, also explain the problem and how to recover. A color change alone is not enough.
Button on dark surface#
Use Section or Theme.Context with surface="dark" to provide dark surface context to supporting components.
Custom button content#
Use customContent when the standard label and icon do not cover the content you need. You are responsible for aligning custom content.
Button with custom SVG#
A custom SVG can be part of the button's children. Give meaningful graphics an accessible name and hide decorative graphics from assistive technology. The button still needs a meaningful accessible name.