A floating action button (FAB) carries the one action a screen is for: compose, add, start. It floats above the content, holds an icon and no label, and there is at most one in view. When the action needs a word, use the extended FAB.
Usage
ariaLabel is required: a FAB has no visible text.
import { createFab } from 'material';
const fab = createFab({ icon: addIcon, ariaLabel: 'Create new item' });
fab.on('click', () => createItem());
document.body.append(fab.element);
<m-fab aria-label="Create new item"></m-fab>
<script type="module">
const fab = document.querySelector('m-fab');
fab.setAttribute('icon', addIcon);
fab.addEventListener('click', () => createItem());
</script>
import { Fab } from 'material/react';
import { createItem, addIcon } from './app';
export function Example() {
return (
<Fab onClick={() => createItem()} icon={addIcon} ariaLabel="Create new item" />
);
}
<script setup lang="ts">
import { MFab } from 'material/vue';
import { createItem, addIcon } from './app';
</script>
<template>
<MFab @click="createItem()" :icon="addIcon" aria-label="Create new item" />
</template>
<script lang="ts">
import { Fab } from 'material/svelte';
import { createItem, addIcon } from './app';
</script>
<Fab onclick={() => createItem()} icon={addIcon} ariaLabel="Create new item" />
import { Fab } from 'material/solid';
import { createItem, addIcon } from './app';
export function Example() {
return (
<Fab onClick={() => createItem()} icon={addIcon} ariaLabel="Create new item" />
);
}
Examples
Color and size
variant is one of the container styles primary-container (the default),
secondary-container and tertiary-container, or the tone styles primary, secondary and
tertiary; each takes its color and the matching on- color from the theme. size is
default, medium or large, and the icon grows with it, so one SVG serves every size.
small and surface are deprecated.
import { createFab } from 'material';
const fab = createFab({
icon: editIcon,
ariaLabel: 'Edit',
variant: 'tertiary',
size: 'large',
});
document.body.append(fab.element);
<m-fab variant="tertiary" size="large" aria-label="Edit"></m-fab>
<script type="module">
const fab = document.querySelector('m-fab');
fab.setAttribute('icon', editIcon);
</script>
import { Fab } from 'material/react';
import { editIcon } from './app';
export function Example() {
return (
<Fab variant="tertiary" size="large" icon={editIcon} ariaLabel="Edit" />
);
}
<script setup lang="ts">
import { MFab } from 'material/vue';
import { editIcon } from './app';
</script>
<template>
<MFab variant="tertiary" size="large" :icon="editIcon" aria-label="Edit" />
</template>
<script lang="ts">
import { Fab } from 'material/svelte';
import { editIcon } from './app';
</script>
<Fab variant="tertiary" size="large" icon={editIcon} ariaLabel="Edit" />
import { Fab } from 'material/solid';
import { editIcon } from './app';
export function Example() {
return (
<Fab variant="tertiary" size="large" icon={editIcon} ariaLabel="Edit" />
);
}
In a corner
position fixes the FAB to a corner of the viewport, 16dp from both edges. Leave it unset to
place the FAB yourself, as in a card, a sheet or a
docked toolbar.
import { createFab } from 'material';
const fab = createFab({
icon: addIcon,
ariaLabel: 'Create new item',
position: 'bottom-right',
});
document.body.append(fab.element);
<m-fab position="bottom-right" aria-label="Create new item"></m-fab>
<script type="module">
const fab = document.querySelector('m-fab');
fab.setAttribute('icon', addIcon);
</script>
import { Fab } from 'material/react';
import { addIcon } from './app';
export function Example() {
return (
<Fab icon={addIcon} position="bottom-right" ariaLabel="Create new item" />
);
}
<script setup lang="ts">
import { MFab } from 'material/vue';
import { addIcon } from './app';
</script>
<template>
<MFab :icon="addIcon" position="bottom-right" aria-label="Create new item" />
</template>
<script lang="ts">
import { Fab } from 'material/svelte';
import { addIcon } from './app';
</script>
<Fab icon={addIcon} position="bottom-right" ariaLabel="Create new item" />
import { Fab } from 'material/solid';
import { addIcon } from './app';
export function Example() {
return (
<Fab icon={addIcon} position="bottom-right" ariaLabel="Create new item" />
);
}
A FAB rests at elevation level 3 and rises to 4 on hover; lower() moves it to the lowered
levels, 1 and 2, and raise() restores them. animate: true scales it in when it is added to
the page.
API
Options
iconIcon as an HTML string, usually an SVG
stringundefinedariaLabelAccessible name
stringvariantColor style
'primary-container' | 'secondary-container' | 'tertiary-container' | 'primary' | 'secondary' | 'tertiary''primary-container'sizeContainer, icon and corner
'default' | 'medium' | 'large''default'positionFixes the FAB to a corner of the viewport
'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'undefineddisabledCreates it disabled
booleanfalseiconSizeAdds an mtrl-icon--<value> class to the icon, for your own CSS; the stylesheet has no rules for it
stringundefinedanimateScales the FAB in when it is added to the page
booleanfalserippleWhether a press shows the ripple
booleantruerippleConfigHow long, in ms, a released wave lingers before it is removed
{ duration? }undefinedtypeThe button's type attribute
'button' | 'submit' | 'reset''button'valueThe button's value attribute, for forms
stringundefinedclassExtra classes on the element
stringundefinedprefixPrefix for CSS class names
string'mtrl'Methods
setIcon(icon) / getIcon()The icon HTML
icon: stringFabComponent / stringsetPosition(position)Moves it to another corner
position: stringFabComponentgetPosition()The current corner, or null when unpositioned
string | nulllower() / raise()The lowered or the normal elevation levels
FabComponentenable() / disable()The disabled state
FabComponentsetValue(value) / getValue()The value attribute
value: stringFabComponent / stringaddClass(...classes)Adds classes to the element
...classes: string[]FabComponentgetClass(name)Prefixes a name: 'fab' gives 'mtrl-fab'
name: stringstringon(event, handler) / off(event, handler)Adds or removes a listener
event: string, handler: FunctionFabComponentdestroy()Removes the element and its listeners
voidelementThe button
HTMLButtonElementiconThe icon manager
{ setIcon, getIcon, getElement }disabledThe disabled-state manager
{ enable, disable, isDisabled }lifecycleThe lifecycle manager
{ destroy }Events
clickPressed; not fired while disabled
{ event, element, originalEvent }focus / blurTook or lost focus
{ event, element, originalEvent }event and originalEvent are the same DOM event; element is the button.
Styling
Colors are the theme's roles: each style is the role it is named after, on its on- role, and
the state layer takes the icon color.
.mtrl-fab { }
.mtrl-fab--primary-container, .mtrl-fab--secondary-container, .mtrl-fab--tertiary-container { }
.mtrl-fab--primary, .mtrl-fab--secondary, .mtrl-fab--tertiary { }
.mtrl-fab--default, .mtrl-fab--medium, .mtrl-fab--large { }
.mtrl-fab--lowered, .mtrl-fab--disabled, .mtrl-fab--animate-enter { }
.mtrl-fab--top-left, .mtrl-fab--top-right, .mtrl-fab--bottom-left, .mtrl-fab--bottom-right { }
.mtrl-fab__icon { }
Measurements
From the Compose M3 token files named in the last column.
| Attribute | Value | Source |
|---|---|---|
| Container, default | 56dp | FabBaselineTokens |
| Container, medium | 80dp | FabMediumTokens |
| Container, large | 96dp | FabLargeTokens |
| Container, small (deprecated) | 40dp | FabSmallTokens |
| Corner, default | 16dp (large) |
FabBaselineTokens |
| Corner, medium | 20dp (large-increased) |
Android fab_tokens.xml |
| Corner, large | 28dp (extra-large) |
FabLargeTokens |
| Corner, small | 12dp (medium) |
FabSmallTokens |
| Icon, default and small | 24dp | FabBaselineTokens, FabSmallTokens |
| Icon, medium | 28dp | FabMediumTokens |
| Icon, large | 32dp | FabLargeTokens; Android says 36dp |
| Elevation, rest / hover / focus / pressed | levels 3 / 4 / 3 / 3 | FabPrimaryContainerTokens |
| Lowered, rest / hover / focus / pressed | levels 1 / 2 / 1 / 1 | ExtendedFabPrimaryTokens |
| Disabled | on-surface at 12% container and 38% icon, level 0 |
library policy; the FAB tokens define no disabled state |
| Offset from the corner | 16dp | the position rules in _fab.scss |
| Entrance | 0.3s, emphasized decelerate | the fab-enter keyframes in _fab.scss |