-
Notifications
You must be signed in to change notification settings - Fork 1
Use dnd-kit sortable context for scrollable reordering #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
0suu
wants to merge
1
commit into
suu/10-sort-device-list
from
codex/add-scroll-support-while-sorting-in-dnd-kit-rbcq5p
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,18 @@ import CircularProgress from "@mui/material/CircularProgress"; | |||||
| import Alert from "@mui/material/Alert"; | ||||||
| import IconButton from "@mui/material/IconButton"; | ||||||
| import { motion, AnimatePresence } from "framer-motion"; | ||||||
| import { | ||||||
| DndContext, | ||||||
| DragEndEvent, | ||||||
| DragOverEvent, | ||||||
| DragStartEvent, | ||||||
| MouseSensor, | ||||||
| TouchSensor, | ||||||
| useSensor, | ||||||
| useSensors, | ||||||
| } from "@dnd-kit/core"; | ||||||
| import { SortableContext, rectSortingStrategy, useSortable } from "@dnd-kit/sortable"; | ||||||
| import { CSS } from "@dnd-kit/utilities"; | ||||||
|
|
||||||
| import { AppDispatch } from "../store/store"; | ||||||
| import { | ||||||
|
|
@@ -28,7 +40,7 @@ import { shouldHideDevice } from "../deviceDefinitions"; | |||||
| import { useTranslation } from "../useTranslation"; | ||||||
| import SwapVertIcon from "@mui/icons-material/SwapVert"; | ||||||
| import DragIndicatorIcon from "@mui/icons-material/DragIndicator"; | ||||||
| import { AnyDevice } from "../../../api/types"; | ||||||
| import { AnyDevice, DeviceStatusResponseBody } from "../../../api/types"; | ||||||
|
|
||||||
| // Animation variants | ||||||
| const containerVariants = { | ||||||
|
|
@@ -68,6 +80,10 @@ export const DeviceListScreen: React.FC<{ onDeviceSelect: (deviceId: string) => | |||||
| const [reorderList, setReorderList] = useState<string[]>([]); | ||||||
| const [draggingId, setDraggingId] = useState<string | null>(null); | ||||||
| const [dragOverId, setDragOverId] = useState<string | null>(null); | ||||||
| const sensors = useSensors( | ||||||
| useSensor(MouseSensor, { activationConstraint: { distance: 5 } }), | ||||||
| useSensor(TouchSensor, { pressDelay: 150, pressThreshold: 5 }) | ||||||
| ); | ||||||
|
|
||||||
| const orderedDevices = useMemo(() => { | ||||||
| const idToDevice = new Map<string, AnyDevice>(); | ||||||
|
|
@@ -177,10 +193,6 @@ export const DeviceListScreen: React.FC<{ onDeviceSelect: (deviceId: string) => | |||||
| }; | ||||||
| }, [draggingId]); | ||||||
|
|
||||||
| const handleReorder = (visibleOrder: string[]) => { | ||||||
| setReorderList(visibleOrder); | ||||||
| }; | ||||||
|
|
||||||
| const moveIdInList = (list: string[], fromId: string, toId: string) => { | ||||||
| if (fromId === toId) return list; | ||||||
| const fromIndex = list.indexOf(fromId); | ||||||
|
|
@@ -192,37 +204,34 @@ export const DeviceListScreen: React.FC<{ onDeviceSelect: (deviceId: string) => | |||||
| return next; | ||||||
| }; | ||||||
|
|
||||||
| const handleDragStart = (id: string) => (event: React.DragEvent<HTMLDivElement>) => { | ||||||
| setDraggingId(id); | ||||||
| const handleDragStart = (event: DragStartEvent) => { | ||||||
| if (!isReorderMode) return; | ||||||
| setDraggingId(String(event.active.id)); | ||||||
| setDragOverId(null); | ||||||
| event.dataTransfer.effectAllowed = "move"; | ||||||
| }; | ||||||
|
|
||||||
| const handleDragOver = (overId: string) => (event: React.DragEvent<HTMLDivElement>) => { | ||||||
| event.preventDefault(); | ||||||
| if (!draggingId || draggingId === overId) return; | ||||||
|
|
||||||
| // Only track hover state, don't update the list during drag | ||||||
| setDragOverId(overId); | ||||||
| }; | ||||||
|
|
||||||
| const handleDragLeave = () => { | ||||||
| setDragOverId(null); | ||||||
| const handleDragOver = (event: DragOverEvent) => { | ||||||
| if (!isReorderMode) return; | ||||||
| const overId = event.over?.id; | ||||||
| if (!overId || !draggingId || draggingId === overId) return; | ||||||
| setDragOverId(String(overId)); | ||||||
| }; | ||||||
|
|
||||||
| const handleDrop = (overId: string) => (event: React.DragEvent<HTMLDivElement>) => { | ||||||
| event.preventDefault(); | ||||||
|
|
||||||
| if (draggingId && overId && draggingId !== overId) { | ||||||
| // Update the list only once on drop | ||||||
| setReorderList((prev) => moveIdInList(prev, draggingId, overId)); | ||||||
| const handleDragEnd = (event: DragEndEvent) => { | ||||||
| if (!isReorderMode) return; | ||||||
| const { active, over } = event; | ||||||
| if (active?.id && over?.id) { | ||||||
| const activeId = String(active.id); | ||||||
| const overId = String(over.id); | ||||||
| if (activeId !== overId) { | ||||||
| setReorderList((prev) => moveIdInList(prev, activeId, overId)); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| setDraggingId(null); | ||||||
| setDragOverId(null); | ||||||
| }; | ||||||
|
|
||||||
| const handleDragEnd = () => { | ||||||
| const handleDragCancel = () => { | ||||||
| setDraggingId(null); | ||||||
| setDragOverId(null); | ||||||
| }; | ||||||
|
|
@@ -322,64 +331,117 @@ export const DeviceListScreen: React.FC<{ onDeviceSelect: (deviceId: string) => | |||||
| </Box> | ||||||
| )} | ||||||
|
|
||||||
| <motion.div | ||||||
| variants={containerVariants} | ||||||
| initial="hidden" | ||||||
| animate="visible" | ||||||
| <DndContext | ||||||
| sensors={sensors} | ||||||
| onDragStart={handleDragStart} | ||||||
| onDragOver={handleDragOver} | ||||||
| onDragEnd={handleDragEnd} | ||||||
| onDragCancel={handleDragCancel} | ||||||
| > | ||||||
| <Box sx={gridSx}> | ||||||
| <AnimatePresence> | ||||||
| {(isReorderMode | ||||||
| <SortableContext | ||||||
| items={ | ||||||
| isReorderMode | ||||||
| ? reorderList | ||||||
| .map((deviceId) => filteredDeviceMap.get(deviceId)) | ||||||
| .filter((device): device is AnyDevice => !!device) | ||||||
| : filteredDevices | ||||||
| ).map((device) => ( | ||||||
| <motion.div variants={itemVariants} layout key={device.deviceId}> | ||||||
| <Box | ||||||
| sx={{ | ||||||
| position: "relative", | ||||||
| opacity: draggingId === device.deviceId ? 0.5 : 1, | ||||||
| transition: "opacity 0.2s ease", | ||||||
| outline: dragOverId === device.deviceId ? "2px solid" : "none", | ||||||
| outlineColor: "primary.main", | ||||||
| outlineOffset: "2px", | ||||||
| borderRadius: 2, | ||||||
| }} | ||||||
| draggable={isReorderMode} | ||||||
| onDragStart={isReorderMode ? handleDragStart(device.deviceId) : undefined} | ||||||
| onDragOver={isReorderMode ? handleDragOver(device.deviceId) : undefined} | ||||||
| onDragLeave={isReorderMode ? handleDragLeave : undefined} | ||||||
| onDrop={isReorderMode ? handleDrop(device.deviceId) : undefined} | ||||||
| onDragEnd={isReorderMode ? handleDragEnd : undefined} | ||||||
| > | ||||||
| {isReorderMode && ( | ||||||
| <IconButton | ||||||
| size="small" | ||||||
| sx={{ | ||||||
| position: "absolute", | ||||||
| top: 8, | ||||||
| right: 8, | ||||||
| bgcolor: "background.paper", | ||||||
| boxShadow: 1, | ||||||
| pointerEvents: "none", | ||||||
| }} | ||||||
| aria-label={t("Reorder")} | ||||||
| > | ||||||
| <DragIndicatorIcon fontSize="small" /> | ||||||
| </IconButton> | ||||||
| )} | ||||||
| <DeviceCard | ||||||
| device={device} | ||||||
| status={statusMap[device.deviceId]} | ||||||
| onSelect={onDeviceSelect} | ||||||
| /> | ||||||
| </Box> | ||||||
| </motion.div> | ||||||
| ))} | ||||||
| </AnimatePresence> | ||||||
| </Box> | ||||||
| </motion.div> | ||||||
| : filteredDevices.map((device) => device.deviceId) | ||||||
| } | ||||||
| strategy={rectSortingStrategy} | ||||||
| > | ||||||
| <motion.div variants={containerVariants} initial="hidden" animate="visible"> | ||||||
| <Box sx={gridSx}> | ||||||
| <AnimatePresence> | ||||||
| {(isReorderMode | ||||||
| ? reorderList | ||||||
| .map((deviceId) => filteredDeviceMap.get(deviceId)) | ||||||
| .filter((device): device is AnyDevice => !!device) | ||||||
| : filteredDevices | ||||||
| ).map((device) => ( | ||||||
| <motion.div variants={itemVariants} layout key={device.deviceId}> | ||||||
| <SortableDeviceCard | ||||||
| device={device} | ||||||
| isReorderMode={isReorderMode} | ||||||
| draggingId={draggingId} | ||||||
| dragOverId={dragOverId} | ||||||
| t={t} | ||||||
| onSelect={onDeviceSelect} | ||||||
| statusMap={statusMap} | ||||||
| /> | ||||||
| </motion.div> | ||||||
| ))} | ||||||
| </AnimatePresence> | ||||||
| </Box> | ||||||
| </motion.div> | ||||||
| </SortableContext> | ||||||
| </DndContext> | ||||||
| </Container> | ||||||
| ); | ||||||
| }; | ||||||
|
|
||||||
| type SortableDeviceCardProps = { | ||||||
| device: AnyDevice; | ||||||
| isReorderMode: boolean; | ||||||
| draggingId: string | null; | ||||||
| dragOverId: string | null; | ||||||
| t: (key: string) => string; | ||||||
| onSelect: (deviceId: string) => void; | ||||||
| statusMap: Record<string, DeviceStatusResponseBody | undefined>; | ||||||
| }; | ||||||
|
|
||||||
| const SortableDeviceCard: React.FC<SortableDeviceCardProps> = ({ | ||||||
| device, | ||||||
| isReorderMode, | ||||||
| draggingId, | ||||||
| dragOverId, | ||||||
| t, | ||||||
| onSelect, | ||||||
| statusMap, | ||||||
| }) => { | ||||||
| const { attributes, listeners, setNodeRef, transform, transition, isDragging, isOver } = useSortable({ | ||||||
| id: device.deviceId, | ||||||
| disabled: !isReorderMode, | ||||||
| }); | ||||||
|
|
||||||
| const style = { | ||||||
| transform: transform ? CSS.Transform.toString(transform) : undefined, | ||||||
| transition, | ||||||
| }; | ||||||
|
|
||||||
| return ( | ||||||
| <Box | ||||||
| ref={setNodeRef} | ||||||
| sx={{ | ||||||
| position: "relative", | ||||||
| opacity: isDragging || draggingId === device.deviceId ? 0.5 : 1, | ||||||
| transition: "opacity 0.2s ease", | ||||||
| outline: dragOverId === device.deviceId || isOver ? "2px solid" : "none", | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the
Suggested change
|
||||||
| outlineColor: "primary.main", | ||||||
| outlineOffset: "2px", | ||||||
| borderRadius: 2, | ||||||
| }} | ||||||
| style={style} | ||||||
| {...attributes} | ||||||
| {...listeners} | ||||||
| > | ||||||
| {isReorderMode && ( | ||||||
| <IconButton | ||||||
| size="small" | ||||||
| sx={{ | ||||||
| position: "absolute", | ||||||
| top: 8, | ||||||
| right: 8, | ||||||
| bgcolor: "background.paper", | ||||||
| boxShadow: 1, | ||||||
| pointerEvents: "none", | ||||||
| }} | ||||||
| aria-label={t("Reorder")} | ||||||
| > | ||||||
| <DragIndicatorIcon fontSize="small" /> | ||||||
| </IconButton> | ||||||
| )} | ||||||
| <DeviceCard | ||||||
| device={device} | ||||||
| status={statusMap[device.deviceId]} | ||||||
| onSelect={onSelect} | ||||||
| /> | ||||||
| </Box> | ||||||
| ); | ||||||
| }; | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition
draggingId === device.deviceIdis redundant here. TheisDraggingboolean from theuseSortablehook already correctly identifies if this is the item being dragged. UsingisDraggingalone is cleaner and more idiomatic when working withdnd-kit.