fix(button): click event fired twice in IE11 (fix #179)
This commit is contained in:
parent
ee8dc3122a
commit
e269b5d040
2 changed files with 36 additions and 25 deletions
|
|
@ -20,7 +20,7 @@ export class LionButton extends DisabledWithTabIndexMixin(
|
||||||
<slot></slot>
|
<slot></slot>
|
||||||
${this._renderAfter()}
|
${this._renderAfter()}
|
||||||
<slot name="_button"></slot>
|
<slot name="_button"></slot>
|
||||||
<div class="click-area" @click="${this.__clickDelegationHandler}"></div>
|
<div class="click-area"></div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
@ -128,6 +128,7 @@ export class LionButton extends DisabledWithTabIndexMixin(
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.role = 'button';
|
this.role = 'button';
|
||||||
|
this.__setupDelegationInConstructor();
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
|
|
@ -143,17 +144,20 @@ export class LionButton extends DisabledWithTabIndexMixin(
|
||||||
_redispatchClickEvent(oldEvent) {
|
_redispatchClickEvent(oldEvent) {
|
||||||
// replacing `MouseEvent` with `oldEvent.constructor` breaks IE
|
// replacing `MouseEvent` with `oldEvent.constructor` breaks IE
|
||||||
const newEvent = new MouseEvent(oldEvent.type, oldEvent);
|
const newEvent = new MouseEvent(oldEvent.type, oldEvent);
|
||||||
|
newEvent.__isRedispatchedOnNativeButton = true;
|
||||||
this.__enforceHostEventTarget(newEvent);
|
this.__enforceHostEventTarget(newEvent);
|
||||||
this.$$slot('_button').dispatchEvent(newEvent);
|
this.$$slot('_button').dispatchEvent(newEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prevent click on the fake element and cause click on the native button.
|
* Prevent normal click and redispatch click on the native button unless already redispatched.
|
||||||
*/
|
*/
|
||||||
__clickDelegationHandler(e) {
|
__clickDelegationHandler(e) {
|
||||||
e.stopPropagation();
|
if (!e.__isRedispatchedOnNativeButton) {
|
||||||
|
e.stopImmediatePropagation();
|
||||||
this._redispatchClickEvent(e);
|
this._redispatchClickEvent(e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
__enforceHostEventTarget(event) {
|
__enforceHostEventTarget(event) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -165,6 +169,12 @@ export class LionButton extends DisabledWithTabIndexMixin(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__setupDelegationInConstructor() {
|
||||||
|
// do not move to connectedCallback, otherwise IE11 breaks
|
||||||
|
// more info: https://github.com/ing-bank/lion/issues/179#issuecomment-511763835
|
||||||
|
this.addEventListener('click', this.__clickDelegationHandler, true);
|
||||||
|
}
|
||||||
|
|
||||||
__setupDelegation() {
|
__setupDelegation() {
|
||||||
this.addEventListener('keydown', this.__keydownDelegationHandler);
|
this.addEventListener('keydown', this.__keydownDelegationHandler);
|
||||||
this.addEventListener('keyup', this.__keyupDelegationHandler);
|
this.addEventListener('keyup', this.__keyupDelegationHandler);
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,11 @@ import {
|
||||||
import '../lion-button.js';
|
import '../lion-button.js';
|
||||||
|
|
||||||
function getTopElement(el) {
|
function getTopElement(el) {
|
||||||
const { left, top } = el.getBoundingClientRect();
|
const { left, top, width, height } = el.getBoundingClientRect();
|
||||||
// to support elementFromPoint() in polyfilled browsers we have to use document
|
// to support elementFromPoint() in polyfilled browsers we have to use document
|
||||||
const crossBrowserRoot = el.shadowRoot.elementFromPoint ? el.shadowRoot : document;
|
const crossBrowserRoot =
|
||||||
return crossBrowserRoot.elementFromPoint(left, top);
|
el.shadowRoot && el.shadowRoot.elementFromPoint ? el.shadowRoot : document;
|
||||||
|
return crossBrowserRoot.elementFromPoint(left + width / 2, top + height / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('lion-button', () => {
|
describe('lion-button', () => {
|
||||||
|
|
@ -151,7 +152,7 @@ describe('lion-button', () => {
|
||||||
const clickSpy = sinon.spy();
|
const clickSpy = sinon.spy();
|
||||||
const el = await fixture(
|
const el = await fixture(
|
||||||
html`
|
html`
|
||||||
<lion-button @click="${clickSpy}"></lion-button>
|
<lion-button @click="${clickSpy}">foo</lion-button>
|
||||||
`,
|
`,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -164,27 +165,22 @@ describe('lion-button', () => {
|
||||||
expect(clickSpy.callCount).to.equal(1);
|
expect(clickSpy.callCount).to.equal(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('event after redispatching', async () => {
|
describe('native button behavior', async () => {
|
||||||
async function prepareClickEvent(el, host) {
|
async function prepareClickEvent(el) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (host) {
|
|
||||||
// click on host like in native button
|
|
||||||
makeMouseEvent('click', { x: 11, y: 11 }, el);
|
|
||||||
} else {
|
|
||||||
// click on click-area which is then redispatched
|
|
||||||
makeMouseEvent('click', { x: 11, y: 11 }, getTopElement(el));
|
makeMouseEvent('click', { x: 11, y: 11 }, getTopElement(el));
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return oneEvent(el, 'click');
|
return oneEvent(el, 'click');
|
||||||
}
|
}
|
||||||
|
|
||||||
let hostEvent;
|
let nativeButtonEvent;
|
||||||
let redispatchedEvent;
|
let lionButtonEvent;
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
const el = await fixture('<lion-button></lion-button>');
|
const nativeButtonEl = await fixture('<button>foo</button>');
|
||||||
hostEvent = await prepareClickEvent(el, true);
|
const lionButtonEl = await fixture('<lion-button>foo</lion-button>');
|
||||||
redispatchedEvent = await prepareClickEvent(el, false);
|
nativeButtonEvent = await prepareClickEvent(nativeButtonEl);
|
||||||
|
lionButtonEvent = await prepareClickEvent(lionButtonEl);
|
||||||
});
|
});
|
||||||
|
|
||||||
const sameProperties = [
|
const sameProperties = [
|
||||||
|
|
@ -194,14 +190,19 @@ describe('lion-button', () => {
|
||||||
'cancelable',
|
'cancelable',
|
||||||
'clientX',
|
'clientX',
|
||||||
'clientY',
|
'clientY',
|
||||||
'target',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
sameProperties.forEach(property => {
|
sameProperties.forEach(property => {
|
||||||
it(`has same value of the property "${property}"`, async () => {
|
it(`has same value of the property "${property}" as in native button event`, () => {
|
||||||
expect(redispatchedEvent[property]).to.equal(hostEvent[property]);
|
expect(lionButtonEvent[property]).to.equal(nativeButtonEvent[property]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('has host in the target property', async () => {
|
||||||
|
const el = await fixture('<lion-button>foo</lion-button>');
|
||||||
|
const event = await prepareClickEvent(el);
|
||||||
|
expect(event.target).to.equal(el);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue