Raw
1 import { useDroppable } from "@dnd-kit/core"
2 import {
3 SortableContext,
4 horizontalListSortingStrategy,
5 rectSortingStrategy,
6 verticalListSortingStrategy,
7 } from "@dnd-kit/sortable"
8 import { Text, clx } from "@medusajs/ui"
9 import { ReactNode } from "react"
10 import { useTranslation } from "react-i18next"
11 import type { LayoutSection } from "./types"
12 import { getSectionTailId } from "./entries"
13
14 type SectionTailProps = {
15 sectionId: string
16 className?: string
17 }
18
19 /**
20 * A persistent drop area at the end of a section. Without it the only place to
21 * drop something "at the end" is the few pixels past the last entry, with no
22 * visual cue that a cross-section move landed. This gives a generous, clearly
23 * marked target that highlights while hovered.
24 */
25 function SectionTail({ sectionId, className }: SectionTailProps) {
26 const { setNodeRef, isOver } = useDroppable({
27 id: getSectionTailId(sectionId),
28 })
29 const { t } = useTranslation()
30
31 return (
32 <div
33 ref={setNodeRef}
34 className={clx(
35 "text-ui-fg-muted flex min-h-16 items-center justify-center rounded-lg border border-dashed transition-colors",
36 isOver
37 ? "border-ui-border-interactive bg-ui-bg-highlight text-ui-fg-subtle"
38 : "border-ui-border-strong",
39 className
40 )}
41 // Faint diagonal stripes mark this as a drop area rather than content.
42 style={{
43 backgroundImage:
44 "repeating-linear-gradient(-45deg, rgb(212 212 216 / 0.12), rgb(212 212 216 / 0.12) 10px, transparent 10px, transparent 20px)",
45 }}
46 >
47 <Text size="small" leading="compact">
48 {t("layout.dropToSectionEnd")}
49 </Text>
50 </div>
51 )
52 }
53
54 type SectionDropzoneProps = {
55 section: LayoutSection
56 items: string[]
57 children: ReactNode
58 }
59
60 /**
61 * Renders the droppable/sortable area for a single layout section in edit
62 * mode. The visual treatment branches on `section.ordering`:
63 *
64 * - `"list"`: we own a flex-column container (matches idle-mode stacking),
65 * sorted with `verticalListSortingStrategy`.
66 * - `"grid"`: we stay out of the way with `display: contents` so the
67 * `SortableEntry` wrappers become direct children of whatever grid (or
68 * other) container the `LayoutComponent` defines for this section,
69 * preserving its column count/gaps/breakpoints. Sorted with
70 * `rectSortingStrategy`, which computes swaps from real 2D rects instead
71 * of assuming a single vertical axis. When the section is empty there's no
72 * element to anchor the droppable to, so we render a full-width placeholder
73 * box instead.
74 * - `"horizontal-stretched"`: owns a single, non-wrapping flex row (like
75 * `"list"` owns a column), sorted with `horizontalListSortingStrategy`.
76 * `[&>*]:flex-1` gives each entry an equal share of the width and
77 * `[&>*]:min-w-0` lets them shrink so wide content truncates instead of
78 * forcing unequal widths. Owning the container (rather than delegating via
79 * `display: contents` like `"grid"`) is deliberate: the `[&>*]` utilities are
80 * applied by the entries' direct parent, so they actually reach them in edit
81 * mode. No `SectionTail` — a width-filling row has no end gap; reordering past
82 * the last item covers end placement.
83 * - `"horizontal-list"`: like `"horizontal-stretched"` but entries keep their
84 * natural size and are vertically centered (`items-center`)
85 *
86 * `"list"` and `"grid"` render a `SectionTail` last as a generous
87 * end-of-section drop target.
88 */
89 export function SectionDropzone({
90 section,
91 items,
92 children,
93 }: SectionDropzoneProps) {
94 const { setNodeRef, isOver } = useDroppable({ id: section.id })
95
96 if (section.ordering === "grid") {
97 return (
98 <SortableContext items={items} strategy={rectSortingStrategy}>
99 {items.length === 0 ? (
100 <div
101 ref={setNodeRef}
102 className={clx(
103 "col-span-full min-h-10 w-full rounded-md transition-colors",
104 isOver && "bg-ui-bg-highlight"
105 )}
106 />
107 ) : (
108 // `display: contents` keeps the SortableEntry wrappers as direct
109 // children of the author's grid, so this node has no box of its own.
110 // Drops onto the grid are covered by the entries and the SectionTail.
111 <div className="contents">{children}</div>
112 )}
113 <SectionTail sectionId={section.id} className="col-span-full" />
114 </SortableContext>
115 )
116 }
117
118 if (section.ordering === "horizontal-stretched") {
119 return (
120 <SortableContext items={items} strategy={horizontalListSortingStrategy}>
121 <div
122 ref={setNodeRef}
123 className={clx(
124 "flex min-h-10 flex-row items-stretch gap-x-3 rounded-md transition-colors [&>*]:min-w-0 [&>*]:flex-1",
125 isOver && items.length === 0 && "bg-ui-bg-highlight"
126 )}
127 >
128 {children}
129 </div>
130 </SortableContext>
131 )
132 }
133
134 if (section.ordering === "horizontal-list") {
135 return (
136 <SortableContext items={items} strategy={horizontalListSortingStrategy}>
137 <div
138 ref={setNodeRef}
139 className={clx(
140 "flex min-h-10 flex-row items-center gap-x-3 rounded-md transition-colors",
141 isOver && items.length === 0 && "bg-ui-bg-highlight"
142 )}
143 >
144 {children}
145 </div>
146 </SortableContext>
147 )
148 }
149
150 return (
151 <SortableContext items={items} strategy={verticalListSortingStrategy}>
152 <div
153 ref={setNodeRef}
154 className={clx(
155 "flex min-h-10 flex-col gap-y-3 rounded-md transition-colors",
156 isOver && items.length === 0 && "bg-ui-bg-highlight"
157 )}
158 >
159 {children}
160 <SectionTail sectionId={section.id} />
161 </div>
162 </SortableContext>
163 )
164 }