Skip to content

Instantly share code, notes, and snippets.

@Arkellys
Last active May 4, 2024 19:40
Show Gist options
  • Save Arkellys/f364dac120d75fd544b0e3d783e6e51c to your computer and use it in GitHub Desktop.
Save Arkellys/f364dac120d75fd544b0e3d783e6e51c to your computer and use it in GitHub Desktop.
Electron react-grid-layout fiddle
node_modules
out
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<link href="./styles.css" rel="stylesheet">
<title>Hello World!</title>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<link rel="stylesheet" href="./node_modules/react-grid-layout/css/styles.css">
<link rel="stylesheet" href="./node_modules/react-resizable/css/styles.css">
</head>
<body>
<div id="root"></div>
<!-- You can also require other files to run in this process -->
<script type="text/jsx" src="renderer.js">
</body>
</html>
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('node:path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
sandbox: false
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
{
"name": "react-grid-layout Fiddle",
"productName": "react-grid-layout Fiddle",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "Arkellys",
"scripts": {
"start": "electron ."
},
"dependencies": {
"react-grid-layout": "1.4.4",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"electron": "28.2.0"
}
}
import * as React from "react";
import { createRoot } from 'react-dom/client';
import { Responsive, WidthProvider } from "react-grid-layout";
const layout = [
{"w": 4, "h": 4, "x": 0, "y": 0, "i": "ZEHSHAR", "moved": false, "static": false },
{ "w": 4, "h": 4, "x": 7, "y": 0, "i": "LCVGWGC", "moved": false, "static": false },
{ "w": 3, "h": 4, "x": 4, "y": 0, "i": "WZU8TGR", "moved": false, "static": false },
{ "w": 7, "h": 4, "x": 2, "y": 4, "i": "7AFEP6P", "moved": false, "static": false }
];
const Block = React.memo(({ id }) => (
<div className="block">
<span className="dragHandle"></span>
<h3>{id}</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque viverra
vulputate sem, ac lobortis arcu malesuada vitae. Nunc lacus magna,
ultricies nec pulvinar nec, pulvinar condimentum nulla. Ut tincidunt nec
massa et ullamcorper.
</p>
</div>
));
const TestGrid = () => {
const ResponsiveGridLayout = React.useMemo(() => WidthProvider(Responsive), []);
const children = React.useMemo(() => (
layout.map(({ i }) => (
<div key={i}>
<Block id={i} />
</div>
))
), []);
return (
<div>
<ResponsiveGridLayout
layouts={{ xs: layout }}
breakpoints={{ xs: 640, xxs: 0 }}
cols={{ xs: 12, xxs: 1 }}
rowHeight={50}
draggableHandle=".dragHandle"
isResizable={false}
>
{children}
</ResponsiveGridLayout>
</div>
);
}
const root = createRoot(document.getElementById('root'));
root.render(
<TestGrid />
);
.block {
background-color: #cccccc;
position: relative;
padding: 10px;
display: flex;
flex-direction: column;
align-items: center;
height: calc(100% - 20px);
width: calc(100% - 20px);
border: 1px solid black;
}
.dragHandle {
width: 15px;
height: 15px;
background: red;
position: absolute;
left: 10px;
top: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment