@dxos/react-ui-mosaic 0.7.1 → 0.7.2-main.f1adc9f

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.
@@ -0,0 +1,705 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var node_exports = {};
30
+ __export(node_exports, {
31
+ Mosaic: () => Mosaic,
32
+ Path: () => Path,
33
+ getTransformCSS: () => getTransformCSS,
34
+ isTileComponentProps: () => isTileComponentProps,
35
+ isTileProps: () => isTileProps,
36
+ swapItems: () => swapItems,
37
+ useContainer: () => useContainer,
38
+ useItemsWithOrigin: () => useItemsWithOrigin,
39
+ useItemsWithPreview: () => useItemsWithPreview,
40
+ useMosaic: () => useMosaic
41
+ });
42
+ module.exports = __toCommonJS(node_exports);
43
+ var import_react = require("react");
44
+ var import_react2 = __toESM(require("react"));
45
+ var import_react3 = require("react");
46
+ var import_react4 = require("react");
47
+ var import_debug = require("@dxos/debug");
48
+ var import_core = require("@dnd-kit/core");
49
+ var import_sortable = require("@dnd-kit/sortable");
50
+ var import_react5 = __toESM(require("react"));
51
+ var import_react_dom = require("react-dom");
52
+ var import_util = require("@dxos/util");
53
+ var import_react6 = __toESM(require("react"));
54
+ var import_react_ui_theme = require("@dxos/react-ui-theme");
55
+ var import_react7 = __toESM(require("react"));
56
+ var import_react_ui_card = require("@dxos/react-ui-card");
57
+ var import_util2 = require("@dxos/util");
58
+ var import_utilities = require("@dnd-kit/utilities");
59
+ var import_core2 = require("@dnd-kit/core");
60
+ var import_react8 = __toESM(require("react"));
61
+ var import_log = require("@dxos/log");
62
+ var import_sortable2 = require("@dnd-kit/sortable");
63
+ var import_react9 = __toESM(require("react"));
64
+ var import_core3 = require("@dnd-kit/core");
65
+ var import_sortable3 = require("@dnd-kit/sortable");
66
+ var import_react_compose_refs = require("@radix-ui/react-compose-refs");
67
+ var import_react10 = __toESM(require("react"));
68
+ var DEFAULT_TRANSITION = 200;
69
+ var DEFAULT_TYPE = "unknown";
70
+ var DEFAULT_COMPONENT = /* @__PURE__ */ (0, import_react2.forwardRef)(() => /* @__PURE__ */ import_react2.default.createElement(import_react2.default.Fragment, null, "missing component"));
71
+ var MosaicOperations = [
72
+ "transfer",
73
+ "copy",
74
+ "rearrange",
75
+ "reject"
76
+ ];
77
+ var MosaicContainerContext = /* @__PURE__ */ (0, import_react2.createContext)({
78
+ id: "never",
79
+ type: DEFAULT_TYPE,
80
+ Component: DEFAULT_COMPONENT
81
+ });
82
+ var MosaicContainer = ({ children, id, debug, type = DEFAULT_TYPE, Component: Component2 = DEFAULT_COMPONENT, transitionDuration = DEFAULT_TRANSITION, modifier, getOverlayProps, getOverlayStyle, onOver, onDrop, onMove, onDragEnd }) => {
83
+ const mosaic = useMosaic();
84
+ const container = {
85
+ id,
86
+ debug,
87
+ type,
88
+ Component: Component2,
89
+ transitionDuration,
90
+ modifier,
91
+ getOverlayProps,
92
+ getOverlayStyle,
93
+ onOver,
94
+ onDrop,
95
+ onMove,
96
+ onDragEnd
97
+ };
98
+ (0, import_react2.useEffect)(() => {
99
+ mosaic.setContainer(container.id, container);
100
+ return () => {
101
+ };
102
+ }, Object.values(container));
103
+ return /* @__PURE__ */ import_react2.default.createElement(MosaicContainerContext.Provider, {
104
+ value: container
105
+ }, children);
106
+ };
107
+ var useContainer = () => (0, import_react.useContext)(MosaicContainerContext);
108
+ var Debug = ({ data, position }) => /* @__PURE__ */ import_react6.default.createElement("pre", {
109
+ className: (0, import_react_ui_theme.mx)("font-mono text-xs overflow-hidden m-2 p-1 z-[100]", "bg-neutral-50 dark:bg-neutral-700 border border-neutral-300 rounded", position === "bottom-left" && "fixed left-0 bottom-0", position === "bottom-right" && "fixed right-0 bottom-0")
110
+ }, JSON.stringify(data, void 0, 1));
111
+ var swapItems = (items, from, to) => {
112
+ const fromIndex = items.findIndex((item) => item.id === from.id);
113
+ const toIndex = items.findIndex((item) => item.id === to.id);
114
+ return fromIndex !== -1 && toIndex !== -1 ? (0, import_util2.arrayMove)(items, fromIndex, toIndex) : items;
115
+ };
116
+ var SEPARATOR = "~";
117
+ var Path = {
118
+ create: (...args) => args.join(SEPARATOR),
119
+ parts: (path) => path.split(SEPARATOR),
120
+ length: (path) => path.split(SEPARATOR).length,
121
+ first: (path) => path.split(SEPARATOR)[0] ?? path,
122
+ last: (path) => path.split(SEPARATOR).at(-1) ?? path,
123
+ parent: (path) => path.split(SEPARATOR).slice(0, -1).join(SEPARATOR),
124
+ hasRoot: (path, id) => Path.first(path) === id,
125
+ hasChild: (path, compare) => Path.parent(compare) === path,
126
+ hasDescendent: (path, compare) => compare !== path && compare.startsWith(path),
127
+ siblings: (path, compare) => Path.parent(path) === Path.parent(compare),
128
+ onPath: (path, id) => Path.parts(path).includes(id)
129
+ };
130
+ var isTileProps = (props) => typeof props.path === "string" && typeof props.item === "object" && props.item ? "id" in props.item && typeof props.item.id === "string" : false;
131
+ var isTileComponentProps = (props) => isTileProps(props) && typeof props.operation === "string" && MosaicOperations.includes(props.operation);
132
+ var getTransformCSS = (transform) => transform ? import_utilities.CSS.Transform.toString(Object.assign(transform, {
133
+ scaleX: 1,
134
+ scaleY: 1
135
+ })) : void 0;
136
+ var DefaultComponent = /* @__PURE__ */ (0, import_react7.forwardRef)(({ draggableStyle, draggableProps, item: { id }, path }, forwardRef4) => {
137
+ return /* @__PURE__ */ import_react7.default.createElement(import_react_ui_card.Card.Root, {
138
+ ref: forwardRef4,
139
+ style: draggableStyle
140
+ }, /* @__PURE__ */ import_react7.default.createElement(import_react_ui_card.Card.Header, null, /* @__PURE__ */ import_react7.default.createElement(import_react_ui_card.Card.DragHandle, draggableProps), /* @__PURE__ */ import_react7.default.createElement(import_react_ui_card.Card.Title, {
141
+ title: Path.create(path, id),
142
+ classNames: "truncate font-mono text-xs"
143
+ })));
144
+ });
145
+ DefaultComponent.displayName = "DefaultComponent";
146
+ var DEFAULT_COMPONENT_ID = "__default";
147
+ var MosaicContext = /* @__PURE__ */ (0, import_react5.createContext)(void 0);
148
+ var defaultDropAnimation = {
149
+ duration: 0
150
+ };
151
+ var MosaicRoot = ({ Component: Component2 = DefaultComponent, debug, children }) => {
152
+ const [containers, setContainers] = (0, import_react5.useState)({
153
+ [DEFAULT_COMPONENT_ID]: {
154
+ id: DEFAULT_COMPONENT_ID,
155
+ Component: Component2
156
+ }
157
+ });
158
+ const handleSetContainer = (id, container) => {
159
+ setContainers((containers2) => {
160
+ if (container) {
161
+ return {
162
+ ...containers2,
163
+ [id]: container
164
+ };
165
+ } else {
166
+ const { [id]: _, ...rest } = containers2;
167
+ return rest;
168
+ }
169
+ });
170
+ };
171
+ const [activeItem, setActiveItem] = (0, import_react5.useState)();
172
+ const [overItem, setOverItem] = (0, import_react5.useState)();
173
+ const [operation, setOperation] = (0, import_react5.useState)("reject");
174
+ const [moveDetails, setMoveDetails] = (0, import_react5.useState)();
175
+ const [dropAnimation, setDropAnimation] = (0, import_react5.useState)(defaultDropAnimation);
176
+ const collisionDetection = (args) => {
177
+ const pointerCollisions = (0, import_core.pointerWithin)(args);
178
+ if (pointerCollisions.length > 0) {
179
+ return pointerCollisions;
180
+ }
181
+ return (0, import_core.rectIntersection)(args);
182
+ };
183
+ const modifiers = (props) => {
184
+ const { transform } = props;
185
+ if (activeItem) {
186
+ const container = containers[Path.first(activeItem.path)];
187
+ return container?.modifier?.(activeItem, props) ?? transform;
188
+ } else {
189
+ return transform;
190
+ }
191
+ };
192
+ const sensors = (0, import_core.useSensors)((0, import_core.useSensor)(import_core.MouseSensor, {
193
+ activationConstraint: {
194
+ distance: 10
195
+ }
196
+ }), (0, import_core.useSensor)(import_core.TouchSensor, {
197
+ activationConstraint: {
198
+ delay: 200,
199
+ tolerance: 5
200
+ }
201
+ }), (0, import_core.useSensor)(import_core.KeyboardSensor, {
202
+ coordinateGetter: import_sortable.sortableKeyboardCoordinates
203
+ }));
204
+ const handleDragStart = (event) => {
205
+ setActiveItem((0, import_util.pick)(event.active.data.current, [
206
+ "path",
207
+ "type",
208
+ "item",
209
+ "position"
210
+ ]));
211
+ };
212
+ const handleDragMove = (event) => {
213
+ const nextOverItem = (0, import_util.pick)(event.over?.data.current, [
214
+ "path",
215
+ "type",
216
+ "item",
217
+ "position"
218
+ ]);
219
+ const nextOverContainer = nextOverItem && nextOverItem?.path ? containers[Path.first(nextOverItem.path)] : void 0;
220
+ if (!event.over || !nextOverItem || !nextOverContainer) {
221
+ setOperation("reject");
222
+ setOverItem(void 0);
223
+ return;
224
+ }
225
+ if (overItem !== nextOverItem) {
226
+ setOverItem(nextOverItem);
227
+ }
228
+ if (activeItem && nextOverItem && nextOverContainer?.onMove) {
229
+ const { operation: nextOperation, details: nextDetails } = nextOverContainer.onMove({
230
+ active: activeItem,
231
+ over: nextOverItem,
232
+ details: {
233
+ delta: event.delta
234
+ }
235
+ });
236
+ setOperation(nextOperation);
237
+ if (
238
+ // always set falsy symbols
239
+ !nextDetails || // always set if details become truthy
240
+ !moveDetails && nextDetails || // only update if details change
241
+ Object.entries(nextDetails).find(([key, value]) => !(key in moveDetails) || moveDetails[key] !== value)
242
+ ) {
243
+ setMoveDetails(nextDetails);
244
+ }
245
+ }
246
+ };
247
+ const handleDragOver = (event) => {
248
+ const overItem2 = (0, import_util.pick)(event.over?.data.current, [
249
+ "path",
250
+ "type",
251
+ "item",
252
+ "position"
253
+ ]);
254
+ const overContainer = overItem2 && overItem2?.path ? containers[Path.first(overItem2.path)] : void 0;
255
+ if (overContainer?.onMove) {
256
+ return;
257
+ }
258
+ const activeContainer = activeItem && containers[Path.first(activeItem.path)];
259
+ if (!event.over || !overItem2 || !overContainer || !activeItem || !activeContainer) {
260
+ setOperation("reject");
261
+ setOverItem(void 0);
262
+ return;
263
+ }
264
+ let operation2 = "reject";
265
+ if (overContainer.onOver) {
266
+ operation2 = overContainer.onOver({
267
+ active: activeItem,
268
+ over: overItem2,
269
+ details: moveDetails
270
+ });
271
+ }
272
+ setOperation(operation2);
273
+ setOverItem(overItem2);
274
+ };
275
+ const handleDragCancel = (event) => {
276
+ setOperation("reject");
277
+ setActiveItem(void 0);
278
+ setOverItem(void 0);
279
+ };
280
+ const handleDragEnd = (event) => {
281
+ const overContainer = overItem && containers[Path.first(overItem.path)];
282
+ if (operation !== "reject" && activeItem && overItem) {
283
+ let nextDropAnimation = defaultDropAnimation;
284
+ const activeContainer = containers[Path.first(activeItem.path)];
285
+ if (activeContainer) {
286
+ nextDropAnimation = activeContainer.onDrop?.({
287
+ operation,
288
+ active: activeItem,
289
+ over: overItem,
290
+ details: moveDetails
291
+ });
292
+ if (overContainer && overContainer !== activeContainer) {
293
+ nextDropAnimation = overContainer.onDrop?.({
294
+ operation,
295
+ active: activeItem,
296
+ over: overItem,
297
+ details: moveDetails
298
+ });
299
+ }
300
+ }
301
+ setDropAnimation(nextDropAnimation);
302
+ }
303
+ Object.values(containers).forEach((container) => container?.onDragEnd?.(event));
304
+ setTimeout(() => {
305
+ setOperation("reject");
306
+ setOverItem(void 0);
307
+ setActiveItem(void 0);
308
+ }, overContainer?.transitionDuration ?? DEFAULT_TRANSITION);
309
+ };
310
+ return /* @__PURE__ */ import_react5.default.createElement(MosaicContext.Provider, {
311
+ value: {
312
+ containers,
313
+ setContainer: handleSetContainer,
314
+ activeItem,
315
+ overItem,
316
+ operation,
317
+ moveDetails,
318
+ dropAnimation
319
+ }
320
+ }, /* @__PURE__ */ import_react5.default.createElement(import_core.DndContext, {
321
+ collisionDetection,
322
+ modifiers: [
323
+ modifiers
324
+ ],
325
+ sensors,
326
+ // TODO(burdon): Allow container to veto drop.
327
+ cancelDrop: (event) => false,
328
+ onDragStart: handleDragStart,
329
+ onDragMove: handleDragMove,
330
+ onDragOver: handleDragOver,
331
+ onDragCancel: handleDragCancel,
332
+ onDragEnd: handleDragEnd
333
+ }, children, debug && /* @__PURE__ */ (0, import_react_dom.createPortal)(/* @__PURE__ */ import_react5.default.createElement(MosaicDebug, {
334
+ containers,
335
+ operation,
336
+ activeItem,
337
+ overItem
338
+ }), document.body)));
339
+ };
340
+ var MosaicDebug = ({ containers, operation, activeItem, overItem }) => {
341
+ return /* @__PURE__ */ import_react5.default.createElement(Debug, {
342
+ position: "bottom-right",
343
+ data: {
344
+ containers: Object.keys(containers).map((id) => id),
345
+ operation,
346
+ active: {
347
+ id: activeItem?.item?.id.slice(0, 16),
348
+ path: activeItem?.path && Path.create(...Path.parts(activeItem.path).map((part) => part.slice(0, 16))),
349
+ type: activeItem?.type,
350
+ position: activeItem?.position
351
+ },
352
+ over: {
353
+ id: overItem?.item?.id.slice(0, 16),
354
+ path: overItem?.path && Path.create(...Path.parts(overItem.path).map((part) => part.slice(0, 16))),
355
+ type: overItem?.type,
356
+ position: overItem?.position
357
+ }
358
+ }
359
+ });
360
+ };
361
+ var useMosaic = () => (0, import_react4.useContext)(MosaicContext) ?? (0, import_debug.raise)(new Error("Missing MosaicContext"));
362
+ var useItemsWithPreview = ({ path, items }) => {
363
+ const { operation, activeItem, overItem } = useMosaic();
364
+ const [itemsWithPreview, setItemsWithPreview] = (0, import_react3.useState)(items);
365
+ const overParent = (
366
+ // If over the sortable context itself & active item is foreign, then treat self as the parent.
367
+ // This allows items to be appended to the end of the sortable.
368
+ overItem?.path === path && activeItem && Path.parent(activeItem.path) !== overItem.path ? path : overItem && !!overItem.path && Path.parent(overItem.path)
369
+ );
370
+ const [lastOverParent, setLastOverParent] = (0, import_react3.useState)(overParent);
371
+ (0, import_react3.useEffect)(() => {
372
+ if (!activeItem || !overItem) {
373
+ setLastOverParent(void 0);
374
+ setItemsWithPreview(items);
375
+ return;
376
+ }
377
+ if (lastOverParent === overParent) {
378
+ return;
379
+ }
380
+ const activeIsChild = Path.hasChild(path, activeItem.path);
381
+ const overIsChild = Path.hasChild(path, overItem.path);
382
+ const overSelf = overItem.path === path;
383
+ if (operation === "reject" || operation === "rearrange") {
384
+ setLastOverParent(void 0);
385
+ setItemsWithPreview(items);
386
+ return;
387
+ } else {
388
+ setLastOverParent(overParent);
389
+ }
390
+ if (!activeIsChild && overIsChild) {
391
+ setItemsWithPreview((items2) => {
392
+ const position = overItem.position;
393
+ return [
394
+ ...items2.slice(0, position),
395
+ activeItem.item,
396
+ ...items2.slice(position)
397
+ ];
398
+ });
399
+ } else if (!activeIsChild && overSelf) {
400
+ setItemsWithPreview((items2) => [
401
+ ...items2,
402
+ activeItem.item
403
+ ]);
404
+ } else if (activeIsChild && !overIsChild) {
405
+ setItemsWithPreview((items2) => items2.filter((item) => item.id !== activeItem.item.id));
406
+ } else {
407
+ setItemsWithPreview(items);
408
+ }
409
+ }, [
410
+ operation,
411
+ activeItem,
412
+ overItem,
413
+ overParent,
414
+ lastOverParent,
415
+ path,
416
+ items
417
+ ]);
418
+ return items.length === itemsWithPreview.length ? items : itemsWithPreview;
419
+ };
420
+ var useItemsWithOrigin = (path, items) => {
421
+ const { operation, activeItem, overItem } = useMosaic();
422
+ if (!activeItem || !overItem) {
423
+ return items;
424
+ }
425
+ const activeIsChild = Path.hasChild(path, activeItem.path);
426
+ const overIsChild = Path.hasChild(path, overItem.path);
427
+ if (activeIsChild && !overIsChild && operation !== "rearrange") {
428
+ return items.map((item) => {
429
+ if (item.id === activeItem.item.id) {
430
+ return {
431
+ ...item,
432
+ id: `${item.id}--origin`
433
+ };
434
+ } else {
435
+ return item;
436
+ }
437
+ });
438
+ } else {
439
+ return items;
440
+ }
441
+ };
442
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/ui/react-ui-mosaic/src/mosaic/DragOverlay.tsx";
443
+ var MosaicDragOverlay = ({ delay = 200, debug = false, ...overlayProps }) => {
444
+ const { containers, operation, activeItem, overItem, dropAnimation } = useMosaic();
445
+ const [state, setState] = (0, import_react8.useState)({});
446
+ const timer = (0, import_react8.useRef)();
447
+ (0, import_react8.useEffect)(() => {
448
+ if (activeItem && operation !== "reject") {
449
+ let container2;
450
+ let OverlayComponent2;
451
+ if (overItem?.path) {
452
+ container2 = containers[Path.first(overItem.path)];
453
+ OverlayComponent2 = container2?.Component;
454
+ }
455
+ if (!OverlayComponent2) {
456
+ container2 = containers[Path.first(activeItem.path)];
457
+ OverlayComponent2 = container2?.Component ?? DefaultComponent;
458
+ }
459
+ clearTimeout(timer.current);
460
+ timer.current = setTimeout(() => setState({
461
+ container: container2,
462
+ OverlayComponent: OverlayComponent2
463
+ }), timer.current ? delay : 0);
464
+ } else {
465
+ setState({});
466
+ }
467
+ }, [
468
+ activeItem,
469
+ overItem
470
+ ]);
471
+ const container = state.container ?? (activeItem && containers[Path.first(activeItem.path)]);
472
+ const OverlayComponent = state.OverlayComponent ?? container?.Component;
473
+ const FallbackComponent = (activeItem && containers[Path.first(activeItem.path)])?.Component ?? DefaultComponent;
474
+ return (
475
+ // TODO(burdon): Set custom animations (e.g., in/out/around).
476
+ /* @__PURE__ */ import_react8.default.createElement(import_core2.DragOverlay, {
477
+ adjustScale: false,
478
+ ...overlayProps,
479
+ dropAnimation,
480
+ style: {
481
+ ...container?.getOverlayStyle?.()
482
+ }
483
+ }, activeItem?.path && container && OverlayComponent && /* @__PURE__ */ import_react8.default.createElement(OverlayErrorBoundary, {
484
+ fallback: /* @__PURE__ */ import_react8.default.createElement(FallbackComponent, {
485
+ ...container.getOverlayProps?.(),
486
+ item: activeItem.item,
487
+ path: activeItem.path,
488
+ operation,
489
+ active: "overlay"
490
+ })
491
+ }, /* @__PURE__ */ import_react8.default.createElement(OverlayComponent, {
492
+ ...container.getOverlayProps?.(),
493
+ item: activeItem.item,
494
+ path: activeItem.path,
495
+ type: activeItem.type,
496
+ operation,
497
+ active: "overlay"
498
+ }), debug && /* @__PURE__ */ import_react8.default.createElement("div", {
499
+ className: "flex flex-wrap mt-1 p-1 bg-neutral-50 dark:bg-neutral-700 text-xs border rounded overflow-hidden gap-1"
500
+ }, /* @__PURE__ */ import_react8.default.createElement("span", {
501
+ className: "truncate"
502
+ }, /* @__PURE__ */ import_react8.default.createElement("span", {
503
+ className: "text-neutral-400"
504
+ }, "container "), container.id), /* @__PURE__ */ import_react8.default.createElement("span", {
505
+ className: "truncate"
506
+ }, /* @__PURE__ */ import_react8.default.createElement("span", {
507
+ className: "text-neutral-400"
508
+ }, "item "), activeItem.item.id.slice(0, 8)))))
509
+ );
510
+ };
511
+ var OverlayErrorBoundary = class extends import_react8.Component {
512
+ constructor(props) {
513
+ super(props);
514
+ this.state = {
515
+ error: null
516
+ };
517
+ this.fallback = props.fallback ?? /* @__PURE__ */ import_react8.default.createElement("p", null, "ERROR: $", String(this.state.error));
518
+ }
519
+ static getDerivedStateFromError(error) {
520
+ return {
521
+ error
522
+ };
523
+ }
524
+ componentDidCatch(error, info) {
525
+ import_log.log.catch(error, {
526
+ info
527
+ }, {
528
+ F: __dxlog_file,
529
+ L: 140,
530
+ S: this,
531
+ C: (f, a) => f(...a)
532
+ });
533
+ }
534
+ render() {
535
+ if (this.state.error) {
536
+ return this.fallback;
537
+ }
538
+ return this.props.children;
539
+ }
540
+ };
541
+ var MosaicSortableContext = ({ id, items = [], direction = "vertical", children }) => {
542
+ const { operation, activeItem, overItem } = useMosaic();
543
+ const container = useContainer();
544
+ const contextId = id ?? container.id;
545
+ const Sortable = direction === "vertical" ? Column : Row;
546
+ const ids = items.map((item) => {
547
+ const path = Path.create(contextId, item.id);
548
+ const isPreview = activeItem && activeItem.item.id === item.id && overItem && overItem.path && (Path.siblings(overItem.path, path) || Path.hasChild(overItem.path, path)) && operation !== "reject";
549
+ return isPreview ? activeItem.path : path;
550
+ });
551
+ return /* @__PURE__ */ import_react9.default.createElement(Sortable, {
552
+ id: contextId,
553
+ items: ids
554
+ }, children);
555
+ };
556
+ var Column = ({ children, id, items }) => /* @__PURE__ */ import_react9.default.createElement(import_sortable2.SortableContext, {
557
+ id,
558
+ items,
559
+ strategy: import_sortable2.verticalListSortingStrategy
560
+ }, children);
561
+ var Row = ({ children, id, items }) => /* @__PURE__ */ import_react9.default.createElement(import_sortable2.SortableContext, {
562
+ id,
563
+ items,
564
+ strategy: import_sortable2.horizontalListSortingStrategy
565
+ }, children);
566
+ var DraggableTile = ({ path: parentPath, type = DEFAULT_TYPE, item, Component: Component2 = DefaultComponent, position, draggableStyle, ...props }) => {
567
+ const { operation } = useMosaic();
568
+ const path = Path.create(parentPath, item.id);
569
+ const {
570
+ setNodeRef,
571
+ attributes,
572
+ listeners,
573
+ /* transform, */
574
+ isDragging
575
+ } = (0, import_core3.useDraggable)({
576
+ id: path,
577
+ data: {
578
+ path,
579
+ type,
580
+ item,
581
+ position
582
+ }
583
+ });
584
+ return /* @__PURE__ */ import_react10.default.createElement(Component2, {
585
+ ref: setNodeRef,
586
+ item,
587
+ path,
588
+ type,
589
+ position,
590
+ operation,
591
+ isDragging,
592
+ draggableStyle: {
593
+ // TODO(burdon): Override by container?
594
+ // transform: getTransformCSS(transform),
595
+ ...draggableStyle
596
+ },
597
+ draggableProps: {
598
+ ...attributes,
599
+ ...listeners
600
+ },
601
+ ...props
602
+ });
603
+ };
604
+ var DroppableTile = /* @__PURE__ */ (0, import_react10.forwardRef)(({ path: parentPath, type = DEFAULT_TYPE, item, Component: Component2 = DefaultComponent, position, ...props }, forwardedRef) => {
605
+ const { operation } = useMosaic();
606
+ const path = parentPath === item.id ? parentPath : Path.create(parentPath, item.id);
607
+ const { setNodeRef, isOver } = (0, import_core3.useDroppable)({
608
+ id: path,
609
+ data: {
610
+ path,
611
+ type,
612
+ item,
613
+ position
614
+ }
615
+ });
616
+ const ref = (0, import_react_compose_refs.useComposedRefs)(setNodeRef, forwardedRef);
617
+ return /* @__PURE__ */ import_react10.default.createElement(Component2, {
618
+ item,
619
+ path,
620
+ type,
621
+ position,
622
+ operation,
623
+ isOver,
624
+ ...props,
625
+ ref
626
+ });
627
+ });
628
+ var SortableTile = ({ path: parentPath, type = DEFAULT_TYPE, item, Component: OverrideComponent, position, draggableStyle, ...props }) => {
629
+ const { operation, activeItem, overItem } = useMosaic();
630
+ const { transitionDuration, Component: ContainerComponent } = useContainer();
631
+ const path = Path.create(parentPath, item.id);
632
+ const isPreview = activeItem && activeItem.item.id === item.id && overItem && activeItem.path && overItem.path && !Path.siblings(activeItem.path, overItem.path) && (Path.siblings(overItem.path, path) || Path.hasChild(overItem.path, path)) && operation !== "reject";
633
+ const { setNodeRef, attributes, listeners, transform, isDragging, isOver } = (0, import_sortable3.useSortable)({
634
+ id: isPreview ? activeItem.path : path,
635
+ data: {
636
+ path,
637
+ type,
638
+ item,
639
+ position
640
+ },
641
+ animateLayoutChanges: (args) => (0, import_sortable3.defaultAnimateLayoutChanges)({
642
+ ...args,
643
+ wasDragging: item.id !== activeItem?.item.id
644
+ })
645
+ });
646
+ let active;
647
+ if (isDragging) {
648
+ if (operation === "rearrange" || operation === "reject") {
649
+ active = "rearrange";
650
+ } else if (activeItem && activeItem.path && Path.parent(activeItem.path) !== parentPath) {
651
+ active = "destination";
652
+ } else {
653
+ active = "origin";
654
+ }
655
+ }
656
+ const Component2 = OverrideComponent ?? ContainerComponent ?? DefaultComponent;
657
+ return /* @__PURE__ */ import_react10.default.createElement(Component2, {
658
+ ref: setNodeRef,
659
+ item,
660
+ path,
661
+ type,
662
+ position,
663
+ operation,
664
+ active,
665
+ isDragging,
666
+ isOver,
667
+ draggableStyle: {
668
+ transform: getTransformCSS(transform),
669
+ transition: transform ? `transform ${transitionDuration}ms ease` : "none",
670
+ ...draggableStyle
671
+ },
672
+ draggableProps: {
673
+ ...attributes,
674
+ ...listeners
675
+ },
676
+ ...props
677
+ });
678
+ };
679
+ var Mosaic = {
680
+ Root: MosaicRoot,
681
+ Container: MosaicContainer,
682
+ DragOverlay: MosaicDragOverlay,
683
+ SortableContext: MosaicSortableContext,
684
+ DraggableTile,
685
+ DroppableTile,
686
+ SortableTile,
687
+ // TODO(wittjosiah): Consider factoring out or using inline styles instead depending on DXOS UI theme/tailwind.
688
+ DefaultComponent,
689
+ // TODO(burdon): Don't export; move to util.
690
+ Debug
691
+ };
692
+ // Annotate the CommonJS export names for ESM import in node:
693
+ 0 && (module.exports = {
694
+ Mosaic,
695
+ Path,
696
+ getTransformCSS,
697
+ isTileComponentProps,
698
+ isTileProps,
699
+ swapItems,
700
+ useContainer,
701
+ useItemsWithOrigin,
702
+ useItemsWithPreview,
703
+ useMosaic
704
+ });
705
+ //# sourceMappingURL=index.cjs.map