Skip to content

Instantly share code, notes, and snippets.

@offlinehacker
Last active March 9, 2022 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save offlinehacker/4f449662532d668f025aa987175716b9 to your computer and use it in GitHub Desktop.
Save offlinehacker/4f449662532d668f025aa987175716b9 to your computer and use it in GitHub Desktop.
DOM functions
export const getScrollParent = (node?: Element | null): Element | null => {
if (node == null) {
return null;
}
if (node.scrollHeight > node.clientHeight) {
return node;
} else {
return getScrollParent(asElement(node.parentNode));
}
};
export const isClickInside = (event: MouseEvent, element: HTMLElement) => {
if (!element || !event) return false;
const rect = element.getBoundingClientRect();
return (
event.clientX > rect.left &&
event.clientX < rect.right &&
event.clientY > rect.top &&
event.clientY < rect.bottom
);
};
export const isEventSupported = (name: string, el?: HTMLElement) => {
if (el) {
return `on${name}` in el;
}
el = document.createElement("div");
const supported = `on${name}` in el;
el.remove();
return supported;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment