Toggle Button
A two-state button that can be either on or off.
Default
<script setup>
import { ref } from 'vue'
const isChecked = ref(false)
</script>
<template>
<PToggleButton v-model="isChecked" aria-label="Toggle bold" label="Bold" />
</template>
Sizes
<script setup>
import { ref } from 'vue'
const isChecked = ref(false)
</script>
<template>
<PStack align="center">
<PToggleButton v-model="isChecked" size="sm" aria-label="Toggle bold" label="Bold" />
<PToggleButton v-model="isChecked" size="md" aria-label="Toggle bold" label="Bold" />
<PToggleButton v-model="isChecked" size="lg" aria-label="Toggle bold" label="Bold" />
</PStack>
</template>
Ghost
<script setup>
import TextBoldIcon from '@gdsicon/vue/text-bold'
import { ref } from 'vue'
const checked = ref(false)
</script>
<template>
<PToggleButton v-model="checked" variant="ghost" aria-label="Toggle bold">
<TextBoldIcon />
</PToggleButton>
</template>
Disabled
<script setup>
import TextBoldIcon from '@gdsicon/vue/text-bold'
import { ref } from 'vue'
const uncheck = ref(false)
const checked = ref(true)
</script>
<template>
<PStack align="center" class="pxd-toggle-button-group">
<PToggleButton v-model="uncheck" disabled aria-label="Toggle bold" icon>
<TextBoldIcon />
</PToggleButton>
<PToggleButton v-model="uncheck" disabled variant="ghost" aria-label="Toggle bold" icon>
<TextBoldIcon />
</PToggleButton>
<PToggleButton v-model="checked" disabled aria-label="Toggle bold" icon>
<TextBoldIcon />
</PToggleButton>
<PToggleButton v-model="checked" disabled variant="ghost" aria-label="Toggle bold" icon>
<TextBoldIcon />
</PToggleButton>
</PStack>
</template>
Group
<script setup>
import { ref } from 'vue'
const functions = ref([])
const toggleButtonOptions = [
{ label: 'Bold', value: 'bold' },
{ label: 'Italic', value: 'italic' },
{ label: 'Underline', value: 'underline' },
]
</script>
<template>
<PToggleButtonGroup
v-model="functions"
:options="toggleButtonOptions"
/>
</template>
Single
<script setup>
import { ref } from 'vue'
const functions = ref('bold')
const toggleButtonOptions = [
{ label: 'Bold', value: 'bold' },
{ label: 'Italic', value: 'italic' },
{ label: 'Underline', value: 'underline' },
]
</script>
<template>
<PToggleButtonGroup
v-model="functions"
:multiple="false"
:options="toggleButtonOptions"
/>
</template>
Sizes
<script setup>
import { ref } from 'vue'
const functions = ref([])
const toggleButtonOptions = [
{ label: 'Bold', value: 'bold' },
{ label: 'Italic', value: 'italic' },
{ label: 'Underline', value: 'underline' },
]
</script>
<template>
<PToggleButtonGroup
v-model="functions"
size="sm"
:options="toggleButtonOptions"
/>
</template>
Gap
ToggleButtonGroup extends the Stack component, However, the border will only be merged if gap is set to 0 of string | number type. (e.g. gap="0")
<script setup>
import { ref } from 'vue'
const functions = ref([])
const toggleButtonOptions = [
{ label: 'Bold', value: 'bold' },
{ label: 'Italic', value: 'italic' },
{ label: 'Underline', value: 'underline' },
]
</script>
<template>
<PStack direction="vertical">
<PToggleButtonGroup
v-model="functions"
gap="0"
:options="toggleButtonOptions"
/>
<PToggleButtonGroup
v-model="functions"
:gap="{ xs: 1, sm: 2, md: 4 }"
:options="toggleButtonOptions"
/>
</PStack>
</template>
Vertical
<script setup>
import TextBoldIcon from '@gdsicon/vue/text-bold'
import TextItalicIcon from '@gdsicon/vue/text-italic'
import { ref } from 'vue'
const functions = ref(['bold'])
</script>
<template>
<PToggleButtonGroup
v-model="functions"
gap="1"
direction="vertical"
>
<PToggleButton value="bold">
<TextBoldIcon />
</PToggleButton>
<PToggleButton value="italic">
<TextItalicIcon />
</PToggleButton>
</PToggleButtonGroup>
</template>
Disabled
<script setup>
import { ref } from 'vue'
const functions = ref(['italic'])
const toggleButtonOptions = [
{ label: 'Bold', value: 'bold' },
{ label: 'Italic', value: 'italic' },
{ label: 'Underline', value: 'underline' },
]
</script>
<template>
<PToggleButtonGroup
v-model="functions"
disabled
:options="toggleButtonOptions"
/>
</template>
Props
| Name | Type | Default | Description |
|---|---|---|---|
| variant | 'ghost' | 'default' | default | Visual style: ghost is transparent, default adds a border |
| disabled | boolean | - | Disable the button, also forced on by the parent group |
| label | string | number | null | - | Text rendered in the default slot when no slot content is given |
| icon | boolean | false | Render as a square icon-only button with centered content |
| size | 'sm' | 'md' | 'lg' | - | Button size, inherited from the group or config provider when unset |
| value | string | number | boolean | true | Value this button contributes to the group model |
| model-value | string | number | boolean | (string | number | boolean)[] | - | Checked state, an array of values inside a group |
Events
| Name | Type | Description |
|---|---|---|
| change | (value: string | number | boolean | (string | number | boolean)[], details: ChangeDetails) => void | Emitted when the button is clicked. |
| update:model-value | (value: string | number | boolean | (string | number | boolean)[]) => void | Emitted when the button writes its new checked state to the model. |
ToggleButtonGroup Props
| Name | Type | Default | Description |
|---|---|---|---|
| gap | ResponsiveValue<string | number> | 0 | Space between buttons, use 0 to merge the borders |
| size | 'sm' | 'md' | 'lg' | - | Size applied to every button in the group |
| disabled | boolean | - | Disable every button in the group |
| multiple | boolean | true | Allow several buttons to stay active at the same time |
| options | { label, value, disabled? }[] | - | Buttons rendered from the default slot, one per option |
| variant | 'ghost' | 'default' | ghost | Default variant for every button, overridable per button |
| model-value | string | number | boolean | (string | number | boolean)[] | () => [] | Active value, or an array of values when multiple |
ToggleButtonGroup Events
| Name | Type | Description |
|---|---|---|
| change | (value: string | number | boolean | (string | number | boolean)[], details: ChangeDetails) => void | Emitted when a button inside the group is clicked. |
| update:model-value | (value: string | number | boolean | (string | number | boolean)[]) => void | Emitted when the group writes the new selection to the model. |
Slots
| Name | Description |
|---|---|
| default | Default slot |
ToggleButtonGroup Slots
| Name | Description |
|---|---|
| default | Default slot |