Skip to content

Simplify react lib design #2938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add the GridStackContainer and update docs
  • Loading branch information
Aysnine committed Mar 2, 2025
commit 94a994f32c4063238c9623e4ef85ea0bf0871d69
45 changes: 43 additions & 2 deletions react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ Open in [CodeSandbox](https://codesandbox.io/p/sandbox/github/gridstack/gridstac
- [x] Custom handle
- [x] Drag between two grid stacks

Welcome to give any suggestions and ideas, you can submit an issue or contact me by email. :)

## Usage

This is not an npm package, it's just a demo project. Please copy the `src/lib` code to your project to use it.
Expand All @@ -24,6 +22,37 @@ This is not an npm package, it's just a demo project. Please copy the `src/lib`

Render item with widget id selector.

Code here: [src/examples/000-simple/index.tsx](src/examples/000-simple/index.tsx)

```tsx
function Simple() {
const [uncontrolledInitialOptions] = useState<GridStackOptions>(() => ({
...defaultGridOptions,
children: [
{ id: "item1", h: 2, w: 2, x: 0, y: 0 },
{ id: "item2", h: 2, w: 2, x: 2, y: 0 },
],
}));

return (
<GridStackContainer
initialOptions={uncontrolledInitialOptions}
renderRawContent
>
<GridStackItem id="item1">
<div style={{ color: "yellow" }}>hello</div>
</GridStackItem>

<GridStackItem id="item2">
<div style={{ color: "blue" }}>grid</div>
</GridStackItem>
</GridStackContainer>
);
}
```

Or split the grid stack container to provide grid stack context and render component for access to grid stack context.

Code here: [src/examples/001-simple/index.tsx](src/examples/001-simple/index.tsx)

```tsx
Expand All @@ -38,6 +67,7 @@ function Simple() {

return (
<GridStackProvider initialOptions={uncontrolledInitialOptions}>
{/* Custom toolbar component. Access to grid stack context by useGridStackContext hook. */}
<Toolbar />

<GridStackRender renderRawContent>
Expand Down Expand Up @@ -210,6 +240,17 @@ function CustomHandle() {

### Components

#### GridStackContainer

Top-level component that provides GridStack context and GridStack root container. Equivalent to `GridStackProvider` and `GridStackRender` combined.

```typescript
type GridStackContainerProps = {
initialOptions: GridStackOptions; // GridStack initialization options
children: React.ReactNode;
};
```

#### GridStackProvider

Top-level component that provides GridStack context.
Expand Down
6 changes: 5 additions & 1 deletion react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "./demo.css";
import { Simple0 } from "./examples/000-simple";
import { Simple } from "./examples/001-simple";
import { Nested } from "./examples/002-nested";
import { CustomHandle } from "./examples/003-custom-handle";
Expand All @@ -7,8 +8,11 @@ import { Advanced } from "./examples/009-advanced";
export default function App() {
return (
<div>
<h2 id="simple">Simple</h2>
<h2 id="simple0">Simple</h2>
<p>Render content by GridStackItem with id selector.</p>
<Simple0 />
<h2 id="simple">Simple With Toolbar</h2>
<p>With toolbar</p>
<Simple />
<h2 id="nested">Nested</h2>
<p>Only use gridstack.js native subGridOpts.</p>
Expand Down
30 changes: 30 additions & 0 deletions react/src/examples/000-simple/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { GridStackOptions } from "gridstack";
import { useState } from "react";
import { defaultGridOptions } from "../../default-grid-options";
import { GridStackItem } from "../../lib";
import { GridStackContainer } from "../../lib/grid-stack-container";

export function Simple0() {
const [uncontrolledInitialOptions] = useState<GridStackOptions>(() => ({
...defaultGridOptions,
children: [
{ id: "item1", h: 2, w: 2, x: 0, y: 0 },
{ id: "item2", h: 2, w: 2, x: 2, y: 0 },
],
}));

return (
<GridStackContainer
initialOptions={uncontrolledInitialOptions}
renderRawContent
>
<GridStackItem id="item1">
<div style={{ color: "yellow" }}>hello</div>
</GridStackItem>

<GridStackItem id="item2">
<div style={{ color: "blue" }}>grid</div>
</GridStackItem>
</GridStackContainer>
);
}
23 changes: 23 additions & 0 deletions react/src/lib/grid-stack-container.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GridStackOptions } from "gridstack";
import { PropsWithChildren } from "react";
import { GridStackProvider } from "./grid-stack-provider";
import { GridStackRender } from "./grid-stack-render";

export type GridStackContainerProps = PropsWithChildren<{
initialOptions: GridStackOptions;
renderRawContent?: boolean;
}>;

export function GridStackContainer({
children,
initialOptions,
renderRawContent,
}: GridStackContainerProps) {
return (
<GridStackProvider initialOptions={initialOptions}>
<GridStackRender renderRawContent={renderRawContent}>
{children}
</GridStackRender>
</GridStackProvider>
);
}
3 changes: 3 additions & 0 deletions react/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export type { GridStackContainerProps } from "./grid-stack-container";
export { GridStackContainer } from "./grid-stack-container";

export type { GridStackContextType } from "./grid-stack-context";
export { GridStackContext, useGridStackContext } from "./grid-stack-context";

Expand Down