Data
Grid layout
A grid of items that each user moves and resizes, to build a dashboard for example.
Example
Each CrGridItem has a title (label), a position (x, y, in cells starting from 0) and a size (w columns, h rows). The user moves it by grabbing it anywhere (or by its ⠿ handle), and resizes it by dragging its corner: the grid appears, the target slot is drawn with a dashed outline, the other items move aside during the drag, then everything packs upwards.
'use client';
import { useRef } from 'react';
import { CrButton, CrGridLayout, CrGridItem } from '@cristal-ui/react';
export default function GridLayoutBasic() {
const grid = useRef<HTMLCrGridLayoutElement>(null);
return (
<div style={{ width: '100%' }}>
<CrButton size="s" variant="secondary" onClick={() => grid.current?.reset()}>
Reset
</CrButton>
<CrGridLayout ref={grid} label="Sales dashboard" columns={6} rowHeight={70} storageKey="docs-grid-en" style={{ marginTop: 16 }}>
<CrGridItem id="ventes" label="Sales" x={0} y={0} w={2} h={2}>
<strong style={{ fontSize: 26 }}>$48,920</strong>
</CrGridItem>
<CrGridItem id="clients" label="New customers" x={2} y={0} w={2} h={2}>
<strong style={{ fontSize: 26 }}>312</strong>
</CrGridItem>
<CrGridItem id="panier" label="Average order" x={4} y={0} w={2} h={2}>
<strong style={{ fontSize: 26 }}>$64</strong>
</CrGridItem>
<CrGridItem id="notes" label="Notes" x={0} y={2} w={4} h={2} minW={2}>
Drag a card, pull its corner to resize it.
</CrGridItem>
</CrGridLayout>
</div>
);
}Grabbing a card
The grab property of CrGridLayout chooses how the mouse grabs a card:
grab | Behaviour |
|---|---|
surface (default) | anywhere on the card: dragging starts as soon as the pointer moves |
hold | anywhere, after holding the pointer down for a moment (300 ms) without moving; a click followed by a quick movement selects text as usual |
handle | by the ⠿ handle only |
In every case the ⠿ handle moves the card immediately, and pressing a button, a checkbox, a link, a field or a scrollbar inside the card does not move it. With a finger, on a touch screen, only the handle moves the card: swiping on the card scrolls the page.
Look and resize handles
variant changes the look of a CrGridItem, resizeHandle its resize handle.
'use client';
import { useState } from 'react';
import { CrGridItem, CrGridLayout, CrSegmented } from '@cristal-ui/react';
const variants = ['card', 'elevated', 'soft', 'outline', 'glass'] as const;
const handles = ['corner', 'knob', 'edges'] as const;
// The glass variant needs a colourful background to show.
const backdrop = 'radial-gradient(circle at 15% 20%, #c4b5fd, transparent 45%), radial-gradient(circle at 85% 70%, #f9a8d4, transparent 45%), #e0f2fe';
export default function GridLayoutVariants() {
const [variant, setVariant] = useState<(typeof variants)[number]>('elevated');
const [handle, setHandle] = useState<(typeof handles)[number]>('corner');
const item = { variant, resizeHandle: handle };
return (
<div style={{ width: '100%', display: 'grid', gap: 16 }}>
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 16 }}>
<CrSegmented label="Look" value={variant} onCrChange={(event) => setVariant(event.detail as typeof variant)}>
{variants.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</CrSegmented>
<CrSegmented label="Resize" value={handle} onCrChange={(event) => setHandle(event.detail as typeof handle)}>
{handles.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</CrSegmented>
</div>
<div style={{ padding: 16, borderRadius: 16, background: variant === 'glass' ? backdrop : undefined }}>
<CrGridLayout label="Indicators" columns={6} rowHeight={70}>
<CrGridItem {...item} id="v-ventes" label="Sales" x={0} y={0} w={2} h={2}>
<strong style={{ fontSize: 26 }}>48 920 €</strong>
</CrGridItem>
<CrGridItem {...item} id="v-clients" label="New customers" x={2} y={0} w={2} h={2}>
<strong style={{ fontSize: 26 }}>312</strong>
</CrGridItem>
<CrGridItem {...item} id="v-panier" label="Average basket" x={4} y={0} w={2} h={2}>
<strong style={{ fontSize: 26 }}>64 €</strong>
</CrGridItem>
<CrGridItem {...item} id="v-notes" label="Notes" x={0} y={2} w={4} h={2} minW={2}>
Drag the corner or the edges to resize the card.
</CrGridItem>
</CrGridLayout>
</div>
</div>
);
}variant | Look |
|---|---|
card (default) | bordered card, light shadow |
elevated | no border, a soft shadow that grows on hover |
soft | tinted with the primary colour, coloured title |
outline | outline only, the card shows the background |
glass | frosted glass: put it on a colourful background or an image, light or dark enough for the text to stay readable |
resizeHandle | Handle |
|---|---|
corner (default) | an arc that follows the card's rounded corner |
knob | a round knob on the corner |
edges | the corner, plus the right edge (width only) and the bottom edge (height only); a bar shows on the hovered edge |
Grid
CrGridLayout property | Role |
|---|---|
columns | number of columns (12 by default) |
rowHeight | height of a row in pixels (80 by default) |
gap | space between items in pixels (16 by default) |
editable | false to lock the layout (no handles) |
grab | where the mouse grabs a card: surface, hold or handle |
stackBelow | width in pixels below which items stack (480 by default) |
storageKey | remembers the user's layout from one visit to the next |
onCrLayoutChange gives the layout (id, x, y, w, h of each item) after each change, to save it on the server for example; the reset() method goes back to the initial layout.
Items
- Give each item an
id: it is what finds the item's place in the saved layout. - Without
xory, an item takes the first free slot: handy for adding an item from a list. minW,minH,maxWandmaxHconstrain the size;removableadds a × button that emitsonCrRemove(then remove the item from your list).- The
actionsslot places buttons in the header. - When the content overflows the card, it scrolls; the area then receives focus (Tab) and can be scrolled with the arrows, Page Down and Page Up.
Keyboard
On an item's handle (Tab):
| Key | Action |
|---|---|
| ← → ↑ ↓ | moves the item by one cell |
| Shift + ← → | one column less or more |
| Shift + ↑ ↓ | one row less or more |
Each change is announced: "Sales: column 3, row 1.", "Sales: 4 columns by 3 rows.".
Small screens
Below 480 pixels wide (stackBelow to change this threshold), items stack at full width, in grid order, and the handles disappear.
If the user has asked their system to reduce motion, items change position without sliding.
API
Properties
| Property | Type | Default | Description |
|---|---|---|---|
columns | number | 12 | Nombre de colonnes de la grille. |
editable | boolean | true | Permet de déplacer et de redimensionner les éléments. |
gap | number | 16 | Espace entre les éléments, en pixels. |
grab | "handle" | "hold" | "surface" | 'surface' | Où attraper une carte à la souris : n'importe où (`surface`), après un appui maintenu (`hold`, le texte reste sélectionnable) ou par la poignée seule (`handle`). |
label | string | — | Nom de la grille, lu par les lecteurs d’écran (« Grid », ou « Grille » en français, par défaut). |
rowHeight | number | 80 | Hauteur d'une ligne, en pixels. |
stackBelow | number | 480 | Largeur (px) sous laquelle les éléments s'empilent sur toute la largeur, sans poignées. |
storageKey | string | — | Clé localStorage : la disposition de l'utilisateur est retenue d'une visite à l'autre. |
Events
| React | event.detail | Description |
|---|---|---|
onCrLayoutChange | GridLayoutItem[] | Émis après chaque déplacement ou redimensionnement ; detail : id, x, y, w, h de chaque élément. |
Slots
| Slot | Description |
|---|---|
| default | Les éléments cr-grid-item. |
CSS parts
To style it from outside: cr-grid-layout::part(name) { … }
| Part | Description |
|---|
CrGridItem API
Properties
| Property | Type | Default | Description |
|---|---|---|---|
h | number | 2 | Hauteur en lignes. |
label (obligatoire) | string | — | Titre affiché dans l'en-tête. |
maxH | number | 12 | Hauteur maximale en lignes. |
maxW | number | 12 | Largeur maximale en colonnes. |
minH | number | 1 | Hauteur minimale en lignes. |
minW | number | 1 | Largeur minimale en colonnes. |
removable | boolean | false | Ajoute un bouton × qui émet crRemove. |
resizeHandle | "corner" | "edges" | "knob" | 'corner' | Poignée d'agrandissement : arc dans le coin (`corner`), pastille (`knob`) ou bords droit et bas en plus du coin (`edges`). |
variant | "card" | "elevated" | "glass" | "outline" | "soft" | 'card' | Apparence : carte bordée (`card`), ombrée (`elevated`), teintée (`soft`), simple contour (`outline`) ou verre dépoli (`glass`). |
w | number | 3 | Largeur en colonnes. |
x | number | — | Colonne de départ (à partir de 0) ; sans valeur, l’élément prend la première place libre. |
y | number | — | Ligne de départ (à partir de 0). |
Events
| React | event.detail | Description |
|---|---|---|
onCrGridItemUpdate | void | Interne : prévient le grille qu'une position ou une taille a changé. |
onCrRemove | void | Émis par le bouton × : retirez alors l’élément de votre liste. |
Slots
| Slot | Description |
|---|---|
| default | Le contenu. |
slot="actions" | Boutons placés dans l'en-tête. |
CSS parts
To style it from outside: cr-grid-item::part(name) { … }
| Part | Description |
|---|---|
handle | La poignée de déplacement. |
resize | Les poignées de redimensionnement. |