Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

fix: add Shadow DOM support for touch dragging

Wes754822191353
diff --git a/src/draggable.js b/src/draggable.js
index 367f374f..461957c1 100644
--- a/src/draggable.js
+++ b/src/draggable.js
@@ -936,17 +936,23 @@ export class Draggable {
let canTouchScroll = false;
while (touches && $parent && $parent !== this.$trigger) {
- const overflowY = getTargetValue($parent, 'overflow-y');
- if (overflowY !== 'hidden' && overflowY !== 'visible') {
- const { scrollTop, scrollHeight, clientHeight } = $parent;
- if (scrollHeight > clientHeight) {
- canTouchScroll = true;
- isAtTop = scrollTop <= 3;
- isAtBottom = scrollTop >= (scrollHeight - clientHeight) - 3;
- break;
+ if ($parent.nodeType === Node.ELEMENT_NODE) {
+ const overflowY = getTargetValue($parent, 'overflow-y');
+ if (overflowY !== 'hidden' && overflowY !== 'visible') {
+ const { scrollTop, scrollHeight, clientHeight } = $parent;
+ if (scrollHeight > clientHeight) {
+ canTouchScroll = true;
+ isAtTop = scrollTop <= 3;
+ isAtBottom = scrollTop >= (scrollHeight - clientHeight) - 3;
+ break;
+ }
}
}
- $parent = /** @type {HTMLElement} */($parent.parentNode);
+ const nextParent = $parent.parentNode;
+ if (!nextParent || nextParent.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
+ break;
+ }
+ $parent = /** @type {HTMLElement} */(nextParent);
}
if (canTouchScroll && ((!isAtTop && !isAtBottom) || (isAtTop && movedY < 0) || (isAtBottom && movedY > 0))) {
diff --git a/tests/suites/draggables.test.js b/tests/suites/draggables.test.js
index 995eb3b7..03851b5b 100644
--- a/tests/suites/draggables.test.js
+++ b/tests/suites/draggables.test.js
@@ -91,4 +91,58 @@ suite('Draggables', () => {
duration: 50,
});
});
+
+ test('Touch dragging should work in Shadow DOM', resolve => {
+ const $container = document.querySelector('#css-tests');
+ const $host = document.createElement('div');
+ $container.appendChild($host);
+
+ const shadowRoot = $host.attachShadow({ mode: 'open' });
+ const $target = document.createElement('div');
+ $target.style.cssText = 'width: 100px; height: 100px;';
+ shadowRoot.appendChild($target);
+
+ let updates = 0;
+
+ const draggable = createDraggable($target, {
+ onUpdate: () => updates++,
+ onSettle: () => {
+ expect(updates).to.be.above(0);
+ draggable.revert();
+ $host.remove();
+ resolve();
+ }
+ });
+
+ const createTouchEvent = ($el, type, x, y) => {
+ const touch = new Touch({
+ identifier: 1,
+ target: $el,
+ clientX: x,
+ clientY: y,
+ pageX: x,
+ pageY: y,
+ });
+
+ $el.dispatchEvent(new TouchEvent('touch' + type, {
+ touches: type === 'end' ? [] : [touch],
+ changedTouches: [touch],
+ bubbles: true,
+ composed: true,
+ cancelable: true
+ }));
+ };
+
+ createTouchEvent($target, 'start', 50, 50);
+
+ createTimer({
+ duration: 50,
+ onUpdate: self => {
+ createTouchEvent($target, 'move', 50, 50 + (self.progress * 50));
+ },
+ onComplete: () => {
+ createTouchEvent($target, 'end', 50, 100);
+ }
+ });
+ });
});