Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(controls): stop event propagation from controls #1210

Merged
merged 3 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/lib/Controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class Controls {
this.controlsEl.addEventListener('mouseleave', this.mouseleaveHandler);
this.controlsEl.addEventListener('focusin', this.focusinHandler);
this.controlsEl.addEventListener('focusout', this.focusoutHandler);
this.controlsEl.addEventListener('click', this.clickHandler);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move the associated removeEventListener call out of its conditional check as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this component is used in all of the viewers across Preview, so this is a wide-ranging change. Does it have any side effects? Have we tested all the various viewers?


if (this.hasTouch) {
this.containerEl.addEventListener('touchstart', this.mousemoveHandler);
this.controlsEl.addEventListener('click', this.clickHandler);
}
}

Expand All @@ -61,10 +61,10 @@ class Controls {
this.controlsEl.removeEventListener('mouseleave', this.mouseleaveHandler);
this.controlsEl.removeEventListener('focusin', this.focusinHandler);
this.controlsEl.removeEventListener('focusout', this.focusoutHandler);
this.controlsEl.removeEventListener('click', this.clickHandler);

if (this.hasTouch) {
this.containerEl.removeEventListener('touchstart', this.mousemoveHandler);
this.controlsEl.removeEventListener('click', this.clickHandler);
}

this.buttonRefs.forEach(ref => {
Expand Down Expand Up @@ -175,9 +175,14 @@ class Controls {
* @param {Event} event - A DOM-normalized event object.
* @return {void}
*/
clickHandler = () => {
// If we are not focused in on the page num input, allow hiding after timeout
this.shouldHide = true;
clickHandler = event => {
if (event) {
event.stopPropagation();
}

if (this.hasTouch) {
jstoffan marked this conversation as resolved.
Show resolved Hide resolved
this.shouldHide = true;
}
};

/**
Expand Down
48 changes: 48 additions & 0 deletions src/lib/__tests__/Controls-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-unused-expressions */
import Browser from '../Browser';
import Controls from '../Controls';
import { CLASS_HIDDEN } from '../constants';

Expand Down Expand Up @@ -32,11 +33,48 @@ describe('lib/Controls', () => {
});

describe('constructor()', () => {
let container;
let containerElEventListener;
let controlsElEventListener;

beforeEach(() => {
container = document.getElementById('test-controls-container');
const controlElement = document.createElement('div');
controlsElEventListener = sandbox.stub(controlElement, 'addEventListener');
sandbox.stub(container, 'appendChild').returns(controlElement);
containerElEventListener = sandbox.stub(container, 'addEventListener');
});

afterEach(() => {
container = undefined;
containerElEventListener = undefined;
controlsElEventListener = undefined;
});

it('should create the correct DOM structure', () => {
expect(controls.containerEl).to.equal(document.getElementById('test-controls-container'));

expect(controls.controlsEl.classList.contains('bp-controls')).to.true;
});

it('should add the correct event listeners', () => {
sandbox.stub(Browser, 'hasTouch').returns(false);
controls = new Controls(container);

expect(containerElEventListener).to.be.calledWith('mousemove', controls.mousemoveHandler);
expect(controlsElEventListener).to.be.calledWith('mouseenter', controls.mouseenterHandler);
expect(controlsElEventListener).to.be.calledWith('mouseleave', controls.mouseleaveHandler);
expect(controlsElEventListener).to.be.calledWith('focusin', controls.focusinHandler);
expect(controlsElEventListener).to.be.calledWith('focusout', controls.focusoutHandler);
expect(controlsElEventListener).to.be.calledWith('click', controls.clickHandler);
});

it('should add the correct event listeners when browser has touch', () => {
sandbox.stub(Browser, 'hasTouch').returns(true);
controls = new Controls(container);

expect(containerElEventListener).to.be.calledWith('touchstart', controls.mousemoveHandler);
});
});

describe('destroy()', () => {
Expand Down Expand Up @@ -249,9 +287,19 @@ describe('lib/Controls', () => {

describe('clickHandler()', () => {
it('should stop block hiding', () => {
controls.hasTouch = true;

controls.clickHandler();
expect(controls.shouldHide).to.be.true;
});

it('should call stopPropagation on event when called', () => {
const stopPropagation = sandbox.stub();

controls.clickHandler({ stopPropagation });

expect(stopPropagation).to.be.called;
});
});

describe('add()', () => {
Expand Down