Add helper to disable dragging on inner anchors and images #37
Replies: 4 comments
-
We could always go for the simple, and let folks handle the complex case on the consumption side (if needed). Something like this: let cleanup = disableDraggingOfInnerAnchorsAndImages({ element });
const observer = new MutationObserver(function onChange(
event: MutationRecord[],
) {
for (const record of event) {
if (record.type !== 'childList') {
return;
}
cleanup();
cleanup = disableDraggingOfInnerAnchorsAndImages({ element });
}
});
observer.observe(element, {
subtree: true,
childList: true,
attributes: false,
}); However, if we think the complex use case is common enough, baking it into the helper would likely yield a safer experience (people then don't need to think about this problem) |
Beta Was this translation helpful? Give feedback.
-
@danieldelcore any thoughts on which option to go for? ^ |
Beta Was this translation helpful? Give feedback.
-
WIP for simple case: import { type CleanupFn } from '../internal-types';
import { combine } from './combine';
function tryToBlockDragging(element: Element): CleanupFn | null {
if (
!(
element instanceof HTMLImageElement ||
element instanceof HTMLAnchorElement
)
) {
return null;
}
// dragging already disabled, don't need to disable
if (!element.draggable) {
return null;
}
element.draggable = false;
return function cleanup() {
element.draggable = true;
};
}
function isCleanupFn(value: CleanupFn | null): value is CleanupFn {
return Boolean(value);
}
export function disableDraggingOnInternalImagesAndAnchors(
element: HTMLElement,
): CleanupFn {
const cleanups: CleanupFn[] = Array.from(element.querySelectorAll('a,img'))
.map(tryToBlockDragging)
.filter(isCleanupFn);
return combine(...cleanups);
} Still unsure what to do with watching
|
Beta Was this translation helpful? Give feedback.
-
Hi @alexreardon |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
<a>
and<img>
elements are draggable by default. Often you want to set up adraggable({element})
that may contain<a>
and<img>
elements, but you don't want inner<a>
and<img>
elements to be draggable. I think we should create an optional utility function to help with this.Initial concept
Usage
Implementation 1. → Watch for child DOM changes
disableDraggingOfInnerAnchorsAndImages
, but it is super explicit for nowMutationObserver
sImplementation 2. → No child DOM watching
Beta Was this translation helpful? Give feedback.
All reactions