@dnd-kit/dom 0.0.2 → 0.0.3-beta-20240617115949

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.
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Sensor, configurator, Plugin, CorePlugin, DragDropManager as DragDropManager$1, Draggable as Draggable$1, descriptor, Droppable as Droppable$1 } from '@dnd-kit/abstract';
2
2
  import { effect, batch, effects, untracked, reactive, computed, deepEqual, signal } from '@dnd-kit/state';
3
- import { ScrollDirection, Listeners, getDocument, DOMRectangle, Styles, getWindow, createPlaceholder, isKeyboardEvent, supportsPopover, showPopover, supportsStyle, animateTransform, canScroll, detectScrollIntent, scheduler, getScrollableAncestors, scrollIntoViewIfNeeded, getFirstScrollableAncestor } from '@dnd-kit/dom/utilities';
3
+ import { ScrollDirection, Listeners, getDocument, DOMRectangle, Styles, getComputedStyles, createPlaceholder, isKeyboardEvent, parseTranslate, supportsPopover, showPopover, supportsStyle, animateTransform, canScroll, detectScrollIntent, scheduler, getScrollableAncestors, scrollIntoViewIfNeeded, getWindow, getFirstScrollableAncestor } from '@dnd-kit/dom/utilities';
4
4
  import { exceedsDistance, Axes } from '@dnd-kit/geometry';
5
5
  import { defaultCollisionDetection } from '@dnd-kit/collision';
6
6
 
