(node, e)
| 10 | * @returns {boolean} |
| 11 | */ |
| 12 | const doesNodeContainClick = (node, e) => { |
| 13 | if (_.some([e, node], _.isNil)) return false |
| 14 | |
| 15 | // if there is an e.target and it is in the document, use a simple node.contains() check |
| 16 | if (e.target) { |
| 17 | _.invoke(e.target, 'setAttribute', 'data-suir-click-target', true) |
| 18 | |
| 19 | if (document.querySelector('[data-suir-click-target=true]')) { |
| 20 | _.invoke(e.target, 'removeAttribute', 'data-suir-click-target') |
| 21 | return node.contains(e.target) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // Below logic handles cases where the e.target is no longer in the document. |
| 26 | // The result of the click likely has removed the e.target node. |
| 27 | // Instead of node.contains(), we'll identify the click by X/Y position. |
| 28 | |
| 29 | // return early if the event properties aren't available |
| 30 | // prevent measuring the node and repainting if we don't need to |
| 31 | const { clientX, clientY } = e |
| 32 | if (_.some([clientX, clientY], _.isNil)) return false |
| 33 | |
| 34 | // false if the node is not visible |
| 35 | const clientRects = node.getClientRects() |
| 36 | // Heads Up! |
| 37 | // getClientRects returns a DOMRectList, not an array nor a plain object |
| 38 | // We explicitly avoid _.isEmpty and check .length to cover all possible shapes |
| 39 | if (!node.offsetWidth || !node.offsetHeight || !clientRects || !clientRects.length) return false |
| 40 | |
| 41 | // false if the node doesn't have a valid bounding rect |
| 42 | const { top, bottom, left, right } = _.first(clientRects) |
| 43 | if (_.some([top, bottom, left, right], _.isNil)) return false |
| 44 | |
| 45 | // we add a small decimal to the upper bound just to make it inclusive |
| 46 | // don't add an whole pixel (1) as the event/node values may be decimal sensitive |
| 47 | return _.inRange(clientY, top, bottom + 0.001) && _.inRange(clientX, left, right + 0.001) |
| 48 | } |
| 49 | |
| 50 | export default doesNodeContainClick |
no outgoing calls
no test coverage detected
searching dependent graphs…