|
1 | 1 | function isElementClickable(element) { |
2 | 2 | if (!element.getBoundingClientRect || !element.scrollIntoView || !element.contains || !element.getClientRects || !document.elementFromPoint) { |
3 | | - return false; |
| 3 | + return false |
4 | 4 | } |
5 | 5 |
|
6 | 6 | const getOverlappingElement = (element, context = document) => { |
7 | | - const elemDimension = element.getBoundingClientRect(); |
8 | | - const x = elemDimension.left + (element.clientWidth / 2); |
9 | | - const y = elemDimension.top + (element.clientHeight / 2); |
| 7 | + const elemDimension = element.getBoundingClientRect() |
| 8 | + const x = elemDimension.left + element.clientWidth / 2 |
| 9 | + const y = elemDimension.top + element.clientHeight / 2 |
10 | 10 |
|
11 | | - return context.elementFromPoint(x, y); |
12 | | - }; |
| 11 | + return context.elementFromPoint(x, y) |
| 12 | + } |
13 | 13 |
|
14 | 14 | const getOverlappingRects = (element, context = document) => { |
15 | | - const rects = element.getClientRects(); |
16 | | - const rect = rects[0]; |
17 | | - const x = rect.left + (rect.width / 2); |
18 | | - const y = rect.top + (rect.height / 2); |
| 15 | + const rects = element.getClientRects() |
| 16 | + const rect = rects[0] |
| 17 | + const x = rect.left + rect.width / 2 |
| 18 | + const y = rect.top + rect.height / 2 |
19 | 19 |
|
20 | | - return context.elementFromPoint(x, y); |
21 | | - }; |
| 20 | + return context.elementFromPoint(x, y) |
| 21 | + } |
22 | 22 |
|
23 | 23 | const getOverlappingElements = (element, context) => { |
24 | | - return [getOverlappingElement(element, context), getOverlappingRects(element, context)]; |
25 | | - }; |
| 24 | + return [getOverlappingElement(element, context), getOverlappingRects(element, context)] |
| 25 | + } |
26 | 26 |
|
27 | 27 | const isOverlappingElementMatch = (elementsFromPoint, element) => { |
28 | 28 | if (elementsFromPoint.some(elementFromPoint => elementFromPoint === element || element.contains(elementFromPoint))) { |
29 | | - return true; |
| 29 | + return true |
30 | 30 | } |
31 | 31 |
|
32 | | - let elementsWithShadowRoot = [...new Set(elementsFromPoint)]; |
33 | | - elementsWithShadowRoot = elementsWithShadowRoot.filter(elem => elem && elem.shadowRoot && elem.shadowRoot.elementFromPoint); |
| 32 | + let elementsWithShadowRoot = [...new Set(elementsFromPoint)] |
| 33 | + elementsWithShadowRoot = elementsWithShadowRoot.filter(elem => elem && elem.shadowRoot && elem.shadowRoot.elementFromPoint) |
34 | 34 |
|
35 | | - let shadowElementsFromPoint = []; |
| 35 | + let shadowElementsFromPoint = [] |
36 | 36 | for (const shadowElement of elementsWithShadowRoot) { |
37 | | - shadowElementsFromPoint.push(...getOverlappingElements(element, shadowElement.shadowRoot)); |
| 37 | + shadowElementsFromPoint.push(...getOverlappingElements(element, shadowElement.shadowRoot)) |
38 | 38 | } |
39 | | - shadowElementsFromPoint = [...new Set(shadowElementsFromPoint)]; |
40 | | - shadowElementsFromPoint = shadowElementsFromPoint.filter(element => !elementsFromPoint.includes(element)); |
| 39 | + shadowElementsFromPoint = [...new Set(shadowElementsFromPoint)] |
| 40 | + shadowElementsFromPoint = shadowElementsFromPoint.filter(element => !elementsFromPoint.includes(element)) |
41 | 41 |
|
42 | 42 | if (shadowElementsFromPoint.length === 0) { |
43 | | - return false; |
| 43 | + return false |
44 | 44 | } |
45 | 45 |
|
46 | | - return isOverlappingElementMatch(shadowElementsFromPoint, element); |
47 | | - }; |
| 46 | + return isOverlappingElementMatch(shadowElementsFromPoint, element) |
| 47 | + } |
48 | 48 |
|
49 | | - const isElementInViewport = (element) => { |
50 | | - const rect = element.getBoundingClientRect(); |
| 49 | + const isElementInViewport = element => { |
| 50 | + const rect = element.getBoundingClientRect() |
51 | 51 |
|
52 | | - const windowHeight = (window.innerHeight || document.documentElement.clientHeight); |
53 | | - const windowWidth = (window.innerWidth || document.documentElement.clientWidth); |
| 52 | + const windowHeight = window.innerHeight || document.documentElement.clientHeight |
| 53 | + const windowWidth = window.innerWidth || document.documentElement.clientWidth |
54 | 54 |
|
55 | | - const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) > 0); |
56 | | - const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) > 0); |
| 55 | + const vertInView = rect.top <= windowHeight && rect.top + rect.height > 0 |
| 56 | + const horInView = rect.left <= windowWidth && rect.left + rect.width > 0 |
57 | 57 |
|
58 | | - return (vertInView && horInView); |
59 | | - }; |
| 58 | + return vertInView && horInView |
| 59 | + } |
60 | 60 |
|
61 | | - return element.disabled !== true && isElementInViewport(element) && isOverlappingElementMatch(getOverlappingElements(element), element); |
| 61 | + return element.disabled !== true && isElementInViewport(element) && isOverlappingElementMatch(getOverlappingElements(element), element) |
62 | 62 | } |
63 | 63 |
|
64 | | -module.exports = isElementClickable; |
| 64 | +export default isElementClickable |
0 commit comments