A split button pairs one action with a button that opens more choices: Save, with "Save a copy" behind the chevron; Send, with "Schedule send". Use it when one action is the obvious default and the rest are variations of it; for unrelated choices use a menu, for alternatives of equal weight a button group.
Usage
The leading button does the common thing; the trailing one opens a menu of items, and its
chevron turns over while the menu is open.
import { createSplitButton } from 'material';
const splitButton = createSplitButton({
text: 'Watch later',
icon: watchIcon,
trailingLabel: 'More watch options',
items: [
{ id: 'queue', text: 'Add to queue' },
{ id: 'playlist', text: 'Save to playlist' },
],
});
splitButton.on('click', () => watchLater());
document.body.append(splitButton.element);
<m-split-button trailing-label="More watch options">
Watch later
<m-menu-item value="queue">Add to queue</m-menu-item>
<m-menu-item value="playlist">Save to playlist</m-menu-item>
</m-split-button>
<script type="module">
const splitButton = document.querySelector('m-split-button');
splitButton.setAttribute('icon', watchIcon);
splitButton.addEventListener('click', () => watchLater());
</script>
import { SplitButton, MenuItem } from 'material/react';
import { watchLater, watchIcon } from './app';
export function Example() {
return (
<SplitButton onClick={() => watchLater()} icon={watchIcon} trailingLabel="More watch options">
Watch later
<MenuItem value="queue">Add to queue</MenuItem>
<MenuItem value="playlist">Save to playlist</MenuItem>
</SplitButton>
);
}
<script setup lang="ts">
import { MSplitButton, MMenuItem } from 'material/vue';
import { watchLater, watchIcon } from './app';
</script>
<template>
<MSplitButton @click="watchLater()" :icon="watchIcon" trailing-label="More watch options">
Watch later
<MMenuItem value="queue">Add to queue</MMenuItem>
<MMenuItem value="playlist">Save to playlist</MMenuItem>
</MSplitButton>
</template>
<script lang="ts">
import { SplitButton, MenuItem } from 'material/svelte';
import { watchLater, watchIcon } from './app';
</script>
<SplitButton onclick={() => watchLater()} icon={watchIcon} trailingLabel="More watch options">
Watch later
<MenuItem value="queue">Add to queue</MenuItem>
<MenuItem value="playlist">Save to playlist</MenuItem>
</SplitButton>
import { SplitButton, MenuItem } from 'material/solid';
import { watchLater, watchIcon } from './app';
export function Example() {
return (
<SplitButton onClick={() => watchLater()} icon={watchIcon} trailingLabel="More watch options">
Watch later
<MenuItem value="queue">Add to queue</MenuItem>
<MenuItem value="playlist">Save to playlist</MenuItem>
</SplitButton>
);
}
A chosen item emits select: the factory's payload has the item, the web component's detail
its value, the item's id.
Examples
Variant and size
variant is filled, tonal, outlined or elevated, shared by both halves, and size runs
from xs to xl like the button's. Opening the menu changes a state layer and the shape,
never the color.
import { createSplitButton } from 'material';
const splitButton = createSplitButton({
text: 'Save',
variant: 'tonal',
size: 'm',
items: [
{ id: 'save-as', text: 'Save as…' },
{ id: 'save-copy', text: 'Save a copy' },
],
});
document.body.append(splitButton.element);
<m-split-button variant="tonal" size="m">
Save
<m-menu-item value="save-as">Save as…</m-menu-item>
<m-menu-item value="save-copy">Save a copy</m-menu-item>
</m-split-button>
import { SplitButton, MenuItem } from 'material/react';
export function Example() {
return (
<SplitButton variant="tonal" size="m">
Save
<MenuItem value="save-as">Save as…</MenuItem>
<MenuItem value="save-copy">Save a copy</MenuItem>
</SplitButton>
);
}
<script setup lang="ts">
import { MSplitButton, MMenuItem } from 'material/vue';
</script>
<template>
<MSplitButton variant="tonal" size="m">
Save
<MMenuItem value="save-as">Save as…</MMenuItem>
<MMenuItem value="save-copy">Save a copy</MMenuItem>
</MSplitButton>
</template>
<script lang="ts">
import { SplitButton, MenuItem } from 'material/svelte';
</script>
<SplitButton variant="tonal" size="m">
Save
<MenuItem value="save-as">Save as…</MenuItem>
<MenuItem value="save-copy">Save a copy</MenuItem>
</SplitButton>
import { SplitButton, MenuItem } from 'material/solid';
export function Example() {
return (
<SplitButton variant="tonal" size="m">
Save
<MenuItem value="save-as">Save as…</MenuItem>
<MenuItem value="save-copy">Save a copy</MenuItem>
</SplitButton>
);
}
setText() and setIcon() change the leading button. Without items, the factory only
reports that the trailing button was activated, with expand, collapse and change, and the
app opens whatever it likes. layer: 'top' shows the menu in the browser's top layer.
API
Options
textLabel of the leading button
stringundefinediconIcon of the leading button, as an HTML string
stringundefinedvariantVisual style, shared by both halves
'filled' | 'tonal' | 'outlined' | 'elevated''filled'sizeSize of both halves
'xs' | 's' | 'm' | 'l' | 'xl''s'disabledWhether both halves are disabled
booleanfalsetrailingLabelAccessible name of the trailing button
string'More options'ariaLabelAccessible name of the leading button, when its label is not enough
stringundefinedgroupLabelAccessible name of the pair
stringundefineditemsMenu items for the trailing button to open
MenuContent[]undefinedlayerRenders the menu beside the trailing button and shows it in the top layer
'top'undefinedonClickWhat the leading button does
(event: SplitButtonEvent) => voidundefinedonSelectCalled with the chosen item
(event: SplitButtonEvent) => voidundefinedonHandlers for any of the events
{ [event]: handler }undefinedclassAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'mtrl'Methods
setText(text) / getText()The leading button's label
text: stringSplitButtonComponent / stringsetIcon(icon)The leading button's icon
icon: stringSplitButtonComponentsetItems(items) / getItems()Replaces or reads the choices the trailing button opens
items: MenuContent[]SplitButtonComponent / MenuContent[]expand() / collapse()Opens or closes what the trailing button opens
SplitButtonComponentisExpanded()Whether it is open
booleanenable() / disable()Both halves together
SplitButtonComponentisDisabled()Whether the pair is disabled
booleanon(event, handler) / off(event, handler)Adds or removes a listener
event: string, handler: FunctionSplitButtonComponentdestroy()Takes it off the page and releases the menu
voidelementThe group holding both halves
HTMLElementleadingElementThe leading button
HTMLButtonElementtrailingElementThe trailing button
HTMLButtonElementEvents
clickThe leading button was activated
{ splitButton, expanded, originalEvent }expand / collapseThe trailing button opened or closed its choices
{ splitButton, expanded, originalEvent }changeEither of the two, with the new state
{ splitButton, expanded, originalEvent }selectA menu item was chosen
{ splitButton, expanded, originalEvent, item }The web component's select carries { value }; its click is the leading button's.
Styling
Colors come from the button. Filled and tonal halves stay flat on hover, as in a connected button group, so the pair reads as one control; an elevated split button keeps its elevation.
.mtrl-split-button { }
.mtrl-split-button--filled, .mtrl-split-button--xl, .mtrl-split-button--expanded { }
.mtrl-split-button__leading, .mtrl-split-button__trailing, .mtrl-split-button__chevron { }
.toolbar .mtrl-split-button {
--mtrl-split-button-inner-shape: 8px; /* where the halves meet */
--mtrl-split-button-inner-shape-active: 12px; /* the same, hovered or pressed */
}
Measurements
From the M3 split button tokens, in dp.
| Size | Height | Leading padding | Trailing padding | Chevron | Inner corner | Inner corner, active |
|---|---|---|---|---|---|---|
xs |
32 | 12 / 10 | 13 | 22 | 4 | 8 |
s |
40 | 16 / 12 | 13 | 22 | 4 | 12 |
m |
56 | 24 / 24 | 15 | 26 | 8 | 12 |
l |
96 | 48 / 48 | 29 | 38 | 12 | 20 |
xl |
136 | 64 / 64 | 43 | 50 | 16 | 20 |
The halves are 2dp apart, the outer corners are a full pill, and the leading button is at least
48dp wide. The resting inner corner departs from the tokens at m, l and xl (4, 8 and
12dp), which read as square beside a full pill; set --mtrl-split-button-inner-shape to go
back to them. While the menu is closed, the chevron sits 1 to 6dp off center, so it looks
centered between two differently shaped ends; open, it centers and turns 180 degrees.