Docs/Examples
Dashboard
Widgets on a grid — moved by their headers, resized by their corners a cell at a time, in rows as tall as their content, with fewer columns on narrow screens.
Loading the demo…
<script setup lang="ts">
import { DnDProvider } from '@vue-dnd-kit/core';
import Dashboard from './Dashboard.vue';
</script>
<template>
<DnDProvider>
<Dashboard />
</DnDProvider>
</template>What to look at
Dashboard.vue: the grid is one sortable list withstrategy: 'grid', which sorts widgets of different sizes. It counts its columns from its own width (useElementSize): four, three or two. Its rows are at least 92 px tall and grow with their content, andgrid-auto-flow: densefills the holes a resize leaves.Widget.vue: a widget moves by its header (handle), so its content stays free: the tasks get ticked, the notes take text. Its corner is a draggable of its own, and resizes it.WidgetBody.vue: what the widgets show. Type into the notes: the row grows with the text, and the widgets beside it with the row. Nothing measures them: it is the grid's own layout, and the library reads the sizes as they are when a drag starts.widgets.ts: the widgets, their sizes in cells, and the grid's numbers.
Expand the demo: the grid gets four columns, and a widget made narrower on a small screen is as wide as it was again.
Resizing: the corner is a draggable
The corner lies inside the widget, and a press drags the closest draggable: the corner, not the widget. It has no overlay and never moves. The widget grows instead.
makeDraggable(corner, {
id: cornerId,
type: 'resize',
overlay: false,
activation: { touch: { distance: 4 } }, // a finger resizes at once, not after a long press
attributes: false, // no tab stop: Shift + arrows on the header resize
});
The size follows the pointer: how far it went since the press, in whole cells. The grid shows that size while the corner is held, and its other widgets make room.
const preview = computed<Size | null>(() => {
const start = from.value;
if (!resizing.value || !start || !pointer.value) return null;
return {
w: clamp(cellsIn(pixels(own.value.w, props.cell) + pointer.value.x - start.x, props.cell), 1, props.columns),
h: clamp(cellsIn(pixels(own.value.h, ROW) + pointer.value.y - start.y, ROW), 1, MAX_ROWS),
};
});
The drop keeps it: a monitor hears the corner's onDragEnd. Escape cancels the
drag instead, and the widget goes back to its own size.
useDnDMonitor({
onDragEnd: ({ active: dropped }) => {
if (dropped.id === cornerId() && preview.value) emit('resize', preview.value);
},
});
The grid sits the resize out
A corner isn't a widget, and the grid would refuse it: the drop on a target that refuses a drag is
a cancel. So while a corner is dragged, the grid is disabled, and the corner's drop is on no
target at all.
const { active } = useDnD();
const { items } = makeSortableList(grid, {
items: widgets,
strategy: 'grid',
label: 'Dashboard',
disabled: () => !!active.value?.type.includes('resize'),
});
Sizes that adapt
A widget keeps the size it was given. On fewer columns it is never wider than the grid
(Math.min(w, columns)), and it gets its width back when the grid has room again. The rows have a
minimum height, not a height: the content decides, and the widgets in a row all stretch to the
tallest.
The keyboard
Tab to a widget's header and press Space to pick the widget up. The arrows move it among the others, Space drops it, Escape puts it back. With the header focused, Shift and the arrows resize the widget a cell at a time, and screen readers hear its new size.
Read more: Strategies · Handles · Without an overlay · useDnDMonitor() · Week calendar