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: do not dispatch cell-focus on mouse up outside of cell #3587

Merged
merged 5 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 8 additions & 5 deletions packages/grid/src/vaadin-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import './vaadin-grid-column.js';
import './vaadin-grid-styles.js';
import { beforeNextRender } from '@polymer/polymer/lib/utils/render-status.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { isAndroid, isFirefox, isIOS, isSafari, isTouch } from '@vaadin/component-base/src/browser-utils.js';
import { isAndroid, isChrome, isFirefox, isIOS, isSafari, isTouch } from '@vaadin/component-base/src/browser-utils.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { TabindexMixin } from '@vaadin/component-base/src/tabindex-mixin.js';
import { processTemplates } from '@vaadin/component-base/src/templates.js';
Expand Down Expand Up @@ -685,13 +685,16 @@ class Grid extends ElementMixin(
// focusable slot wrapper, that is why cells are not focused with
// mousedown. Workaround: listen for mousedown and focus manually.
cellContent.addEventListener('mousedown', () => {
if (window.chrome) {
if (isChrome) {
Copy link
Contributor Author

@sissbruecker sissbruecker Mar 21, 2022

Choose a reason for hiding this comment

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

The previous check did not work properly in tests with headless Chrome.

// Chrome bug: focusing before mouseup prevents text selection, see http://crbug.com/771903
const mouseUpListener = () => {
if (!cellContent.contains(this.getRootNode().activeElement)) {
const mouseUpListener = (event) => {
// If focus is on element within the cell content — respect it, do not change
const contentContainsFocusedElement = cellContent.contains(this.getRootNode().activeElement);
// Only focus if mouse is released on cell content itself
const mouseUpWithinCell = event.target === cellContent || cellContent.contains(event.target);
vursen marked this conversation as resolved.
Show resolved Hide resolved
if (!contentContainsFocusedElement && mouseUpWithinCell) {
cell.focus();
}
// If focus is in the cell content — respect it, do not change.
document.removeEventListener('mouseup', mouseUpListener, true);
};
document.addEventListener('mouseup', mouseUpListener, true);
Expand Down
26 changes: 26 additions & 0 deletions packages/grid/test/keyboard-navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
down as mouseDown,
fixtureSync,
focusin,
isChrome,
keyboardEventFor,
keyDownOn,
keyUpOn,
Expand Down Expand Up @@ -1889,6 +1890,31 @@ describe('keyboard navigation', () => {

grid.removeEventListener('cell-focus', spy);
});

// Chrome uses a workaround to dispatch cell-focus in mouse up,
// should dispatch event on mouse up events on cell itself
(isChrome ? it : it.skip)('should dispatch cell-focus on mouse up inside of cell', () => {
vursen marked this conversation as resolved.
Show resolved Hide resolved
const spy = sinon.spy();
grid.addEventListener('cell-focus', spy);

// Mouse down and release on cell content
const cell = getRowFirstCell(0);
mouseDown(cell._content);
mouseUp(cell._content);
expect(spy.calledOnce).to.be.true;
});

// Chrome uses a workaround to dispatch cell-focus in mouse up,
// should not dispatch event on mouse up events outside of cell
// Regression test for https://github.com/vaadin/flow-components/issues/2863
(isChrome ? it : it.skip)('should not dispatch cell-focus on mouse up outside of cell', () => {
const spy = sinon.spy();
grid.addEventListener('cell-focus', spy);

mouseDown(getRowFirstCell(0)._content);
mouseUp(document.body);
expect(spy.calledOnce).to.be.false;
});
});
});

Expand Down