Actions · Material 3: FAB, Extended FAB, FAB menu
FAB
The one primary action of a screen. Standard (icon), extended (icon and label), or a FAB menu of related actions.
Examples and variants
Standard, extended, menu
The menu FAB's plus turns into a close mark.
function FabVariants() {
return (
<div className="flex items-center gap-4">
<Fab icon={<PlusIcon />} aria-label="New project" />
<Fab variant="extended" icon={<PencilIcon />}>
Compose
</Fab>
<Fab
icon={<PlusIcon />}
aria-label="Create"
size="sm"
menu={
<>
<DropdownMenuItem>
<PencilIcon /> Note
</DropdownMenuItem>
<DropdownMenuItem>
<CopyIcon /> Duplicate
</DropdownMenuItem>
</>
}
/>
</div>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/fab.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install @base-ui/react cn lucide-reactCopy these 2 files to the same paths in your project. Imports use the
@/alias for your project root.components/ui/dropdown-menu.tsxdropdown-menu
components/ui/dropdown-menu.tsx"use client" import * as React from "react" import { Menu as MenuPrimitive } from "@base-ui/react/menu" import { ChevronRightIcon } from "lucide-react" import { cn } from "cn" import { layoutTop } from "@/lib/motion/layout-offset" import { usePortalMotion } from "@/lib/overlay" function DropdownMenu(props: MenuPrimitive.Root.Props) { return <MenuPrimitive.Root {...props} /> } function DropdownMenuTrigger(props: MenuPrimitive.Trigger.Props) { return <MenuPrimitive.Trigger data-slot="menu-trigger" {...props} /> } /* On phones (< 640px) a root menu presents as a bottom sheet with a scrim (component-model.md §3: one Menu, adaptive presentation). Submenus and presentation="popover" keep the anchored popover everywhere. */ function DropdownMenuContent({ className, children, side, align, sideOffset = 6, presentation = "auto", ...props }: MenuPrimitive.Popup.Props & Pick<MenuPrimitive.Positioner.Props, "side" | "align" | "sideOffset"> & { presentation?: "auto" | "popover" }) { const submenu = (props as Record<string, unknown>)["data-submenu"] !== undefined const sheet = presentation === "auto" && !submenu const ref = React.useRef<HTMLDivElement>(null) usePortalMotion(ref) return ( <MenuPrimitive.Portal> {sheet ? <MenuPrimitive.Backdrop className="menu-sheet-scrim fixed inset-0 z-dropdown" /> : null} <MenuPrimitive.Positioner className={cn("z-dropdown", sheet && "menu-sheet-positioner")} side={side} align={align} sideOffset={sideOffset} > <MenuPrimitive.Popup ref={ref} data-slot="menu-content" data-sheet={sheet || undefined} className={cn( "overlay-surface overlay-grow z-dropdown min-w-(--menu-min-width) rounded-(--menu-corner) p-(--menu-padding) text-sm text-foreground", className )} {...props} > <MenuHighlight>{children}</MenuHighlight> </MenuPrimitive.Popup> </MenuPrimitive.Positioner> </MenuPrimitive.Portal> ) } export function MenuHighlight({ children }: { children: React.ReactNode }) { const ref = React.useRef<HTMLDivElement>(null) const [box, setBox] = React.useState({ top: 0, height: 0, stretch: false, danger: false }) const [ready, setReady] = React.useState(false) React.useLayoutEffect(() => { const root = ref.current if (!root) return const read = () => { const item = root.querySelector<HTMLElement>("[data-highlighted]") if (!item) return const motion = root.closest<HTMLElement>("[data-motion]")?.dataset.motion setBox({ top: layoutTop(item, root), height: item.offsetHeight, stretch: item.hasAttribute("data-popup-open") && motion !== "calm" && motion !== "still", danger: item.dataset.tone === "danger", }) setReady(true) } read() const observer = new MutationObserver(read) observer.observe(root, { subtree: true, attributes: true, attributeFilter: ["data-highlighted", "data-popup-open", "data-motion"], }) const resize = new ResizeObserver(read) resize.observe(root) return () => { observer.disconnect() resize.disconnect() } }, []) return ( <div ref={ref} className="relative"> {ready ? ( <span aria-hidden className={cn( "menu-pill pointer-events-none absolute left-1 rounded-(--menu-item-corner) bg-(--menu-item-highlight) transition-[background-color] duration-(--duration-fast)", box.danger && "bg-(--color-danger-subtle)" )} style={{ top: box.top, height: box.height, right: box.stretch ? "calc(var(--motion-distance-lg) * -1)" : "var(--space-1)", }} /> ) : null} {children} </div> ) } function DropdownMenuItem({ className, tone = "neutral", ...props }: MenuPrimitive.Item.Props & { tone?: "neutral" | "danger" }) { return ( <MenuPrimitive.Item data-slot="menu-item" data-tone={tone} className={cn( "menu-item relative z-[1] flex cursor-default items-center gap-inline-sm rounded-(--menu-item-corner) text-sm outline-none data-disabled:text-(--menu-item-label-disabled)", tone === "danger" && "text-(--color-danger-fg)", className )} {...props} /> ) } function DropdownMenuCheckboxItem({ className, ...props }: MenuPrimitive.CheckboxItem.Props) { return ( <MenuPrimitive.CheckboxItem data-slot="menu-item" className={cn( "menu-item relative z-[1] flex cursor-default items-center rounded-(--menu-item-corner) text-sm outline-none", className )} {...props} /> ) } function DropdownMenuRadioGroup(props: MenuPrimitive.RadioGroup.Props) { return <MenuPrimitive.RadioGroup {...props} /> } function DropdownMenuRadioItem({ className, ...props }: MenuPrimitive.RadioItem.Props) { return ( <MenuPrimitive.RadioItem data-slot="menu-item" className={cn( "menu-item relative z-[1] flex cursor-default items-center rounded-(--menu-item-corner) text-sm outline-none", className )} {...props} /> ) } function DropdownMenuLabel({ className, ...props }: MenuPrimitive.GroupLabel.Props) { return ( <MenuPrimitive.GroupLabel className={cn("px-2 py-1 text-xs text-muted-foreground", className)} {...props} /> ) } function DropdownMenuSeparator({ className, ...props }: React.ComponentProps<"div">) { return <div aria-hidden className={cn("h-stack-sm", className)} {...props} /> } function DropdownMenuSub(props: MenuPrimitive.SubmenuRoot.Props) { return <MenuPrimitive.SubmenuRoot {...props} /> } function DropdownMenuSubTrigger({ className, children, ...props }: MenuPrimitive.SubmenuTrigger.Props) { return ( <MenuPrimitive.SubmenuTrigger data-slot="menu-sub-trigger" className={cn( "menu-item relative z-[1] flex w-full cursor-default items-center gap-inline-sm rounded-(--menu-item-corner) text-sm outline-none", className )} {...props} > {children} <ChevronRightIcon className="ml-auto size-icon-sm text-muted-foreground" /> </MenuPrimitive.SubmenuTrigger> ) } function DropdownMenuSubContent(props: MenuPrimitive.Popup.Props) { return <DropdownMenuContent data-submenu="" {...props} /> } function DropdownMenuShortcut({ className, ...props }: React.ComponentProps<"span">) { return <span className={cn("ml-auto text-xs text-muted-foreground", className)} {...props} /> } export { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuItem, DropdownMenuCheckboxItem, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuSub, DropdownMenuSubTrigger, DropdownMenuSubContent, DropdownMenuShortcut, }components/ui/fab.tsxfab
components/ui/fab.tsx"use client" import * as React from "react" import { Button as ButtonPrimitive } from "@base-ui/react/button" import { cn } from "cn" import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" /* FAB: the one primary action of a screen. variant="standard" is icon-only; "extended" adds a label. With `menu`, it opens related actions (FAB menu) and the icon turns into a close mark. */ type FabProps = ButtonPrimitive.Props & { variant?: "standard" | "extended" size?: "sm" | "md" icon: React.ReactNode /** Required for the standard (icon-only) FAB. */ "aria-label"?: string menu?: React.ReactNode } const fabClass = (variant: FabProps["variant"], size: FabProps["size"]) => cn( "fab liquid-press inline-flex shrink-0 items-center justify-center gap-(--space-2) rounded-(--fab-corner) active:rounded-(--fab-corner-pressed) bg-(--fab-container) font-medium text-(--fab-label) shadow-(--fab-elevation) outline-none hover:bg-(--fab-container-hover) hover:shadow-(--fab-elevation-hover) active:bg-(--fab-container-pressed) focus-visible:shadow-(--focus-ring) [&_svg]:size-(--fab-icon-size) [&_svg]:shrink-0", size === "sm" ? "h-(--fab-size-sm) min-w-(--fab-size-sm)" : "h-(--fab-size) min-w-(--fab-size)", variant === "extended" ? "px-(--fab-padding-x-extended)" : "" ) function Fab({ className, variant = "standard", size = "md", icon, children, menu, ...props }: FabProps) { const content = ( <> <span className="fab-icon inline-flex">{icon}</span> {variant === "extended" ? <span>{children}</span> : null} </> ) if (menu) { return ( <DropdownMenu> <DropdownMenuTrigger data-slot="fab" className={cn(fabClass(variant, size), className)} {...props}> {content} </DropdownMenuTrigger> <DropdownMenuContent side="top" align="end"> {menu} </DropdownMenuContent> </DropdownMenu> ) } return ( <ButtonPrimitive data-slot="fab" className={cn(fabClass(variant, size), className)} {...props}> {content} </ButtonPrimitive> ) } export { Fab }
Imports
What the examples on this page import:
import {
import { AppBar } from "@/components/ui/app-bar"
import { Badge } from "@/components/ui/badge"
import { Breadcrumb } from "@/components/ui/breadcrumb"
import { Button } from "@/components/ui/button"
import { DropdownMenuItem } from "@/components/ui/dropdown-menu"
import { Fab } from "@/components/ui/fab"
import { Navigation, NavigationItem } from "@/components/ui/navigation"
import { Pagination } from "@/components/ui/pagination"
import { SplitButton } from "@/components/ui/split-button"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { Toolbar, ToolbarButton, ToolbarGroup, ToolbarSeparator } from "@/components/ui/toolbar"Anatomy
- 1Container · --fab-size, --fab-corner, --fab-container, --fab-elevation.
- 2Icon · --fab-icon-size.
- 3Label · Extended only; --fab-padding-x-extended.
- 4Menu · With `menu`, opens above.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "standard" | "extended" | "standard" | Icon only, or icon and label. |
size | "sm" | "md" | "md" | 40 or 56px. |
icon | ReactNode | — | Required. |
menu | ReactNode | — | Turns it into a FAB menu. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--fab-size | var(--size-fab) | 56px | same |
--fab-size-sm | var(--size-fab-sm) | 40px | same |
--fab-corner | var(--shape-lg) | 22px | same |
--fab-corner-pressed | var(--fab-corner) | 22px | same |
--fab-container | var(--color-brand) | oklch(0.18 0.008 260) | oklch(0.98 0 0) |
--fab-label | var(--color-fg-on-accent) | oklch(0.97 0.004 95) | oklch(0.14 0 0) |
--fab-elevation | var(--elevation-3) | 0 1px 1px -0.5px oklch(0.22 0.01 260 / 8%), 0 3px 3px -1.5px oklch(0.22 0.01 260 / 8%), 0 6px 6px -3px oklch(0.22 0.01 260 / 8%), 0 12px 12px -6px oklch(0.22 0.01 260 / 8%), 0 24px 24px -12px oklch(0.22 0.01 260 / 8%), 0 48px 48px -24px oklch(0.22 0.01 260 / 6%) | 0 8px 20px oklch(0 0 0 / 45%), 0 20px 44px oklch(0 0 0 / 40%) |
--fab-elevation-hover | var(--elevation-4) | 0 2.8px 2.2px oklch(0.22 0.01 260 / 3.4%), 0 6.7px 5.3px oklch(0.22 0.01 260 / 4.8%), 0 12.5px 10px oklch(0.22 0.01 260 / 6%), 0 22.3px 17.9px oklch(0.22 0.01 260 / 7.2%), 0 41.8px 33.4px oklch(0.22 0.01 260 / 8.6%), 0 100px 80px oklch(0.22 0.01 260 / 12%) | 0 12px 24px oklch(0 0 0 / 50%), 0 28px 56px oklch(0 0 0 / 45%) |
--fab-container-hover | var(--color-brand-hover) | oklch(0.28 0.008 260) | oklch(0.9 0 0) |
--fab-container-pressed | var(--color-brand-pressed) | oklch(0.14 0.008 260) | oklch(0.82 0 0) |
--fab-icon-size | var(--size-icon-xl) | 24px | same |
--fab-padding-x-extended | var(--space-5) | 20px | same |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Hover / press | Lifts to a deeper shadow; squashes on press. | --fab-elevation-hover |
| Open menu | The icon rotates 45° (plus to close). | --duration-medium, --ease-bounce |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Standard FABs need aria-label.
- One FAB per screen.
Usage
Do
- Use for the screen's main creative action.
Don’t
- Don't use for destructive or minor actions.
Code
"use client"
import * as React from "react"
import { Button as ButtonPrimitive } from "@base-ui/react/button"
import { cn } from "cn"
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"
/*
FAB: the one primary action of a screen. variant="standard" is icon-only;
"extended" adds a label. With `menu`, it opens related actions (FAB menu)
and the icon turns into a close mark.
*/
type FabProps = ButtonPrimitive.Props & {
variant?: "standard" | "extended"
size?: "sm" | "md"
icon: React.ReactNode
/** Required for the standard (icon-only) FAB. */
"aria-label"?: string
menu?: React.ReactNode
}
const fabClass = (variant: FabProps["variant"], size: FabProps["size"]) =>
cn(
"fab liquid-press inline-flex shrink-0 items-center justify-center gap-(--space-2) rounded-(--fab-corner) active:rounded-(--fab-corner-pressed) bg-(--fab-container) font-medium text-(--fab-label) shadow-(--fab-elevation) outline-none hover:bg-(--fab-container-hover) hover:shadow-(--fab-elevation-hover) active:bg-(--fab-container-pressed) focus-visible:shadow-(--focus-ring) [&_svg]:size-(--fab-icon-size) [&_svg]:shrink-0",
size === "sm" ? "h-(--fab-size-sm) min-w-(--fab-size-sm)" : "h-(--fab-size) min-w-(--fab-size)",
variant === "extended" ? "px-(--fab-padding-x-extended)" : ""
)
function Fab({ className, variant = "standard", size = "md", icon, children, menu, ...props }: FabProps) {
const content = (
<>
<span className="fab-icon inline-flex">{icon}</span>
{variant === "extended" ? <span>{children}</span> : null}
</>
)
if (menu) {
return (
<DropdownMenu>
<DropdownMenuTrigger data-slot="fab" className={cn(fabClass(variant, size), className)} {...props}>
{content}
</DropdownMenuTrigger>
<DropdownMenuContent side="top" align="end">
{menu}
</DropdownMenuContent>
</DropdownMenu>
)
}
return (
<ButtonPrimitive data-slot="fab" className={cn(fabClass(variant, size), className)} {...props}>
{content}
</ButtonPrimitive>
)
}
export { Fab }