Last active
March 9, 2022 15:43
-
-
Save offlinehacker/4f449662532d668f025aa987175716b9 to your computer and use it in GitHub Desktop.
DOM functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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