ToggleButtonGroup
A ToggleButtonGroup is a group of ToggleButtons that allows users to select one or multiple buttons from the group.
Guidelines
Using toggle button groups
Buttons within the toggle button group must carry a label. In the case of icon-only buttons, the label will be available only to screen reader users.
Use the ToggleButtonGroup when you require users to select either one or multiple options from a group. If you want users to choose from a set of actions, with the restriction that only one can be active simultaneously, use the ButtonGroup instead.
Specifications
A toggle button group consists of normal (framed) buttons, which may include the following elements:
- Icon (optional)
Icons simplify user recognition and provide the ability to shorten button labels to a minimum. - Label
Button labels should be as short as possible, with text that clearly states what option will be selected when the button is toggled on. - White line
For toggled-on buttons, a white line is added to visually separate them, whether they are on the same line or placed on different rows in the stacked version.
Refer to the ToggleButtonGroup component in Codex Figma.
Stacked buttons
When the ToggleButtonGroup exceeds the available space on a single line, the buttons will wrap onto the next line.
Interaction states
ToggleButtonGroups have the following visually separate states:
- Default
- Hover on one button
- Active button
- Focus on one button
- One button is selected
- One button is disabled
- Hover on the selected button
- The selected button is active
- Focus on the selected button
- Two buttons are selected
- All buttons are selected
- All buttons are disabled
Demos
Single value
To allow only a single value to be selected, initialize v-model
to null
.
Selected value: (null)
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
/>
<p>
Selected value:
{{ selectedValue || '(null)' }}
</p>
</template>
<script>
import { defineComponent, ref } from 'vue';
import { CdxToggleButtonGroup } from '@wikimedia/codex';
export default defineComponent( {
name: 'SingleValueToggleButtonGroup',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 1, label: 'One' },
{ value: 2, label: 'Two' },
{ value: 3, label: 'Three' },
{ value: 4, label: 'Four' },
{ value: 5, label: 'Five' }
];
// Initializing to null indicates that this is a single-select group
const selectedValue = ref( null );
return {
buttons,
selectedValue
};
}
} );
</script>
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
></cdx-toggle-button-group>
<p>
Selected value:
{{ selectedValue || '(null)' }}
</p>
</template>
<script>
const { defineComponent, ref } = require( 'vue' );
const { CdxToggleButtonGroup } = require( '@wikimedia/codex' );
// @vue/component
module.exports = defineComponent( {
name: 'SingleValueToggleButtonGroup',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 1, label: 'One' },
{ value: 2, label: 'Two' },
{ value: 3, label: 'Three' },
{ value: 4, label: 'Four' },
{ value: 5, label: 'Five' }
];
// Initializing to null indicates that this is a single-select group
const selectedValue = ref( null );
return {
buttons,
selectedValue
};
}
} );
</script>
Name | Value |
---|---|
View | |
Reading direction |
Initially selected single value
To start with one button already selected, initialize v-model
to that value.
Use the icon
property to add an icon before the text of a button. Use the disabled
property to disable individual buttons. For more information on how to control the appearance of each button, see the ButtonGroup documentation.
Selected value: framed
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
/>
<p>
Selected value:
{{ selectedValue || '(null)' }}
</p>
</template>
<script>
import { defineComponent, ref } from 'vue';
import { CdxToggleButtonGroup } from '@wikimedia/codex';
import {
cdxIconImageLayoutBasic,
cdxIconImageLayoutFrame,
cdxIconImageLayoutFrameless,
cdxIconImageLayoutThumbnail
} from '@wikimedia/codex-icons';
export default defineComponent( {
name: 'InitiallySelectedSingleValueToggleButtonGroup',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 'basic', label: 'Basic', icon: cdxIconImageLayoutBasic },
{ value: 'framed', label: 'Framed', icon: cdxIconImageLayoutFrame },
{ value: 'frameless', label: 'Frameless', icon: cdxIconImageLayoutFrameless },
{ value: 'thumbnail', label: 'Thumbnail', icon: cdxIconImageLayoutThumbnail, disabled: true }
];
// Initially select the 'Framed' button
const selectedValue = ref( 'framed' );
return {
buttons,
selectedValue
};
}
} );
</script>
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
></cdx-toggle-button-group>
<p>
Selected value:
{{ selectedValue || '(null)' }}
</p>
</template>
<script>
const { defineComponent, ref } = require( 'vue' );
const { CdxToggleButtonGroup } = require( '@wikimedia/codex' );
const {
cdxIconImageLayoutBasic,
cdxIconImageLayoutFrame,
cdxIconImageLayoutFrameless,
cdxIconImageLayoutThumbnail
} = require( './icons.json' );
// @vue/component
module.exports = defineComponent( {
name: 'InitiallySelectedSingleValueToggleButtonGroup',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 'basic', label: 'Basic', icon: cdxIconImageLayoutBasic },
{ value: 'framed', label: 'Framed', icon: cdxIconImageLayoutFrame },
{ value: 'frameless', label: 'Frameless', icon: cdxIconImageLayoutFrameless },
{ value: 'thumbnail', label: 'Thumbnail', icon: cdxIconImageLayoutThumbnail, disabled: true }
];
// Initially select the 'Framed' button
const selectedValue = ref( 'framed' );
return {
buttons,
selectedValue
};
}
} );
</script>
Multiple values
To allow multiple values to be selected, initialize v-model
to an empty array ([]
). To start with some of buttons already selected, initialize v-model
to an array of the values of those buttons.
Selected values: (none)
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
/>
<p>
Selected values:
{{ selectedValue.join( ', ' ) || '(none)' }}
</p>
</template>
<script>
import { defineComponent, ref } from 'vue';
import { CdxToggleButtonGroup } from '@wikimedia/codex';
import { cdxIconBell, cdxIconCalendar, cdxIconUserAvatar } from '@wikimedia/codex-icons';
export default defineComponent( {
name: 'MultiValueToggleButtonGroup',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 'notification', label: null, icon: cdxIconBell, ariaLabel: 'Notification' },
{ value: 'calendar', label: null, icon: cdxIconCalendar, ariaLabel: 'Calendar' },
{ value: 'user', label: null, icon: cdxIconUserAvatar, ariaLabel: 'User' }
];
// Initializing to an array indicates that this is a multi-select group
const selectedValue = ref( [] );
return {
buttons,
selectedValue
};
}
} );
</script>
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
></cdx-toggle-button-group>
<p>
Selected values:
{{ selectedValue.join( ', ' ) || '(none)' }}
</p>
</template>
<script>
const { defineComponent, ref } = require( 'vue' );
const { CdxToggleButtonGroup } = require( '@wikimedia/codex' );
const { cdxIconBell, cdxIconCalendar, cdxIconUserAvatar } = require( './icons.json' );
// @vue/component
module.exports = defineComponent( {
name: 'MultiValueToggleButtonGroup',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 'notification', label: null, icon: cdxIconBell, ariaLabel: 'Notification' },
{ value: 'calendar', label: null, icon: cdxIconCalendar, ariaLabel: 'Calendar' },
{ value: 'user', label: null, icon: cdxIconUserAvatar, ariaLabel: 'User' }
];
// Initializing to an array indicates that this is a multi-select group
const selectedValue = ref( [] );
return {
buttons,
selectedValue
};
}
} );
</script>
Disabled
The entire component can be disabled by setting the disabled
prop. Individual buttons can be disabled by setting their .disabled
property.
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
:disabled="true"
/>
</template>
<script>
import { defineComponent, ref } from 'vue';
import { CdxToggleButtonGroup } from '@wikimedia/codex';
export default defineComponent( {
name: 'SingleValueToggleButtonGroup',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 1, label: 'One' },
{ value: 2, label: 'Two' },
{ value: 3, label: 'Three' },
{ value: 4, label: 'Four', disabled: true },
{ value: 5, label: 'Five' }
];
const selectedValue = ref( null );
return {
buttons,
selectedValue
};
}
} );
</script>
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
:disabled="true"
></cdx-toggle-button-group>
</template>
<script>
const { defineComponent, ref } = require( 'vue' );
const { CdxToggleButtonGroup } = require( '@wikimedia/codex' );
// @vue/component
module.exports = defineComponent( {
name: 'SingleValueToggleButtonGroup',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 1, label: 'One' },
{ value: 2, label: 'Two' },
{ value: 3, label: 'Three' },
{ value: 4, label: 'Four', disabled: true },
{ value: 5, label: 'Five' }
];
const selectedValue = ref( null );
return {
buttons,
selectedValue
};
}
} );
</script>
Overflowing buttons
When the button group is too large to fit on one line, the buttons overflow to the next line.
Selected values: (none)
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
/>
<p>
Selected values:
{{ selectedValue.join( ', ' ) || '(none)' }}
</p>
</template>
<script>
import { defineComponent, ref } from 'vue';
import { CdxToggleButtonGroup } from '@wikimedia/codex';
export default defineComponent( {
name: 'MaximumToggleButtonGroup',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 1, label: 'First button' },
{ value: 2, label: 'Second button' },
{ value: 3, label: 'Third button with a long label' },
{ value: 4, label: 'Fourth button with a long label' },
{ value: 5, label: 'Fifth button' }
];
const selectedValue = ref( [] );
return {
buttons,
selectedValue
};
}
} );
</script>
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
></cdx-toggle-button-group>
<p>
Selected values:
{{ selectedValue.join( ', ' ) || '(none)' }}
</p>
</template>
<script>
const { defineComponent, ref } = require( 'vue' );
const { CdxToggleButtonGroup } = require( '@wikimedia/codex' );
// @vue/component
module.exports = defineComponent( {
name: 'MaximumToggleButtonGroup',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 1, label: 'First button' },
{ value: 2, label: 'Second button' },
{ value: 3, label: 'Third button with a long label' },
{ value: 4, label: 'Fourth button with a long label' },
{ value: 5, label: 'Fifth button' }
];
const selectedValue = ref( [] );
return {
buttons,
selectedValue
};
}
} );
</script>
Custom button display
The contents of the buttons can be customized using the default slot. The ButtonGroupItem
object for each button is available through the button
binding, and the selected state of each button is available through the selected
binding. In this example, the value of the button is displayed after its label, but only if the button is selected.
Selected value: 2
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
>
<template #default="{ button }">
{{ button.label }}
<span>
(value: {{ button.value }})
</span>
</template>
</cdx-toggle-button-group>
<p>
Selected value:
{{ selectedValue || '(null)' }}
</p>
</template>
<script>
import { defineComponent, ref } from 'vue';
import { CdxToggleButtonGroup } from '@wikimedia/codex';
export default defineComponent( {
name: 'ToggleButtonGroupWithSlot',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 1, label: 'One' },
{ value: 2, label: 'Two' }
];
const selectedValue = ref( 2 );
return {
buttons,
selectedValue
};
}
} );
</script>
<template>
<cdx-toggle-button-group
v-model="selectedValue"
:buttons="buttons"
>
<template #default="{ button }">
{{ button.label }}
<span>
(value: {{ button.value }})
</span>
</template>
</cdx-toggle-button-group>
<p>
Selected value:
{{ selectedValue || '(null)' }}
</p>
</template>
<script>
const { defineComponent, ref } = require( 'vue' );
const { CdxToggleButtonGroup } = require( '@wikimedia/codex' );
// @vue/component
module.exports = defineComponent( {
name: 'ToggleButtonGroupWithSlot',
components: {
CdxToggleButtonGroup
},
setup() {
const buttons = [
{ value: 1, label: 'One' },
{ value: 2, label: 'Two' }
];
const selectedValue = ref( 2 );
return {
buttons,
selectedValue
};
}
} );
</script>
Vue usage
Whether the group is single-select or multi-select is automatically detected based on the value bound to v-model
: if it's an array, the group allows multiple selections; otherwise, it only allows a single selection. To initially select nothing, initialize the v-model
value to null
(for single-select groups) or []
(for multi-select groups).
Props
Prop name | Description | Type | Default |
---|---|---|---|
buttons (required) | Buttons to display. See the ButtonGroupItem type. | ButtonGroupItem[] | |
modelValue (required) | Selected value, or array of selected values. If this is a string or number, the button whose value equals that string or number is selected, and only a single selection is allowed. If this is an array, the buttons whose values equal any of the values in the array are selected, and multiple selections are allowed. To select none of the buttons initially, set this to null (for single-selection groups) or to [] (for multi-selection groups).Must be bound with v-model . | string|number|null|( string|number )[] | |
disabled | Whether the entire group is disabled. If this is set to true, all buttons in the group are disabled. Buttons can also be disabled individually by setting their disabled property to true. | boolean | false |
Events
Event name | Properties | Description |
---|---|---|
update:modelValue | modelValue string|number|( string|number )[] - The new model value | Emitted when modelValue changes. |
Slots
Name | Description | Bindings |
---|---|---|
default | Content of an individual button | button ButtonGroupItem - Object describing the button to displayselected boolean - Whether the button is selected |