@@ -191,7 +191,7 @@ var Cursor = class extends Plugin {
191
191
  var ATTR_PREFIX = "data-dnd-";
192
192
  var CSS_PREFIX = "--dnd-";
193
193
  var ATTRIBUTE = `${ATTR_PREFIX}dragging`;
194
- var cssRules = `[${ATTRIBUTE}] {position: fixed !important; pointer-events: none !important; touch-action: none !important; z-index: calc(infinity); will-change: transform;top: var(${CSS_PREFIX}top, 0px) !important;left: var(${CSS_PREFIX}left, 0px) !important;width: var(${CSS_PREFIX}width, auto) !important;height: var(${CSS_PREFIX}height, auto) !important;}[${ATTRIBUTE}][style*="${CSS_PREFIX}translate"] {translate: var(${CSS_PREFIX}translate) !important;}[style*="${CSS_PREFIX}transition"] {transition: var(${CSS_PREFIX}transition) !important;}*:where([${ATTRIBUTE}][popover]){overflow:visible;background:unset;border:unset;margin:unset;padding:unset;color:inherit;}[${ATTRIBUTE}]::backdrop {display: none}`;
194
+ var cssRules = `[${ATTRIBUTE}] {position: fixed !important; pointer-events: none !important; touch-action: none !important; z-index: calc(infinity); will-change: transform;top: var(${CSS_PREFIX}top, 0px) !important;left: var(${CSS_PREFIX}left, 0px) !important;width: var(${CSS_PREFIX}width, auto) !important;height: var(${CSS_PREFIX}height, auto) !important;}[${ATTRIBUTE}][style*="${CSS_PREFIX}translate"] {translate: var(${CSS_PREFIX}translate) !important;}[style*="${CSS_PREFIX}transition"] {transition: var(${CSS_PREFIX}transition) !important;}*:where([${ATTRIBUTE}][popover]){overflow:visible;background:var(${CSS_PREFIX}background);border:var(${CSS_PREFIX}border);margin:unset;padding:unset;color:inherit;}[${ATTRIBUTE}]::backdrop {display: none}`;
195
195
  var PLACEHOLDER_ATTRIBUTE = `${ATTR_PREFIX}placeholder`;
196
196
  var IGNORED_ATTRIBUTES = [ATTRIBUTE, PLACEHOLDER_ATTRIBUTE, "popover"];
197
197
  var IGNORED_STYLES = ["view-transition-name"];
@@ -200,6 +200,7 @@ var Feedback = class extends Plugin {
200
200
  super(manager);
201
201
  let style;
202
202
  let initialCoordinates;
203
+ let initialTranslate = { x: 0, y: 0 };
203
204
  let currentTransform;
204
205
  let transformOrigin;
205
206
  let moved = false;
@@ -218,7 +219,7 @@ var Feedback = class extends Plugin {
218
219
  initialCoordinates = void 0;
219
220
  currentTransform = void 0;
220
221
  transformOrigin = void 0;
221
- moved = false;
222
+ initialTranslate = { x: 0, y: 0 };
222
223
  return;
223
224
  }
224
225
  if (!source)
@@ -229,15 +230,21 @@ var Feedback = class extends Plugin {
229
230
  const shape = new DOMRectangle(element, true);
230
231
  const { width, height, top, left } = shape;
231
232
  const styles = new Styles(element);
232
- const { transition } = getWindow(element).getComputedStyle(element);
233
+ const { background, border, transition, translate } = getComputedStyles(element);
233
234
  const droppable = manager.registry.droppables.get(source.id);
234
235
  const clone = feedback === "clone";
235
- const placeholder = createPlaceholder(element, clone, {
236
+ const placeholder = feedback !== "move" ? createPlaceholder(element, clone, {
236
237
  [PLACEHOLDER_ATTRIBUTE]: ""
237
- });
238
+ }) : null;
238
239
  const isKeyboardOperation = untracked(
239
240
  () => isKeyboardEvent(manager.dragOperation.activatorEvent)
240
241
  );
242
+ if (translate !== "none") {
243
+ const parsedTranslate = parseTranslate(translate);
244
+ if (parsedTranslate) {
245
+ initialTranslate = parsedTranslate;
246
+ }
247
+ }
241
248
  if (!initialCoordinates) {
242
249
  initialCoordinates = {
243
250
  x: left,
@@ -266,11 +273,14 @@ var Feedback = class extends Plugin {
266
273
  height,
267
274
  top: projected.top,
268
275
  left: projected.left,
276
+ background,
277
+ border,
269
278
  translate: currentTransform ? `${currentTransform.x}px ${currentTransform.y}px 0` : ""
270
279
  },
271
280
  CSS_PREFIX
272
281
  );
273
- element.insertAdjacentElement("afterend", placeholder);
282
+ if (placeholder)
283
+ element.insertAdjacentElement("afterend", placeholder);
274
284
  if (supportsPopover(element)) {
275
285
  if (!element.hasAttribute("popover")) {
276
286
  element.setAttribute("popover", "");
@@ -282,7 +292,7 @@ var Feedback = class extends Plugin {
282
292
  top: projected.top - actual.top,
283
293
  left: projected.left - actual.left
284
294
  };
285
- if (Math.abs(offset.top) || Math.abs(offset.left)) {
295
+ if (offset.left > 0.01 || offset.top > 0.01) {
286
296
  styles.set(
287
297
  {
288
298
  top: projected.top + offset.top,
@@ -290,8 +300,13 @@ var Feedback = class extends Plugin {
290
300
  },
291
301
  CSS_PREFIX
292
302
  );
303
+ } else {
304
+ offset.left = 0;
305
+ offset.top = 0;
293
306
  }
294
307
  const resizeObserver = new ResizeObserver(() => {
308
+ if (!placeholder)
309
+ return;
295
310
  const placeholderShape = new DOMRectangle(placeholder, true);
296
311
  const origin = transformOrigin ?? { x: 1, y: 1 };
297
312
  const dX = (width - placeholderShape.width) * origin.x + delta.x;
@@ -315,60 +330,63 @@ var Feedback = class extends Plugin {
315
330
  }
316
331
  manager.dragOperation.shape = new DOMRectangle(element);
317
332
  });
318
- if (droppable) {
333
+ if (droppable && placeholder) {
319
334
  droppable.placeholder = placeholder;
320
335
  untracked(droppable.refreshShape);
321
336
  }
322
- dragOperation.shape = shape;
323
- resizeObserver.observe(placeholder);
324
- const elementMutationObserver = new MutationObserver(() => {
325
- for (const attribute of Array.from(element.attributes)) {
326
- if (attribute.name.startsWith("aria-") || IGNORED_ATTRIBUTES.includes(attribute.name)) {
327
- continue;
328
- }
329
- if (attribute.name === "style") {
330
- if (supportsStyle(element) && supportsStyle(placeholder)) {
331
- placeholder.setAttribute("style", clone ? "" : "opacity: 0;");
332
- placeholder.style.setProperty("transition", "none");
333
- for (const key of Object.values(element.style)) {
334
- if (key.startsWith(CSS_PREFIX) || IGNORED_STYLES.includes(key)) {
335
- continue;
337
+ dragOperation.shape = new DOMRectangle(element);
338
+ let elementMutationObserver;
339
+ let documentMutationObserver;
340
+ if (placeholder) {
341
+ resizeObserver.observe(placeholder);
342
+ elementMutationObserver = new MutationObserver(() => {
343
+ for (const attribute of Array.from(element.attributes)) {
344
+ if (attribute.name.startsWith("aria-") || IGNORED_ATTRIBUTES.includes(attribute.name)) {
345
+ continue;
346
+ }
347
+ if (attribute.name === "style") {
348
+ if (supportsStyle(element) && supportsStyle(placeholder)) {
349
+ placeholder.setAttribute("style", clone ? "" : "opacity: 0;");
350
+ placeholder.style.setProperty("transition", "none");
351
+ for (const key of Object.values(element.style)) {
352
+ if (key.startsWith(CSS_PREFIX) || IGNORED_STYLES.includes(key)) {
353
+ continue;
354
+ }
355
+ placeholder.style.setProperty(
356
+ key,
357
+ element.style.getPropertyValue(key)
358
+ );
336
359
  }
337
- placeholder.style.setProperty(
338
- key,
339
- element.style.getPropertyValue(key)
340
- );
341
360
  }
361
+ continue;
342
362
  }
343
- continue;
363
+ placeholder.setAttribute(attribute.name, attribute.value);
344
364
  }
345
- placeholder.setAttribute(attribute.name, attribute.value);
346
- }
347
- if (clone) {
348
- placeholder.innerHTML = element.innerHTML;
349
- }
350
- });
351
- elementMutationObserver.observe(element, {
352
- attributes: true,
353
- subtree: true
354
- });
355
- const documentMutationObserver = new MutationObserver((entries) => {
356
- for (const entry of entries) {
357
- if (Array.from(entry.addedNodes).includes(element)) {
358
- element.insertAdjacentElement("afterend", placeholder);
359
- showPopover(element);
360
- return;
365
+ if (clone) {
366
+ placeholder.innerHTML = element.innerHTML;
361
367
  }
362
- }
363
- });
364
- documentMutationObserver.observe(element.ownerDocument.body, {
365
- childList: true,
366
- subtree: true
367
- });
368
+ });
369
+ elementMutationObserver.observe(element, {
370
+ attributes: true,
371
+ subtree: true
372
+ });
373
+ documentMutationObserver = new MutationObserver((entries) => {
374
+ for (const entry of entries) {
375
+ if (Array.from(entry.addedNodes).includes(element)) {
376
+ element.insertAdjacentElement("afterend", placeholder);
377
+ showPopover(element);
378
+ return;
379
+ }
380
+ }
381
+ });
382
+ documentMutationObserver.observe(element.ownerDocument.body, {
383
+ childList: true,
384
+ subtree: true
385
+ });
386
+ }
368
387
  const cleanupEffect2 = effect(function updateTransform() {
369
388
  const { transform, status: status2 } = dragOperation;
370
- const { x, y } = transform;
371
- if (!x && !y && !moved) {
389
+ if (!transform.x && !transform.y && !moved) {
372
390
  return;
373
391
  }
374
392
  if (!moved) {
@@ -376,6 +394,8 @@ var Feedback = class extends Plugin {
376
394
  }
377
395
  if (status2.dragging) {
378
396
  const translateTransition = isKeyboardOperation ? "250ms ease" : "0ms linear";
397
+ const x = transform.x + initialTranslate.x;
398
+ const y = transform.y + initialTranslate.y;
379
399
  const shape2 = new DOMRectangle(element);
380
400
  styles.set(
381
401
  {
@@ -409,14 +429,14 @@ var Feedback = class extends Plugin {
409
429
  }
410
430
  };
411
431
  let cleanup = () => {
412
- elementMutationObserver.disconnect();
413
- documentMutationObserver.disconnect();
432
+ elementMutationObserver?.disconnect();
433
+ documentMutationObserver?.disconnect();
414
434
  resizeObserver.disconnect();
415
435
  styles.reset();
416
436
  if (moved && element.isConnected) {
417
- placeholder.replaceWith(element);
437
+ placeholder?.replaceWith(element);
418
438
  }
419
- placeholder.remove();
439
+ placeholder?.remove();
420
440
  element.removeAttribute(ATTRIBUTE);
421
441
  if (supportsPopover(element)) {
422
442
  element.removeAttribute("popover");
@@ -426,6 +446,7 @@ var Feedback = class extends Plugin {
426
446
  if (droppable) {
427
447
  droppable.placeholder = void 0;
428
448
  }
449
+ moved = false;
429
450
  };
430
451
  const dropEffectCleanup = effect(function dropAnimation() {
431
452
  if (dragOperation.status.dropped) {
@@ -438,7 +459,9 @@ var Feedback = class extends Plugin {
438
459
  }
439
460
  manager.renderer.rendering.then(() => {
440
461
  showPopover(element);
441
- const shape2 = new DOMRectangle(placeholder, true);
462
+ const target = placeholder ?? element;
463
+ styles.remove(["translate"], CSS_PREFIX);
464
+ const shape2 = new DOMRectangle(target);
442
465
  const currentShape = new DOMRectangle(element, true).translate(
443
466
  transform.x,
444
467
  transform.y
@@ -464,9 +487,6 @@ var Feedback = class extends Plugin {
464
487
  duration: moved ? 250 : 0,
465
488
  easing: "ease"
466
489
  },
467
- onReady() {
468
- styles.remove(["translate", "transition"], CSS_PREFIX);
469
- },
470
490
  onFinish() {
471
491
  requestAnimationFrame(restoreFocus);
472
492
  onComplete?.();
@@ -1331,12 +1351,11 @@ var Droppable = class extends Droppable$1 {
1331
1351
  return this.placeholder ?? this.internal?.element.value;
1332
1352
  }
1333
1353
  refreshShape(ignoreTransform) {
1334
- const { element } = this;
1354
+ const { element, shape } = this;
1335
1355
  if (!element || this.visible === false) {
1336
1356
  this.shape = void 0;
1337
1357
  return void 0;
1338
1358
  }
1339
- const { shape } = this;
1340
1359
  const updatedShape = new DOMRectangle(element, ignoreTransform);
1341
1360
  if (updatedShape && shape?.equals(updatedShape)) {
1342
1361
  return shape;