Skip to content

Commit 43e798d

Browse files
authored
fix(react-drag-drop): support dynamic root element IDs in DragDropCon… (#12240)
* 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. * perf: memoize root element lookup to avoid repeated DOM queries * fix(DragDropContainer): handle case when DOM is not available by returning null for root element * feat(DragDropContainer): add appendTo prop for customizable drag overlay target
1 parent 1e5d932 commit 43e798d

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export interface DragDropContainerProps extends DndContextProps {
6666
variant?: 'default' | 'DataList' | 'DualListSelectorList';
6767
/** Additional classes to apply to the drag overlay */
6868
overlayProps?: any;
69+
/** The parent container to append the drag overlay to. Defaults to document.body. */
70+
appendTo?: HTMLElement | (() => HTMLElement);
6971
}
7072

7173
export const DragDropContainer: React.FunctionComponent<DragDropContainerProps> = ({
@@ -77,6 +79,7 @@ export const DragDropContainer: React.FunctionComponent<DragDropContainerProps>
7779
onCancel = () => {},
7880
variant = 'default',
7981
overlayProps,
82+
appendTo = () => document.body,
8083
...props
8184
}: DragDropContainerProps) => {
8285
const itemsCopy = useRef<Record<string, DraggableObject[]> | null>(null);
@@ -288,6 +291,9 @@ export const DragDropContainer: React.FunctionComponent<DragDropContainerProps>
288291
};
289292

290293
const dragOverlay = <DragOverlay>{activeId && getDragOverlay()}</DragOverlay>;
294+
295+
const portalTarget = typeof appendTo === 'function' ? appendTo() : appendTo || document.body;
296+
291297
return (
292298
<DndContext
293299
sensors={sensors}
@@ -299,7 +305,7 @@ export const DragDropContainer: React.FunctionComponent<DragDropContainerProps>
299305
{...props}
300306
>
301307
{children}
302-
{canUseDOM ? ReactDOM.createPortal(dragOverlay, document.getElementById('root')) : dragOverlay}
308+
{canUseDOM && portalTarget ? ReactDOM.createPortal(dragOverlay, portalTarget) : dragOverlay}
303309
</DndContext>
304310
);
305311
};

0 commit comments

Comments
 (0)