Skip to content

Commit 149bab8

Browse files
committed
🐛(frontend) disable DND grid item when dialog open
We could drag and drop the items even if the modal was opened, which could cause some unexpected behaviors. This commit disables the DND functionality when a dialog box is open.
1 parent 5491179 commit 149bab8

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

src/frontend/apps/impress/src/features/docs/docs-grid/components/DocGridContentList.tsx

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getEventCoordinates } from '@dnd-kit/utilities';
33
import { useModal } from '@gouvfr-lasuite/cunningham-react';
44
import { TreeViewMoveModeEnum } from '@gouvfr-lasuite/ui-kit';
55
import { useQueryClient } from '@tanstack/react-query';
6-
import { useMemo, useRef } from 'react';
6+
import { useEffect, useMemo, useRef, useState } from 'react';
77
import { Trans, useTranslation } from 'react-i18next';
88

99
import { AlertModal, Card, Text } from '@/components';
@@ -15,6 +15,7 @@ import {
1515
useDeleteDocInvitation,
1616
} from '@/docs/doc-share';
1717
import { useMoveDoc } from '@/docs/doc-tree';
18+
import { useResponsiveStore } from '@/stores/useResponsiveStore';
1819

1920
import { DocDragEndData, useDragAndDrop } from '../hooks/useDragAndDrop';
2021

@@ -151,9 +152,24 @@ export const DraggableDocGridContentList = ({
151152
const cannotMoveDoc =
152153
!canDrag || (canDrop !== undefined && !canDrop) || isError;
153154

154-
if (docs.length === 0) {
155-
return null;
156-
}
155+
const [isDraggableDisabled, setIsDraggableDisabled] = useState(false);
156+
157+
useEffect(() => {
158+
const checkModal = () => {
159+
const modalOpen = document.querySelector('[role="dialog"]');
160+
setIsDraggableDisabled(!!modalOpen);
161+
};
162+
163+
checkModal();
164+
165+
const observer = new MutationObserver(checkModal);
166+
observer.observe(document.body, {
167+
childList: true,
168+
subtree: true,
169+
});
170+
171+
return () => observer.disconnect();
172+
}, []);
157173

158174
return (
159175
<>
@@ -170,6 +186,7 @@ export const DraggableDocGridContentList = ({
170186
dragMode={!!selectedDoc}
171187
canDrag={!!canDrag}
172188
updateCanDrop={updateCanDrop}
189+
disabled={isDraggableDisabled}
173190
/>
174191
))}
175192
<DragOverlay dropAnimation={null}>
@@ -216,13 +233,15 @@ export const DraggableDocGridContentList = ({
216233
};
217234

218235
interface DraggableDocGridItemProps {
236+
disabled: boolean;
219237
doc: Doc;
220238
dragMode: boolean;
221239
canDrag: boolean;
222240
updateCanDrop: (canDrop: boolean, isOver: boolean) => void;
223241
}
224242

225243
export const DraggableDocGridItem = ({
244+
disabled,
226245
doc,
227246
dragMode,
228247
canDrag,
@@ -238,18 +257,24 @@ export const DraggableDocGridItem = ({
238257
id={doc.id}
239258
data={doc}
240259
>
241-
<Draggable id={doc.id} data={doc}>
260+
<Draggable id={doc.id} data={doc} disabled={disabled}>
242261
<DocsGridItem dragMode={dragMode} doc={doc} />
243262
</Draggable>
244263
</Droppable>
245264
);
246265
};
247266

248267
export const DocGridContentList = ({ docs }: DocGridContentListProps) => {
268+
const { isDesktop } = useResponsiveStore();
269+
249270
if (docs.length === 0) {
250271
return null;
251272
}
252273

274+
if (isDesktop) {
275+
return <DraggableDocGridContentList docs={docs} />;
276+
}
277+
253278
return docs.map((doc) => (
254279
<DocsGridItem dragMode={false} doc={doc} key={doc.id} />
255280
));

src/frontend/apps/impress/src/features/docs/docs-grid/components/DocsGrid.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import { useImport } from '../../docs-import/hooks/useImport';
1414
import { useInfiniteDocsTrashbin } from '../api';
1515
import { useResponsiveDocGrid } from '../hooks/useResponsiveDocGrid';
1616

17-
import {
18-
DocGridContentList,
19-
DraggableDocGridContentList,
20-
} from './DocGridContentList';
17+
import { DocGridContentList } from './DocGridContentList';
2118
import { DocsGridLoader } from './DocsGridLoader';
2219

2320
type DocsGridProps = {
@@ -153,11 +150,7 @@ export const DocsGrid = ({
153150
</Box>
154151
</Box>
155152
<Box role="rowgroup">
156-
{isDesktop ? (
157-
<DraggableDocGridContentList docs={docs} />
158-
) : (
159-
<DocGridContentList docs={docs} />
160-
)}
153+
<DocGridContentList docs={docs} />
161154
</Box>
162155
</Box>
163156
{hasNextPage && !loading && (

src/frontend/apps/impress/src/features/docs/docs-grid/components/Draggable.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { Data, useDraggable } from '@dnd-kit/core';
2+
import { PropsWithChildren } from 'react';
23

34
type DraggableProps<T> = {
45
id: string;
56
data?: Data<T>;
6-
children: React.ReactNode;
7+
disabled?: boolean;
78
};
89

9-
export const Draggable = <T,>(props: DraggableProps<T>) => {
10+
export const Draggable = <T,>(props: PropsWithChildren<DraggableProps<T>>) => {
1011
const { attributes, listeners, setNodeRef } = useDraggable({
1112
id: props.id,
1213
data: props.data,
14+
disabled: props.disabled,
1315
});
1416

1517
return (

0 commit comments

Comments
 (0)