@kivacore/ui-pro-shared 0.0.9 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/package.json +187 -21
  2. package/src/.kiva/config.json +27 -0
  3. package/src/components/avatar-group.tsx +222 -0
  4. package/src/components/kiva/bulk-update-dialog/bulk-update-dialog.tsx +240 -0
  5. package/src/components/kiva/bulk-update-dialog/index.tsx +6 -0
  6. package/src/components/kiva/bulk-update-dialog/types.ts +33 -0
  7. package/src/components/kiva/calendar/animations.ts +40 -0
  8. package/src/components/kiva/calendar/calendar-body.tsx +106 -0
  9. package/src/components/kiva/calendar/calendar-sidebar.tsx +103 -0
  10. package/src/components/kiva/calendar/calendar.tsx +243 -0
  11. package/src/components/kiva/calendar/constants.ts +12 -0
  12. package/src/components/kiva/calendar/contexts/calendar-context.tsx +696 -0
  13. package/src/components/kiva/calendar/contexts/dnd-context.tsx +173 -0
  14. package/src/components/kiva/calendar/dialogs/add-edit-event-dialog.tsx +287 -0
  15. package/src/components/kiva/calendar/dialogs/delete-event-dialog.tsx +73 -0
  16. package/src/components/kiva/calendar/dialogs/event-details-dialog.tsx +163 -0
  17. package/src/components/kiva/calendar/dialogs/events-list-dialog.tsx +122 -0
  18. package/src/components/kiva/calendar/dnd/draggable-event.tsx +66 -0
  19. package/src/components/kiva/calendar/dnd/droppable-area.tsx +53 -0
  20. package/src/components/kiva/calendar/dnd/resizable-event.tsx +190 -0
  21. package/src/components/kiva/calendar/header/calendar-header.tsx +84 -0
  22. package/src/components/kiva/calendar/header/date-navigator.tsx +188 -0
  23. package/src/components/kiva/calendar/header/filter.tsx +80 -0
  24. package/src/components/kiva/calendar/header/today-button.tsx +49 -0
  25. package/src/components/kiva/calendar/header/user-select.tsx +111 -0
  26. package/src/components/kiva/calendar/header/view-tabs.tsx +144 -0
  27. package/src/components/kiva/calendar/helpers.ts +617 -0
  28. package/src/components/kiva/calendar/hooks/use-disclosure.ts +19 -0
  29. package/src/components/kiva/calendar/hooks/use-local-storage.ts +40 -0
  30. package/src/components/kiva/calendar/hooks/use-media-query.ts +25 -0
  31. package/src/components/kiva/calendar/hooks.ts +5 -0
  32. package/src/components/kiva/calendar/index.tsx +27 -0
  33. package/src/components/kiva/calendar/interfaces.ts +35 -0
  34. package/src/components/kiva/calendar/schemas.ts +20 -0
  35. package/src/components/kiva/calendar/settings/settings.tsx +116 -0
  36. package/src/components/kiva/calendar/skeletons/calendar-header-skeleton.tsx +27 -0
  37. package/src/components/kiva/calendar/skeletons/calendar-skeleton.tsx +19 -0
  38. package/src/components/kiva/calendar/skeletons/day-view-skeleton.tsx +40 -0
  39. package/src/components/kiva/calendar/skeletons/month-view-skeleton.tsx +36 -0
  40. package/src/components/kiva/calendar/skeletons/week-view-skeleton.tsx +48 -0
  41. package/src/components/kiva/calendar/skeletons/year-view-skeleton.tsx +42 -0
  42. package/src/components/kiva/calendar/types.ts +25 -0
  43. package/src/components/kiva/calendar/views/agenda-view/agenda-events.tsx +150 -0
  44. package/src/components/kiva/calendar/views/month-view/calendar-month-view.tsx +82 -0
  45. package/src/components/kiva/calendar/views/month-view/day-cell.tsx +218 -0
  46. package/src/components/kiva/calendar/views/month-view/event-bullet.tsx +46 -0
  47. package/src/components/kiva/calendar/views/month-view/month-event-badge.tsx +154 -0
  48. package/src/components/kiva/calendar/views/timeline/calendar-timeline-view.tsx +100 -0
  49. package/src/components/kiva/calendar/views/timeline/lib/timeline-layout.ts +130 -0
  50. package/src/components/kiva/calendar/views/timeline/lib/timeline-model.ts +504 -0
  51. package/src/components/kiva/calendar/views/timeline/timeline-event-bar.tsx +68 -0
  52. package/src/components/kiva/calendar/views/timeline/timeline-grid-header.tsx +125 -0
  53. package/src/components/kiva/calendar/views/timeline/timeline-row.tsx +159 -0
  54. package/src/components/kiva/calendar/views/timeline/timeline-section.tsx +61 -0
  55. package/src/components/kiva/calendar/views/timeline/use-timeline-sections.ts +278 -0
  56. package/src/components/kiva/calendar/views/week-and-day-view/calendar-day-view.tsx +277 -0
  57. package/src/components/kiva/calendar/views/week-and-day-view/calendar-time-line.tsx +47 -0
  58. package/src/components/kiva/calendar/views/week-and-day-view/calendar-week-view.tsx +229 -0
  59. package/src/components/kiva/calendar/views/week-and-day-view/day-view-multi-day-events-row.tsx +79 -0
  60. package/src/components/kiva/calendar/views/week-and-day-view/event-block.tsx +116 -0
  61. package/src/components/kiva/calendar/views/week-and-day-view/render-grouped-events.tsx +54 -0
  62. package/src/components/kiva/calendar/views/week-and-day-view/week-view-multi-day-events-row.tsx +159 -0
  63. package/src/components/kiva/calendar/views/year-view/calendar-year-view.tsx +159 -0
  64. package/src/components/kiva/data-table-side-filters/components/side-filter-active-chips.tsx +137 -0
  65. package/src/components/kiva/data-table-side-filters/components/side-filter-clear.tsx +34 -0
  66. package/src/components/kiva/data-table-side-filters/components/side-filter-search.tsx +67 -0
  67. package/src/components/kiva/data-table-side-filters/components/side-filter-section.tsx +77 -0
  68. package/src/components/kiva/data-table-side-filters/controllers/side-filter-async-options.tsx +354 -0
  69. package/src/components/kiva/data-table-side-filters/controllers/side-filter-boolean.tsx +66 -0
  70. package/src/components/kiva/data-table-side-filters/controllers/side-filter-checkbox-list.tsx +137 -0
  71. package/src/components/kiva/data-table-side-filters/controllers/side-filter-date-range.tsx +204 -0
  72. package/src/components/kiva/data-table-side-filters/controllers/side-filter-numeric-range.tsx +162 -0
  73. package/src/components/kiva/data-table-side-filters/controllers/side-filter-options-dropdown.tsx +191 -0
  74. package/src/components/kiva/data-table-side-filters/controllers/side-filter-text.tsx +53 -0
  75. package/src/components/kiva/data-table-side-filters/core/query-translator.ts +367 -0
  76. package/src/components/kiva/data-table-side-filters/core/types.ts +81 -0
  77. package/src/components/kiva/data-table-side-filters/data-table-side-filters.tsx +483 -0
  78. package/src/components/kiva/data-table-side-filters/hooks/use-data-table-side-filters.tsx +160 -0
  79. package/src/components/kiva/data-table-side-filters/hooks/use-show-more.tsx +62 -0
  80. package/src/components/kiva/data-table-side-filters/index.tsx +44 -0
  81. package/src/components/kiva/data-table-side-filters/lib/render-mode.ts +62 -0
  82. package/src/components/kiva/date-time-picker.tsx +163 -0
  83. package/src/components/kiva/editable-record-detail.tsx +809 -0
  84. package/src/components/kiva/enum-option-editor/enum-option-editor.tsx +891 -0
  85. package/src/components/kiva/enum-option-editor/index.tsx +26 -0
  86. package/src/components/kiva/enum-option-editor/kiva-enum-option-editor.tsx +320 -0
  87. package/src/components/kiva/enum-option-editor/types.ts +90 -0
  88. package/src/components/kiva/kanban.tsx +1376 -0
  89. package/src/components/kiva/kiva-data-export-menu.tsx +543 -0
  90. package/src/components/kiva/map.tsx +1631 -0
  91. package/src/components/kiva/pivot-calendar/components/group-sidebar.tsx +95 -0
  92. package/src/components/kiva/pivot-calendar/components/pivot-calendar-toolbar.tsx +113 -0
  93. package/src/components/kiva/pivot-calendar/components/stats-cell.tsx +126 -0
  94. package/src/components/kiva/pivot-calendar/helpers.ts +374 -0
  95. package/src/components/kiva/pivot-calendar/hooks/use-pivot-calendar.ts +222 -0
  96. package/src/components/kiva/pivot-calendar/index.tsx +32 -0
  97. package/src/components/kiva/pivot-calendar/pivot-calendar.tsx +107 -0
  98. package/src/components/kiva/pivot-calendar/types.ts +154 -0
  99. package/src/components/kiva/pivot-calendar/views/days-of-month-view.tsx +37 -0
  100. package/src/components/kiva/pivot-calendar/views/days-of-week-view.tsx +37 -0
  101. package/src/components/kiva/pivot-calendar/views/month-calendar-view.tsx +154 -0
  102. package/src/components/kiva/pivot-calendar/views/months-of-year-view.tsx +40 -0
  103. package/src/components/kiva/pivot-calendar/views/pivot-grid-view.tsx +251 -0
  104. package/src/components/kiva/pivot-filter/index.tsx +9 -0
  105. package/src/components/kiva/pivot-filter/pivot-filter-item.tsx +159 -0
  106. package/src/components/kiva/pivot-filter/pivot-filter-settings.tsx +161 -0
  107. package/src/components/kiva/pivot-filter/pivot-filter.tsx +187 -0
  108. package/src/components/kiva/pivot-filter/types.ts +82 -0
  109. package/src/components/kiva/pivot-grid/hooks/use-pivot-grid.ts +683 -0
  110. package/src/components/kiva/pivot-grid/index.tsx +23 -0
  111. package/src/components/kiva/pivot-grid/lib/pivot-grid-color-rules.ts +98 -0
  112. package/src/components/kiva/pivot-grid/lib/pivot-grid-export.ts +265 -0
  113. package/src/components/kiva/pivot-grid/lib/pivot-grid-model.ts +1110 -0
  114. package/src/components/kiva/pivot-grid/pivot-grid-demo-data.ts +200 -0
  115. package/src/components/kiva/pivot-grid/pivot-grid-export-menu.tsx +66 -0
  116. package/src/components/kiva/pivot-grid/pivot-grid-toolbar.tsx +85 -0
  117. package/src/components/kiva/pivot-grid/pivot-grid.tsx +601 -0
  118. package/src/components/kiva/pivot-grid/types.ts +195 -0
  119. package/src/components/kiva/place-autocomplete.tsx +484 -0
  120. package/src/components/kiva/record-delete-confirm-dialog/index.tsx +10 -0
  121. package/src/components/kiva/record-delete-confirm-dialog/record-delete-confirm-dialog.tsx +266 -0
  122. package/src/components/kiva/record-delete-confirm-dialog/types.ts +25 -0
  123. package/src/hooks/kiva/_internal/autonumber-setting.ts +112 -0
  124. package/src/hooks/kiva/_internal/form-layout-to-view.ts +665 -0
  125. package/src/hooks/kiva/_internal/kiva-hook-utils.ts +143 -0
  126. package/src/hooks/kiva/_internal/use-computed-field-evaluator.tsx +310 -0
  127. package/src/hooks/kiva/_internal/use-field-actions-evaluator.tsx +398 -0
  128. package/src/hooks/kiva/_internal/use-form-actions-evaluator.tsx +363 -0
  129. package/src/hooks/kiva/_internal/use-form-view-engine.tsx +2200 -0
  130. package/src/hooks/kiva/_internal/use-kiva-form-view-fields.tsx +190 -0
  131. package/src/hooks/kiva/_internal/use-kiva-kanban-cards.tsx +311 -0
  132. package/src/hooks/kiva/use-data-export.tsx +265 -0
  133. package/src/hooks/kiva/use-kiva-calendar.tsx +992 -0
  134. package/src/hooks/kiva/use-kiva-data-export.tsx +173 -0
  135. package/src/hooks/kiva/use-kiva-data-grid.tsx +4750 -0
  136. package/src/hooks/kiva/use-kiva-data-view-select.tsx +1273 -0
  137. package/src/hooks/kiva/use-kiva-form-view.tsx +1581 -0
  138. package/src/hooks/kiva/use-kiva-inventory.tsx +184 -0
  139. package/src/hooks/kiva/use-kiva-kanban.tsx +1750 -0
  140. package/src/hooks/kiva/use-kiva-map-view.tsx +2255 -0
  141. package/src/hooks/kiva/use-kiva-pivot-calendar.tsx +759 -0
  142. package/src/hooks/kiva/use-kiva-pivot-filter.tsx +1523 -0
  143. package/src/hooks/kiva/use-kiva-pivot-grid.tsx +543 -0
  144. package/src/hooks/kiva/use-kiva-tenant.tsx +313 -0
  145. package/src/hooks/kiva/use-parameter-persistence.ts +191 -0
