Skip to content

Commit 12e53e5

Browse files
committed
fix: repair the DND example
Fixes #993
1 parent a2384c6 commit 12e53e5

File tree

2 files changed

+67
-60
lines changed

2 files changed

+67
-60
lines changed

docs/scenarios/react-beautiful-dnd.md

+66-59
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,56 @@ sidebar_label: React Beautiful DND
55
slug: /react-beautiful-dnd/
66
---
77

8-
The example below integrates React Virtuoso with [React Beautiful DND](https://github.com/atlassian/react-beautiful-dnd).
8+
The example below integrates React Virtuoso with the maintained fork of [React Beautiful DND](https://github.com/atlassian/react-beautiful-dnd), [hello-pangea/dnd](https://github.com/hello-pangea/dnd).
9+
The example works with Beautiful DND too, but causes warnings with StrictMode.
910

1011
```jsx live import=react-beautiful-dnd
11-
import React, { useCallback, useEffect, useState } from 'react'
12-
import ReactDOM from 'react-dom'
13-
import { Virtuoso } from 'react-virtuoso'
14-
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd'
12+
import React, { useEffect, useState } from "react";
13+
import ReactDOM from "react-dom";
14+
import { Virtuoso } from "react-virtuoso";
15+
// Works with react-beautiful-dnd, too, but causes issues with StrictMode
16+
import { DragDropContext, Droppable, Draggable } from "@hello-pangea/dnd";
1517

1618
// Virtuoso's resize observer can this error,
1719
// which is caught by DnD and aborts dragging.
18-
window.addEventListener('error', (e) => {
19-
if (e.message === 'ResizeObserver loop completed with undelivered notifications.' || e.message === 'ResizeObserver loop limit exceeded') {
20-
e.stopImmediatePropagation()
20+
window.addEventListener("error", (e) => {
21+
if (
22+
e.message ===
23+
"ResizeObserver loop completed with undelivered notifications." ||
24+
e.message === "ResizeObserver loop limit exceeded"
25+
) {
26+
e.stopImmediatePropagation();
2127
}
22-
})
28+
});
2329

2430
// Generate our initial big data set
2531
const initial = Array.from({ length: 1000 }, (_, k) => ({
2632
id: `id:${k}`,
27-
text: `item ${k}`,
28-
}))
33+
text: `item ${k}`
34+
}));
2935

3036
function reorder(list, startIndex, endIndex) {
31-
const result = Array.from(list)
32-
const [removed] = result.splice(startIndex, 1)
33-
result.splice(endIndex, 0, removed)
37+
const result = Array.from(list);
38+
const [removed] = result.splice(startIndex, 1);
39+
result.splice(endIndex, 0, removed);
3440

35-
return result
41+
return result;
3642
}
3743

3844
function Item({ provided, item, isDragging }) {
3945
return (
40-
<div style={{ paddingBottom: '8px' }}>
46+
<div style={{ paddingBottom: "8px" }}>
4147
<div
4248
{...provided.draggableProps}
4349
{...provided.dragHandleProps}
4450
ref={provided.innerRef}
4551
style={provided.draggableProps.style}
46-
className={`item ${isDragging ? 'is-dragging' : ''}`}
52+
className={`item ${isDragging ? "is-dragging" : ""}`}
4753
>
4854
{item.text}
4955
</div>
5056
</div>
51-
)
52-
}
53-
54-
const HeightPreservingItem = ({ children, ...props }) => {
55-
const [size, setSize] = useState(0)
56-
const knownSize = props['data-known-size']
57-
useEffect(() => {
58-
setSize((prevSize) => {
59-
return knownSize == 0 ? prevSize : knownSize
60-
})
61-
}, [knownSize])
62-
return (
63-
<div
64-
{...props}
65-
className="height-preserving-container"
66-
// check styling in the style tag below
67-
style={{ '--child-height': `${size}px`, }}
68-
>
69-
{children}
70-
</div>
71-
)
57+
);
7258
}
7359

7460
export default function App() {
@@ -89,46 +75,67 @@ export default function App() {
8975
[setItems]
9076
)
9177

78+
const HeightPreservingItem = React.useCallback(({ children, ...props }) => {
79+
const [size, setSize] = useState(0);
80+
const knownSize = props["data-known-size"];
81+
useEffect(() => {
82+
setSize((prevSize) => {
83+
return knownSize == 0 ? prevSize : knownSize;
84+
});
85+
}, [knownSize]);
86+
return (
87+
<div
88+
{...props}
89+
className="height-preserving-container"
90+
style={{
91+
"--child-height": `${size}px`
92+
}}
93+
>
94+
{children}
95+
</div>
96+
);
97+
}, []);
98+
9299
return (
93100
<div style={{ padding: '1rem' }}>
94101
<style>
95-
{`
96-
.height-preserving-container:empty {
97-
min-height: calc(var(--child-height));
98-
box-sizing: border-box;
99-
}
100-
`}
101-
</style>
102-
<DragDropContext onDragEnd={onDragEnd}>
103-
<Droppable
104-
droppableId="droppable"
105-
mode="virtual"
106-
renderClone={(provided, snapshot, rubric) => (
107-
<Item provided={provided} isDragging={snapshot.isDragging} item={items[rubric.source.index]} />
108-
)}
109-
>
102+
{`
103+
.height-preserving-container:empty {
104+
min-height: calc(var(--child-height));
105+
box-sizing: border-box;
106+
}
107+
`}
108+
</style>
109+
<DragDropContext onDragEnd={onDragEnd}>
110+
<Droppable
111+
droppableId="droppable"
112+
mode="virtual"
113+
renderClone={(provided, snapshot, rubric) => (
114+
<Item provided={provided} isDragging={snapshot.isDragging} item={items[rubric.source.index]} />
115+
)}
116+
>
110117
{(provided) => {
111118
return (
112119
<Virtuoso
113120
components={{
114-
Item: HeightPreservingItem,
121+
Item: HeightPreservingItem
115122
}}
116123
scrollerRef={provided.innerRef}
117124
data={items}
118125
style={{ width: 300, height: 500 }}
119126
itemContent={(index, item) => {
120127
return (
121-
<Draggable draggableId={item.id} index={index} key={item.id}>
122-
{(provided) => <Item provided={provided} item={item} isDragging={false} />}
128+
<Draggable draggableId={item.id} index={index} key={item.id} >
129+
{(provided) => ( <Item provided={provided} item={item} isDragging={false} />)}
123130
</Draggable>
124-
)
131+
);
125132
}}
126133
/>
127-
)
134+
);
128135
}}
129136
</Droppable>
130137
</DragDropContext>
131138
</div>
132-
)
139+
);
133140
}
134141
```

docusaurus/sandpack/src/theme/CodeBlock/index.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ const RenderSandpack = (props) => {
169169
: {}),
170170
...(props['import'] === '@tanstack/react-table' ? { '@tanstack/react-table': 'latest' } : {}),
171171
...(props['import'] === 'react-sortable-hoc' ? { 'react-sortable-hoc': '^1.11.0' } : {}),
172-
...(props['import'] === 'react-beautiful-dnd' ? { 'react-beautiful-dnd': 'latest' } : {}),
172+
...(props['import'] === 'react-beautiful-dnd' ? { '@hello-pangea/dnd': '16.3.0' } : {}),
173173
...(props['import'] === '@emotion/styled' ? { '@emotion/styled': 'latest', '@emotion/react': 'latest' } : {}),
174174
},
175175
}}

0 commit comments

Comments
 (0)