Components
Button
Button allows users to perform actions.
HTML
Unable to parse html
const PreviewEn = () => { return <Button>My button</Button>; }; render(<PreviewEn />)
Usage
The class name ds-button is set on <button>.
There are three data attributes you can use for customization:
data-icon - add if the button only has an icon.
data-variant - sets the variant of the button (primary, secondary, tertiary).
data-fullwidth - add if the button should be full width.
CSS variables and data attributes
Sizes are controlled with data-size and colors with data-color. The component will inherit the closest parent where these are set.
| Name | Value |
|---|---|
| data-variant | secondary, tertiary |
| data-icon | |
| data-fullwidth |
| Name | Value |
|---|---|
| --dsc-button-background--active | var(--ds-color-base-active) |
| --dsc-button-background--hover | var(--ds-color-base-hover) |
| --dsc-button-background | var(--ds-color-base-default) |
| --dsc-button-color | var(--ds-color-base-contrast-default) |
| --dsc-button-color--hover | var(--ds-color-base-contrast-default) |
| --dsc-button-border-width | var(--ds-border-width-default) |
| --dsc-button-border-style | solid |
| --dsc-button-border-color | transparent |
| --dsc-button-gap | var(--ds-size-2) |
| --dsc-button-padding | var(--ds-size-2) var(--ds-size-4) |
| --dsc-button-size | var(--ds-size-12) |
Example
Variants
variant is used to change the appearance of the button.
HTML
Unable to parse html
const Variants = () => { return ( <> <Button variant='primary'>Primary</Button> <Button variant='secondary'>Secondary</Button> <Button variant='tertiary'>Tertiary</Button> </> ); }; render(<Variants />)
Icon
If you only have an icon in the button, you can set data-icon="true" to make it square. If you have other content, such as text, the button will automatically get spacing around the icon.
HTML
Unable to parse html
const IconsEn = () => ( <> <Button icon aria-label='Icon only'> <PencilWritingIcon aria-hidden /> </Button> <Button> <PencilWritingIcon aria-hidden /> Edit </Button> </> ); render(<IconsEn />)
Buttons that load
If you want a button that loads, you have to add a loading indicator yourself.
Note: Neither aria-busy nor aria-disabled automatically prevents the button from triggering onClick.
You must ensure that the callback function does not run when the button is disabled.
React
In React, we also set type="button" by default, to avoid unexpected submits in forms.
- variant
- Description
Specify which variant to use
- Type
"primary" | "secondary" | "tertiary"- Default
primary
- icon
- Description
Toggle icon only styling, pass icon as children When combined with loading, the loading-icon will be shown instead of the icon.
- Type
boolean- Default
false
- loading
- Description
Toggle loading state. Pass an element if you want to display a custom loader.
- Type
ReactNode- Default
false
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior.
- Type
boolean- Default
false
- type
- Description
Specify the type of button. Unset when `asChild` is true
- Type
"button" | "submit" | "reset"- Default
'button'
- command
- Description
Native invoker commands. Specifies actions to perform on an element specified by commandfor. Polyfilled by designsystemet-web and includes a custom --show-non-modal command. "show-modal", "close", "request-close", "show-popover", "hide-popover", "toggle-popover", "--show-non-modal"
- Type
string
- commandfor
- Description
Specifies the target element for "command". value is ID of target
- Type
string
- commandFor
- Type
string
| Name | Type | Default | Description |
|---|---|---|---|
| variant | "primary" | "secondary" | "tertiary" | primary | Specify which variant to use |
| icon | boolean | false | Toggle icon only styling, pass icon as children When combined with loading, the loading-icon will be shown instead of the icon. |
| loading | ReactNode | false | Toggle loading state. Pass an element if you want to display a custom loader. |
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. |
| type | "button" | "submit" | "reset" | 'button' | Specify the type of button. Unset when `asChild` is true |
| command | string | - | Native invoker commands. Specifies actions to perform on an element specified by commandfor. Polyfilled by designsystemet-web and includes a custom --show-non-modal command. "show-modal", "close", "request-close", "show-popover", "hide-popover", "toggle-popover", "--show-non-modal" |
| commandfor | string | - | Specifies the target element for "command". value is ID of target |
| commandFor | string | - | - |
Extra logic for buttons that load
In React, we use loading={true} to show that the button is loading something. Here you can also pass in your own loading component if our Spinner does not fit.
React
Unable to parse html
const LoadingEn = () => ( <> <Button variant='primary' loading> Loading… </Button> <Button variant='secondary' loading> Loading… </Button> <Button variant='tertiary' loading> Loading… </Button> </> ); render(<LoadingEn />)
In the example above, aria-disabled is automatically set when loading={true}. This is the recommended way to show that a button is disabled. Then the button can still get focus when the user navigates with the keyboard. This way, screen readers and other assistive devices are informed that the button exists, but that it is not active.
Button as link
Links can be styled to look like a button, but consider whether the Link component is a better choice first. To change the button to a link, use asChild. Read more about composition in Designsystemet.
HTML
Unable to parse html
const AsLinkEn = () => ( <Button asChild> <a target='_blank' rel='noreferrer' href='https://www.designsystemet.no'> Go to designsystemet.no </a> </Button> ); render(<AsLinkEn />)