Skip to content

Commit

Permalink
Add config option to fix scrollbar clicking
Browse files Browse the repository at this point in the history
As mentioned in cibernox#409, clicking a scrollbar will close an open basic-dropdown. This is due to mousedown being used instead of click. To not break existing behaviour, I've added a config option which can be passed to the parent container (basic-dropdown) or the content component.
  • Loading branch information
k-fish committed Aug 29, 2018
1 parent ecb6c75 commit 254b4c1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
14 changes: 12 additions & 2 deletions addon/components/basic-dropdown/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ export default Component.extend({
let dropdown = this.get('dropdown');
this.triggerElement = this.triggerElement || document.querySelector(`[data-ebd-id=${dropdown.uniqueId}-trigger]`);
this.dropdownElement = document.getElementById(this.dropdownId);
document.addEventListener('mousedown', this.handleRootMouseDown, true);
if (this.get('useClickEvent')) {
document.addEventListener('click', this.handleRootMouseDown, true);
} else {
document.addEventListener('mousedown', this.handleRootMouseDown, true);
}

if (this.get('isTouchDevice')) {
document.addEventListener('touchstart', this.touchStartHandler, true);
document.addEventListener('touchend', this.handleRootMouseDown, true);
Expand Down Expand Up @@ -360,7 +365,12 @@ export default Component.extend({
this.removeScrollHandling();
this.scrollableAncestors = [];
this.stopObservingDomMutations();
document.removeEventListener('mousedown', this.handleRootMouseDown, true);

if (this.get('useClickEvent')) {
document.removeEventListener('click', this.handleRootMouseDown, true);
} else {
document.removeEventListener('mousedown', this.handleRootMouseDown, true);
}
if (this.get('isTouchDevice')) {
document.removeEventListener('touchstart', this.touchStartHandler, true);
document.removeEventListener('touchend', this.handleRootMouseDown, true);
Expand Down
1 change: 1 addition & 0 deletions addon/templates/components/basic-dropdown.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
hPosition=(readonly hPosition)
renderInPlace=(readonly renderInPlace)
preventScroll=(readonly preventScroll)
useClickEvent=(readonly useClickEvent)
vPosition=(readonly vPosition)
destination=(readonly destination)
top=(readonly top)
Expand Down
44 changes: 44 additions & 0 deletions tests/integration/components/basic-dropdown/content-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,50 @@ module('Integration | Component | basic-dropdown/content', function(hooks) {
await click('#other-div');
});

// Mousedown while the component is opened (scrollbar fix)
test('Using mouse down with useClickEvent being true will not close a component if it is opened', async function(assert) {
assert.expect(0);
this.dropdown = {
uniqueId: 'e123',
isOpen: true,
actions: {
close() {
assert.ok(true, 'The close action should not be called');
},
reposition() {}
}
};
await render(hbs`
<div id="destination-el"></div>
<div id="other-div"></div>
{{#basic-dropdown/content useClickEvent=true dropdown=dropdown destination='destination-el'}}Lorem ipsum{{/basic-dropdown/content}}
`);

await triggerEvent('#other-div', 'mousedown');
});

// Mousedown while the component is opened (scrollbar fix)
test('Using mouse down with useClickEvent being false will close a component if it is opened', async function(assert) {
assert.expect(1);
this.dropdown = {
uniqueId: 'e123',
isOpen: true,
actions: {
close() {
assert.ok(true, 'The close action gets called');
},
reposition() {}
}
};
await render(hbs`
<div id="destination-el"></div>
<div id="other-div"></div>
{{#basic-dropdown/content useClickEvent=false dropdown=dropdown destination='destination-el'}}Lorem ipsum{{/basic-dropdown/content}}
`);

await triggerEvent('#other-div', 'mousedown');
});

test('Clicking anywhere inside the dropdown content doesn\'t invoke the close action', async function(assert) {
assert.expect(0);
this.dropdown = {
Expand Down

0 comments on commit 254b4c1

Please sign in to comment.