A button lets people take an action with one tap: save, send, add to cart. M3 has five, from most to least emphasis: filled for the one main action, tonal, elevated, outlined, and text for the least.
Usage
import { createButton } from 'material';
const button = createButton({ text: 'Save' });
button.on('click', () => save());
document.body.append(button.element);
<m-button>Save</m-button>
<script type="module">
const button = document.querySelector('m-button');
button.addEventListener('click', () => save());
</script>
import { Button } from 'material/react';
import { save } from './app';
export function Example() {
return (
<Button onClick={() => save()}>Save</Button>
);
}
<script setup lang="ts">
import { MButton } from 'material/vue';
import { save } from './app';
</script>
<template>
<MButton @click="save()">Save</MButton>
</template>
<script lang="ts">
import { Button } from 'material/svelte';
import { save } from './app';
</script>
<Button onclick={() => save()}>Save</Button>
import { Button } from 'material/solid';
import { save } from './app';
export function Example() {
return (
<Button onClick={() => save()}>Save</Button>
);
}
Examples
Emphasis, size and shape
variant sets the emphasis, size runs from xs to xl, and shape is round (a pill)
or square (corners that grow with the size).
import { createButton } from 'material';
const button = createButton({ text: 'Discard', variant: 'outlined', size: 'm', shape: 'square' });
document.body.append(button.element);
<m-button variant="outlined" size="m" shape="square">Discard</m-button>
import { Button } from 'material/react';
export function Example() {
return (
<Button variant="outlined" size="m" shape="square">Discard</Button>
);
}
<script setup lang="ts">
import { MButton } from 'material/vue';
</script>
<template>
<MButton variant="outlined" size="m" shape="square">Discard</MButton>
</template>
<script lang="ts">
import { Button } from 'material/svelte';
</script>
<Button variant="outlined" size="m" shape="square">Discard</Button>
import { Button } from 'material/solid';
export function Example() {
return (
<Button variant="outlined" size="m" shape="square">Discard</Button>
);
}
An icon
An icon goes with the label or alone. Alone, the button needs ariaLabel to name it.
import { createButton } from 'material';
const button = createButton({ text: 'Add to cart', variant: 'tonal', icon: addIcon });
document.body.append(button.element);
<m-button variant="tonal">Add to cart</m-button>
<script type="module">
const button = document.querySelector('m-button');
button.setAttribute('icon', addIcon);
</script>
import { Button } from 'material/react';
import { addIcon } from './app';
export function Example() {
return (
<Button variant="tonal" icon={addIcon}>Add to cart</Button>
);
}
<script setup lang="ts">
import { MButton } from 'material/vue';
import { addIcon } from './app';
</script>
<template>
<MButton variant="tonal" :icon="addIcon">Add to cart</MButton>
</template>
<script lang="ts">
import { Button } from 'material/svelte';
import { addIcon } from './app';
</script>
<Button variant="tonal" icon={addIcon}>Add to cart</Button>
import { Button } from 'material/solid';
import { addIcon } from './app';
export function Example() {
return (
<Button variant="tonal" icon={addIcon}>Add to cart</Button>
);
}
Disabled while it works
An action changes the button: here its label and disabled.
import { createButton } from 'material';
const button = createButton({ text: 'Send' });
button.on('click', () => sending());
document.body.append(button.element);
function sending() {
button.setText('Sending');
button.disable();
}
<m-button>Send</m-button>
<script type="module">
const button = document.querySelector('m-button');
button.addEventListener('click', () => sending());
function sending() {
button.textContent = "Sending";
button.toggleAttribute('disabled', true);
}
</script>
import { useState } from 'react';
import { Button } from 'material/react';
export function Example() {
const [text, setText] = useState("Send");
const [disabled, setDisabled] = useState(false);
const sending = () => {
setText("Sending");
setDisabled(true);
};
return (
<Button onClick={() => sending()} disabled={disabled}>{text}</Button>
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MButton } from 'material/vue';
const text = ref("Send");
const disabled = ref(false);
function sending() {
text.value = "Sending";
disabled.value = true;
}
</script>
<template>
<MButton @click="sending()" :disabled="disabled">{{ text }}</MButton>
</template>
<script lang="ts">
import { Button } from 'material/svelte';
let text = $state("Send");
let disabled = $state(false);
function sending() {
text = "Sending";
disabled = true;
}
</script>
<Button onclick={() => sending()} disabled={disabled}>{text}</Button>
import { createSignal } from 'solid-js';
import { Button } from 'material/solid';
export function Example() {
const [text, setText] = createSignal("Send");
const [disabled, setDisabled] = createSignal(false);
const sending = () => {
setText("Sending");
setDisabled(true);
};
return (
<Button onClick={() => sending()} disabled={disabled()}>{text()}</Button>
);
}
The factory can also show progress in the icon slot (progress, setProgress(),
setLoading()); the web component does not take it yet. A button created with loading: true
is busy from the start — the loading state shows immediately, including on server-rendered
buttons, and setLoading(true) disables it synchronously, before the progress chunk arrives.
The lazily created progress instance is exposed as progress, so the public progress API
controls the displayed indicator. An optional lean stylesheet,
material/styles/button-filled-small-round, covers buttons configured variant: 'filled',
size: 's' and shape: 'round'; other variants, sizes or shapes, including changes through
setters, need material/styles/button. Recipes built on buttons, such as an
upload with progress, a form submission or a multi-step process, are planned for
Showcase.
API
Options
variantVisual style of the button (filled, outlined, text, elevated, tonal)
string'filled'sizeSize of the button (xs, s, m, l, xl)
string's'shapeShape of the button (round, square)
string'round'disabledWhether the button is initially disabled
booleanfalsetextText content displayed inside the button
stringundefinediconHTML content (typically SVG) for the button icon
stringundefinediconSizeAccepted but not applied. The value is appended to a CSS class (mtrl-icon--<value>) rather than used as a length, and the stylesheet defines no such modifier, so it has no effect. Size the icon from your own CSS, or size the SVG itself
stringundefinedclassAdditional CSS classes to add to the button
stringundefinedvalueButton value attribute
stringundefinedtypeButton type attribute (button, submit, reset)
string'button'rippleWhether a press shows the ripple, which is the pressed state layer (0.10) as in Compose; without it, a static 0.10 layer shows the press
booleantrueprefixPrefix for CSS class names
string'mtrl'rippleConfigHow long, in ms, a released wave lingers before it is removed: the wave is the 0.10 pressed state layer, drawn by the stylesheet
{ duration? }undefinedariaLabelARIA label for accessibility (important for icon-only buttons)
stringundefinedprogressProgress indicator configuration
boolean|objectundefinedshowProgressWhether to show progress initially
booleanfalseloadingCreates the button busy: the loading state shows from the start, including server-rendered buttons
booleanfalseMethods
Value Methods
getValue()Gets the button's current value attribute
stringsetValue(value)Sets the button's value attribute
value: stringButtonComponentState Methods
enable()Enables the button, making it interactive
ButtonComponentdisable()Disables the button, making it non-interactive
ButtonComponentsetActive(active)Sets the active state of the button (e.g., when a related menu is open)
active: booleanButtonComponentVariant Methods
setVariant(variant)Changes the button's visual style variant
variant: stringButtonComponentgetVariant()Gets the button's current variant
stringSize Methods
setSize(size)Sets the button's size (xs, s, m, l, xl)
size: stringButtonComponentgetSize()Gets the button's current size
stringShape Methods
setShape(shape)Sets the button's shape (round, square)
shape: stringButtonComponentgetShape()Gets the button's current shape
stringContent Methods
setText(content)Sets the button's text content
content: stringButtonComponentgetText()Gets the button's current text content
stringsetIcon(icon)Sets the button's icon HTML content (empty string removes icon)
icon: stringButtonComponentgetIcon()Gets the button's current icon HTML content
stringhasIcon()Checks if the button has an icon
booleansetAriaLabel(label)Sets the button's aria-label attribute for accessibility
label: stringButtonComponentProgress Methods (when progress is configured)
showProgress()Shows the progress indicator
Promise<ButtonComponent>showProgressSync()Shows the progress indicator synchronously
ButtonComponenthideProgress()Hides the progress indicator
Promise<ButtonComponent>hideProgressSync()Hides the progress indicator synchronously
ButtonComponentsetProgress(value)Sets progress value (0-100)
value: numberPromise<ButtonComponent>setProgressSync(value)Sets progress value synchronously
value: numberButtonComponentsetIndeterminate(indeterminate)Sets indeterminate mode
indeterminate: booleanPromise<ButtonComponent>setIndeterminateSync(indeterminate)Sets indeterminate mode synchronously
indeterminate: booleanButtonComponentsetLoading(loading, text?)Shows progress and disables the button; restores the previous text on false unless new text is given
loading: boolean, text?: stringPromise<ButtonComponent>setLoadingSync(loading, text?)The same, without awaiting the lazy progress import
loading: boolean, text?: stringButtonComponentEvent Methods
on(event, handler)Adds an event listener
event: string, handler: FunctionButtonComponentoff(event, handler)Removes an event listener
event: string, handler: FunctionButtonComponentStyle Methods
addClass(...classes)Adds CSS classes to the button element
...classes: string[]ButtonComponentLifecycle Methods
destroy()Destroys the button component and cleans up resources
voidEvents
clickFires when the button is clicked
{ event, element, originalEvent }focusFires when the button receives focus
{ event, element, originalEvent }blurFires when the button loses focus
{ event, element, originalEvent }event and originalEvent are the same DOM event; element is the button element. Handlers
are not called while the button is disabled.
Styling
The custom property reaches the factory's button and the web component alike; the classes are the factory's, inside the web component's shadow root.
.mtrl-button { }
.mtrl-button--filled, .mtrl-button--tonal, .mtrl-button--elevated,
.mtrl-button--outlined, .mtrl-button--text { }
.mtrl-button--xs, .mtrl-button--s, .mtrl-button--m, .mtrl-button--l, .mtrl-button--xl { }
.mtrl-button--square { }
.mtrl-button__icon, .mtrl-button__text { }
.mtrl-button--progress, .mtrl-button__progress { }
.checkout {
--mtrl-button-shape: 8px; /* the corner radius */
}
Measurements
| Size | Height |
|---|---|
xs |
32dp |
s (default) |
40dp |
m |
56dp |
l |
96dp |
xl |
136dp |