An icon button is a button whose whole label is its icon: for an action people recognize without words, where space is tight, as in a toolbar, an app bar or media controls. As a toggle, it holds a binary state, such as favorited or muted, and swaps its icon to show it.
Usage
ariaLabel is required: it is the button's only name.
import { createIconButton } from 'material';
const iconButton = createIconButton({ icon: menuIcon, ariaLabel: 'Open menu' });
iconButton.on('click', () => openMenu());
document.body.append(iconButton.element);
<m-icon-button aria-label="Open menu"></m-icon-button>
<script type="module">
const iconButton = document.querySelector('m-icon-button');
iconButton.setAttribute('icon', menuIcon);
iconButton.addEventListener('click', () => openMenu());
</script>
import { IconButton } from 'material/react';
import { openMenu, menuIcon } from './app';
export function Example() {
return (
<IconButton onClick={() => openMenu()} icon={menuIcon} ariaLabel="Open menu" />
);
}
<script setup lang="ts">
import { MIconButton } from 'material/vue';
import { openMenu, menuIcon } from './app';
</script>
<template>
<MIconButton @click="openMenu()" :icon="menuIcon" aria-label="Open menu" />
</template>
<script lang="ts">
import { IconButton } from 'material/svelte';
import { openMenu, menuIcon } from './app';
</script>
<IconButton onclick={() => openMenu()} icon={menuIcon} ariaLabel="Open menu" />
import { IconButton } from 'material/solid';
import { openMenu, menuIcon } from './app';
export function Example() {
return (
<IconButton onClick={() => openMenu()} icon={menuIcon} ariaLabel="Open menu" />
);
}
Examples
Variant, size, shape and width
variant sets the emphasis, from filled, tonal and outlined to standard; size runs
from xs to xl, shape is round or square, and width is narrow, default or wide.
import { createIconButton } from 'material';
const iconButton = createIconButton({
icon: editIcon,
ariaLabel: 'Edit',
variant: 'tonal',
size: 'm',
shape: 'square',
width: 'wide',
});
document.body.append(iconButton.element);
<m-icon-button variant="tonal" size="m" shape="square" width="wide" aria-label="Edit"></m-icon-button>
<script type="module">
const iconButton = document.querySelector('m-icon-button');
iconButton.setAttribute('icon', editIcon);
</script>
import { IconButton } from 'material/react';
import { editIcon } from './app';
export function Example() {
return (
<IconButton variant="tonal" size="m" shape="square" width="wide" icon={editIcon} ariaLabel="Edit" />
);
}
<script setup lang="ts">
import { MIconButton } from 'material/vue';
import { editIcon } from './app';
</script>
<template>
<MIconButton variant="tonal" size="m" shape="square" width="wide" :icon="editIcon" aria-label="Edit" />
</template>
<script lang="ts">
import { IconButton } from 'material/svelte';
import { editIcon } from './app';
</script>
<IconButton variant="tonal" size="m" shape="square" width="wide" icon={editIcon} ariaLabel="Edit" />
import { IconButton } from 'material/solid';
import { editIcon } from './app';
export function Example() {
return (
<IconButton variant="tonal" size="m" shape="square" width="wide" icon={editIcon} ariaLabel="Edit" />
);
}
A toggle
With toggle, a click flips the selected state and emits change; selectedIcon is shown
while selected. Make the unselected icon outlined and the selected one filled, so the state
reads without the color. selectedIcon alone does not turn toggle mode on.
import { createIconButton } from 'material';
const iconButton = createIconButton({
icon: heartOutlineIcon,
selectedIcon: heartFilledIcon,
toggle: true,
selected: false,
ariaLabel: 'Add to favorites',
});
iconButton.on('change', ({ selected }) => setFavorite(selected));
document.body.append(iconButton.element);
<m-icon-button toggle aria-label="Add to favorites"></m-icon-button>
<script type="module">
const iconButton = document.querySelector('m-icon-button');
iconButton.setAttribute('icon', heartOutlineIcon);
iconButton.setAttribute('selected-icon', heartFilledIcon);
iconButton.addEventListener('change', (event) => setFavorite(event.detail.selected));
</script>
import { useState } from 'react';
import { IconButton } from 'material/react';
import { setFavorite, heartOutlineIcon, heartFilledIcon } from './app';
export function Example() {
const [selected, setSelected] = useState(false);
return (
<IconButton selected={selected} onChange={(event) => { setSelected(event.detail.selected); setFavorite(event.detail.selected); }} icon={heartOutlineIcon} selectedIcon={heartFilledIcon} toggle ariaLabel="Add to favorites" />
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MIconButton } from 'material/vue';
import { setFavorite, heartOutlineIcon, heartFilledIcon } from './app';
const selected = ref(false);
</script>
<template>
<MIconButton v-model="selected" @change="setFavorite($event.detail.selected)" :icon="heartOutlineIcon" :selected-icon="heartFilledIcon" toggle aria-label="Add to favorites" />
</template>
<script lang="ts">
import { IconButton } from 'material/svelte';
import { setFavorite, heartOutlineIcon, heartFilledIcon } from './app';
let selected = $state(false);
</script>
<IconButton bind:selected onchange={(event) => setFavorite(event.detail.selected)} icon={heartOutlineIcon} selectedIcon={heartFilledIcon} toggle ariaLabel="Add to favorites" />
import { createSignal } from 'solid-js';
import { IconButton } from 'material/solid';
import { setFavorite, heartOutlineIcon, heartFilledIcon } from './app';
export function Example() {
const [selected, setSelected] = createSignal(false);
return (
<IconButton selected={selected()} onChange={(event) => { setSelected(event.detail.selected); setFavorite(event.detail.selected); }} icon={heartOutlineIcon} selectedIcon={heartFilledIcon} toggle ariaLabel="Add to favorites" />
);
}
Changing it
An action changes the icon and the label together.
import { createIconButton } from 'material';
const iconButton = createIconButton({ icon: playIcon, ariaLabel: 'Play', variant: 'filled' });
iconButton.on('click', () => playing());
document.body.append(iconButton.element);
function playing() {
iconButton.setIcon(pauseIcon);
iconButton.setAriaLabel('Pause');
}
<m-icon-button variant="filled" aria-label="Play"></m-icon-button>
<script type="module">
const iconButton = document.querySelector('m-icon-button');
iconButton.setAttribute('icon', playIcon);
iconButton.addEventListener('click', () => playing());
function playing() {
iconButton.setAttribute('icon', pauseIcon);
iconButton.setAttribute('aria-label', "Pause");
}
</script>
import { useState } from 'react';
import { IconButton } from 'material/react';
import { playIcon, pauseIcon } from './app';
export function Example() {
const [icon, setIcon] = useState(playIcon);
const [ariaLabel, setAriaLabel] = useState("Play");
const playing = () => {
setIcon(pauseIcon);
setAriaLabel("Pause");
};
return (
<IconButton onClick={() => playing()} variant="filled" icon={icon} ariaLabel={ariaLabel} />
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MIconButton } from 'material/vue';
import { playIcon, pauseIcon } from './app';
const icon = ref(playIcon);
const ariaLabel = ref("Play");
function playing() {
icon.value = pauseIcon;
ariaLabel.value = "Pause";
}
</script>
<template>
<MIconButton @click="playing()" variant="filled" :icon="icon" :aria-label="ariaLabel" />
</template>
<script lang="ts">
import { IconButton } from 'material/svelte';
import { playIcon, pauseIcon } from './app';
let icon = $state(playIcon);
let ariaLabel = $state("Play");
function playing() {
icon = pauseIcon;
ariaLabel = "Pause";
}
</script>
<IconButton onclick={() => playing()} variant="filled" icon={icon} ariaLabel={ariaLabel} />
import { createSignal } from 'solid-js';
import { IconButton } from 'material/solid';
import { playIcon, pauseIcon } from './app';
export function Example() {
const [icon, setIcon] = createSignal(playIcon);
const [ariaLabel, setAriaLabel] = createSignal("Play");
const playing = () => {
setIcon(pauseIcon);
setAriaLabel("Pause");
};
return (
<IconButton onClick={() => playing()} variant="filled" icon={icon()} ariaLabel={ariaLabel()} />
);
}
API
Options
iconThe icon, as an HTML string
stringundefinedariaLabelAccessible name; describes the action, not the icon
stringvariantVisual style, in descending order of emphasis
'filled' | 'tonal' | 'outlined' | 'standard''standard'sizeContainer size
'xs' | 's' | 'm' | 'l' | 'xl''s'shapeResting corner shape
'round' | 'square''round'widthWidth relative to the size's container
'narrow' | 'default' | 'wide''default'toggleToggle mode: a click selects and deselects, and emits change
booleanfalseselectedIconIcon shown while selected, in toggle mode
stringundefinedselectedWhether the button starts selected; only read when toggle is true
booleanfalsetoggleOnClickWhether a click flips the selected state; false when a container, such as a button group, owns the selection
booleantruedisabledWhether the button starts disabled
booleanfalsetypeThe underlying button's type attribute
'button' | 'submit' | 'reset''button'valueValue attribute, for use in a form
stringundefinedrippleWhether a press shows the ripple
booleantruerippleConfigHow long, in ms, a released wave lingers before it is removed
{ duration? }undefinedclassAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'mtrl'tooltipAccepted but not applied: no tooltip is rendered. Use the tooltip component beside the button
booleantrueMethods
Content
setIcon(icon)Replaces the icon
icon: stringIconButtonComponentgetIcon()The icon's HTML
stringsetSelectedIcon(icon)Replaces the selected-state icon
icon: stringIconButtonComponentgetSelectedIcon()The selected-state icon's HTML, or an empty string
stringsetAriaLabel(label)Sets the accessible name
label: stringIconButtonComponentsetValue(value)Sets the value attribute
value: stringIconButtonComponentgetValue()The value attribute
stringAppearance
setVariant(variant) / getVariant()The variant
variant: stringIconButtonComponent / stringsetSize(size) / getSize()The size
size: stringIconButtonComponent / stringsetShape(shape) / getShape()The shape
shape: stringIconButtonComponent / stringsetWidth(width) / getWidth()The width
width: stringIconButtonComponent / stringState
select() / deselect()Selects or deselects it, in toggle mode, without emitting change
IconButtonComponenttoggleSelected()Flips the selected state
IconButtonComponentisSelected()Whether it is selected
booleanisToggle()Whether toggle mode is on
booleanenable() / disable()The disabled state
IconButtonComponentEvents, styles and lifecycle
on(event, handler) / off(event, handler)Adds or removes a listener
event: string, handler: FunctionIconButtonComponentaddClass(...classes) / removeClass(...classes)Adds or removes CSS classes
...classes: string[]IconButtonComponentdestroy()Takes it off the page and releases its listeners
voidelementThe button element
HTMLButtonElementiconThe icon slot: setIcon, getIcon, getElement
IconAPIdisabledThe disabled feature: enable, disable, isDisabled
objectlifecycleThe lifecycle feature: destroy
objectEvents
changeA click changed a toggle button's selected state
{ selected, value }clickThe button was activated; not fired while disabled
{ event, element, originalEvent }focus / blurThe button took or lost focus
{ event, element, originalEvent }The web component's change carries { selected, value }.
Styling
The press morph is driven by :active, so it has no state class. The custom properties are
shared with the button: --mtrl-button-shape sets the resting radius of both shapes.
.mtrl-icon-button { }
.mtrl-icon-button__icon { }
.mtrl-icon-button--filled, .mtrl-icon-button--tonal, .mtrl-icon-button--outlined,
.mtrl-icon-button--standard { }
.mtrl-icon-button--xs, .mtrl-icon-button--m, .mtrl-icon-button--l, .mtrl-icon-button--xl { }
.mtrl-icon-button--square, .mtrl-icon-button--narrow, .mtrl-icon-button--wide { }
.mtrl-icon-button--toggle, .mtrl-icon-button--selected, .mtrl-icon-button--disabled { }
.toolbar {
--mtrl-button-shape: 12px; /* at rest */
--mtrl-button-shape-pressed: 8px; /* while pressed */
--mtrl-button-shape-selected: 12px; /* while selected */
}
Measurements
In dp. s is the default size and default width equals the container; a round button's
radius is half its height.
| Size | Container | Icon | Square radius | Pressed radius | Narrow width | Wide width |
|---|---|---|---|---|---|---|
xs |
32 | 20 | 12 | 8 | 28 | 40 |
s |
40 | 24 | 12 | 8 | 32 | 52 |
m |
56 | 24 | 16 | 12 | 48 | 72 |
l |
96 | 32 | 28 | 16 | 64 | 128 |
xl |
136 | 40 | 28 | 16 | 104 | 184 |