How to Build Responsive Layouts using Angular, React, or Vue
Creating Interactive Dashboards in Minutes
Although creating a nice interactive dashboard sounds like a common task for frontend developers sometimes it could be really tricky. Building functionalities like resizing, dragging, and rearranging layout elements can quickly become a complex endeavor. This article explores how to simplify this process using an open-source library that lets you create stunning draggable, resizable, and responsive layouts with minimal code.
First of all, let’s take a quick look at the desired result and behavior:

The Power of Gridstack.js
This magic we can achieve this using an open-source project — gridstack.js (5.8K stars on GitHub).
gridstack.js is a modern Typescript library designed to help developers create beautiful draggable, resizable, responsive layouts with just a few lines of code.
You can create more advanced grids: allow users to persist changes, create a sidebar of widgets to drag into your grid, nested grids, and much more.
Here is what this library provides:
- pure TS can be used in any UI framework
- mobile support
- draggable, resizable items
- drag-drop from the sidebar to insert/delete
- responsive layouts, column-based
- save/restore
- drag between multiple grids
- nested grids, including creation on the fly to any depth
- Angular (included), demo and external Vue, React, Ember, Knockout.js bindings, and more
Getting Started with Gridstack.js in Angular
I would like to show how to use gridstack.js in an Angular application. First of all, you need to install gridstack.js from a package manager:
yarn add gridstack
// or
npm install --save gridstackImport gridstack styles into your application:
@import "gridstack/dist/gridstack.min.css";Add the next code to your app.component.html:
<gridstack [options]="gridOptions" (changeCB)="onChange($event)">
@for (gridItem of items; track gridItem.id){
<gridstack-item [options]="gridItem">
<div class="item-content">
Item {{gridItem.id}}
</div>
</gridstack-item>
}
</gridstack>If you want you can add some styles to your app.component.scss:
gridstack {
height: 500px;
background: #FAFAD2;
margin: 10px;
padding: 40px;
}
.item-content {
text-align: center;
background-color: #c9f199;
border: 1px solid #000;
border-radius: 5px;
height: 100%;
}And finally, add typescript logic in your app.component.ts:
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import {GridStackOptions, GridStackWidget} from "gridstack";
import {GridstackModule, nodesCB} from "gridstack/dist/angular";
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, GridstackModule],
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
export class AppComponent {
public gridOptions: GridStackOptions = { margin: 5 }
public items: GridStackWidget[] = [
{x:0, y:0, w:4, id:'1'},
{x:1, y:0, w:3, id:'2'},
{x:0, y:1, h:4, w:4, id:'3'},
{x:3, y:1, w:3, id:'4'},
{x:0, y:2, w:2, id:'5'},
];
public onChange(data: nodesCB) {
console.log('change ', data.nodes.length > 1 ? data.nodes : data.nodes[0]);
}
}Let’s take a closer look at GridStackOptions. It defines options for the parent grid and provides a huge amount of customization. For example, you can set cellHeight option that defines a minimum step for scaling your grid item.
GridStackWidget is an interface for gridstack children. You can set minimal and maximum weight, height, moving options, and others. For instance, you can define the grid item position (x, y, w, h). You should be aware that the X position has 12 cells by default.
You can also track changes with the onChange method that provides information about changed grids:
public onChange(data: nodesCB) {
console.log('change ', data.nodes.length > 1 ? data.nodes : data.nodes[0]);
}Conclusion
Gridstack.js empowers developers to create dynamic and user-friendly dashboards with minimal effort. Its versatility and cross-framework compatibility make it a valuable tool for building modern and responsive web applications. By utilizing gridstack.js, you can significantly reduce development time and deliver exceptional user experiences.
Useful sources
- Gridstack demos for React, Vue, and others
- Gridstack API documentation
- Angular wrapper documentation
- The application code source of this article is on my GitHub
- The demo of my application
Thank you for reading. If you have any questions you can find me on LinkedIn. Feel free to leave comments. You can also read my previous articles: