Skip to content

Commit

Permalink
fix: dragging preview bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Loxeris committed Apr 24, 2024
1 parent 519c7c5 commit b123fc0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
33 changes: 30 additions & 3 deletions src/components/ui/DrawerItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import Link from "next/link";
import {
ListItemButton,
Expand All @@ -18,6 +19,10 @@ import {
attachClosestEdge,
extractClosestEdge,
} from "@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge";
import { setCustomNativeDragPreview } from "@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview";
import { ThemeProvider as MUIThemeProvider } from "@mui/material/styles";
import { ThemeProvider } from "@/contexts/ThemeProvider";
import { useMUITheme } from "@/hooks/theme";

export default function DrawerItem({
item: { title, icon, path },
Expand All @@ -30,6 +35,7 @@ export default function DrawerItem({
}) {
const dragRef = React.useRef(null);
const handleRef = React.useRef(null);
const theme = useMUITheme();

const [closestEdge, setClosestEdge]: any = useState<Edge | null>(null);

Expand All @@ -44,6 +50,27 @@ export default function DrawerItem({
element: element,
dragHandle: handleItem,
getInitialData: () => ({ index, title }),
onGenerateDragPreview: ({ nativeSetDragImage }) => {
setCustomNativeDragPreview({
nativeSetDragImage,
getOffset: () => ({ x: 165, y: 20 }),
render: ({ container }) => {
const root = createRoot(container);
root.render(
<ThemeProvider>
<MUIThemeProvider theme={theme}>
<DrawerItem
item={{ title, icon, path }}
index={index}
groupTitle={groupTitle}
/>
</MUIThemeProvider>
</ThemeProvider>,
);
return () => root.unmount();
},
});
},
}),
dropTargetForElements({
element: element,
Expand Down Expand Up @@ -87,7 +114,7 @@ export default function DrawerItem({
},
}),
);
}, [index, groupTitle]);
}, [index, groupTitle, icon, path, theme]);

return (
<>
Expand All @@ -103,13 +130,13 @@ export default function DrawerItem({
<Icon component={icon} />
</ListItemIcon>
<ListItemText primary={title} />
<div>
<ListItemIcon sx={{ minWidth: "24px" }}>
<Icon
component={DragIndicatorIcon}
sx={{ cursor: "grab" }}
ref={handleRef}
/>
</div>
</ListItemIcon>
{closestEdge && <DropIndicator edge={closestEdge} />}
</ListItemButton>
</>
Expand Down
5 changes: 4 additions & 1 deletion src/components/ui/DrawerItemGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export default function DrawerItemGroup({
element: dropItem,
getData: () => ({ title }),
onDragStart: () => setHovered(true),
onDrop: () => setHovered(false),
onDrop: () => {
setHovered(false);
handleChange(title)(null, true);
},
onDragEnter: () => setHovered(true),
onDragLeave: () => setHovered(false),
});
Expand Down

0 comments on commit b123fc0

Please sign in to comment.