technology
Jenzy

Jenzy

web - developer

GitHub reference https://github.com/react-grid-layout/react-grid-layout

React Grid layout supports responsive layout of components in the context. It only react without jQuery integrated.

Its feature:

Features

How to install it:

npm install react-grid-layout

How to implement with NextJs app using typescript:

// Installation

// initualize nextjs project
npx create-next-app@latest

// install react-grid-layout
npm i --save-dev @types/react-grid-layout

Initialize code (base setup) of using react-grid-layout:

    "use client";
    import { useState } from "react";
    import ReactGridLayout, { Layout } from "react-grid-layout";

    import "react-grid-layout/css/styles.css"; // used to follow the style of react-grid-layout library
    import "react-resizable/css/styles.css"; // used to do style when component is on resizing

    export default function Home() {
      const [layout, setLayout] = useState([
        { i: "a", x: 0, y: 0, w: 3, h: 2 },
        { i: "b", x: 1, y: 1, w: 3, h: 2 },
        { i: "c", x: 2, y: 2, w: 3, h: 2 },
      ]);

      const handleLayoutChange = (newLayout: Layout[]) => {
        setLayout(newLayout);
      };

      return (
        <main>
          <ReactGridLayout
            className="layout"
            layout={layout}
            cols={12}
            rowHeight={30}
            width={1400}
            autoSize
            isResizable
            transformScale={1}
            useCSSTransforms
            onLayoutChange={handleLayoutChange}
          >
            <div key="a">a</div>
            <div key="b">b</div>
            <div key="c">c</div>
          </ReactGridLayout>
        </main>
      );
    }

GridLayout was replaced by the tag in the new version of React-grid-layout, which uses TypeScript. This will use in static width of container and customize layout with CSS.

Note: All component’s width can be static of draggable, by just add a attribute more with layout.

{ i: "a", x: 0, y: 0, w: 3, h: 2, static:true },

If you want to resize all the angles we must be add one more attribute inside of ReactGridLayout tag:

 resizeHandles={['se', 'sw', 'ne', 'nw']}

Note: Since the i variable in the layout is tied to the component key, it will still display by following the layout index even if the i variable’s index is not where the ordered components are.

Related Posts

Charts - Rechart

placeholder for testing imagme
technology

The chart library in Shadcn used Rechart under development style.

Disable CROS in Google Chrome

Disable cross origin in Google Chrome
technology

Disable Cross origin in Google Chrome for web development.

EJ2 editor for Text rich editor.

placeholder for testing imagme
technology

EJ2 (Essential JS 2) Editor from Syncfusion. The EJ2 Editor is part...