Skip to content

Commit d9fc9cc

Browse files
committed
fix(react-drag-drop): support dynamic root element IDs in DragDropContainer
Fixes #12217 - Replace hardcoded document.getElementById('root') with dynamic root element lookup - Add getRootElement() helper that tries common root IDs: 'root', 'app', 'main', '__next' - Fallback to document.body if no common root element is found - Enables usage in applications with non-standard root element IDs (e.g., id="app") This fix allows OCP 4.22 and other applications to use @patternfly/react-drag-drop regardless of their React root element configuration.
1 parent d589d3a commit d9fc9cc

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

packages/react-drag-drop/src/components/DragDrop/DragDropContainer.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,21 @@ export const DragDropContainer: React.FunctionComponent<DragDropContainerProps>
288288
};
289289

290290
const dragOverlay = <DragOverlay>{activeId && getDragOverlay()}</DragOverlay>;
291+
292+
// Find the React root element dynamically instead of hardcoding 'root'
293+
const getRootElement = () => {
294+
// Try common root element IDs
295+
const commonRootIds = ['root', 'app', 'main', '__next'];
296+
for (const id of commonRootIds) {
297+
const element = document.getElementById(id);
298+
if (element) {
299+
return element;
300+
}
301+
}
302+
// Fallback to document.body if no common root is found
303+
return document.body;
304+
};
305+
291306
return (
292307
<DndContext
293308
sensors={sensors}
@@ -299,7 +314,7 @@ export const DragDropContainer: React.FunctionComponent<DragDropContainerProps>
299314
{...props}
300315
>
301316
{children}
302-
{canUseDOM ? ReactDOM.createPortal(dragOverlay, document.getElementById('root')) : dragOverlay}
317+
{canUseDOM ? ReactDOM.createPortal(dragOverlay, getRootElement()) : dragOverlay}
303318
</DndContext>
304319
);
305320
};

0 commit comments

Comments
 (0)