mui-collage-fab
v0.18.0
Published
Material UI floating action button that unfolds a stack of overlapping Fab circles into a row, a fan or a ring.
Maintainers
Readme
MUI CollageFab
| Collapsed | Expanded | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | | |
A Material UI floating action button that behaves like a collapsed stack of overlapping circles. Collapsed, a set of Fabs sits perfectly stacked and reads as a single button: the anchor's disc in the middle, with the other children grown by ringPeek into an even collar whose colours blend into the centre and into each other over fadeWidth. On hover (mouse) or tap (touch), the parent grows into the box its children travel through and they fan out — into a row, into two axes with directions, or onto a ring with spread="radial" — while their CSS masks slide in lockstep with the movement, so the unfold reads as one continuous morph instead of a pop.
The trick is that each child is a full circle clipped by a static mask that the item slides through: a linear-gradient band for a row, a conic-gradient wedge whose apex sits on the anchor for a ring. As the element translates, the mask-position is translated back by the same amount, keeping the cut line fixed in container space. Because the cut is a gradient rather than a hard clip, the parent's filter: drop-shadow(...) traces the soft edges cleanly.
Not affiliated with or endorsed by MUI.
Install
npm install mui-collage-fab @mui/material @emotion/react @emotion/styledreact, react-dom, @mui/material and the two emotion packages are peer
dependencies, so the package never ships its own copy of React or MUI. The
published bundle targets ES2020 and runs on Node.js 18 or newer when server rendered.
Usage
import { Fab } from '@mui/material';
import { Settings, BugReport, Speed } from '@mui/icons-material';
import { CollageFab } from 'mui-collage-fab';
<CollageFab
direction="left"
trigger="hover"
gap={14}
fadeWidth={12}
aria-label="Tools"
slotProps={{
item: (ownerState) => (ownerState.isAnchor ? { sx: { outline: '2px solid white' } } : {}),
}}
>
{/* Index 0 is the anchor: it never moves and is never clipped. */}
<Fab color="primary">
<Settings />
</Fab>
<Fab color="secondary">
<BugReport />
</Fab>
<Fab color="success">
<Speed />
</Fab>
</CollageFab>;Controlled usage, when the open state has to live in your own state:
const [open, setOpen] = useState(false);
<CollageFab
trigger="manual"
open={open}
onOpen={() => setOpen(true)}
onClose={() => setOpen(false)}
>
{/* children */}
</CollageFab>;Props
| Prop | Type | Default | Description |
| -------------------- | ------------------------------------------ | -------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| children | React.ReactNode | — | The buttons to stack. Every valid element is wrapped in a masked slot shaped by variant. Order matters: index 0 is the anchor, the rest fan out along direction or their own entry in directions. |
| direction | 'left' \| 'right' \| 'up' \| 'down' | 'left' | The axis the buttons fan out on, starting from the anchor child. left matches the original right-anchored behaviour. |
| directions | ('left' \| 'right' \| 'up' \| 'down')[] | — | A direction per child, in child order, so one group can fan over several axes. Entry 0 belongs to the anchor and has no visible effect; an entry that is missing or past the end of the list falls back to direction. |
| spread | 'line' \| 'radial' | 'line' | How the children leave the anchor. 'line' fans them along their directions; 'radial' sends them onto a ring one pitch around the anchor, each cut by a wedge that lets it emerge from behind the anchor. |
| radialStart | number | 0 | Angle in degrees the first ring child sits on when spread is 'radial'. |
| radialStep | number | — | Angle in degrees between two ring children when spread is 'radial'. Defaults to 360 / (count - 1), which spaces them evenly around the anchor. |
| size | 'small' \| 'medium' \| 'large' \| number | 'large' | Diameter of every button. Named sizes resolve to small 40px, medium 48px, large 56px; numeric values are used verbatim. |
| variant | 'circular' \| 'rounded' \| 'square' | 'circular' | The shape of every button. 'circular' clips each slice to a circle, 'rounded' to a rounded square and 'square' to a sharp-cornered square. The mask still slices the silhouette along the main axis, so the stack reads as one shape when collapsed and as separate shapes when expanded. |
| gap | number | 16 | Main-axis gap in pixels between two buttons when expanded. |
| fadeWidth | number | 12 | Width in pixels of the soft cut line between two slices. On a ring it is also the width of the ramp where the petals blend into the anchor and into each other. |
| ringPeek | number | 6 | How many pixels the petals of a collapsed ring or multi-axis group grow beyond the anchor's radius, so their colours show as an even collar blended into the button. 0 hides them completely. |
| trigger | 'hover' \| 'click' \| 'manual' \| 'auto' | 'hover' | How the group reacts to pointers. 'hover' opens on mouse enter, closes on mouse leave and toggles on touch or pen tap; 'click' toggles on any click; 'manual' disables all built-in pointer interaction; 'auto' behaves like 'hover' where the primary pointer can hover and like 'click' elsewhere. |
| open | boolean | — | If true, the group is expanded. Use it to control the component. |
| defaultOpen | boolean | false | If true, the group is expanded by default (uncontrolled). |
| onOpen | (event: React.SyntheticEvent) => void | — | Callback fired when the group is expanded. |
| onClose | (event: React.SyntheticEvent) => void | — | Callback fired when the group is collapsed. |
| disableClickAway | boolean | false | If true, the group does not collapse when the user clicks away from it. |
| transitionDuration | number | 400 | Duration of the morph, in milliseconds. |
| transitionEasing | string | 'cubic-bezier(0.4, 0, 0.2, 1)' | Easing of the morph. |
| sx | SxProps<Theme> | — | System prop that defines system overrides as well as additional CSS styles. |
| classes | Partial<CollageFabClasses> | — | Override or extend the styles applied to the component. |
| slots | CollageFabSlots | — | Custom components for the root and item slots. |
| slotProps | { root?, item? } | — | Props for each slot. slotProps.item accepts either a props object or an (ownerState) => props callback, which is the per-child escape hatch. |
| ...divProps | React.HTMLAttributes<HTMLDivElement> | — | The remaining div attributes are spread onto the root. |
Slots
root— The animated container. A replacement must forwardref, whichClickAwayListenerneeds.item— One masked wrapper per child, shaped byvariant.
ownerState
CollageFabOwnerState carries open, direction, directions, spread, radialStart, radialStep, trigger, size, variant, cornerRadius, gap, fadeWidth, ringPeek, count, transitionDuration and transitionEasing. CollageFabItemOwnerState adds index, isAnchor and geometry.
Theme-level defaults and overrides work through the MuiCollageFab component key:
const theme = createTheme({
components: {
MuiCollageFab: {
defaultProps: { gap: 10 },
styleOverrides: { item: { borderRadius: '8px' } },
},
},
});Notes
- The parent must pin the anchor. The root animates its own extent, so it only fans out if its parent keeps the anchor still: the corner the fan leaves behind (
justify-content: flex-endfordirection="left",flex-startfor"right",align-items: flex-endfor"up",flex-startfor"down"), or the centre for an axis that fans both ways. A radial group keeps its single-button box and paints the ring outside it, so it only needs room around it. Otherwise the root grows in place and pushes surrounding content instead of fanning out. - Each child is forced to
box-shadow: noneand 100% size, so the parent owns the global shadow shape. Pass visual styles via each child'ssx; the component overrides sizing and shadow only. Overriding that reset requiresslotProps.item.sx['& > *']with!important. - The single
drop-shadowlives on the root, because a filter traces the soft mask edges while abox-shadowwould trace the wrapper rectangles. Settingoverflow: hiddenon the root viasxclips the fan-out. - Shapes.
variantcovers the three common cases; children inherit the wrapper's corner radius, so the shape applies to the whole group rather than to each button's ownFabdefault. Any other radius is reachable withslotProps.item.sx. - Collapsed slices are hidden from assistive technology. While collapsed, the item wrappers are
aria-hiddenand made non-focusable, so they leave the tab order and the accessibility tree — undertrigger="manual"the anchor stays live so it can be the handle. The root stays focusable: Enter or Space opens the group, focus moves to the first item, and Escape closes it and returns focus to the root. - With a single child no mask is applied — it behaves like a plain
Fab. directionis physical, not logical. It is not mirrored under RTL; right-to-left layouts should setdirection="left"explicitly.directionsgives each child its own direction, in child order; children that share one fan a pitch apart, so a group can split into a row and a column.spread="radial"rings the children around the anchor at one pitch, withradialStartrotating the ring andradialStep(default360 / (count - 1)) spacing the ring children evenly. The anchor stays at the centre and never moves: it is the base disc, and every petal cuts the hub it covers out of its own mask, ramping back in overfadeWidthso the centre blends into the rim. While the petals are stacked they grow byringPeek, which makes the rim the same width all the way round; both numbers are unchanged for a group that fans over several axes.
Demo site
- Demo — https://radomir.dev/mui-collage-fab/
- Components API — https://radomir.dev/mui-collage-fab/#/docs
- Playground — https://radomir.dev/mui-collage-fab/#/playground
Routing is hash-based, so the built site works on any static host without a server-side fallback.
| URL | Page |
| ------------- | --------------------------------------------------------- |
| / | Demo — the component across button counts and directions. |
| #/docs | Components API reference for CollageFab. |
| anything else | Redirects to #/. |
Testing
src/components/collageFabGeometry.ts holds the mask and translation maths, so it can be
tested without a DOM: tests/geometry.test.ts asserts the translate offsets, the mask axis
and stop order, and the invariant that a slice's mask position and translation cancel out —
the cut line stays pinned while the button travels. tests/behaviour.test.tsx runs the
component in jsdom for triggers, keyboard flow, controlled state and click-away.
e2e/collagefab.spec.ts is the browser-level check, because jsdom cannot compute masks:
it reads the real transform and mask-position after hovering and verifies they move by
the same pitch in opposite directions, and checks the mixed-axis and radial layouts.
e2e/conic-mask.spec.ts proves the compensated conic mask clips the same in chromium,
firefox and webkit against a single shared screenshot baseline.
Development
Requires Node.js 22.12 or newer. The development toolchain is pinned in mise.toml.
npm install
npm run dev| Command | Description |
| -------------------------------- | ------------------------------------------------------------------------ |
| npm run dev | Start the Vite dev server with HMR. |
| npm run build | Type-check with tsc --noEmit, then build the demo to dist/. |
| npm run build:preview | Same, with the asset base taken from SITE_BASE_PATH. |
| npm run build:lib | Build the publishable package to dist-lib/ (ESM + CJS + types). |
| npm run preview | Serve the production build locally. |
| npm test | Run the Vitest suite (geometry + behaviour in jsdom). |
| npm run test:coverage | Same suite with a v8 coverage report. |
| npm run test:e2e | Build, then run the Playwright checks against vite preview. |
| npm run types:test | Type-check the public API surface (expectTypeOf + @ts-expect-error). |
| npm run check:package | publint, attw --pack and a tarball content assertion. |
| npm run check:size | Enforce the size-limit budgets for dist-lib. |
| npm run lint | Oxlint, type-aware, with hooks and jsx-a11y rules. |
| npm run format:check | Oxfmt check. npm run format rewrites in place. |
| npm run check:node | Assert @types/node, mise.toml and devEngines share a major. |
| npm run check:deps | Dependency-cruiser: cycles, orphans and library/site imports. |
| npm run check:unused | Knip: unused files, exports and dependencies. |
| npm run check:release | semantic-release --dry-run, validating the release config. |
| npm run check:actions-security | Zizmor audit of the workflows. |
| npm run doctor | Run every check below in one pass. |
| npm run doctor:fast | Same, minus the browser suite and npm audit. |
Checks
npm run doctor (listr2) runs the checks in sequence and keeps going after a
failure, so one pass reports everything that is broken:
format:check,lint,typecheck,check:deps— style, static analysis and the dependency graph.types:test— the public prop unions and slot overrides, pinned by type tests.test:coverage— Vitest with coverage thresholds of 85 statements / 75 branches.build,build:lib— demo and library bundles.check:package—publint,attw --packand a tarball assertion, so the publishedexports/types map and file list stay correct.check:size— size-limit budgets for the ESM and CJS bundles.docs:check,actionlint,auditandtest:e2e— skipped with a reason when the script exists but the tool is missing (for exampleactionlintlocally).
Property tests (fast-check, tests/geometry.property.test.ts) generate sizes, gaps,
fade widths, counts and directions and assert the geometry invariants rather than
enumerating cases: one pitch of translation per slice, a cut line pinned in container
space, and a mask window wide enough to cover the fan. check:unused (knip) catches dead
files, exports and dependencies; check:deps (dependency-cruiser) rejects runtime cycles,
orphan modules and imports that reach from the published library into demo code;
check:release loads every semantic-release plugin through --dry-run; the e2e suite
also runs a Pixel-class mobile project with touch gesture specs, and zizmor audits the
workflows themselves in CI.
--fast drops test:e2e and audit, --only=lint,unit runs a subset. Each failed step
keeps its output in node_modules/.cache/doctor/<step>.log. CI runs
npm run doctor -- --fast plus a separate e2e job covering chromium, firefox, webkit and
a Pixel-class mobile viewport, zizmor on the workflows, and dependency-review on pull
requests.
Deployment: .github/workflows/deploy.yml builds the demo on every push to
main and pushes dist/ to the gh-pages branch, which GitHub Pages serves.
The branch holds nothing but built files. Every pull request gets that same
build one directory deeper, at
https://radomir.dev/mui-collage-fab/pr-preview/pr-<number>/:
.github/workflows/preview.yml builds it with SITE_BASE_PATH set to that
path, the preview action writes it to gh-pages, comments the URL on the pull
request, and removes the directory again when the pull request closes. The
deploy workflow keeps pr-preview/ intact rather than cleaning the branch, so
a push to main does not take the open previews with it.
.github/workflows/publish.yml runs semantic-release on
every push to main: conventional commit subjects decide the version (fix:
patch, feat: minor, BREAKING CHANGE: major), and a release publishes the
package, writes CHANGELOG.md, and commits the bump back with [skip ci].
Pull request titles are linted to the same convention, which matters because
squash merges put the PR title in the commit subject. The production build uses a /mui-collage-fab/ asset
base while the dev server stays at the root, so the local URL is unchanged —
see vite.config.ts.
