From 12e53e5d75a728dbddbb908aad70b6d9674f459e Mon Sep 17 00:00:00 2001 From: Petyo Ivanov Date: Mon, 16 Oct 2023 16:41:37 +0300 Subject: [PATCH] fix: repair the DND example Fixes #993 --- docs/scenarios/react-beautiful-dnd.md | 125 +++++++++--------- .../sandpack/src/theme/CodeBlock/index.jsx | 2 +- 2 files changed, 67 insertions(+), 60 deletions(-) diff --git a/docs/scenarios/react-beautiful-dnd.md b/docs/scenarios/react-beautiful-dnd.md index a7a38b27f..85fd660e1 100644 --- a/docs/scenarios/react-beautiful-dnd.md +++ b/docs/scenarios/react-beautiful-dnd.md @@ -5,70 +5,56 @@ sidebar_label: React Beautiful DND slug: /react-beautiful-dnd/ --- -The example below integrates React Virtuoso with [React Beautiful DND](https://github.com/atlassian/react-beautiful-dnd). +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). +The example works with Beautiful DND too, but causes warnings with StrictMode. ```jsx live import=react-beautiful-dnd -import React, { useCallback, useEffect, useState } from 'react' -import ReactDOM from 'react-dom' -import { Virtuoso } from 'react-virtuoso' -import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd' +import React, { useEffect, useState } from "react"; +import ReactDOM from "react-dom"; +import { Virtuoso } from "react-virtuoso"; +// Works with react-beautiful-dnd, too, but causes issues with StrictMode +import { DragDropContext, Droppable, Draggable } from "@hello-pangea/dnd"; // Virtuoso's resize observer can this error, // which is caught by DnD and aborts dragging. -window.addEventListener('error', (e) => { - if (e.message === 'ResizeObserver loop completed with undelivered notifications.' || e.message === 'ResizeObserver loop limit exceeded') { - e.stopImmediatePropagation() +window.addEventListener("error", (e) => { + if ( + e.message === + "ResizeObserver loop completed with undelivered notifications." || + e.message === "ResizeObserver loop limit exceeded" + ) { + e.stopImmediatePropagation(); } -}) +}); // Generate our initial big data set const initial = Array.from({ length: 1000 }, (_, k) => ({ id: `id:${k}`, - text: `item ${k}`, -})) + text: `item ${k}` +})); function reorder(list, startIndex, endIndex) { - const result = Array.from(list) - const [removed] = result.splice(startIndex, 1) - result.splice(endIndex, 0, removed) + const result = Array.from(list); + const [removed] = result.splice(startIndex, 1); + result.splice(endIndex, 0, removed); - return result + return result; } function Item({ provided, item, isDragging }) { return ( -
+
{item.text}
- ) -} - -const HeightPreservingItem = ({ children, ...props }) => { - const [size, setSize] = useState(0) - const knownSize = props['data-known-size'] - useEffect(() => { - setSize((prevSize) => { - return knownSize == 0 ? prevSize : knownSize - }) - }, [knownSize]) - return ( -
- {children} -
- ) + ); } export default function App() { @@ -89,46 +75,67 @@ export default function App() { [setItems] ) + const HeightPreservingItem = React.useCallback(({ children, ...props }) => { + const [size, setSize] = useState(0); + const knownSize = props["data-known-size"]; + useEffect(() => { + setSize((prevSize) => { + return knownSize == 0 ? prevSize : knownSize; + }); + }, [knownSize]); + return ( +
+ {children} +
+ ); + }, []); + return (
- - ( - - )} - > + {` + .height-preserving-container:empty { + min-height: calc(var(--child-height)); + box-sizing: border-box; + } + `} + + + ( + + )} + > {(provided) => { return ( { return ( - - {(provided) => } + + {(provided) => ( )} - ) + ); }} /> - ) + ); }}
- ) + ); } ``` diff --git a/docusaurus/sandpack/src/theme/CodeBlock/index.jsx b/docusaurus/sandpack/src/theme/CodeBlock/index.jsx index 1afc82c9a..7ea0e514f 100644 --- a/docusaurus/sandpack/src/theme/CodeBlock/index.jsx +++ b/docusaurus/sandpack/src/theme/CodeBlock/index.jsx @@ -169,7 +169,7 @@ const RenderSandpack = (props) => { : {}), ...(props['import'] === '@tanstack/react-table' ? { '@tanstack/react-table': 'latest' } : {}), ...(props['import'] === 'react-sortable-hoc' ? { 'react-sortable-hoc': '^1.11.0' } : {}), - ...(props['import'] === 'react-beautiful-dnd' ? { 'react-beautiful-dnd': 'latest' } : {}), + ...(props['import'] === 'react-beautiful-dnd' ? { '@hello-pangea/dnd': '16.3.0' } : {}), ...(props['import'] === '@emotion/styled' ? { '@emotion/styled': 'latest', '@emotion/react': 'latest' } : {}), }, }}