Markup
Variants
Variant and Size have two rows. On a single FAB, the class goes on the FAB button. On a group, it goes on each button in the group. Edge sets the side of the panel. The FAB has no data-fab-side, so it docks on the panel’s edge.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Button (default) (demo: + size-lg) | — | — | One button. The usual FAB |
| Structure | Group (demo: + subtle) (demo: + size-sm) | canon #fab-group |
— | Related actions, such as a set of share buttons. The group docks as one item, and the toggle is one of its buttons |
| Edge | End (default) (demo: + vertical) (hint: Follows the reading direction) | — | — | The end edge of the reading direction: the left in Arabic, the right in English |
| Edge | Start (demo: + vertical) (hint: Follows the reading direction) | [data-panel-side="start"] |
.nds-panel |
The start edge of the reading direction |
| Edge | Left (demo: + vertical) (hint: Stays on the left in every language) | [data-panel-side="left"] |
.nds-panel |
The left edge in every language |
| Edge | Right (demo: + vertical) (hint: Stays on the right in every language) | [data-panel-side="right"] |
.nds-panel |
The right edge in every language |
| Edge | Bottom (demo: + horizontal) | [data-panel-side="bottom"] |
.nds-panel |
The middle of the bottom edge. The panel is a sheet that rises from the bottom |
| Variant | Primary (default) | .nds-primary |
.nds-btn.nds-fab |
The main action on the page |
| Variant | Primary (default) | .nds-primary |
.nds-fab > .nds-btn |
The same, on each button of a group |
| Variant | Neutral | .nds-neutral |
.nds-btn.nds-fab |
A strong action that is not the brand color |
| Variant | Neutral | .nds-neutral |
.nds-fab > .nds-btn |
The same, on each button of a group |
| Variant | Secondary | .nds-secondary |
.nds-btn.nds-fab |
A supporting action, with a light fill |
| Variant | Secondary | .nds-secondary |
.nds-fab > .nds-btn |
The same, on each button of a group |
| Variant | Secondary outline | .nds-secondary-outline |
.nds-btn.nds-fab |
A supporting action, with a border |
| Variant | Secondary outline | .nds-secondary-outline |
.nds-fab > .nds-btn |
The same, on each button of a group |
| Variant | Subtle (id: subtle) | .nds-subtle |
.nds-btn.nds-fab |
A low-emphasis action, with the page background and a border |
| Variant | Subtle (id: subtle) | .nds-subtle |
.nds-fab > .nds-btn |
The same, on each button of a group |
| Size | LG (default) (id: size-lg) | — | — | 56px, 48px on a phone. It needs no class |
| Size | MD | .nds-md |
.nds-btn.nds-fab |
48px, 40px on a phone |
| Size | MD | .nds-md |
.nds-fab > .nds-btn |
The same, on each button of a group |
| Size | SM (id: size-sm) | .nds-sm |
.nds-btn.nds-fab |
40px, 32px on a phone |
| Size | SM (id: size-sm) | .nds-sm |
.nds-fab > .nds-btn |
The same, on each button of a group |
| Shape | Circle (default) | .nds-circle |
.nds-btn.nds-fab |
A round button |
| Shape | Square | — | .nds-btn.nds-fab |
A button with rounded corners |
| Direction | Vertical (default) (id: vertical) | .nds-vertical |
.nds-btn-group |
The buttons stack in a column along a side edge |
| Direction | Horizontal (id: horizontal) | — | .nds-btn-group |
The buttons sit in a row. For the bottom edge |
| Thumb | Thumb (hint: Sits against the screen edge, square on that side) | .nds-fab-thumb |
.nds-fab |
The FAB sits against the screen edge, with square corners on that side. It moves aside when its panel opens from the same edge |
| Badge | Badge (hint: A count on the edge of the button) | canon #fab-badge |
.nds-btn.nds-fab > i |
A count on the top corner of the button, such as the number of settings that are on. It goes inside the icon, in data-count. The icon is aria-hidden, so add the count to aria-label too. NDS.setBadge(fab, count) does both |
| Badge | Badge (hint: A count on the edge of the button) | canon #fab-badge |
.nds-fab > .nds-btn[data-panel-toggle] > i |
The same, on the button of a group that opens the panel |
| Gap | None (default) | — | — | The dock’s own gap between FABs |
| Gap | SM | [data-fab-gap="sm"] |
.nds-fab |
8px of extra space between the FAB and the one before it |
| Gap | MD | [data-fab-gap="md"] |
.nds-fab |
16px of extra space, to start a new set of FABs |
| Gap | LG | [data-fab-gap="lg"] |
.nds-fab |
32px of extra space, for FABs that do not belong together |
Behavior
Edge
The Edge options set the side of the panel, and the FAB follows it. To place a FAB on an edge of its own, write data-fab-side. start and end follow the reading direction. left, right and bottom stay on that edge in every language. If its panel is not on the page yet, the FAB reads data-panel-side from its toggle button. A FAB with no data-fab-side and no side, or with a top sheet, docks on the end edge.
Thumb
nds-fab-thumb puts the FAB against the screen edge. The corners on that side turn square and the border there goes away, so it looks like a tab. When its panel slides in from the same edge, the thumb moves aside with the panel and comes back when the panel closes. On the bottom edge, it sits on the bottom of the screen and rises with the sheet. A thumb with an explicit data-fab-side moves only when that edge is the panel’s edge.
Group
A button group with nds-fab docks as one item, with one shadow around the whole group. Put data-panel-toggle on one of its buttons: the group docks on that panel’s edge, and with nds-fab-thumb the whole group moves aside. Style it as any button group: the variant and size go on its buttons. Only the outer nds-fab docks: a button inside it with nds-fab stays in the group.
Gap
data-fab-gap adds space between a FAB and the one before it in the dock, on top of the dock’s own gap. Use it to split the FABs on one edge into sets, such as the page actions and a chat button. On a side edge the space goes below the FAB, and on the bottom edge to its left. The FAB with the gap is the first of its set, so give it the lowest data-fab-order in that set.
Built-in Features
-
Auto-initialization
The script moves every
nds-fabto its dock after the page shows. There is no dock to write and nothing to call. -
Fixed Edge Docks
There are three docks: left, right and the middle of the bottom. A
startorendFAB lands in the left or the right dock, so two FABs on one edge always share a dock. -
Direction-Aware Routing
When the page direction changes, a
startorendFAB moves to the other side, and so does a FAB that follows its panel. AleftorrightFAB stays. -
Panel Following
A FAB with no
data-fab-sidedocks on the edge of the panel it opens. Change the panel's side and the FAB follows. -
Automatic Stacking
FABs on one edge stack, ordered by
data-fab-order. On a side edge, the lowest number is at the bottom. On the bottom edge, they sit in a row from left to right. -
Page-End Tuck
Near the end of a page that scrolls, the docks slide out past their edge, so a FAB never covers the footer. They also slide out while a hero fills the bottom of the screen.
-
Mixed Shapes and Sizes
Circles, thumbs and groups of any size share a dock. The dock lines them up on its edge and keeps each one's size and shape.
-
Floating Look
A docked FAB is 16px larger than the same button in the page (8px on a phone), and it casts a shadow. Secondary, secondary outline and subtle get a solid fill, so the page does not show through.
-
Programmatic Control
A FAB made by a script docks with one call.
NDS.Fab.destroy()puts a FAB back in its view before an app removes that view.
Best Practices
- Keep FABs few: one for the main action, such as add, compose, filters or chat. Put more actions in a Panel that the FAB opens.
- Write the
hiddenattribute on every FAB. The script removes it once the FAB is in its dock, so the FAB never shows at the place you wrote it. - Leave out
data-fab-sideon a FAB that opens a panel, so the FAB stays on the panel’s edge. - Use
startandendso the edge follows the reading direction. Useleftorrightonly for an action that must stay on one side in every language. - Give the main action the lowest
data-fab-order, so it sits nearest the edge. - Give an icon-only FAB an
aria-labelthat names the action. - Do not write the square corners of a thumb yourself. The dock sets them from its edge, so they stay right when the FAB moves.
- In an app that swaps views, call
NDS.Fab.destroy(view)orNDS.init.destroy(view)before you remove a view. A FAB lives in a dock on<body>, so it stays on the screen after its view is gone.
API
Classes
| Class | Element | Effect |
|---|---|---|
nds-fab |
.nds-btn or .nds-btn-group |
Makes it a FAB: the script moves it to its dock |
nds-fab-dock |
div |
A dock. The script makes one for each edge in use, at the end of <body>. To place a plain button at an edge, write a dock yourself with data-fab-dock-side and put the button in it. The script moves only buttons with nds-fab |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-fab-side |
.nds-fab |
The edge: auto (the default), start, end, left, right or bottom. See Edge under Behavior |
data-fab-order |
.nds-fab |
The place in the stack. A lower number sits nearer the edge. The default is 0 |
data-fab-gap |
.nds-fab |
Extra space between the FAB and the one before it: sm (8px), md (16px) or lg (32px). See Gap under Behavior |
data-panel-toggle |
.nds-fab, or a button in a group |
The id of the Panel it opens. The FAB docks on that panel’s edge |
data-fab-dock-side |
.nds-fab-dock |
The edge of a dock you write yourself: left, right (the default) or bottom |
data-fab-riding |
.nds-fab-thumb |
The script writes it while the thumb moves aside for its panel |
data-fab-tucked |
<html> |
The script writes it while the docks slide out, near the page end or over a hero |
CSS Custom Properties
The script makes the docks, so set the dock properties on :root, or on a dock you write yourself. Set --fab-gap on the FAB.
| Property | Default | Controls |
|---|---|---|
--fab-dock-offset |
calc(var(--nds-viewport-padding) / 2) |
Space between a dock and the screen edges: half the page gutter. On a touch screen, a left or right dock sits twice this far from the bottom, clear of the rounded screen corners |
--fab-dock-gap |
var(--spacing-md) |
Space between FABs in a dock |
--fab-gap |
set by data-fab-gap |
Extra space of a FAB with data-fab-gap. Any value replaces the size |
--fab-dock-z |
899 |
Stack order of the docks. Panels (999) and modals sit above them |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_fab.scss.
| Token | Preview | Value |
|---|---|---|
--background-defaultRead by box-shadow, --btn-bg, --btn-bg-disabled | var(--colors-base-white)Dark mode var(--colors-neutral-800) | |
--border-neutral-secondaryRead by --btn-border, --btn-border-disabled | var(--colors-neutral-200)Dark mode var(--colors-neutral-700) | |
--button-background-secondary-defaultRead by --btn-bg, --btn-bg-disabled | var(--colors-neutral-100)Dark mode var(--colors-neutral-700) | |
--button-background-secondary-hoveredRead by --btn-bg-hover | var(--colors-neutral-100)Dark mode var(--colors-neutral-700) | |
--button-background-secondary-pressedRead by --btn-bg-active | var(--colors-neutral-200)Dark mode var(--colors-neutral-600) | |
--button-background-secondary-selectedRead by --btn-bg-selected | var(--colors-neutral-200)Dark mode var(--colors-neutral-600) |
JavaScript
The NDS.Fab script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
A FAB in the page docks by itself. Call the API for a FAB that a script makes after the page loads.
| Method | Effect |
|---|---|
NDS.Fab.init() |
Docks every nds-fab that is not docked yet |
NDS.Fab.register(fab, edge) |
Docks one FAB now and returns its dock. edge is optional and replaces the FAB’s data-fab-side |
NDS.Fab.destroy(fab) |
Puts a FAB back where it was written, hidden again. Pass a container to do this for every FAB written inside it. Returns the count |
NDS.Fab.getDock(edge) |
Returns the dock at left, right or bottom, and makes it if it is missing |
NDS.Fab.resolvePosition(fab) |
Returns the edge the FAB docks on: left, right or bottom |
A FAB fires no events.
The full API is in the banner of _js/nds-fab.js.