Skip to content

Commit

Permalink
fix(doesNodeContainClick): only use x/y if !e.target
Browse files Browse the repository at this point in the history
  • Loading branch information
levithomason committed Feb 5, 2018
1 parent 3341abd commit 89217ef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/lib/doesNodeContainClick.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@ import _ from 'lodash'
const doesNodeContainClick = (node, e) => {
if (_.some([e, node], _.isNil)) return false

// first check if the node contains the e.target, simplest use case
if (node.contains(e.target)) return true
// if there is an e.target and it is in the document, use a simple node.contains() check
if (e.target) {
e.target.setAttribute('data-suir-click-target', true)

if (document.querySelector('[data-suir-click-target=true]')) {
e.target.removeAttribute('data-suir-click-target')
return node.contains(e.target)
}
}

// Below logic handles cases where the e.target is no longer in the document.
// The result of the click likely has removed the e.target node.
// Instead of node.contains(), we'll identify the click by X/Y position.

// return early if the event properties aren't available
// prevent measuring the node and repainting if we don't need to
Expand Down
27 changes: 27 additions & 0 deletions test/specs/lib/doesNodeContainClick-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ describe('doesNodeContainClick', () => {
})
})

describe('e.target', () => {
it('returns node.contains(e.target) if exists', () => {
const node = makeNode()
const target = { fake: 'target node' }
const event = makeEvent({ target })

node.contains.should.not.have.been.called()

doesNodeContainClick(node, event)

node.contains.should.have.been.calledOnce()
node.contains.should.have.been.calledWithExactly(event.target)
})

it("does not call node.contains(e.target) if doesn't exist", () => {
const node = makeNode()
const target = null
const event = makeEvent({ target })

node.contains.should.not.have.been.called()

doesNodeContainClick(node, event)

node.contains.should.not.have.been.called()
})
})

describe('nil event properties', () => {
it('returns false if the e.clientX is nil', () => {
doesNodeContainClick(makeNode(), { clientX: null }).should.equal(false)
Expand Down

0 comments on commit 89217ef

Please sign in to comment.