Toggle
A two-state button that can be on or off.
vue
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Toggle } from '@vase-ui/vue/toggle';
import styles from './index.module.css';
const ICON_PROPS = {
width: '16',
height: '16',
viewBox: '0 0 16 16',
fill: 'currentColor',
style: { display: 'block' },
};
const HeartFilledIcon: FunctionalComponent = () =>
h('svg', ICON_PROPS, [
h('path', {
d: 'M7.99961 13.8667C7.88761 13.8667 7.77561 13.8315 7.68121 13.7611C7.43321 13.5766 1.59961 9.1963 1.59961 5.8667C1.59961 3.80856 3.27481 2.13336 5.33294 2.13336C6.59054 2.13336 7.49934 2.81176 7.99961 3.3131C8.49988 2.81176 9.40868 2.13336 10.6663 2.13336C12.7244 2.13336 14.3996 3.80803 14.3996 5.8667C14.3996 9.1963 8.56601 13.5766 8.31801 13.7616C8.22361 13.8315 8.11161 13.8667 7.99961 13.8667Z',
}),
]);
const HeartOutlineIcon: FunctionalComponent = () =>
h('svg', ICON_PROPS, [
h('path', {
'fill-rule': 'evenodd',
'clip-rule': 'evenodd',
d: 'm7.99961 4.8232-.75505-.75666c-.40333-.40419-1.0559-.86651-1.91162-.86651-1.46903 0-2.66666 1.19764-2.66666 2.66667 0 .5412.24648 1.2356.75339 2.04713.49581.79376 1.17682 1.59861 1.89311 2.33647 1.06989 1.1022 2.1604 1.9962 2.68705 2.4102.52751-.4149 1.61735-1.3085 2.68657-2.4101.7163-.73792 1.3973-1.54278 1.8932-2.33656.5069-.81154.7533-1.50594.7533-2.04714 0-1.46947-1.1975-2.66667-2.6666-2.66667-.85574 0-1.50831.46232-1.91164.86651zm-.01387-1.52394c-.5031-.49988-1.40673-1.1659-2.6528-1.1659-2.05813 0-3.73333 1.6752-3.73333 3.73334 0 3.3296 5.8336 7.7099 6.0816 7.8944a.532.532 0 0 0 .3184.1056c.112 0 .224-.0352.3184-.1051.248-.185 6.08159-4.5653 6.08159-7.8949 0-2.05867-1.6752-3.73334-3.7333-3.73334-1.24617 0-2.14985.66611-2.65293 1.166q-.0069.00686-.0137.01367c.00002-.00003-.00002.00002 0 0-.00459-.0046-.00927-.00914-.01393-.01377',
}),
]);
</script>
<template>
<div :class="styles.Panel">
<Toggle v-slot="{ state }" aria-label="Favorite" :class="styles.Button">
<HeartFilledIcon v-if="state.pressed" />
<HeartOutlineIcon v-else />
</Toggle>
</div>
</template>css
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
width: 2rem;
height: 2rem;
padding: 0;
margin: 0;
border: none;
border-radius: 0;
background-color: transparent;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
color: white;
}
@media (hover: hover) {
&:hover:enabled {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:enabled {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&[aria-pressed='true'] {
color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
color: white;
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}vue
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Toggle } from '@vase-ui/vue/toggle';
const ICON_PROPS = {
width: '16',
height: '16',
viewBox: '0 0 16 16',
fill: 'currentColor',
style: { display: 'block' },
};
const HeartFilledIcon: FunctionalComponent = () =>
h('svg', ICON_PROPS, [
h('path', {
d: 'M7.99961 13.8667C7.88761 13.8667 7.77561 13.8315 7.68121 13.7611C7.43321 13.5766 1.59961 9.1963 1.59961 5.8667C1.59961 3.80856 3.27481 2.13336 5.33294 2.13336C6.59054 2.13336 7.49934 2.81176 7.99961 3.3131C8.49988 2.81176 9.40868 2.13336 10.6663 2.13336C12.7244 2.13336 14.3996 3.80803 14.3996 5.8667C14.3996 9.1963 8.56601 13.5766 8.31801 13.7616C8.22361 13.8315 8.11161 13.8667 7.99961 13.8667Z',
}),
]);
const HeartOutlineIcon: FunctionalComponent = () =>
h('svg', ICON_PROPS, [
h('path', {
'fill-rule': 'evenodd',
'clip-rule': 'evenodd',
d: 'm7.99961 4.8232-.75505-.75666c-.40333-.40419-1.0559-.86651-1.91162-.86651-1.46903 0-2.66666 1.19764-2.66666 2.66667 0 .5412.24648 1.2356.75339 2.04713.49581.79376 1.17682 1.59861 1.89311 2.33647 1.06989 1.1022 2.1604 1.9962 2.68705 2.4102.52751-.4149 1.61735-1.3085 2.68657-2.4101.7163-.73792 1.3973-1.54278 1.8932-2.33656.5069-.81154.7533-1.50594.7533-2.04714 0-1.46947-1.1975-2.66667-2.6666-2.66667-.85574 0-1.50831.46232-1.91164.86651zm-.01387-1.52394c-.5031-.49988-1.40673-1.1659-2.6528-1.1659-2.05813 0-3.73333 1.6752-3.73333 3.73334 0 3.3296 5.8336 7.7099 6.0816 7.8944a.532.532 0 0 0 .3184.1056c.112 0 .224-.0352.3184-.1051.248-.185 6.08159-4.5653 6.08159-7.8949 0-2.05867-1.6752-3.73334-3.7333-3.73334-1.24617 0-2.14985.66611-2.65293 1.166q-.0069.00686-.0137.01367c.00002-.00003-.00002.00002 0 0-.00459-.0046-.00927-.00914-.01393-.01377',
}),
]);
</script>
<template>
<Toggle
v-slot="{ state }"
aria-label="Favorite"
class="flex size-8 items-center justify-center border-none rounded-none bg-transparent text-neutral-950 dark:text-white select-none hover:enabled:bg-neutral-100 dark:hover:enabled:bg-neutral-800 active:enabled:bg-neutral-200 dark:active:enabled:bg-neutral-700 aria-pressed:text-neutral-950 dark:aria-pressed:text-white focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
>
<HeartFilledIcon v-if="state.pressed" />
<HeartOutlineIcon v-else />
</Toggle>
</template>Anatomy
Import the component and use it as a single part:
vue
<script setup lang="ts">
import { Toggle } from '@vase-ui/vue/toggle';
</script>
<template>
<Toggle />
</template>API reference
Toggle
A two-state button that can be on or off.
Renders a <button> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultPressed | boolean | undefined | false | The first pressed state of a toggle that owns its state. |
disabled | boolean | undefined | false | The user cannot press the toggle. |
modelValue | boolean | undefined | The pressed state. With no value, the toggle owns its state. | |
value | string | undefined | The value of the toggle in a ToggleGroup. Give it when the group has v-model or
defaultValue. With none, the toggle has a value of its own. | |
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: ToggleState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ToggleState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Events
| Event | Arguments | Description |
|---|---|---|
pressedChange | [pressed: boolean, details: ToggleChangeEventDetails] | The user presses the toggle. details.cancel() keeps the state as it was. |
update:modelValue | [pressed: boolean] | Comes after the change event with the new state, unless a listener cancels the change. v-model uses it. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[aria-pressed="true"] | The toggle is pressed. |
:disabled | The toggle is disabled, and it is a native button. |
[aria-disabled="true"] | The toggle is disabled, and it is not a native button, or a Toolbar.Button keeps it in the tab order. |
Differences from Base UI
Vase UI ports Toggle from Base UI 1.9.0.
API
| Base UI | Vase UI |
|---|---|
Toggle 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.