DashboardGrid
DashboardGrid lays out tiles on a grid. In edit mode the tiles are dragged and resized with the mouse, a finger or the keyboard, the other tiles make room, and every move is announced to screen readers. The layout adapts to the width of the grid, renders on the server and has no dependency.
Import #
import { DashboardGrid, DashboardGridItemTemplate } from '@exhaustive-ui/core/dashboardgrid';Basic #
DashboardGrid places tiles on a grid of columns, 12 by default, and rows of rowHeight pixels, with a gap between them. The layout is an array of tiles, each with an id, a column x and a row y counted from 0, a width w in columns and a height h in rows. The grid is a CSS grid, so the layout renders on the server and before the JavaScript loads.
The item template draws a tile: it receives the tile as the implicit variable, its position as displayed, and the editable, grabbed, dragging and resizing flags. Write it as <ng-template [pDashboardGridItem]="layout" let-tile> to type tile with the type of your layout, or as <ng-template #item let-tile>. With editable, tiles are dragged to move them and resized by their corner, and the grid updates layout with a new array, so bind it two-way with [(layout)].
Drag Handle #
Without a drag handle, a tile drags from anywhere but its buttons, links and form fields. dragHandle takes a CSS selector, such as .drag-handle, and only the matching element of a tile starts a drag: the rest of the tile stays free for selecting text, scrolling and clicking, and on touch screens the page still scrolls when a finger moves over a tile. The grid gives the handle the grab cursor. The keyboard still moves the whole tile.
12 unread messages, 3 flagged.
Design review at 2:30 PM.
5 tasks due this week.
The buttons of the tiles still work.
Resize and Compaction #
resizeHandles lists the sides and corners that resize a tile: n, s, e, w, ne, nw, se and sw, with se alone by default. e is the inline end, the right side in left-to-right pages and the left side in right-to-left ones. A tile stays within its minW, maxW, minH and maxH, in cells, and within the columns; resizable: false or draggable: false turn off one of the two for a tile.
While a tile grows, the tiles in its way are pushed down. With compact set to vertical, the default, the tiles then move up to fill the gaps; with none they stay where they are dropped, and empty rows are kept.
Static Tiles #
A static tile never moves: it can not be dragged or resized, a dragged tile can not be dropped over it, and the tiles pushed or compacted go around it. It is not focusable in edit mode, since nothing can be done to it. Use it for a header, a notice or a tile the user must keep in view.
Responsive #
The grid follows its own width, not the width of the window. breakpoints gives the number of columns at or below a maxWidth in pixels, and the smallest matching breakpoint applies. A breakpoint without a layout gets the base layout scaled to its columns; layouts holds a layout per breakpoint name, with the id, x, y, w and h of the tiles. A tile missing from it goes to the first free space, and the edits made at a breakpoint are saved in layouts, so bind it two-way with [(layouts)].
Below stackBelow pixels, 576 by default, the tiles are stacked in a single column in reading order, each with its height, and can not be edited. onBreakpointChange tells the breakpoint, the columns and whether the tiles are stacked. Resize the window to see the layout change.
12 columns
Add and Remove #
addItem adds a tile: without x and y, or when its place is taken, it goes to the first free space, found with findFreeSpace, and it is also placed in every layout of layouts. removeItem removes a tile by its id, and the others move up. Both update layout, emit onLayoutChange with the add or remove action and are announced to screen readers.
A tile holds any component. Give the content of a tile flex-1 min-h-0 to fill its height, as the charts below do.
- Apps (16%)
- Messages (8%)
- Media (24%)
- System (10%)
Saving the Layout #
onLayoutChange is emitted once a tile is dropped, resized, added or removed, after layout or layouts is updated, with the layout as displayed, the breakpoint that was edited, the columns, the action and the item. A keyboard move only emits when the tile is dropped, and Escape emits nothing. Save the positions from there: this demo keeps them in the local storage of the browser, so the layout is the same after a reload.
Save only the id, x, y, w and h of the tiles and merge them with the tiles of the application when loading, so that new tiles and new settings are not lost.
Accessibility #
Screen Reader
The grid is a list and each tile a list item, in reading order, row by row, whatever the order of the layout array, so the tab order and the reading order follow what is on screen. A tile is named by its label, or by the field or function given to itemLabel. Name the grid with ariaLabel or ariaLabelledBy: it then gets the group role.
In edit mode, every tile that can move or be resized is focusable and described with aria-describedby by the instructions to move it. A tile is moved and resized with the keyboard alone, and a polite live region announces each step: "Picked up Sales, column 1, row 1, 4 columns wide and 2 rows high", "Moved Sales to column 3, row 2", "Resized Sales to 6 columns wide and 2 rows high", "Dropped Sales at column 3, row 2, 6 columns wide and 2 rows high" or "Cancelled, Sales returned to column 1, row 1". A move blocked by the edge or a static tile, and a size beyond the limits of a tile, are announced too, and so are the tiles added and removed with addItem and removeItem. Translate every text with labels.
The resize handles are drawn for the pointer and hidden from assistive technologies, since Shift and the arrow keys resize a tile. Static tiles are not focusable. Keep the content of a tile reachable: its buttons, links and fields work as usual, and the keys pressed in them never move the tile. The tiles glide to their new place unless the user prefers reduced motion.
<h2 id="overview">Overview</h2>
<p-dashboard-grid [(layout)]="layout" editable ariaLabelledBy="overview" [labels]="{ moved: '{0} déplacé en colonne {1}, ligne {2}' }" />Tile Keyboard Support
| Key | Function |
|---|---|
| tab | Moves focus to the next tile or to the content of the tile. Leaving a picked up tile cancels its move. |
| space / enter | Picks up the focused tile, or drops the picked up tile where it is. |
| left arrow / right arrow | Moves the picked up tile one column to the left or the right, pushing the tiles in its way down. Reversed in right-to-left layouts. |
| up arrow / down arrow | Moves the picked up tile up or down, past the next tile when the layout is compacted, so that the two swap. |
| shift + left arrow / right arrow | Makes the picked up tile one column narrower or wider, within its limits. Reversed in right-to-left layouts. |
| shift + up arrow / down arrow | Makes the picked up tile one row shorter or taller, within its limits. |
| escape | Cancels the move and puts the tiles back where they were. Also cancels a drag or a resize with the pointer. |
The changes are only emitted when the tile is dropped, so a cancelled keyboard move never reaches layout or onLayoutChange.