|
1
|
import { useSortable } from "@dnd-kit/sortable" |
|
2
|
import { CSS } from "@dnd-kit/utilities" |
|
3
|
import { DotsSix, Eye, EyeSlash } from "@medusajs/icons" |
|
4
|
import { IconButton, clx } from "@medusajs/ui" |
|
5
|
import { ReactNode } from "react" |
|
6
|
import { useTranslation } from "react-i18next" |
|
7
|
import type { LayoutControlSize } from "./types" |
|
8
|
|
|
9
|
/** |
|
10
|
* An entry's rendered content plus a placeholder that appears (via the |
|
11
|
* `peer`/`:empty` pair) only when that content produces no DOM — e.g. a section |
|
12
|
* that exists solely while an edit is pending. Keeps such an entry a visible, |
|
13
|
* labeled card rather than collapsing to a 0-height row. Shared by the in-place |
|
14
|
* `SortableEntry` and the `DragOverlay` ghost so the two never drift. |
|
15
|
* |
|
16
|
* `empty:hidden` collapses the content box when it renders nothing so it stops |
|
17
|
* reserving height — otherwise a layout that stretches the entry (e.g. a grid |
|
18
|
* equalizing row heights) would size the empty content to the full cell and |
|
19
|
* stack the placeholder below it, showing two boxes for one empty entry. |
|
20
|
*/ |
|
21
|
export function EntryContent({ |
|
22
|
children, |
|
23
|
className, |
|
24
|
placeholderClassName, |
|
25
|
}: { |
|
26
|
children: ReactNode |
|
27
|
className?: string |
|
28
|
placeholderClassName?: string |
|
29
|
}) { |
|
30
|
const { t } = useTranslation() |
|
31
|
return ( |
|
32
|
<> |
|
33
|
<div className={clx("peer flex flex-col empty:hidden", className)}> |
|
34
|
{children} |
|
35
|
</div> |
|
36
|
<div |
|
37
|
aria-hidden |
|
38
|
className={clx( |
|
39
|
"text-ui-fg-muted hidden min-h-16 items-center justify-center px-2 text-center text-xs peer-[:empty]:flex", |
|
40
|
placeholderClassName |
|
41
|
)} |
|
42
|
> |
|
43
|
{t("layout.empty")} |
|
44
|
</div> |
|
45
|
</> |
|
46
|
) |
|
47
|
} |
|
48
|
|
|
49
|
type SortableEntryProps = { |
|
50
|
widgetId: string |
|
51
|
order: number |
|
52
|
hidden: boolean |
|
53
|
onToggleHidden: () => void |
|
54
|
children: ReactNode |
|
55
|
controlSize?: LayoutControlSize |
|
56
|
} |
|
57
|
|
|
58
|
export function SortableEntry({ |
|
59
|
widgetId, |
|
60
|
order, |
|
61
|
hidden, |
|
62
|
onToggleHidden, |
|
63
|
children, |
|
64
|
controlSize = "default", |
|
65
|
}: SortableEntryProps) { |
|
66
|
const { t } = useTranslation() |
|
67
|
const { |
|
68
|
attributes, |
|
69
|
listeners, |
|
70
|
setNodeRef, |
|
71
|
transform, |
|
72
|
transition, |
|
73
|
isDragging, |
|
74
|
} = useSortable({ id: widgetId }) |
|
75
|
|
|
76
|
// `CSS.Translate.toString` applies translate only, dropping the strategy's |
|
77
|
// scaleX/scaleY (which would stretch the dragged item to match the swapped |
|
78
|
// neighbor's box). The actual dragged ghost is rendered separately via |
|
79
|
// <DragOverlay>. |
|
80
|
const style = { |
|
81
|
transform: CSS.Translate.toString(transform), |
|
82
|
transition, |
|
83
|
} |
|
84
|
|
|
85
|
const xsmall = controlSize === "xsmall" |
|
86
|
const small = controlSize === "small" |
|
87
|
const showLabel = controlSize === "default" |
|
88
|
|
|
89
|
return ( |
|
90
|
<div |
|
91
|
ref={setNodeRef} |
|
92
|
style={style} |
|
93
|
className={clx( |
|
94
|
// `min-w-0` lets the entry shrink below its content's intrinsic width |
|
95
|
// when a layout stretches it into equal grid/flex tracks. The author's |
|
96
|
// container can't set this via `[&>*]` because the `display: contents` |
|
97
|
// SortableContext wrapper hides its children from the `>` combinator. |
|
98
|
"ring-ui-border-base relative min-w-0 rounded-lg ring-1 transition-opacity", |
|
99
|
// Hidden entries are clearly de-emphasized during edit mode so the |
|
100
|
// user can tell at a glance which ones won't render at idle. |
|
101
|
hidden && "opacity-30 grayscale", |
|
102
|
// Hide the original while it's being dragged — the DragOverlay shows |
|
103
|
// the moving copy. Visibility (not display:none) keeps the layout box |
|
104
|
// in place so neighbors can shift against it. |
|
105
|
isDragging && "invisible" |
|
106
|
)} |
|
107
|
> |
|
108
|
{/* `flex flex-col` makes the rendered content a flex item again (as it |
|
109
|
is in idle mode), so it re-establishes its own block formatting |
|
110
|
context and contains any trailing margins of its children — without |
|
111
|
this, a child's bottom margin escapes through these plain wrapper |
|
112
|
divs and inflates this box past the content's background. |
|
113
|
|
|
114
|
`h-full` + `[&>*]:h-full` let the content fill the wrapper when a |
|
115
|
layout stretches the entry to a fixed height (e.g. a grid section |
|
116
|
equalizing row heights). When the wrapper height is auto (list |
|
117
|
sections) these resolve to the content height, so they're inert. */} |
|
118
|
<EntryContent |
|
119
|
className="h-full [&>*]:h-full" |
|
120
|
placeholderClassName="border-ui-border-strong h-full rounded-lg border border-dashed" |
|
121
|
> |
|
122
|
{children} |
|
123
|
</EntryContent> |
|
124
|
{/* Overlay rendered after children so it stacks above them by DOM order */} |
|
125
|
<div |
|
126
|
className={clx( |
|
127
|
"bg-ui-bg-base shadow-elevation-card-rest absolute flex items-center rounded-md", |
|
128
|
xsmall && "right-0 top-0 flex-col gap-y-0.5 p-0 opacity-50", |
|
129
|
small && "right-1.5 top-[min(50%,2.5px)] gap-x-0.5 p-0.5", |
|
130
|
showLabel && "right-2 top-2 gap-x-1 p-1" |
|
131
|
)} |
|
132
|
> |
|
133
|
{showLabel && ( |
|
134
|
<span className="text-ui-fg-muted px-1 font-mono text-xs"> |
|
135
|
{widgetId} ({order}) |
|
136
|
</span> |
|
137
|
)} |
|
138
|
<IconButton |
|
139
|
size="2xsmall" |
|
140
|
variant="transparent" |
|
141
|
onClick={onToggleHidden} |
|
142
|
aria-label={hidden ? t("actions.show") : t("actions.hide")} |
|
143
|
className={clx(xsmall && "h-3 w-3 p-0", small && "h-3.5 w-3.5 p-0.5")} |
|
144
|
> |
|
145
|
{hidden ? <EyeSlash /> : <Eye />} |
|
146
|
</IconButton> |
|
147
|
<button |
|
148
|
type="button" |
|
149
|
className={clx( |
|
150
|
"text-ui-fg-muted cursor-grab touch-none rounded focus:outline-none", |
|
151
|
xsmall && "p-0", |
|
152
|
small && "p-0.5", |
|
153
|
showLabel && "p-1" |
|
154
|
)} |
|
155
|
{...attributes} |
|
156
|
{...listeners} |
|
157
|
aria-label={t("layout.dragToReorder")} |
|
158
|
> |
|
159
|
<DotsSix /> |
|
160
|
</button> |
|
161
|
</div> |
|
162
|
</div> |
|
163
|
) |
|
164
|
} |