FloatingToolbar for contextual actions over a canvas item
FloatingToolbar in Cratis Components shows contextual actions for one item, such as copy, pin, a colour choice and delete for a selected note on a canvas. Render it inside the item it belongs to. It positions itself against that item, not against the viewport.
Basic usage
Section titled “Basic usage”import { Canvas, CanvasItem, canvasTransformActivity } from '@cratis/components/Canvas';import { FloatingToolbar, type FloatingToolbarAction } from '@cratis/components/FloatingToolbar';
export function Note({ selected, onCopy, onDelete }: { selected: boolean; onCopy: () => void; onDelete: () => void }) { const actions: FloatingToolbarAction[] = [ { id: 'copy', label: 'Copy', icon: <CopyIcon />, onClick: onCopy }, { id: 'yellow', label: 'Yellow', swatch: '#facc15', separated: true }, { id: 'delete', label: 'Delete', icon: <TrashIcon />, danger: true, separated: true, onClick: onDelete }, ]; return ( <CanvasItem x={100} y={80}> <div className='note'>Note</div> {selected && <FloatingToolbar label='Note actions' actions={actions} track={canvasTransformActivity.subscribe} />} </CanvasItem> );}Mount the toolbar while the item is selected and unmount it when it is not. Import the stylesheet once, either the aggregate @cratis/components/styles or the area sheet @cratis/components/FloatingToolbar/styles. Follow the styling setup for token defaults.
Actions
Section titled “Actions”Each FloatingToolbarAction is an icon button with a tooltip.
| Field | Effect |
|---|---|
id, label |
The key and the button’s accessible name and tooltip. |
icon |
The glyph: a React node or an icon-font class. |
swatch |
A colour chip instead of the icon. Any CSS colour value, such as #facc15 or var(--my-color). |
active |
Makes the action a toggle. aria-pressed follows it; leave it undefined for a plain action. |
danger |
Styles the action as destructive. |
separated |
Draws a divider before the action. |
disabled |
Blocks onClick and skips the button in arrow-key navigation. |
Pass children to add content after the actions, such as a Menu trigger for less common actions. The menu’s trigger takes part in the toolbar’s arrow-key navigation.
Placement
Section titled “Placement”| Prop | Values | Effect |
|---|---|---|
side |
'above' (default), 'below' |
Which side of the item the toolbar sits on. It flips when there is no room. |
align |
'center' (default), 'start', 'end' |
Which physical edge of the item it lines up with. Physical edges do not flip in right-to-left layouts. |
anchor |
a ref | Positions against another element instead of the element the toolbar is rendered in. |
The toolbar is portaled out of its host, so it is not clipped by a scrolling or overflow: hidden parent and it does not depend on the host being position: relative. It stays the same size at any canvas zoom. It is hidden while the item is scrolled out of the area that clips it.
Following a pan or zoom
Section titled “Following a pan or zoom”A canvas moves its items with a transform, which does not re-render them. Pass track a function that subscribes to that movement and returns the unsubscribe function. On a canvas, canvasTransformActivity.subscribe is that function. A move caused by the host re-rendering is followed without track.
Events and keyboard
Section titled “Events and keyboard”Pointer, mouse, click and double-click events on the toolbar do not reach the canvas, so pressing an action does not start a pan, a drag, a deselect or a marquee. Arrow keys, Home, End, Enter and Space are the toolbar’s own and are not seen by the canvas. Escape and other shortcuts still propagate, so the host can deselect on Escape.
The toolbar is one Tab stop. Arrow keys, Home and End move between the enabled buttons. Because it is portaled, a zero-size stand-in stays in the host: Tab into the stand-in moves focus into the toolbar, and Tab from either end of the toolbar continues from the item’s place in the page.
Accessibility
Section titled “Accessibility”The toolbar has role="toolbar" and an accessible name from label. Each button is named from its action’s label, and toggles expose aria-pressed.
Styling
Section titled “Styling”The toolbar uses --cratis-* tokens (--cratis-surface-card, --cratis-surface-border, --cratis-surface-hover, --cratis-action-background, --cratis-danger-background) and stacks just below the dialog layer, or above an enclosing dialog. Pass className to add your own class.