Skip to content

Commit 1840713

Browse files
committed
✨ Basic drag and drop
1 parent b11bffb commit 1840713

File tree

7 files changed

+263
-11
lines changed

7 files changed

+263
-11
lines changed

next.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const withBundleAnalyzer = require('@next/bundle-analyzer')({
55
});
66

77
module.exports = withBundleAnalyzer({
8-
reactStrictMode: true,
8+
reactStrictMode: false,
99
eslint: {
1010
ignoreDuringBuilds: true,
1111
},

package.json

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "homarr",
3-
"version": "0.4.0",
4-
"private": "false",
5-
"description": "Homarr - A homepage for your server.",
6-
"repository": {
7-
"type": "git",
8-
"url": "https://github.com/ajnart/homarr"
9-
},
2+
"name": "homarr",
3+
"version": "0.4.0",
4+
"private": "false",
5+
"description": "Homarr - A homepage for your server.",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/ajnart/homarr"
9+
},
1010
"scripts": {
1111
"dev": "next dev",
1212
"build": "next build",
@@ -45,6 +45,7 @@
4545
"next": "12.1.5-canary.4",
4646
"prism-react-renderer": "^1.3.1",
4747
"react": "18.0.0",
48+
"react-beautiful-dnd": "^13.1.0",
4849
"react-dom": "18.0.0",
4950
"tabler-icons-react": "^1.46.0"
5051
},
@@ -62,6 +63,7 @@
6263
"@types/jest": "^27.4.1",
6364
"@types/node": "^17.0.23",
6465
"@types/react": "17.0.43",
66+
"@types/react-beautiful-dnd": "^13.1.2",
6567
"@typescript-eslint/eslint-plugin": "^5.16.0",
6668
"@typescript-eslint/parser": "^5.16.0",
6769
"babel-loader": "^8.2.4",

src/components/dnd.tsx

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import React from 'react';
2+
import { createStyles, Text } from '@mantine/core';
3+
import { useListState } from '@mantine/hooks';
4+
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
5+
import AppShelf from './AppShelf/AppShelf';
6+
7+
const useStyles = createStyles((theme) => ({
8+
item: {
9+
...theme.fn.focusStyles(),
10+
display: 'flex',
11+
alignItems: 'center',
12+
borderRadius: theme.radius.md,
13+
border: `1px solid ${
14+
theme.colorScheme === 'dark' ? theme.colors.dark[5] : theme.colors.gray[2]
15+
}`,
16+
padding: `${theme.spacing.sm}px ${theme.spacing.xl}px`,
17+
backgroundColor: theme.colorScheme === 'dark' ? theme.colors.dark[5] : theme.white,
18+
marginBottom: theme.spacing.sm,
19+
},
20+
21+
itemDragging: {
22+
boxShadow: theme.shadows.sm,
23+
},
24+
25+
symbol: {
26+
fontSize: 30,
27+
fontWeight: 700,
28+
width: 60,
29+
},
30+
}));
31+
32+
interface DndListProps {
33+
data: {
34+
position: number;
35+
mass: number;
36+
symbol: string;
37+
name: string;
38+
}[];
39+
}
40+
41+
export function DndList({ data }: DndListProps) {
42+
const { classes, cx } = useStyles();
43+
const [state, handlers] = useListState(data);
44+
45+
const items = state.map((item, index) => (
46+
<Draggable key={item.symbol} index={index} draggableId={item.symbol}>
47+
{(provided, snapshot) => (
48+
<div
49+
className={cx(classes.item, { [classes.itemDragging]: snapshot.isDragging })}
50+
{...provided.draggableProps}
51+
{...provided.dragHandleProps}
52+
ref={provided.innerRef}
53+
>
54+
<Text className={classes.symbol}>{item.symbol}</Text>
55+
<div>
56+
<Text>{item.name}</Text>
57+
<Text color="dimmed" size="sm">
58+
Position: {item.position} • Mass: {item.mass}
59+
</Text>
60+
</div>
61+
</div>
62+
)}
63+
</Draggable>
64+
));
65+
66+
return (
67+
<DragDropContext
68+
onDragEnd={({ destination, source }) =>
69+
handlers.reorder({ from: source.index, to: destination.index })
70+
}
71+
>
72+
<Droppable droppableId="dnd-list" direction="vertical">
73+
{(provided) => (
74+
<div {...provided.droppableProps} ref={provided.innerRef}>
75+
{items}
76+
{provided.placeholder}
77+
</div>
78+
)}
79+
</Droppable>
80+
</DragDropContext>
81+
);
82+
}

src/pages/_document.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Document, { DocumentContext } from 'next/document';
22
import { ServerStyles, createStylesServer } from '@mantine/next';
3+
import { resetServerContext } from 'react-beautiful-dnd';
34

45
const stylesServer = createStylesServer();
56

67
export default class _Document extends Document {
78
static async getInitialProps(ctx: DocumentContext) {
8-
const initialProps = await Document.getInitialProps(ctx);
9+
resetServerContext();
910

11+
const initialProps = await Document.getInitialProps(ctx);
1012
// Add your app specific logic here
1113

1214
return {

src/pages/data.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"data": [
3+
{
4+
"position": 6,
5+
"mass": 12.011,
6+
"symbol": "C",
7+
"name": "Carbon"
8+
},
9+
{
10+
"position": 7,
11+
"mass": 14.007,
12+
"symbol": "N",
13+
"name": "Nitrogen"
14+
},
15+
{
16+
"position": 39,
17+
"mass": 88.906,
18+
"symbol": "Y",
19+
"name": "Yttrium"
20+
},
21+
{
22+
"position": 56,
23+
"mass": 137.33,
24+
"symbol": "Ba",
25+
"name": "Barium"
26+
},
27+
{
28+
"position": 58,
29+
"mass": 140.12,
30+
"symbol": "Ce",
31+
"name": "Cerium"
32+
}
33+
]
34+
}

src/pages/trydnd.tsx

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useEffect } from 'react';
2+
import { DndList } from '../components/dnd';
3+
4+
const data = {
5+
data: [
6+
{
7+
position: 6,
8+
mass: 12.011,
9+
symbol: 'C',
10+
name: 'Carbon',
11+
},
12+
{
13+
position: 7,
14+
mass: 14.007,
15+
symbol: 'N',
16+
name: 'Nitrogen',
17+
},
18+
{
19+
position: 39,
20+
mass: 88.906,
21+
symbol: 'Y',
22+
name: 'Yttrium',
23+
},
24+
{
25+
position: 56,
26+
mass: 137.33,
27+
symbol: 'Ba',
28+
name: 'Barium',
29+
},
30+
{
31+
position: 58,
32+
mass: 140.12,
33+
symbol: 'Ce',
34+
name: 'Cerium',
35+
},
36+
],
37+
};
38+
39+
export default function TryDnd() {
40+
return <DndList data={data.data} />;
41+
}

0 commit comments

Comments
 (0)