Chips are compact elements that stand for one discrete thing. M3 has four types: assist chips for a smart or automated action, filter chips to narrow content, input chips for something the user entered, such as a recipient, and suggestion chips for a generated suggestion, such as a reply.
createChips builds a set, which owns the selection, removal and keyboard navigation of its
chips; createAssistChip, createFilterChip, createInputChip and createSuggestionChip
build one chip on its own.
Usage
A set is multi-select unless multiSelect is false. Its chips are configurations, which the
set builds; without a type, a chip in a set is a filter chip.
import { createChips } from 'material';
const chips = createChips({
label: 'Categories',
chips: [
{ type: 'filter', label: 'JavaScript', value: 'js' },
{ type: 'filter', label: 'TypeScript', value: 'ts' },
{ type: 'filter', label: 'CSS', value: 'css' },
],
});
document.body.append(chips.element);
<m-chips label="Categories">
<m-chip value="js" variant="filter">JavaScript</m-chip>
<m-chip value="ts" variant="filter">TypeScript</m-chip>
<m-chip value="css" variant="filter">CSS</m-chip>
</m-chips>
import { Chips, Chip } from 'material/react';
export function Example() {
return (
<Chips label="Categories">
<Chip value="js" variant="filter">JavaScript</Chip>
<Chip value="ts" variant="filter">TypeScript</Chip>
<Chip value="css" variant="filter">CSS</Chip>
</Chips>
);
}
<script setup lang="ts">
import { MChips, MChip } from 'material/vue';
</script>
<template>
<MChips label="Categories">
<MChip value="js" variant="filter">JavaScript</MChip>
<MChip value="ts" variant="filter">TypeScript</MChip>
<MChip value="css" variant="filter">CSS</MChip>
</MChips>
</template>
<script lang="ts">
import { Chips, Chip } from 'material/svelte';
</script>
<Chips label="Categories">
<Chip value="js" variant="filter">JavaScript</Chip>
<Chip value="ts" variant="filter">TypeScript</Chip>
<Chip value="css" variant="filter">CSS</Chip>
</Chips>
import { Chips, Chip } from 'material/solid';
export function Example() {
return (
<Chips label="Categories">
<Chip value="js" variant="filter">JavaScript</Chip>
<Chip value="ts" variant="filter">TypeScript</Chip>
<Chip value="css" variant="filter">CSS</Chip>
</Chips>
);
}
change reports the selection as one object, { value, selected, changed }. value is the
set's current value: an array in a multi-select set, a string or null in a single-select one.
The web component's detail has value.
Examples
Single selection
selectionRequired keeps the last selected chip selected, in either mode.
import { createChips } from 'material';
const chips = createChips({
label: 'Size',
multiSelect: false,
selectionRequired: true,
chips: [
{ type: 'filter', label: 'S', value: 's' },
{ type: 'filter', label: 'M', value: 'm', selected: true },
{ type: 'filter', label: 'L', value: 'l' },
],
});
document.body.append(chips.element);
<m-chips value="m" selection-required label="Size" selection="single">
<m-chip value="s" variant="filter">S</m-chip>
<m-chip value="m" variant="filter">M</m-chip>
<m-chip value="l" variant="filter">L</m-chip>
</m-chips>
import { useState } from 'react';
import { Chips, Chip } from 'material/react';
export function Example() {
const [value, setValue] = useState("m");
return (
<Chips value={value} onChange={(event) => setValue(event.detail.value)} selectionRequired label="Size" selection="single">
<Chip value="s" variant="filter">S</Chip>
<Chip value="m" variant="filter">M</Chip>
<Chip value="l" variant="filter">L</Chip>
</Chips>
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MChips, MChip } from 'material/vue';
const value = ref("m");
</script>
<template>
<MChips v-model="value" selection-required label="Size" selection="single">
<MChip value="s" variant="filter">S</MChip>
<MChip value="m" variant="filter">M</MChip>
<MChip value="l" variant="filter">L</MChip>
</MChips>
</template>
<script lang="ts">
import { Chips, Chip } from 'material/svelte';
let value = $state("m");
</script>
<Chips bind:value selectionRequired label="Size" selection="single">
<Chip value="s" variant="filter">S</Chip>
<Chip value="m" variant="filter">M</Chip>
<Chip value="l" variant="filter">L</Chip>
</Chips>
import { createSignal } from 'solid-js';
import { Chips, Chip } from 'material/solid';
export function Example() {
const [value, setValue] = createSignal("m");
return (
<Chips value={value()} onChange={(event) => setValue(event.detail.value)} selectionRequired label="Size" selection="single">
<Chip value="s" variant="filter">S</Chip>
<Chip value="m" variant="filter">M</Chip>
<Chip value="l" variant="filter">L</Chip>
</Chips>
);
}
Input chips
Every input chip has a remove button, and Backspace or Delete removes it when focused. In a
set, the set removes it and emits remove with { value, chip, chipValue }; on its own, it leaves the page.
avatar puts a 24dp round image in place of the leading icon.
import { createChips } from 'material';
const chips = createChips({
label: 'To',
chips: [
{ type: 'input', label: 'Ada Lovelace', value: '[email protected]' },
{ type: 'input', label: 'Alan Turing', value: '[email protected]' },
],
});
document.body.append(chips.element);
<m-chips label="To">
<m-chip value="[email protected]" variant="input">Ada Lovelace</m-chip>
<m-chip value="[email protected]" variant="input">Alan Turing</m-chip>
</m-chips>
import { Chips, Chip } from 'material/react';
export function Example() {
return (
<Chips label="To">
<Chip value="[email protected]" variant="input">Ada Lovelace</Chip>
<Chip value="[email protected]" variant="input">Alan Turing</Chip>
</Chips>
);
}
<script setup lang="ts">
import { MChips, MChip } from 'material/vue';
</script>
<template>
<MChips label="To">
<MChip value="[email protected]" variant="input">Ada Lovelace</MChip>
<MChip value="[email protected]" variant="input">Alan Turing</MChip>
</MChips>
</template>
<script lang="ts">
import { Chips, Chip } from 'material/svelte';
</script>
<Chips label="To">
<Chip value="[email protected]" variant="input">Ada Lovelace</Chip>
<Chip value="[email protected]" variant="input">Alan Turing</Chip>
</Chips>
import { Chips, Chip } from 'material/solid';
export function Example() {
return (
<Chips label="To">
<Chip value="[email protected]" variant="input">Ada Lovelace</Chip>
<Chip value="[email protected]" variant="input">Alan Turing</Chip>
</Chips>
);
}
Assist chips
elevated gives assist, filter and suggestion chips the elevated style in place of the
outline.
import { createChips } from 'material';
const chips = createChips({
label: 'Actions',
chips: [
{
type: 'assist',
label: 'Add to calendar',
value: 'calendar',
leadingIcon: calendarIcon,
elevated: true,
},
{
type: 'assist',
label: 'Directions',
value: 'directions',
leadingIcon: locationIcon,
elevated: true,
},
],
});
document.body.append(chips.element);
<m-chips label="Actions">
<m-chip value="calendar" variant="assist" elevated>Add to calendar</m-chip>
<m-chip value="directions" variant="assist" elevated>Directions</m-chip>
</m-chips>
<script type="module">
const chips = document.querySelector('m-chips');
chips.querySelector('m-chip[value="calendar"]').setAttribute('icon', calendarIcon);
chips.querySelector('m-chip[value="directions"]').setAttribute('icon', locationIcon);
</script>
import { Chips, Chip } from 'material/react';
import { calendarIcon, locationIcon } from './app';
export function Example() {
return (
<Chips label="Actions">
<Chip value="calendar" variant="assist" icon={calendarIcon} elevated>Add to calendar</Chip>
<Chip value="directions" variant="assist" icon={locationIcon} elevated>Directions</Chip>
</Chips>
);
}
<script setup lang="ts">
import { MChips, MChip } from 'material/vue';
import { calendarIcon, locationIcon } from './app';
</script>
<template>
<MChips label="Actions">
<MChip value="calendar" variant="assist" :icon="calendarIcon" elevated>Add to calendar</MChip>
<MChip value="directions" variant="assist" :icon="locationIcon" elevated>Directions</MChip>
</MChips>
</template>
<script lang="ts">
import { Chips, Chip } from 'material/svelte';
import { calendarIcon, locationIcon } from './app';
</script>
<Chips label="Actions">
<Chip value="calendar" variant="assist" icon={calendarIcon} elevated>Add to calendar</Chip>
<Chip value="directions" variant="assist" icon={locationIcon} elevated>Directions</Chip>
</Chips>
import { Chips, Chip } from 'material/solid';
import { calendarIcon, locationIcon } from './app';
export function Example() {
return (
<Chips label="Actions">
<Chip value="calendar" variant="assist" icon={calendarIcon} elevated>Add to calendar</Chip>
<Chip value="directions" variant="assist" icon={locationIcon} elevated>Directions</Chip>
</Chips>
);
}
A filter chip's trailingMenu gives it a trailing button that opens a menu; onTrailingClick
handles it. A filter chip opening a price menu is planned for Showcase. A chip
your app makes draggable shows M3's dragged state from dragstart to dragend; material does
not move chips itself.
API
Options
A chip
Passed to the four factories, and in a set's chips or addChip(), with type.
typeIn a set's chips or addChip(); the factories set it
'assist' | 'filter' | 'input' | 'suggestion''filter'labelThe chip's label
string''valueIdentifies the chip to its set and to forms
stringleadingIconLeading icon as SVG markup (icon is an alias)
stringundefinedtrailingIconTrailing icon as SVG markup; not on suggestion chips. On an input chip it replaces the remove icon
stringundefinedavatarInput chips: a 24dp round avatar (markup), in place of the leading icon
stringundefinedelevatedAssist, filter and suggestion chips: the elevated style
booleanfalseselectedFilter and input chips: whether the chip starts selected
booleanfalsedisabledWhether the chip starts disabled
booleanfalseremoveLabelInput chips: the remove button's accessible name
string'Remove {label}'onRemoveInput chips: called when the chip is removed
(chip) => voidundefinedonTrailingClickFilter chips: gives the trailing icon its own button
(chip) => voidundefinedtrailingMenuFilter chips: the trailing button opens a menu (aria-haspopup, a drop-down arrow)
booleanfalsetrailingLabelFilter chips: the trailing button's accessible name
string'{label} options' or 'Remove {label}'onClickCalled when the chip is activated, before it toggles: isSelected() is still the state before the click
ChipEvents['click']undefinedonChangeFilter and input chips: called when a click changes the selected state
(payload: { selected, chip, value }) => voidundefinedonSelectFilter and input chips: called with the chip after onChange
(chip) => voidundefinedrippleWhether a press shows the ripple
booleantrueclassAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'mtrl'A set
Passed to createChips.
chipsThe chips to build and manage
ChipConfig[][]multiSelectWhether several chips can be selected at once
booleantrueselectionRequiredWhether the last selected chip is kept selected
booleanfalselabelA visible label that names the set
stringundefinedlabelPositionWhich side the label sits on
'start' | 'end''start'scrollableWhether the set scrolls horizontally instead of wrapping
booleanfalseverticalWhether the chips stack vertically
booleanfalseonChangeCalled with the selection after the change
(event: { value, selected, changed }) => voidundefinedonEvent handlers registered at creation
{ change?, add?, remove? }undefinedclassAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'mtrl'Methods
A chip
setLabel(text) / getLabel()The label (setText / getText are aliases)
text: stringChipComponent / stringsetLeadingIcon(icon) / getIcon()The leading icon (setIcon is an alias)
icon: stringChipComponent / stringsetTrailingIcon(icon)The trailing icon, or an input chip's remove icon
icon: stringChipComponentsetSelected(selected) / toggleSelected() / isSelected()Selection, for filter and input chips
selected: booleanChipComponent / booleansetValue(value) / getValue()The chip's value
value: stringChipComponent / string | nullenable() / disable() / isDisabled()The disabled state
ChipComponent / booleangetType()The chip's type
ChipTypefocus()Focuses the chip's action
ChipComponentaddClass(...classes)Adds classes to the chip
...classes: string[]ChipComponenton(event, handler) / off(event, handler)Adds or removes a listener
event: string, handler: FunctionChipComponentdestroy()Removes listeners and the element
voidchip.action is the chip's native button. An input chip's remove button and a filter chip's
trailing button (chip.trailingAction) are its siblings, never inside it.
A set
addChip(config) / removeChip(chipOrIndex)Adds or removes a chip
config: ChipConfig / chipOrIndex: ChipComponent | numberChipsComponentgetChips() / getSelectedChips()The chips, or the selected ones
ChipComponent[]getSelectedValues()The selected values, in either mode
(string | null)[]getValue() / setValue(values)An array when multi-select, a string or null when single-select
values: string | string[] | nullstring | string[] | null / ChipsComponentselectByValue(values, triggerEvent?) / clearSelection()Programmatic selection
values: string | string[], triggerEvent?: booleanChipsComponentsetScrollable(on) / setVertical(on)Layout
on: booleanChipsComponentsetLabel(text) / getLabel()The set's label
text: stringChipsComponent / stringsetLabelPosition(position) / getLabelPosition()Which side the label sits on
position: 'start' | 'end'ChipsComponent / stringscrollToChip(chipOrIndex)Scrolls a scrollable set to a chip
chipOrIndex: ChipComponent | numberChipsComponentenableKeyboardNavigation()Enables the arrow-key navigation between chips
ChipsComponenton(event, handler) / off(event, handler)Adds or removes a listener
event: string, handler: FunctionChipsComponentdestroy()Removes the set
voidEvents
clickThe chip was activated
{ event, element, originalEvent }changeA click changed the selected state
{ selected, chip, value }remove / trailingRemoved, or its trailing button activated
focus / blur / keydownThe chip's action took or lost focus, or a key went down
{ event, element, originalEvent }changeThe selection changed
{ value, selected, changed }; changed is null for programmatic changesaddA chip was added
{ value, chip }removeA chip is about to be removed
{ value, chip, chipValue }The web component's change carries { value }, and remove the removed chip's { value }.
Styling
Colors come from the theme's roles; --mtrl-chip-label-color, --mtrl-chip-leading-color,
--mtrl-chip-trailing-color and --mtrl-chip-checkmark-color set a chip's parts.
.mtrl-chip { }
.mtrl-chip--assist, .mtrl-chip--filter, .mtrl-chip--input, .mtrl-chip--suggestion { }
.mtrl-chip--elevated, .mtrl-chip--selected, .mtrl-chip--disabled, .mtrl-chip--dragged { }
.mtrl-chip--leading, .mtrl-chip--trailing, .mtrl-chip--avatar { } /* the parts shown */
.mtrl-chip__action { } /* the native button */
.mtrl-chip__leading-icon, .mtrl-chip__checkmark, .mtrl-chip__label, .mtrl-chip__trailing-icon { }
.mtrl-chip__remove, .mtrl-chip__trailing-action { }
.mtrl-chips { }
.mtrl-chips--scrollable, .mtrl-chips--vertical, .mtrl-chips--with-label, .mtrl-chips--label-end { }
.mtrl-chips__container, .mtrl-chips__label { }
Measurements
From the m3.material.io chips specs and Compose's chip tokens.
| Attribute | Value |
|---|---|
| Height | 32dp |
| Corner | 8dp |
| Outline | 1dp outline-variant, inside the chip; none when selected or elevated |
| Padding | 16dp without icons, 8dp beside an icon, 4dp beside an avatar |
| Icon | 18dp, 8dp from the label |
| Avatar | 24dp, round |
| Label | Label Large |
| Selected | secondary-container |
| Elevated | surface-container-low at elevation 1 |
| Touch target | 48dp |
| Motion | The checkmark and leading icon expand on the fast spatial spring and fade |