Button
A button that can render another tag and stay focusable while it is disabled.
<script setup lang="ts">
import { Button } from '@vase-ui/vue/button';
import styles from './index.module.css';
</script>
<template>
<Button :class="styles.Button">Submit</Button>
</template>.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not(:disabled, [aria-disabled='true']) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:not(:disabled, [aria-disabled='true']) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:is(:disabled, [aria-disabled='true']) {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}<script setup lang="ts">
import { Button } from '@vase-ui/vue/button';
</script>
<template>
<Button
class="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:not-disabled:not-aria-disabled:bg-neutral-100 active:not-disabled:not-aria-disabled:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white aria-disabled:border-neutral-500 aria-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-disabled:not-aria-disabled:bg-neutral-800 dark:active:not-disabled:not-aria-disabled:bg-neutral-700 dark:aria-disabled:border-neutral-400 dark:disabled:border-neutral-400 dark:aria-disabled:text-neutral-400 dark:disabled:text-neutral-400"
>
Submit
</Button>
</template>Usage guidelines
- Submit buttons: Button renders
type="button", so it does not submit its form as a native<button>does. Settype="submit"on Button for a submit button. - Links: Button has the semantics of a button: the role, the keys and the disabled state. Do not use it for a link. See Rendering links as buttons.
Anatomy
Import the component:
<script setup lang="ts">
import { Button } from '@vase-ui/vue/button';
</script>
<template>
<Button />
</template>Examples
Rendering as another tag
Give as another tag, such as div. Button then gives the element role="button", a tab stop and the keys Enter and Space, so it stays accessible from the keyboard.
<script setup lang="ts">
import { Button } from '@vase-ui/vue/button';
</script>
<template>
<Button as="div">Button that can contain complex children</Button>
</template>The server HTML has these attributes for a tag in as. Button knows the element of a component in as or in the element slot only at the mount. Until then, the element has the attributes of a native button and of another element: type="button", role="button", a tab stop and the disabled state. At the mount, Button keeps the attributes of its element.
Rendering links as buttons
A link (<a>) has its own semantics. Do not render a link in as or in the element slot of Button. To make a link look like a button, style the <a> element with CSS.
Loading states
Set focusableWhenDisabled on a button that becomes disabled while it loads. The button then keeps the focus.
Some combinations of a browser and a screen reader do not announce a change of the text in a focused button. Use aria-labelledby to name the button with the text that changes.
<script setup lang="ts">
import { ref, useId } from 'vue';
import { Button } from '@vase-ui/vue/button';
import styles from './index.module.css';
const loading = ref(false);
const labelId = useId();
function submit() {
loading.value = true;
setTimeout(() => {
loading.value = false;
}, 4000);
}
</script>
<template>
<Button
:class="styles.Button"
:disabled="loading"
focusable-when-disabled
:aria-labelledby="labelId"
@click="submit"
>
<span :id="labelId">{{ loading ? 'Submitting' : 'Submit' }}</span>
</Button>
</template>.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not(:disabled, [aria-disabled='true']) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:not(:disabled, [aria-disabled='true']) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:is(:disabled, [aria-disabled='true']) {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}<script setup lang="ts">
import { ref, useId } from 'vue';
import { Button } from '@vase-ui/vue/button';
const loading = ref(false);
const labelId = useId();
function submit() {
loading.value = true;
setTimeout(() => {
loading.value = false;
}, 4000);
}
</script>
<template>
<Button
class="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:not-disabled:not-aria-disabled:bg-neutral-100 active:not-disabled:not-aria-disabled:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white aria-disabled:border-neutral-500 aria-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-disabled:not-aria-disabled:bg-neutral-800 dark:active:not-disabled:not-aria-disabled:bg-neutral-700 dark:aria-disabled:border-neutral-400 dark:disabled:border-neutral-400 dark:aria-disabled:text-neutral-400 dark:disabled:text-neutral-400"
:disabled="loading"
focusable-when-disabled
:aria-labelledby="labelId"
@click="submit"
>
<span :id="labelId">{{ loading ? 'Submitting' : 'Submit' }}</span>
</Button>
</template>API reference
Button
A button that starts an action.
Renders a <button> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | undefined | false | The user cannot press the button. |
focusableWhenDisabled | boolean | undefined | false | A disabled button stays in the tab order, and a focused button keeps the focus. A disabled fieldset still disables a native button. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: ButtonState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ButtonState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
:disabled | The button is disabled, and it is a native button out of the tab order. |
[aria-disabled="true"] | The button is disabled, and it stays in the tab order or is not a native button. :enabled also matches such a native button, so select an enabled button with :not(:disabled, [aria-disabled="true"]). |
Differences from Base UI
Vase UI ports Button from Base UI 1.9.0.
API
| Base UI | Vase UI |
|---|---|
Button prop nativeButton | The part reads the element that it renders. as and the element slot render another element, so no prop says what the developer renders. |
Behavior
| Vase UI | Evidence |
|---|---|
A button part knows the element of a component in as or of the element slot only at the mount, and it needs no prop that says what the element is. Until the mount, the element has the attributes that suit a <button> and each other element: type="button", role="button", a tab stop, and the disabled state of both. So in the server HTML, Tab reaches a <div> before a lazy hydration, and a <button> does not submit its form. At the mount, the part keeps the attributes of its element. Base UI needs nativeButton set to false for an element that is not a <button>. |
|
The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.