Columns, Arrange, and Reset
comins-grid-layout@0.3.0
DashboardGrid and useDashboardGrid support integer column counts from 1 through 12. setColumns clamps other numbers into that range and restores the cached layout when that column count has already been visited.
export function LayoutToolbar() { const dashboard = useDashboardGrid({ initialColumns: 12, initialWidgets });
return ( <> <button type="button" onClick={() => dashboard.commands.setColumns(6)}>6 columns</button> <button type="button" onClick={() => dashboard.commands.autoArrangeWidgets()}>Arrange</button> <button type="button" onClick={() => dashboard.commands.fitWidgetsToColumns()}>Fit all</button> <button type="button" onClick={() => dashboard.commands.fitWidgetToColumns("sales")}>Fit sales</button> <button type="button" onClick={() => dashboard.commands.resetLayout()}>Reset</button> <DashboardGrid className="analytics-grid" columns={dashboard.columns} widgets={dashboard.widgets} onColumnsChange={dashboard.commands.setColumns} onLayoutCommit={dashboard.commands.applyLayoutSnapshot} renderWidget={(widget) => widget.title} /> </> );}autoArrangeWidgetsplaces widgets from left to right in array order, wrapping to a new row when needed. It does not optimally fill every gap.fitWidgetsToColumnsgroups widgets by their startingyand redistributes widths evenly when the group has horizontal space to fill, respecting column bounds and widget size constraints.fitWidgetToColumnsadds the available horizontal space in the selected widget’s row to that widget’s width instead of distributing it among the group.resetLayout()restores the hook’s initial snapshot. Passing a layout or full state snapshot resets to that value instead.onColumnsChangeis required whenresponsiveowns the active column count. Without responsive options, the controlledcolumnsprop is authoritative.classNameadds a consumer class beside.grid-stackand.comins-grid-layout.
Each visited column count owns its own layout cache. CRUD identity changes propagate across existing caches; active layout-only operations change the current cache entry. See Persistence before storing or migrating state.
Playground: /examples/layout/arrange.