-
-
Notifications
You must be signed in to change notification settings - Fork 0
Enhance drag-and-drop experience with SortableGrid and TouchDragOverlay #43
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
812dd43
feat: add TouchDragOverlay component for improved touch drag experience
sumitsahoo 4c01a66
feat: implement SortableGrid component for drag-and-drop functionality
sumitsahoo 070b8a7
fix: ensure TouchDragOverlay only renders when touch position is avai…
sumitsahoo 3c9a153
feat: add scroll-to-top functionality on view change and enhance logo…
sumitsahoo 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
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 |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /** | ||
| * SortableGrid — reusable drag-and-drop grid for page reordering. | ||
| * | ||
| * Renders an interleaved layout of drop-zones and items: | ||
| * [drop_0] [item_0] [drop_1] [item_1] ... [item_N-1] [drop_N] | ||
| * | ||
| * Drop-zones expand when hovering during a drag to show where the item will | ||
| * land. A floating TouchDragOverlay follows the finger on mobile. | ||
| * | ||
| * Usage: | ||
| * const drag = useSortableDrag(handleMove); | ||
| * | ||
| * <SortableGrid | ||
| * itemCount={items.length} | ||
| * drag={drag} | ||
| * onMove={handleMove} | ||
| * renderItem={(slot, isSource) => <PageCard ... />} | ||
| * renderOverlay={(dragIndex) => <OverlayContent ... />} | ||
| * /> | ||
| */ | ||
|
|
||
| import type { SortableDrag } from "../hooks/useSortableDrag.ts"; | ||
| import { TouchDragOverlay } from "./TouchDragOverlay.tsx"; | ||
|
|
||
| interface SortableGridProps { | ||
| /** Total number of items in the list. */ | ||
| itemCount: number; | ||
| /** Drag state bag from useSortableDrag. */ | ||
| drag: SortableDrag; | ||
| /** Called when an item is dropped at a new slot (desktop HTML5 drag path). */ | ||
| onMove: (fromIndex: number, toSlot: number) => void; | ||
| /** | ||
| * Render a single item at the given slot. | ||
| * The item is responsible for spreading `drag.getItemProps(slot)` (or a | ||
| * subset of it) onto its root element to make it draggable. | ||
| */ | ||
| renderItem: (slot: number, isSource: boolean) => React.ReactNode; | ||
| /** Render the content shown inside the floating touch-drag overlay. */ | ||
| renderOverlay?: (dragIndex: number) => React.ReactNode; | ||
| /** Optional class override for the grid wrapper. */ | ||
| className?: string; | ||
| } | ||
|
|
||
| export function SortableGrid({ | ||
| itemCount, | ||
| drag, | ||
| onMove, | ||
| renderItem, | ||
| renderOverlay, | ||
| className, | ||
| }: SortableGridProps) { | ||
| const { dragIndex, dragOverSlot, touchPos, setDragIndex, setDragOverSlot } = drag; | ||
| const isDragging = dragIndex !== null; | ||
|
|
||
| const elements: React.ReactNode[] = []; | ||
|
|
||
| for (let slot = 0; slot <= itemCount; slot++) { | ||
| const isAdjacentToDrag = dragIndex !== null && (slot === dragIndex || slot === dragIndex + 1); | ||
|
|
||
| // ── Drop zone ── | ||
| elements.push( | ||
| <div | ||
| key={`drop-${slot}`} | ||
| data-drop-slot={slot} | ||
| onDragOver={(e) => { | ||
| if (isAdjacentToDrag) return; | ||
| e.preventDefault(); | ||
| setDragOverSlot(slot); | ||
| }} | ||
| onDragLeave={(e) => { | ||
| if (!e.currentTarget.contains(e.relatedTarget as Node)) { | ||
| if (dragOverSlot === slot) setDragOverSlot(null); | ||
| } | ||
| }} | ||
| onDrop={(e) => { | ||
| e.preventDefault(); | ||
| if (dragIndex === null || isAdjacentToDrag) return; | ||
| onMove(dragIndex, slot); | ||
| setDragIndex(null); | ||
| setDragOverSlot(null); | ||
| }} | ||
| className={`self-stretch flex items-center justify-center rounded-lg transition-all duration-200 ${ | ||
| isDragging && !isAdjacentToDrag | ||
| ? dragOverSlot === slot | ||
| ? "w-20 sm:w-24 bg-primary-50 dark:bg-primary-900/20" | ||
| : "w-3 sm:w-4" | ||
| : "w-0" | ||
| }`} | ||
| > | ||
| {isDragging && !isAdjacentToDrag && ( | ||
| <div | ||
| className={`rounded-full transition-all duration-200 ${ | ||
| dragOverSlot === slot | ||
| ? "w-1 bg-primary-500" | ||
| : "w-0.5 bg-primary-200 dark:bg-primary-800" | ||
| }`} | ||
| style={{ height: dragOverSlot === slot ? "80%" : "60%" }} | ||
| /> | ||
| )} | ||
| </div>, | ||
| ); | ||
|
|
||
| // ── Item ── | ||
| if (slot < itemCount) { | ||
| elements.push(renderItem(slot, dragIndex === slot)); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <div | ||
| className={className ?? "flex flex-wrap items-end gap-y-6 overflow-x-auto pb-2 min-h-28"} | ||
| > | ||
| {elements} | ||
| </div> | ||
|
|
||
| {dragIndex !== null && touchPos !== null && renderOverlay && ( | ||
| <TouchDragOverlay touchPos={touchPos}>{renderOverlay(dragIndex)}</TouchDragOverlay> | ||
| )} | ||
| </> | ||
| ); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * TouchDragOverlay — floating preview that follows the finger during touch drag. | ||
| * | ||
| * On desktop, the HTML5 Drag API provides a built-in ghost image. On mobile, | ||
| * touch events have no equivalent, so this component renders a small | ||
| * semi-transparent thumbnail at the current touch position via a portal. | ||
| */ | ||
|
|
||
| import { createPortal } from "react-dom"; | ||
|
|
||
| interface TouchDragOverlayProps { | ||
| /** Current touch coordinates (null when not dragging via touch). */ | ||
| touchPos: { x: number; y: number } | null; | ||
| children: React.ReactNode; | ||
| } | ||
|
|
||
| export function TouchDragOverlay({ touchPos, children }: TouchDragOverlayProps) { | ||
| if (!touchPos) return null; | ||
|
|
||
| return createPortal( | ||
| <div | ||
| style={{ | ||
| position: "fixed", | ||
| left: touchPos.x, | ||
| top: touchPos.y, | ||
| transform: "translate(-50%, -60%)", | ||
| pointerEvents: "none", | ||
| zIndex: 9999, | ||
| }} | ||
| className="opacity-80 scale-90 shadow-xl rounded-lg" | ||
| > | ||
| {children} | ||
| </div>, | ||
| document.body, | ||
| ); | ||
| } |
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
Oops, something went wrong.
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
react-hooks/exhaustive-depssuppression looks unnecessary here since the effect already lists the intended dependencies (activeTool,showPrivacy). Consider removing the eslint-disable comment to avoid masking real dependency issues in future edits.