-
Notifications
You must be signed in to change notification settings - Fork 3
/
array-drag-space.tsx
45 lines (41 loc) · 1 KB
/
array-drag-space.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"use client";
import { cn } from "@/lib/utils";
export const ArrayDragSpace = ({
children,
index,
direction,
onDragToIndex,
onDragToLast,
} : {
children: JSX.Element[] | undefined,
index: number,
direction: "x" | "y",
onDragToIndex?: (event: React.DragEvent<HTMLDivElement>, index: number) => void,
onDragToLast?: (event: React.DragEvent<HTMLDivElement>) => void,
}) => {
const Space = ({
onDragOver,
enabled
} : {
onDragOver?: (event: React.DragEvent<HTMLDivElement>) => void,
enabled?: boolean
}) => (
<div
className={cn(
"rounded-md shrink-0",
enabled && "bg-blue-400/75",
direction === "x" ? "w-2" : "h-2"
)}
onDragOver={onDragOver}
/>
);
if (!children) return null;
return (<>
{children.map((v, i) => (<>
<Space onDragOver={(e) => onDragToIndex?.(e, i)} enabled={index === i} />
{v}
</>)
)}
<Space onDragOver={(e) => onDragToLast?.(e)} enabled={index === children.length} />
</>)
};