
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
@declarative-gridstack/react
Advanced tools
gridstack.js as React components. Use gridstack.js in a declarative way to build complex applications with ease.
gridstack.js as React components. Use gridstack.js in a declarative way to build complex applications with ease.
If you have a React application scaffolded with CRA (create-react-app), it is recommended to remove the <React.StrictMode> component from the index.js file.
<React.StrictMode>
<App />
</React.StrictMode>
with
<App />
Gridstack containers can hold Gridstack items. These items can be resized and repositioned either using @declarative-gridstack/react's API or manually using mouse dragging.
const [layout, setLayout] = useState([
{
id: "3",
x: 1,
y: 0,
w: 9,
h: 1,
data: {
type: "weather",
title: "A weather widget",
data: "Chennai, Tamil Nadu, India",
},
},
{
id: "4",
x: 1,
y: 1,
w: 8,
h: 1,
data: {
type: "map",
title: "A map widget",
data: "Chennai, Tamil Nadu, India",
},
},
]);
The array mentioned above is a model for a master grid that contains two Gridstack items.
A Gridstack item is an object that must contain these properties.
These properties are necessary for an item to be correctly positioned and sized within a Gridstack container.
Arbitary properties,
The master Gridstack container and the Gridstack subgrids both have the capability to hold Gridstack items.
const [widget, setWidget] = useState({
widget: {
id: 3,
x: 6,
y: 0,
w: 6,
h: 3,
data: {
title: "I am a Grid item",
ability: "I can be moved around and resized.",
},
},
});
The object given above is a model for a Gridstack item.
Each subgrid is itself a Gridstack item that has a Gridstack container within it. This container can hold other Gridstack items, allowing for more complex and nested layouts.
const [subgridLayout, setSubgridLayout] = useState([
{
id: 2,
x: 3,
y: 4,
w: 6,
h: 4,
children: [
{
id: 3,
x: 6,
y: 0,
w: 6,
h: 3,
data: {
message: "My ID is 3",
},
},
{
id: 4,
x: 0,
y: 0,
w: 6,
h: 3,
data: {
ability: "I can be moved around!",
},
},
],
},
]);
Based on the model provided above, the layout can be described as having a master container that includes a Gridstack item that serves as a subgrid. This subgrid, in turn, contains two additional Gridstack items.
When an Gridstack item present in a Gridstack container is repositioned, resized or removed, etc. the model is automatically updated.
import {
GridstackItem,
GridstackContainer,
GridstackSubgrid,
} from "@declarative-gridstack/react";
const Widget = (props) => {
const { data } = props;
return (
<div>
<h1>{data.title}</h1>
<div>{data.ability}</div>
</div>
);
};
const Page = () => {
const [layout, setLayout] = useState([
{
id: 3,
x: 6,
y: 0,
w: 6,
h: 3,
data: {
title: "I am a Grid item",
ability: "I can be moved around and resized.",
},
},
]);
const [widget] = layout ?? [];
return (
<GridstackContainer items={layout} setLayout={setLayout}>
<GridstackItem
id={widget.id}
x={widget.x}
y={widget.y}
w={widget.w}
h={widget.h}
>
<Widget data={widget.data}></Widget>
</GridstackItem>
</GridstackContainer>
);
};
The code snippet shows a simple Gridstack container containing a single item. If the user resizes the item by dragging its resize handle, the library automatically updates the item's size information in the model.
A live example would help to clarify this concept more effectively.
The model of a specifc Gridstack layout could then be saved to an HTTP API.
const [layout, setLayout] = useState([
{
id: 3,
x: 6,
y: 0,
w: 6,
h: 3,
data: {
title: "I am a Grid item",
ability: "I can be moved around and resized.",
},
},
]);
const saveLayout = () => {
axios.post("/save-layout", layout).then(() => {});
};
The saved model could then be retrieved from an HTTP API... To construct the back layout again.
useEffect(() => {
const [layout, setLayout] = useState([]);
axios.get("/get-layout").then(({ data }) => {
setLayout(data);
});
});
DND (drag and drop) items are html elements (gridstack items that resides out of a gridstack grid) that could be dragged and dropped into a gridstack grid.
DND items are normal html elements that have the class 'grid-stack-item' in the outer tag and 'grid-stack-item-content' in the inner tag. To ensure proper identification of the items in the DOM, each outer element should also have a unique class ('gs-dnd-item' in the example below) and a 'gs-dnd-item-id' attribute with a unique value. The example below shows two unique DND items:
<div class="gs-dnd-item grid-stack-item" gs-dnd-item-id="-1">
<div class="grid-stack-item-content" style="padding: 5px">
I am vue gridstack DND item! With ID - 2
</div>
</div>
<div class="gs-dnd-item grid-stack-item" gs-dnd-item-id="-2">
<div class="grid-stack-item-content" style="padding: 5px">
I am vue gridstack DND item! With ID - 2
</div>
</div>
Now that we have a couple of Gridstack DND items in the DOM, we need to register them with the Gridstack grid so that they can be accepted when a user drags and drops the DND item into the grid. To do that, follow these steps:
const WidgetContainer = (props) => {
const { children } = props;
return (
<div>
<div>*** Widget Container ***</div>
<div>{children}</div>
</div>
);
};
const Widget = (props) => {
const { data } = props;
return (
<div>
<h1>{data.title}</h1>
<div>{data.ability}</div>
</div>
);
};
const Page = () => {
const uid = useRef(-1);
const uidGenerator = () => {
return uid.current--; // Returns a unique number when called.
};
const [layout, setLayout] = useState([
{
id: 3,
x: 6,
y: 0,
w: 6,
h: 3,
data: {
title: "I am a Grid item",
ability: "I can be moved around and resized.",
},
},
]);
return (
<GridstackContainer
items={layout}
setLayout={setLayout}
// Register dnd items to the grid container.
dnd={{
class: "gs-dnd-item",
dndItems: [
{ id: "-1", data: "I am vue gridstack DND item! With ID -1" },
{ id: "-2", data: "I am vue gridstack DND item! With ID -2" },
],
uidGenerator: uidGenerator,
}}
>
{layout.map((widget) => {
return (
<GridstackItem
id={widget.id}
x={widget.x}
y={widget.y}
w={widget.w}
h={widget.h}
>
<WidgetContainer>
<Widget data={widget}></Widget>
</WidgetContainer>
</GridstackItem>
);
})}
</GridstackContainer>
);
};
Live example of DND items. Click here to view code.
FAQs
gridstack.js as React components. Use gridstack.js in a declarative way to build complex applications with ease.
The npm package @declarative-gridstack/react receives a total of 91 weekly downloads. As such, @declarative-gridstack/react popularity was classified as not popular.
We found that @declarative-gridstack/react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.