-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathindex.client.tsx
More file actions
383 lines (357 loc) · 11.5 KB
/
index.client.tsx
File metadata and controls
383 lines (357 loc) · 11.5 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
'use client'
import type { Modifier } from '@dnd-kit/core'
import type { ClientWidget, WidgetWidth } from 'payload'
import { DndContext, DragOverlay, useDraggable, useDroppable } from '@dnd-kit/core'
import { snapCenterToCursor } from '@dnd-kit/modifiers'
import { ChevronIcon, Popup, PopupList, useTranslation, XIcon } from '@payloadcms/ui'
import React, { useMemo, useState } from 'react'
/**
* Custom modifier that only applies snapCenterToCursor for pointer events.
* During keyboard navigation, we handle positioning ourselves via the coordinate getter.
*/
const snapCenterToCursorOnlyForPointer: Modifier = (args) => {
const { activatorEvent } = args
// Only apply snap for pointer events (mouse/touch), not keyboard
// Check activatorEvent.type since KeyboardEvent may not exist on server
if (activatorEvent && 'key' in activatorEvent) {
return args.transform
}
return snapCenterToCursor(args)
}
import { DashboardStepNav } from './DashboardStepNav.js'
import { useDashboardLayout } from './useDashboardLayout.js'
import { closestInXAxis } from './utils/collisionDetection.js'
import { useDashboardSensors } from './utils/sensors.js'
import { WidgetEditControl } from './WidgetEditControl.js'
export type WidgetItem = {
data?: Record<string, unknown>
id: string
maxWidth: WidgetWidth
minWidth: WidgetWidth
width: WidgetWidth
}
export type WidgetInstanceClient = {
component: React.ReactNode
item: WidgetItem
}
export type DropTargetWidget = {
position: 'after' | 'before'
widget: WidgetInstanceClient
} | null
/* eslint-disable perfectionist/sort-objects */
const WIDTH_TO_PERCENTAGE = {
'x-small': 25,
small: (1 / 3) * 100,
medium: 50,
large: (2 / 3) * 100,
'x-large': 75,
full: 100,
} as const
export function ModularDashboardClient({
clientLayout: initialLayout,
widgets,
}: {
clientLayout: WidgetInstanceClient[]
widgets: ClientWidget[]
}) {
const { t } = useTranslation()
const {
addWidget,
cancel,
cancelModal,
currentLayout,
deleteWidget,
isEditing,
moveWidget,
resetLayout,
resizeWidget,
saveLayout,
setIsEditing,
updateWidgetData,
} = useDashboardLayout(initialLayout)
const [activeDragId, setActiveDragId] = useState<null | string>(null)
const sensors = useDashboardSensors()
return (
<div>
<DndContext
autoScroll={{
enabled: true,
threshold: {
x: 0, // No horizontal scroll
y: 0.2, // Allow vertical scroll at 20% from edge
},
}}
collisionDetection={closestInXAxis}
// https://github.com/clauderic/dnd-kit/issues/926#issuecomment-1640115665
id="dashboard-dnd-context"
onDragCancel={() => {
setActiveDragId(null)
}}
onDragEnd={(event) => {
if (!event.over) {
setActiveDragId(null)
return
}
const droppableId = event.over.id as string
const i = droppableId.lastIndexOf('-')
const slug = droppableId.slice(0, i)
const position = droppableId.slice(i + 1)
if (slug === event.active.id) {
return
}
const moveFromIndex = currentLayout?.findIndex(
(widget) => widget.item.id === event.active.id,
)
let moveToIndex = currentLayout?.findIndex((widget) => widget.item.id === slug)
if (moveFromIndex < moveToIndex) {
moveToIndex--
}
if (position === 'after') {
moveToIndex++
}
moveWidget({ moveFromIndex, moveToIndex })
setActiveDragId(null)
}}
onDragStart={(event) => {
setActiveDragId(event.active.id as string)
}}
sensors={sensors}
>
<div
className={`modular-dashboard ${isEditing ? 'editing' : ''}`}
style={{
display: 'flex',
flexWrap: 'wrap',
// Don't add gap here! We need to use padding on the widgets instead
// to make sure all rows have the same width always.
}}
>
{currentLayout?.length === 0 && (
<div className="modular-dashboard__empty">
<p>
There are no widgets on your dashboard. You can add them from the "Dashboard" menu
located in the top bar.
</p>
</div>
)}
{currentLayout?.map((widget, _index) => (
<React.Fragment key={widget.item.id}>
<DraggableItem
disabled={!isEditing}
id={widget.item.id}
style={{
width: `${WIDTH_TO_PERCENTAGE[widget.item.width]}%`,
padding: '6px',
}}
width={widget.item.width}
>
<div className={`widget-wrapper ${isEditing ? 'widget-wrapper--editing' : ''}`}>
<div aria-hidden={isEditing} className="widget-content" inert={isEditing}>
{widget.component}
</div>
{isEditing && (
<div
className="widget-wrapper__controls"
onPointerDown={(e) => e.stopPropagation()}
>
<WidgetEditControl
onSave={(data) => {
updateWidgetData(widget.item.id, data)
}}
widgetData={widget.item.data}
widgetID={widget.item.id}
/>
<WidgetWidthDropdown
currentWidth={widget.item.width}
maxWidth={widget.item.maxWidth}
minWidth={widget.item.minWidth}
onResize={(width) => resizeWidget(widget.item.id, width)}
/>
<button
className="widget-wrapper__delete-btn"
onClick={() => deleteWidget(widget.item.id)}
type="button"
>
<span className="sr-only">
{t('dashboard:deleteWidget', { id: widget.item.id })}
</span>
<XIcon />
</button>
</div>
)}
</div>
</DraggableItem>
</React.Fragment>
))}
<DragOverlay
className="drag-overlay"
dropAnimation={{
duration: 100,
}}
// Uses custom modifier that only applies for pointer, not keyboard navigation.
modifiers={[snapCenterToCursorOnlyForPointer]}
>
{activeDragId
? (() => {
const draggedWidget = currentLayout?.find(
(widget) => widget.item.id === activeDragId,
)
return draggedWidget ? (
<div
style={{
transform: 'scale(0.25)',
}}
>
<div
className={`widget-wrapper ${isEditing ? 'widget-wrapper--editing' : ''}`}
>
<div className="widget-content">{draggedWidget.component}</div>
</div>
</div>
) : null
})()
: null}
</DragOverlay>
</div>
</DndContext>
<DashboardStepNav
addWidget={addWidget}
cancel={cancel}
isEditing={isEditing}
resetLayout={resetLayout}
saveLayout={saveLayout}
setIsEditing={setIsEditing}
widgets={widgets}
/>
{cancelModal}
</div>
)
}
function WidgetWidthDropdown({
currentWidth,
maxWidth,
minWidth,
onResize,
}: {
currentWidth: WidgetWidth
maxWidth: WidgetWidth
minWidth: WidgetWidth
onResize: (width: WidgetWidth) => void
}) {
// Filter options based on minWidth and maxWidth
const validOptions = useMemo(() => {
const minPercentage = WIDTH_TO_PERCENTAGE[minWidth]
const maxPercentage = WIDTH_TO_PERCENTAGE[maxWidth]
return Object.entries(WIDTH_TO_PERCENTAGE)
.map(([key, value]) => ({
width: key as WidgetWidth,
percentage: value,
}))
.filter((option) => option.percentage >= minPercentage && option.percentage <= maxPercentage)
}, [minWidth, maxWidth])
const isDisabled = validOptions.length <= 1
if (isDisabled) {
return null
}
return (
<Popup
button={
<button
className="widget-wrapper__size-btn"
onPointerDown={(e) => e.stopPropagation()}
type="button"
>
<span className="widget-wrapper__size-btn-text">{currentWidth}</span>
<ChevronIcon className="widget-wrapper__size-btn-icon" />
</button>
}
buttonType="custom"
render={({ close }) => (
<PopupList.ButtonGroup>
{validOptions.map((option) => {
const isSelected = option.width === currentWidth
return (
<PopupList.Button
active={isSelected}
key={option.width}
onClick={() => {
onResize(option.width)
close()
}}
>
<span className="widget-wrapper__size-btn-label">{option.width}</span>
<span className="widget-wrapper__size-btn-percentage">
{option.percentage.toFixed(0)}%
</span>
</PopupList.Button>
)
})}
</PopupList.ButtonGroup>
)}
size="small"
verticalAlign="bottom"
/>
)
}
function DraggableItem(props: {
children: React.ReactNode
disabled?: boolean
id: string
style?: React.CSSProperties
width: WidgetWidth
}) {
const { attributes, isDragging, listeners, setNodeRef } = useDraggable({
id: props.id,
disabled: props.disabled,
})
const mergedStyles: React.CSSProperties = {
...props.style,
opacity: isDragging ? 0.3 : 1,
position: 'relative',
}
// Only apply draggable attributes and listeners when not disabled
// to prevent disabling interactive elements inside the widget
const draggableProps = props.disabled ? {} : { ...listeners, ...attributes }
return (
<div className="widget" data-slug={props.id} data-width={props.width} style={mergedStyles}>
<DroppableItem id={props.id} position="before" />
<div
className="draggable"
id={props.id}
ref={setNodeRef}
{...draggableProps}
style={{
width: '100%',
height: '100%',
}}
>
{props.children}
</div>
<DroppableItem id={props.id} position="after" />
</div>
)
}
function DroppableItem({ id, position }: { id: string; position: 'after' | 'before' }) {
const { setNodeRef, isOver } = useDroppable({ id: `${id}-${position}`, data: { position } })
return (
<div
className="droppable-widget"
data-testid={`${id}-${position}`}
ref={setNodeRef}
style={{
position: 'absolute',
left: position === 'before' ? -2 : 'auto',
right: position === 'after' ? -2 : 'auto',
top: 0,
bottom: 0,
borderRadius: '1000px',
width: '4px',
backgroundColor: isOver ? 'var(--theme-success-400)' : 'transparent',
marginBottom: '10px',
marginTop: '10px',
pointerEvents: 'none',
zIndex: 1000,
}}
/>
)
}