@@ -0,0 +1,1376 @@
1
+ 'use client';
2
+
3
+ // @ts-nocheck
4
+ /* eslint-disable */
5
+ import {
6
+ type CSSProperties,
7
+ type ComponentProps,
8
+ type MouseEvent,
9
+ type ReactNode,
10
+ createContext,
11
+ useCallback,
12
+ use,
13
+ useId,
14
+ useLayoutEffect,
15
+ useMemo,
16
+ useRef,
17
+ useState
18
+ } from 'react';
19
+
20
+ import {
21
+ type AnimateLayoutChanges,
22
+ type SortableContextProps,
23
+ SortableContext,
24
+ arrayMove,
25
+ defaultAnimateLayoutChanges,
26
+ horizontalListSortingStrategy,
27
+ useSortable,
28
+ verticalListSortingStrategy
29
+
30
+ } from '@dnd-kit/sortable';
31
+
32
+ import * as ReactDOM from 'react-dom';
33
+ import {
34
+ DndContext,
35
+ DragOverlay,
36
+ KeyboardCode,
37
+ KeyboardSensor,
38
+ MeasuringStrategy,
39
+ MouseSensor,
40
+ TouchSensor,
41
+ closestCenter,
42
+ closestCorners,
43
+ defaultDropAnimationSideEffects,
44
+ getFirstCollision,
45
+ pointerWithin,
46
+ rectIntersection,
47
+ useDroppable,
48
+ useSensor,
49
+ useSensors,
50
+ type Announcements,
51
+ type CollisionDetection,
52
+ type DndContextProps,
53
+ type DragCancelEvent,
54
+ type DragEndEvent,
55
+ type DragOverEvent,
56
+ type DragStartEvent,
57
+ type DraggableAttributes,
58
+ type DraggableSyntheticListeners,
59
+ type DropAnimation,
60
+ type DroppableContainer,
61
+ type KeyboardCoordinateGetter,
62
+ type UniqueIdentifier
63
+
64
+ } from '@dnd-kit/core';
65
+ import { CSS } from '@dnd-kit/utilities';
66
+ import { Slot } from '@radix-ui/react-slot';
67
+
68
+ import { useComposedRefs } from '@kivacore/ui-pro-shared/lib/compose-refs';
69
+ import { cn } from '@kivacore/ui-pro-shared/lib/utils';
70
+
71
+ const directions: Array<string> = [
72
+ KeyboardCode.Down,
73
+ KeyboardCode.Right,
74
+ KeyboardCode.Up,
75
+ KeyboardCode.Left
76
+ ];
77
+
78
+ const coordinateGetter: KeyboardCoordinateGetter = (event, { context }) => {
79
+ const {
80
+ active, droppableRects, droppableContainers, collisionRect
81
+ }
82
+ = context;
83
+
84
+ if (directions.includes(event.code)) {
85
+ event.preventDefault();
86
+
87
+ if (!active || !collisionRect) return;
88
+
89
+ const filteredContainers: Array<DroppableContainer> = [];
90
+
91
+ for (const entry of droppableContainers.getEnabled()) {
92
+ if (!entry || entry?.disabled) return;
93
+
94
+ const rect = droppableRects.get(entry.id);
95
+
96
+ if (!rect) return;
97
+
98
+ const data = entry.data.current;
99
+
100
+ if (data) {
101
+ const { type, children } = data;
102
+
103
+ if (type === 'container' && children?.length > 0) {
104
+ if (active.data.current?.type !== 'container') {
105
+ return;
106
+ }
107
+ }
108
+ }
109
+
110
+ switch (event.code) {
111
+ case KeyboardCode.Down:
112
+ if (collisionRect.top < rect.top) {
113
+ filteredContainers.push(entry);
114
+ }
115
+ break;
116
+
117
+ case KeyboardCode.Up:
118
+ if (collisionRect.top > rect.top) {
119
+ filteredContainers.push(entry);
120
+ }
121
+ break;
122
+
123
+ case KeyboardCode.Left:
124
+ if (collisionRect.left >= rect.left + rect.width) {
125
+ filteredContainers.push(entry);
126
+ }
127
+ break;
128
+
129
+ case KeyboardCode.Right:
130
+ if (collisionRect.left + collisionRect.width <= rect.left) {
131
+ filteredContainers.push(entry);
132
+ }
133
+ break;
134
+ }
135
+ }
136
+
137
+ const collisions = closestCorners({
138
+ active,
139
+ collisionRect,
140
+ droppableRects,
141
+ droppableContainers: filteredContainers,
142
+ pointerCoordinates: null
143
+ });
144
+ const closestId = getFirstCollision(collisions, 'id');
145
+
146
+ if (closestId != null) {
147
+ const newDroppable = droppableContainers.get(closestId);
148
+ const newNode = newDroppable?.node.current;
149
+ const newRect = newDroppable?.rect.current;
150
+
151
+ if (newNode && newRect) {
152
+ if (newDroppable.id === 'placeholder') {
153
+ return {
154
+ x: newRect.left + (newRect.width - collisionRect.width) / 2,
155
+ y: newRect.top + (newRect.height - collisionRect.height) / 2
156
+ };
157
+ }
158
+
159
+ if (newDroppable.data.current?.type === 'container') {
160
+ return {
161
+ x: newRect.left + 20,
162
+ y: newRect.top + 74
163
+ };
164
+ }
165
+
166
+ return {
167
+ x: newRect.left,
168
+ y: newRect.top
169
+ };
170
+ }
171
+ }
172
+ }
173
+
174
+ return undefined;
175
+ };
176
+
177
+ const ROOT_NAME = 'Kanban';
178
+ const BOARD_NAME = 'KanbanBoard';
179
+ const COLUMN_NAME = 'KanbanColumn';
180
+ const COLUMN_HANDLE_NAME = 'KanbanColumnHandle';
181
+ const ITEM_NAME = 'KanbanItem';
182
+ const ITEM_HANDLE_NAME = 'KanbanItemHandle';
183
+ const OVERLAY_NAME = 'KanbanOverlay';
184
+ const FINAL_ZONE_NAME = 'KanbanFinalZone';
185
+ const FINAL_COLUMN_NAME = 'KanbanFinalColumn';
186
+
187
+ interface KanbanContextValue<T> {
188
+ id: string;
189
+ items: Record<UniqueIdentifier, Array<T>>;
190
+ modifiers: DndContextProps['modifiers'];
191
+ strategy: SortableContextProps['strategy'];
192
+ orientation: 'horizontal' | 'vertical';
193
+ activeId: UniqueIdentifier | null;
194
+ setActiveId: (id: UniqueIdentifier | null) => void;
195
+ getItemValue: (item: T) => UniqueIdentifier;
196
+ flatCursor: boolean;
197
+ finalColumnIds: Set<UniqueIdentifier>;
198
+ hoveredFinalColumn: UniqueIdentifier | null;
199
+ onItemClick?: (value: UniqueIdentifier) => void;
200
+ }
201
+
202
+ const KanbanContext = createContext<KanbanContextValue<unknown> | null>(null);
203
+
204
+ function useKanbanContext(consumerName: string) {
205
+ const context = use(KanbanContext);
206
+
207
+ if (!context) {
208
+ throw new Error(`\`${consumerName}\` must be used within \`${ROOT_NAME}\``);
209
+ }
210
+
211
+ return context;
212
+ }
213
+
214
+ interface GetItemValue<T> {
215
+ getItemValue: (item: T) => UniqueIdentifier;
216
+ }
217
+
218
+ type KanbanProps<T> = Omit<DndContextProps, 'collisionDetection'>
219
+ & (T extends object ? GetItemValue<T> : Partial<GetItemValue<T>>) & {
220
+ value: Record<UniqueIdentifier, Array<T>>;
221
+ onValueChange?: (columns: Record<UniqueIdentifier, Array<T>>) => void;
222
+ onMove?: (
223
+ event: DragEndEvent & { activeIndex: number; overIndex: number }
224
+ ) => void;
225
+ onFinalDrop?: (item: T, finalColumnId: UniqueIdentifier) => void;
226
+ onItemClick?: (value: UniqueIdentifier) => void;
227
+ finalColumns?: Array<UniqueIdentifier>;
228
+ strategy?: SortableContextProps['strategy'];
229
+ orientation?: 'horizontal' | 'vertical';
230
+ flatCursor?: boolean;
231
+ };
232
+
233
+ function Kanban<T>(props: KanbanProps<T>) {
234
+ const {
235
+ value,
236
+ onValueChange,
237
+ onFinalDrop,
238
+ onItemClick,
239
+ finalColumns,
240
+ modifiers,
241
+ strategy = verticalListSortingStrategy,
242
+ orientation = 'horizontal',
243
+ onMove,
244
+ getItemValue: getItemValueProp,
245
+ accessibility,
246
+ flatCursor = false,
247
+ ...kanbanProps
248
+ } = props;
249
+
250
+ const {
251
+ onDragStart: onDragStartProp,
252
+ onDragOver: onDragOverProp,
253
+ onDragEnd: onDragEndProp,
254
+ onDragCancel: onDragCancelProp,
255
+ ...restKanbanProps
256
+ } = kanbanProps;
257
+
258
+ const id = useId();
259
+ const [activeId, setActiveId] = useState<UniqueIdentifier | null>(null);
260
+ const [hoveredFinalColumn, setHoveredFinalColumn]
261
+ = useState<UniqueIdentifier | null>(null);
262
+ const lastOverIdRef = useRef<UniqueIdentifier | null>(null);
263
+ const hasMovedRef = useRef(false);
264
+ const sensors = useSensors(
265
+ useSensor(MouseSensor, {
266
+ activationConstraint: { distance: 5 }
267
+ }),
268
+ useSensor(TouchSensor, {
269
+ activationConstraint: { delay: 200, tolerance: 5 }
270
+ }),
271
+ useSensor(KeyboardSensor, {
272
+ coordinateGetter
273
+ })
274
+ );
275
+
276
+ const finalColumnIds = useMemo(
277
+ () => new Set<UniqueIdentifier>(finalColumns ?? []),
278
+ [finalColumns]
279
+ );
280
+
281
+ const getItemValue = useCallback(
282
+ (item: T): UniqueIdentifier => {
283
+ if (typeof item === 'object' && !getItemValueProp) {
284
+ throw new Error(
285
+ '`getItemValue` is required when using array of objects'
286
+ );
287
+ }
288
+
289
+ return getItemValueProp ? getItemValueProp(item) : (item as UniqueIdentifier);
290
+ },
291
+ [getItemValueProp]
292
+ );
293
+
294
+ const getColumn = useCallback(
295
+ (id: UniqueIdentifier) => {
296
+ if (id in value) return id;
297
+
298
+ for (const [columnId, items] of Object.entries(value)) {
299
+ if (items.some(item => getItemValue(item) === id)) {
300
+ return columnId;
301
+ }
302
+ }
303
+
304
+ return null;
305
+ },
306
+ [value, getItemValue]
307
+ );
308
+
309
+ const collisionDetection: CollisionDetection = useCallback(
310
+ (args) => {
311
+ if (activeId && activeId in value) {
312
+ return closestCenter({
313
+ ...args,
314
+ droppableContainers: args.droppableContainers.filter(
315
+ container => container.id in value
316
+ )
317
+ });
318
+ }
319
+
320
+ const pointerIntersections = pointerWithin(args);
321
+ const intersections
322
+ = pointerIntersections.length > 0 ? pointerIntersections : rectIntersection(args);
323
+ let overId = getFirstCollision(intersections, 'id');
324
+
325
+ if (!overId) {
326
+ if (hasMovedRef.current) {
327
+ lastOverIdRef.current = activeId;
328
+ }
329
+
330
+ return lastOverIdRef.current ? [{ id: lastOverIdRef.current }] : [];
331
+ }
332
+
333
+ if (overId in value) {
334
+ const containerItems = value[overId];
335
+
336
+ if (containerItems && containerItems.length > 0) {
337
+ const closestItem = closestCenter({
338
+ ...args,
339
+ droppableContainers: args.droppableContainers.filter(
340
+ container => container.id !== overId
341
+ && containerItems.some(
342
+ item => getItemValue(item) === container.id
343
+ )
344
+ )
345
+ });
346
+
347
+ if (closestItem.length > 0) {
348
+ overId = closestItem[0]?.id ?? overId;
349
+ }
350
+ }
351
+ }
352
+
353
+ lastOverIdRef.current = overId;
354
+
355
+ return [{ id: overId }];
356
+ },
357
+ [activeId, value, getItemValue]
358
+ );
359
+
360
+ const onDragStart = useCallback(
361
+ (event: DragStartEvent) => {
362
+ onDragStartProp?.(event);
363
+
364
+ if (event.activatorEvent.defaultPrevented) return;
365
+ setActiveId(event.active.id);
366
+ },
367
+ [onDragStartProp]
368
+ );
369
+
370
+ const onDragOver = useCallback(
371
+ (event: DragOverEvent) => {
372
+ onDragOverProp?.(event);
373
+
374
+ if (event.activatorEvent.defaultPrevented) return;
375
+
376
+ const { active, over } = event;
377
+
378
+ if (!over) {
379
+ setHoveredFinalColumn(null);
380
+
381
+ return;
382
+ }
383
+
384
+ if (finalColumnIds.has(over.id)) {
385
+ setHoveredFinalColumn(over.id);
386
+
387
+ return;
388
+ }
389
+ setHoveredFinalColumn(null);
390
+
391
+ const activeColumn = getColumn(active.id);
392
+ const overColumn = getColumn(over.id);
393
+
394
+ if (!activeColumn || !overColumn) return;
395
+
396
+ if (activeColumn === overColumn) {
397
+ const items = value[activeColumn];
398
+
399
+ if (!items) return;
400
+
401
+ const activeIndex = items.findIndex(
402
+ item => getItemValue(item) === active.id
403
+ );
404
+ const overIndex = items.findIndex(
405
+ item => getItemValue(item) === over.id
406
+ );
407
+
408
+ if (activeIndex !== overIndex) {
409
+ const newColumns = { ...value };
410
+
411
+ newColumns[activeColumn] = arrayMove(items, activeIndex, overIndex);
412
+ onValueChange?.(newColumns);
413
+ }
414
+ } else {
415
+ const activeItems = value[activeColumn];
416
+ const overItems = value[overColumn];
417
+
418
+ if (!activeItems || !overItems) return;
419
+
420
+ const activeIndex = activeItems.findIndex(
421
+ item => getItemValue(item) === active.id
422
+ );
423
+
424
+ if (activeIndex === -1) return;
425
+
426
+ const activeItem = activeItems[activeIndex];
427
+
428
+ if (!activeItem) return;
429
+
430
+ const updatedItems = {
431
+ ...value,
432
+ [activeColumn]: activeItems.filter(
433
+ item => getItemValue(item) !== active.id
434
+ ),
435
+ [overColumn]: [...overItems, activeItem]
436
+ };
437
+
438
+ onValueChange?.(updatedItems);
439
+ hasMovedRef.current = true;
440
+ }
441
+ },
442
+ [
443
+ value,
444
+ getColumn,
445
+ getItemValue,
446
+ onValueChange,
447
+ finalColumnIds,
448
+ onDragOverProp
449
+ ]
450
+ );
451
+
452
+ const onDragEnd = useCallback(
453
+ (event: DragEndEvent) => {
454
+ onDragEndProp?.(event);
455
+
456
+ if (event.activatorEvent.defaultPrevented) return;
457
+
458
+ const { active, over } = event;
459
+
460
+ setHoveredFinalColumn(null);
461
+
462
+ if (!over) {
463
+ setActiveId(null);
464
+
465
+ return;
466
+ }
467
+
468
+ if (finalColumnIds.has(over.id)) {
469
+ const activeColumn = getColumn(active.id);
470
+
471
+ if (activeColumn) {
472
+ const items = value[activeColumn];
473
+ const activeItem = items?.find(
474
+ item => getItemValue(item) === active.id
475
+ );
476
+
477
+ if (activeItem && items) {
478
+ const newColumns = { ...value };
479
+
480
+ newColumns[activeColumn] = items.filter(
481
+ item => getItemValue(item) !== active.id
482
+ );
483
+ onValueChange?.(newColumns);
484
+ onFinalDrop?.(activeItem, over.id);
485
+ }
486
+ }
487
+ setActiveId(null);
488
+ hasMovedRef.current = false;
489
+
490
+ return;
491
+ }
492
+
493
+ if (active.id in value && over.id in value) {
494
+ const activeIndex = Object.keys(value).indexOf(active.id as string);
495
+ const overIndex = Object.keys(value).indexOf(over.id as string);
496
+
497
+ if (activeIndex !== overIndex) {
498
+ const orderedColumns = Object.keys(value);
499
+ const newOrder = arrayMove(orderedColumns, activeIndex, overIndex);
500
+
501
+ const newColumns: Record<UniqueIdentifier, Array<T>> = {};
502
+
503
+ for (const key of newOrder) {
504
+ const items = value[key];
505
+
506
+ if (items) {
507
+ newColumns[key] = items;
508
+ }
509
+ }
510
+
511
+ if (onMove) {
512
+ onMove({ ...event, activeIndex, overIndex });
513
+ } else {
514
+ onValueChange?.(newColumns);
515
+ }
516
+ }
517
+ } else {
518
+ const activeColumn = getColumn(active.id);
519
+ const overColumn = getColumn(over.id);
520
+
521
+ if (!activeColumn || !overColumn) {
522
+ setActiveId(null);
523
+
524
+ return;
525
+ }
526
+
527
+ if (activeColumn === overColumn) {
528
+ const items = value[activeColumn];
529
+
530
+ if (!items) {
531
+ setActiveId(null);
532
+
533
+ return;
534
+ }
535
+
536
+ const activeIndex = items.findIndex(
537
+ item => getItemValue(item) === active.id
538
+ );
539
+ const overIndex = items.findIndex(
540
+ item => getItemValue(item) === over.id
541
+ );
542
+
543
+ if (activeIndex !== overIndex) {
544
+ const newColumns = { ...value };
545
+
546
+ newColumns[activeColumn] = arrayMove(items, activeIndex, overIndex);
547
+ if (onMove) {
548
+ onMove({
549
+ ...event,
550
+ activeIndex,
551
+ overIndex
552
+ });
553
+ } else {
554
+ onValueChange?.(newColumns);
555
+ }
556
+ }
557
+ }
558
+ }
559
+
560
+ setActiveId(null);
561
+ hasMovedRef.current = false;
562
+ },
563
+ [
564
+ value,
565
+ getColumn,
566
+ getItemValue,
567
+ onValueChange,
568
+ onMove,
569
+ onFinalDrop,
570
+ finalColumnIds,
571
+ onDragEndProp
572
+ ]
573
+ );
574
+
575
+ const onDragCancel = useCallback(
576
+ (event: DragCancelEvent) => {
577
+ onDragCancelProp?.(event);
578
+
579
+ if (event.activatorEvent.defaultPrevented) return;
580
+
581
+ setActiveId(null);
582
+ setHoveredFinalColumn(null);
583
+ hasMovedRef.current = false;
584
+ },
585
+ [onDragCancelProp]
586
+ );
587
+
588
+ const announcements: Announcements = useMemo(
589
+ () => ({
590
+ onDragStart({ active }) {
591
+ const isColumn = active.id in value;
592
+ const itemType = isColumn ? 'column' : 'item';
593
+ const position = isColumn ? Object.keys(value).indexOf(active.id as string) + 1 : (() => {
594
+ const column = getColumn(active.id);
595
+
596
+ if (!column || !value[column]) return 1;
597
+
598
+ return (
599
+ value[column].findIndex(
600
+ item => getItemValue(item) === active.id
601
+ ) + 1
602
+ );
603
+ })();
604
+ const total = isColumn ? Object.keys(value).length : (() => {
605
+ const column = getColumn(active.id);
606
+
607
+ return column ? (value[column]?.length ?? 0) : 0;
608
+ })();
609
+
610
+ return `Picked up ${itemType} at position ${position} of ${total}`;
611
+ },
612
+ onDragOver({ active, over }) {
613
+ if (!over) return;
614
+
615
+ if (finalColumnIds.has(over.id)) {
616
+ return `Item is over final drop zone`;
617
+ }
618
+
619
+ const isColumn = active.id in value;
620
+ const itemType = isColumn ? 'column' : 'item';
621
+ const position = isColumn ? Object.keys(value).indexOf(over.id as string) + 1 : (() => {
622
+ const column = getColumn(over.id);
623
+
624
+ if (!column || !value[column]) return 1;
625
+
626
+ return (
627
+ value[column].findIndex(
628
+ item => getItemValue(item) === over.id
629
+ ) + 1
630
+ );
631
+ })();
632
+ const total = isColumn ? Object.keys(value).length : (() => {
633
+ const column = getColumn(over.id);
634
+
635
+ return column ? (value[column]?.length ?? 0) : 0;
636
+ })();
637
+
638
+ const overColumn = getColumn(over.id);
639
+ const activeColumn = getColumn(active.id);
640
+
641
+ if (isColumn) {
642
+ return `${itemType} is now at position ${position} of ${total}`;
643
+ }
644
+
645
+ if (activeColumn !== overColumn) {
646
+ return `${itemType} is now at position ${position} of ${total} in ${overColumn}`;
647
+ }
648
+
649
+ return `${itemType} is now at position ${position} of ${total}`;
650
+ },
651
+ onDragEnd({ active, over }) {
652
+ if (!over) return;
653
+
654
+ if (finalColumnIds.has(over.id)) {
655
+ return `Item was dropped into final status`;
656
+ }
657
+
658
+ const isColumn = active.id in value;
659
+ const itemType = isColumn ? 'column' : 'item';
660
+ const position = isColumn ? Object.keys(value).indexOf(over.id as string) + 1 : (() => {
661
+ const column = getColumn(over.id);
662
+
663
+ if (!column || !value[column]) return 1;
664
+
665
+ return (
666
+ value[column].findIndex(
667
+ item => getItemValue(item) === over.id
668
+ ) + 1
669
+ );
670
+ })();
671
+ const total = isColumn ? Object.keys(value).length : (() => {
672
+ const column = getColumn(over.id);
673
+
674
+ return column ? (value[column]?.length ?? 0) : 0;
675
+ })();
676
+
677
+ const overColumn = getColumn(over.id);
678
+ const activeColumn = getColumn(active.id);
679
+
680
+ if (isColumn) {
681
+ return `${itemType} was dropped at position ${position} of ${total}`;
682
+ }
683
+
684
+ if (activeColumn !== overColumn) {
685
+ return `${itemType} was dropped at position ${position} of ${total} in ${overColumn}`;
686
+ }
687
+
688
+ return `${itemType} was dropped at position ${position} of ${total}`;
689
+ },
690
+ onDragCancel({ active }) {
691
+ const isColumn = active.id in value;
692
+ const itemType = isColumn ? 'column' : 'item';
693
+
694
+ return `Dragging was cancelled. ${itemType} was dropped.`;
695
+ }
696
+ }),
697
+ [
698
+ value,
699
+ getColumn,
700
+ getItemValue,
701
+ finalColumnIds
702
+ ]
703
+ );
704
+
705
+ const contextValue = useMemo<KanbanContextValue<T>>(
706
+ () => ({
707
+ id,
708
+ items: value,
709
+ modifiers,
710
+ strategy,
711
+ orientation,
712
+ activeId,
713
+ setActiveId,
714
+ getItemValue,
715
+ flatCursor,
716
+ finalColumnIds,
717
+ hoveredFinalColumn,
718
+ onItemClick
719
+ }),
720
+ [
721
+ id,
722
+ value,
723
+ activeId,
724
+ modifiers,
725
+ strategy,
726
+ orientation,
727
+ getItemValue,
728
+ flatCursor,
729
+ finalColumnIds,
730
+ hoveredFinalColumn,
731
+ onItemClick
732
+ ]
733
+ );
734
+
735
+ return (
736
+ <KanbanContext
737
+ value={contextValue as KanbanContextValue<unknown>}>
738
+ <DndContext
739
+ collisionDetection={collisionDetection}
740
+ modifiers={modifiers}
741
+ sensors={sensors}
742
+ {...restKanbanProps}
743
+ id={id}
744
+ measuring={{
745
+ droppable: {
746
+ strategy: MeasuringStrategy.Always
747
+ }
748
+ }}
749
+ onDragStart={onDragStart}
750
+ onDragOver={onDragOver}
751
+ onDragEnd={onDragEnd}
752
+ onDragCancel={onDragCancel}
753
+ accessibility={{
754
+ announcements,
755
+ screenReaderInstructions: {
756
+ draggable: `
757
+ To pick up a kanban item or column, press space or enter.
758
+ While dragging, use the arrow keys to move the item.
759
+ Press space or enter again to drop the item in its new position, or press escape to cancel.
760
+ `
761
+ },
762
+ ...accessibility
763
+ }} />
764
+ </KanbanContext>
765
+ );
766
+ }
767
+
768
+ const KanbanBoardContext = createContext<boolean>(false);
769
+
770
+ interface KanbanBoardProps extends ComponentProps<'div'> {
771
+ children: ReactNode;
772
+ asChild?: boolean;
773
+ }
774
+
775
+ function KanbanBoard(props: KanbanBoardProps) {
776
+ const {
777
+ asChild, className, ref, ...boardProps
778
+ } = props;
779
+
780
+ const context = useKanbanContext(BOARD_NAME);
781
+
782
+ const columns = useMemo(() => {
783
+ return Object.keys(context.items);
784
+ }, [context.items]);
785
+
786
+ const BoardPrimitive = asChild ? Slot : 'div';
787
+
788
+ return (
789
+ <KanbanBoardContext value>
790
+ <SortableContext
791
+ items={columns}
792
+ strategy={context.orientation === 'horizontal' ? horizontalListSortingStrategy : verticalListSortingStrategy}>
793
+ <BoardPrimitive
794
+ aria-orientation={context.orientation}
795
+ data-orientation={context.orientation}
796
+ data-slot="kanban-board"
797
+ {...boardProps}
798
+ ref={ref}
799
+ className={cn(
800
+ 'flex size-full gap-4',
801
+ context.orientation === 'horizontal' ? 'flex-row' : 'flex-col',
802
+ className
803
+ )} />
804
+ </SortableContext>
805
+ </KanbanBoardContext>
806
+ );
807
+ }
808
+
809
+ interface KanbanColumnContextValue {
810
+ id: string;
811
+ attributes: DraggableAttributes;
812
+ listeners: DraggableSyntheticListeners | undefined;
813
+ setActivatorNodeRef: (node: HTMLElement | null) => void;
814
+ isDragging?: boolean;
815
+ disabled?: boolean;
816
+ }
817
+
818
+ const KanbanColumnContext = createContext<KanbanColumnContextValue | null>(null);
819
+
820
+ function useKanbanColumnContext(consumerName: string) {
821
+ const context = use(KanbanColumnContext);
822
+
823
+ if (!context) {
824
+ throw new Error(
825
+ `\`${consumerName}\` must be used within \`${COLUMN_NAME}\``
826
+ );
827
+ }
828
+
829
+ return context;
830
+ }
831
+
832
+ const animateLayoutChanges: AnimateLayoutChanges = args => defaultAnimateLayoutChanges({ ...args, wasDragging: true });
833
+
834
+ interface KanbanColumnProps extends ComponentProps<'div'> {
835
+ value: UniqueIdentifier;
836
+ children: ReactNode;
837
+ asChild?: boolean;
838
+ asHandle?: boolean;
839
+ disabled?: boolean;
840
+ }
841
+
842
+ function KanbanColumn(props: KanbanColumnProps) {
843
+ const {
844
+ value,
845
+ asChild,
846
+ asHandle,
847
+ disabled,
848
+ className,
849
+ style,
850
+ ref,
851
+ ...columnProps
852
+ } = props;
853
+
854
+ const id = useId();
855
+ const context = useKanbanContext(COLUMN_NAME);
856
+ const inBoard = use(KanbanBoardContext);
857
+ const inOverlay = use(KanbanOverlayContext);
858
+
859
+ if (!inBoard && !inOverlay) {
860
+ throw new Error(
861
+ `\`${COLUMN_NAME}\` must be used within \`${BOARD_NAME}\` or \`${OVERLAY_NAME}\``
862
+ );
863
+ }
864
+
865
+ if (value === '') {
866
+ throw new Error(`\`${COLUMN_NAME}\` value cannot be an empty string`);
867
+ }
868
+
869
+ const {
870
+ attributes,
871
+ listeners,
872
+ setNodeRef,
873
+ setActivatorNodeRef,
874
+ transform,
875
+ transition,
876
+ isDragging
877
+ } = useSortable({
878
+ id: value,
879
+ disabled,
880
+ animateLayoutChanges
881
+ });
882
+
883
+ const composedRef = useComposedRefs(ref, (node: HTMLElement | null) => {
884
+ if (disabled) return;
885
+ setNodeRef(node);
886
+ });
887
+
888
+ const composedStyle = useMemo<CSSProperties>(() => {
889
+ return {
890
+ transform: CSS.Transform.toString(transform),
891
+ transition,
892
+ ...style
893
+ };
894
+ }, [transform, transition, style]);
895
+
896
+ const { items: contextItems, getItemValue: contextGetItemValue } = context;
897
+
898
+ const items = useMemo(() => {
899
+ const columnItems = contextItems[value] ?? [];
900
+
901
+ return columnItems.map(item => contextGetItemValue(item));
902
+ }, [contextItems, value, contextGetItemValue]);
903
+
904
+ const columnContext = useMemo<KanbanColumnContextValue>(
905
+ () => ({
906
+ id,
907
+ attributes,
908
+ listeners,
909
+ setActivatorNodeRef,
910
+ isDragging,
911
+ disabled
912
+ }),
913
+ [
914
+ id,
915
+ attributes,
916
+ listeners,
917
+ setActivatorNodeRef,
918
+ isDragging,
919
+ disabled
920
+ ]
921
+ );
922
+
923
+ const ColumnPrimitive = asChild ? Slot : 'div';
924
+
925
+ return (
926
+ <KanbanColumnContext value={columnContext}>
927
+ <SortableContext
928
+ items={items}
929
+ strategy={context.orientation === 'horizontal' ? horizontalListSortingStrategy : verticalListSortingStrategy}>
930
+ <ColumnPrimitive
931
+ id={id}
932
+ data-disabled={disabled}
933
+ data-dragging={isDragging ? '' : undefined}
934
+ data-slot="kanban-column"
935
+ {...columnProps}
936
+ {...(asHandle && !disabled ? attributes : {})}
937
+ {...(asHandle && !disabled ? listeners : {})}
938
+ ref={composedRef}
939
+ style={composedStyle}
940
+ className={cn(
941
+ 'flex size-full flex-col gap-2 rounded-lg border bg-zinc-100 p-2.5 aria-disabled:pointer-events-none aria-disabled:opacity-50 dark:bg-zinc-900',
942
+ {
943
+ 'touch-none select-none': asHandle,
944
+ 'cursor-default': context.flatCursor,
945
+ 'data-dragging:cursor-grabbing': !context.flatCursor,
946
+ 'cursor-grab': !isDragging && asHandle && !context.flatCursor,
947
+ 'opacity-50': isDragging,
948
+ 'pointer-events-none opacity-50': disabled
949
+ },
950
+ className
951
+ )} />
952
+ </SortableContext>
953
+ </KanbanColumnContext>
954
+ );
955
+ }
956
+
957
+ interface KanbanColumnHandleProps extends ComponentProps<'button'> {
958
+ asChild?: boolean;
959
+ }
960
+
961
+ function KanbanColumnHandle(props: KanbanColumnHandleProps) {
962
+ const {
963
+ asChild, disabled, className, ref, ...columnHandleProps
964
+ } = props;
965
+
966
+ const context = useKanbanContext(COLUMN_NAME);
967
+ const columnContext = useKanbanColumnContext(COLUMN_HANDLE_NAME);
968
+
969
+ const isDisabled = disabled ?? columnContext.disabled;
970
+
971
+ const composedRef = useComposedRefs(ref, (node: HTMLElement | null) => {
972
+ if (isDisabled) return;
973
+ columnContext.setActivatorNodeRef(node);
974
+ });
975
+
976
+ const HandlePrimitive = asChild ? Slot : 'button';
977
+
978
+ return (
979
+ <HandlePrimitive
980
+ type="button"
981
+ aria-controls={columnContext.id}
982
+ data-disabled={isDisabled}
983
+ data-dragging={columnContext.isDragging ? '' : undefined}
984
+ data-slot="kanban-column-handle"
985
+ {...columnHandleProps}
986
+ {...(isDisabled ? {} : columnContext.attributes)}
987
+ {...(isDisabled ? {} : columnContext.listeners)}
988
+ ref={composedRef}
989
+ className={cn(
990
+ 'select-none disabled:pointer-events-none disabled:opacity-50',
991
+ context.flatCursor ? 'cursor-default' : 'cursor-grab data-dragging:cursor-grabbing',
992
+ className
993
+ )}
994
+ disabled={isDisabled} />
995
+ );
996
+ }
997
+
998
+ interface KanbanItemContextValue {
999
+ id: string;
1000
+ attributes: DraggableAttributes;
1001
+ listeners: DraggableSyntheticListeners | undefined;
1002
+ setActivatorNodeRef: (node: HTMLElement | null) => void;
1003
+ isDragging?: boolean;
1004
+ disabled?: boolean;
1005
+ }
1006
+
1007
+ const KanbanItemContext = createContext<KanbanItemContextValue | null>(null);
1008
+
1009
+ function useKanbanItemContext(consumerName: string) {
1010
+ const context = use(KanbanItemContext);
1011
+
1012
+ if (!context) {
1013
+ throw new Error(`\`${consumerName}\` must be used within \`${ITEM_NAME}\``);
1014
+ }
1015
+
1016
+ return context;
1017
+ }
1018
+
1019
+ interface KanbanItemProps extends ComponentProps<'div'> {
1020
+ value: UniqueIdentifier;
1021
+ asHandle?: boolean;
1022
+ asChild?: boolean;
1023
+ disabled?: boolean;
1024
+ }
1025
+
1026
+ function KanbanItem(props: KanbanItemProps) {
1027
+ const {
1028
+ value,
1029
+ style,
1030
+ asHandle,
1031
+ asChild,
1032
+ disabled,
1033
+ className,
1034
+ ref,
1035
+ ...itemProps
1036
+ } = props;
1037
+
1038
+ const id = useId();
1039
+ const context = useKanbanContext(ITEM_NAME);
1040
+ const inBoard = use(KanbanBoardContext);
1041
+ const inOverlay = use(KanbanOverlayContext);
1042
+
1043
+ if (!inBoard && !inOverlay) {
1044
+ throw new Error(`\`${ITEM_NAME}\` must be used within \`${BOARD_NAME}\``);
1045
+ }
1046
+
1047
+ const {
1048
+ attributes,
1049
+ listeners,
1050
+ setNodeRef,
1051
+ setActivatorNodeRef,
1052
+ transform,
1053
+ transition,
1054
+ isDragging
1055
+ } = useSortable({ id: value, disabled });
1056
+
1057
+ if (value === '') {
1058
+ throw new Error(`\`${ITEM_NAME}\` value cannot be an empty string`);
1059
+ }
1060
+
1061
+ const composedRef = useComposedRefs(ref, (node: HTMLElement | null) => {
1062
+ if (disabled) return;
1063
+ setNodeRef(node);
1064
+ });
1065
+
1066
+ const composedStyle = useMemo<CSSProperties>(() => {
1067
+ return {
1068
+ transform: CSS.Transform.toString(transform),
1069
+ transition,
1070
+ ...style
1071
+ };
1072
+ }, [transform, transition, style]);
1073
+
1074
+ const itemContext = useMemo<KanbanItemContextValue>(
1075
+ () => ({
1076
+ id,
1077
+ attributes,
1078
+ listeners,
1079
+ setActivatorNodeRef,
1080
+ isDragging,
1081
+ disabled
1082
+ }),
1083
+ [
1084
+ id,
1085
+ attributes,
1086
+ listeners,
1087
+ setActivatorNodeRef,
1088
+ isDragging,
1089
+ disabled
1090
+ ]
1091
+ );
1092
+
1093
+ const ItemPrimitive = asChild ? Slot : 'div';
1094
+
1095
+ return (
1096
+ <KanbanItemContext value={itemContext}>
1097
+ <ItemPrimitive
1098
+ id={id}
1099
+ data-disabled={disabled}
1100
+ data-dragging={isDragging ? '' : undefined}
1101
+ data-slot="kanban-item"
1102
+ data-value={value}
1103
+ {...itemProps}
1104
+ {...(asHandle && !disabled ? attributes : {})}
1105
+ {...(asHandle && !disabled ? listeners : {})}
1106
+ ref={composedRef}
1107
+ style={composedStyle}
1108
+ className={cn(
1109
+ 'focus-visible:outline-hidden focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-1',
1110
+ {
1111
+ 'touch-none select-none': asHandle,
1112
+ 'cursor-default': context.flatCursor,
1113
+ 'data-dragging:cursor-grabbing': !context.flatCursor,
1114
+ 'cursor-grab': !isDragging && asHandle && !context.flatCursor,
1115
+ 'opacity-50': isDragging,
1116
+ 'pointer-events-none opacity-50': disabled
1117
+ },
1118
+ className
1119
+ )} />
1120
+ </KanbanItemContext>
1121
+ );
1122
+ }
1123
+
1124
+ interface KanbanItemHandleProps extends ComponentProps<'button'> {
1125
+ asChild?: boolean;
1126
+ }
1127
+
1128
+ function KanbanItemHandle(props: KanbanItemHandleProps) {
1129
+ const {
1130
+ asChild, disabled, className, ref, ...itemHandleProps
1131
+ } = props;
1132
+
1133
+ const context = useKanbanContext(ITEM_HANDLE_NAME);
1134
+ const itemContext = useKanbanItemContext(ITEM_HANDLE_NAME);
1135
+
1136
+ const isDisabled = disabled ?? itemContext.disabled;
1137
+
1138
+ const composedRef = useComposedRefs(ref, (node: HTMLElement | null) => {
1139
+ if (isDisabled) return;
1140
+ itemContext.setActivatorNodeRef(node);
1141
+ });
1142
+
1143
+ const HandlePrimitive = asChild ? Slot : 'button';
1144
+
1145
+ return (
1146
+ <HandlePrimitive
1147
+ type="button"
1148
+ aria-controls={itemContext.id}
1149
+ data-disabled={isDisabled}
1150
+ data-dragging={itemContext.isDragging ? '' : undefined}
1151
+ data-slot="kanban-item-handle"
1152
+ {...itemHandleProps}
1153
+ {...(isDisabled ? {} : itemContext.attributes)}
1154
+ {...(isDisabled ? {} : itemContext.listeners)}
1155
+ ref={composedRef}
1156
+ className={cn(
1157
+ 'select-none disabled:pointer-events-none disabled:opacity-50',
1158
+ context.flatCursor ? 'cursor-default' : 'cursor-grab data-dragging:cursor-grabbing',
1159
+ className
1160
+ )}
1161
+ disabled={isDisabled} />
1162
+ );
1163
+ }
1164
+
1165
+ interface KanbanItemTitleProps extends ComponentProps<'button'> {
1166
+ asChild?: boolean;
1167
+ }
1168
+
1169
+ function KanbanItemTitle(props: KanbanItemTitleProps) {
1170
+ const {
1171
+ asChild, className, ref, onClick, ...titleProps
1172
+ } = props;
1173
+
1174
+ const context = useKanbanContext('KanbanItemTitle');
1175
+ const itemContext = useKanbanItemContext('KanbanItemTitle');
1176
+
1177
+ const titleRef = useRef<HTMLButtonElement | null>(null);
1178
+
1179
+ useLayoutEffect(() => {
1180
+ const node = titleRef.current;
1181
+
1182
+ if (!node) return;
1183
+
1184
+ const stop = (e: Event) => {
1185
+ e.stopPropagation();
1186
+ };
1187
+
1188
+ node.addEventListener('pointerdown', stop);
1189
+ node.addEventListener('mousedown', stop);
1190
+ node.addEventListener('touchstart', stop, { passive: true });
1191
+
1192
+ return () => {
1193
+ node.removeEventListener('pointerdown', stop);
1194
+ node.removeEventListener('mousedown', stop);
1195
+ node.removeEventListener('touchstart', stop);
1196
+ };
1197
+ }, []);
1198
+
1199
+ const composedRef = useComposedRefs(ref, titleRef);
1200
+
1201
+ const { onItemClick } = context;
1202
+
1203
+ const handleClick = useCallback(
1204
+ (e: MouseEvent<HTMLButtonElement>) => {
1205
+ e.stopPropagation();
1206
+ onClick?.(e);
1207
+
1208
+ if (!e.defaultPrevented && onItemClick) {
1209
+ const itemEl = (e.currentTarget as HTMLElement).closest('[data-slot="kanban-item"]');
1210
+ const itemValue = itemEl?.getAttribute('data-value');
1211
+
1212
+ if (itemValue) {
1213
+ onItemClick(itemValue);
1214
+ }
1215
+ }
1216
+ },
1217
+ [onClick, onItemClick]
1218
+ );
1219
+
1220
+ const TitlePrimitive = asChild ? Slot : 'button';
1221
+
1222
+ return (
1223
+ <TitlePrimitive
1224
+ type="button"
1225
+ data-slot="kanban-item-title"
1226
+ data-dragging={itemContext.isDragging ? '' : undefined}
1227
+ {...titleProps}
1228
+ ref={composedRef}
1229
+ onClick={handleClick}
1230
+ className={cn(
1231
+ 'cursor-pointer text-left focus-visible:outline-hidden focus-visible:ring-1 focus-visible:ring-ring',
1232
+ className
1233
+ )} />
1234
+ );
1235
+ }
1236
+
1237
+ interface KanbanFinalZoneProps extends ComponentProps<'div'> {
1238
+ asChild?: boolean;
1239
+ }
1240
+
1241
+ function KanbanFinalZone(props: KanbanFinalZoneProps) {
1242
+ const {
1243
+ asChild, className, ref, ...zoneProps
1244
+ } = props;
1245
+
1246
+ const context = useKanbanContext(FINAL_ZONE_NAME);
1247
+
1248
+ const isDraggingItem = !!context.activeId && !(context.activeId in context.items);
1249
+
1250
+ const ZonePrimitive = asChild ? Slot : 'div';
1251
+
1252
+ return (
1253
+ <ZonePrimitive
1254
+ data-slot="kanban-final-zone"
1255
+ data-dragging={isDraggingItem ? '' : undefined}
1256
+ {...zoneProps}
1257
+ ref={ref}
1258
+ className={cn('flex gap-3', className)} />
1259
+ );
1260
+ }
1261
+
1262
+ interface KanbanFinalColumnProps extends ComponentProps<'div'> {
1263
+ value: UniqueIdentifier;
1264
+ asChild?: boolean;
1265
+ }
1266
+
1267
+ function KanbanFinalColumn(props: KanbanFinalColumnProps) {
1268
+ const {
1269
+ value, asChild, className, ref, ...columnProps
1270
+ } = props;
1271
+
1272
+ const context = useKanbanContext(FINAL_COLUMN_NAME);
1273
+ const { setNodeRef, isOver } = useDroppable({ id: value });
1274
+
1275
+ const composedRef = useComposedRefs(ref, setNodeRef);
1276
+
1277
+ if (value === '') {
1278
+ throw new Error(
1279
+ `\`${FINAL_COLUMN_NAME}\` value cannot be an empty string`
1280
+ );
1281
+ }
1282
+
1283
+ const isHovered = context.hoveredFinalColumn === value;
1284
+ const isDraggingItem = !!context.activeId && !(context.activeId in context.items);
1285
+
1286
+ const ColumnPrimitive = asChild ? Slot : 'div';
1287
+
1288
+ return (
1289
+ <ColumnPrimitive
1290
+ data-slot="kanban-final-column"
1291
+ data-over={isOver || isHovered ? '' : undefined}
1292
+ data-dragging={isDraggingItem ? '' : undefined}
1293
+ {...columnProps}
1294
+ ref={composedRef}
1295
+ className={cn(
1296
+ 'flex flex-1 flex-col items-center justify-center gap-1 rounded-xl border-2 border-dashed border-muted-foreground/25 bg-muted/50 p-3 transition-all duration-300 ease-in-out',
1297
+ isDraggingItem ? 'p-6' : '',
1298
+ isOver || isHovered ? 'scale-[1.02]' : '',
1299
+ className
1300
+ )} />
1301
+ );
1302
+ }
1303
+
1304
+ const KanbanOverlayContext = createContext(false);
1305
+
1306
+ const dropAnimation: DropAnimation = {
1307
+ sideEffects: defaultDropAnimationSideEffects({
1308
+ styles: {
1309
+ active: {
1310
+ opacity: '0.4'
1311
+ }
1312
+ }
1313
+ })
1314
+ };
1315
+
1316
+ interface KanbanOverlayProps
1317
+ extends Omit<ComponentProps<typeof DragOverlay>, 'children'> {
1318
+ container?: Element | DocumentFragment | null;
1319
+ children?:
1320
+ | ReactNode
1321
+ | ((params: {
1322
+ value: UniqueIdentifier;
1323
+ variant: 'column' | 'item';
1324
+ }) => ReactNode);
1325
+ }
1326
+
1327
+ function KanbanOverlay(props: KanbanOverlayProps) {
1328
+ const { container: containerProp, children, ...overlayProps } = props;
1329
+
1330
+ const context = useKanbanContext(OVERLAY_NAME);
1331
+
1332
+ const [mounted, setMounted] = useState(false);
1333
+
1334
+ useLayoutEffect(() => {
1335
+ queueMicrotask(() => setMounted(true));
1336
+ }, []);
1337
+
1338
+ const container
1339
+ = containerProp ?? (mounted ? globalThis.document?.body : null);
1340
+
1341
+ if (!container) return null;
1342
+
1343
+ const variant
1344
+ = context.activeId && context.activeId in context.items ? 'column' : 'item';
1345
+
1346
+ return ReactDOM.createPortal(
1347
+ <DragOverlay
1348
+ dropAnimation={dropAnimation}
1349
+ modifiers={context.modifiers}
1350
+ className={cn(!context.flatCursor && 'cursor-grabbing')}
1351
+ {...overlayProps}>
1352
+ <KanbanOverlayContext value>
1353
+ {context.activeId && children ? typeof children === 'function' ? children({
1354
+ value: context.activeId,
1355
+ variant
1356
+ }) : children : null}
1357
+ </KanbanOverlayContext>
1358
+ </DragOverlay>,
1359
+ container
1360
+ );
1361
+ }
1362
+
1363
+ export {
1364
+ Kanban,
1365
+ KanbanBoard,
1366
+ KanbanColumn,
1367
+ KanbanColumnHandle,
1368
+ KanbanFinalColumn,
1369
+ KanbanFinalZone,
1370
+ KanbanItem,
1371
+ KanbanItemHandle,
1372
+ KanbanItemTitle,
1373
+ KanbanOverlay,
1374
+ type KanbanItemTitleProps,
1375
+ type KanbanProps
1376
+ };