Skip to content

Commit 4231a99

Browse files
authored
fix!: prevent virtual keyboard open on overlay close (#3253)
1 parent 0101b7d commit 4231a99

7 files changed

Lines changed: 178 additions & 4 deletions

File tree

‎packages/combo-box/src/vaadin-combo-box-mixin.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { isTouch } from '@vaadin/component-base/src/browser-utils.js';
7+
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
78
import { DisabledMixin } from '@vaadin/component-base/src/disabled-mixin.js';
89
import { KeyboardMixin } from '@vaadin/component-base/src/keyboard-mixin.js';
910
import { processTemplates } from '@vaadin/component-base/src/templates.js';
1011
import { InputMixin } from '@vaadin/field-base/src/input-mixin.js';
12+
import { VirtualKeyboardController } from '@vaadin/field-base/src/virtual-keyboard-controller.js';
1113
import { ComboBoxPlaceholder } from './vaadin-combo-box-placeholder.js';
1214

1315
/**
1416
* @polymerMixin
17+
* @param {function(new:HTMLElement)} subclass
1518
*/
1619
export const ComboBoxMixin = (subclass) =>
17-
class VaadinComboBoxMixinElement extends KeyboardMixin(InputMixin(DisabledMixin(subclass))) {
20+
class VaadinComboBoxMixinElement extends ControllerMixin(KeyboardMixin(InputMixin(DisabledMixin(subclass)))) {
1821
static get properties() {
1922
return {
2023
/**
@@ -295,6 +298,8 @@ export const ComboBoxMixin = (subclass) =>
295298
this.addEventListener('touchstart', bringToFrontListener);
296299

297300
processTemplates(this);
301+
302+
this.addController(new VirtualKeyboardController(this));
298303
}
299304

300305
/**

‎packages/combo-box/test/toggling-dropdown.test.js‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { aTimeout, click, fixtureSync, focusout, isIOS, tap } from '@vaadin/testing-helpers';
2+
import { aTimeout, click, fixtureSync, focusout, isIOS, tap, touchstart } from '@vaadin/testing-helpers';
3+
import { sendKeys } from '@web/test-runner-commands';
34
import sinon from 'sinon';
45
import '@vaadin/polymer-legacy-adapter/template-renderer.js';
56
import './not-animated-styles.js';
@@ -220,6 +221,29 @@ describe('toggling dropdown', () => {
220221
expect(document.activeElement).to.equal(input);
221222
});
222223

224+
describe('virtual keyboard', () => {
225+
it('should disable virtual keyboard on close', () => {
226+
comboBox.open();
227+
comboBox.close();
228+
expect(input.inputMode).to.equal('none');
229+
});
230+
231+
it('should re-enable virtual keyboard on touchstart', () => {
232+
comboBox.open();
233+
comboBox.close();
234+
touchstart(comboBox);
235+
expect(input.inputMode).to.equal('');
236+
});
237+
238+
it('should re-enable virtual keyboard on blur', async () => {
239+
comboBox.open();
240+
comboBox.close();
241+
await aTimeout(0);
242+
await sendKeys({ press: 'Tab' });
243+
expect(input.inputMode).to.equal('');
244+
});
245+
});
246+
223247
describe('filtered items are empty', () => {
224248
it('should close the dropdown on non-existent values', () => {
225249
comboBox.open();

‎packages/date-picker/src/vaadin-date-picker-mixin.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { isIOS } from '@vaadin/component-base/src/browser-utils.js';
7+
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
78
import { KeyboardMixin } from '@vaadin/component-base/src/keyboard-mixin.js';
89
import { DelegateFocusMixin } from '@vaadin/field-base/src/delegate-focus-mixin.js';
910
import { InputMixin } from '@vaadin/field-base/src/input-mixin.js';
11+
import { VirtualKeyboardController } from '@vaadin/field-base/src/virtual-keyboard-controller.js';
1012
import { dateAllowed, dateEquals, extractDateParts, getClosestDate } from './vaadin-date-picker-helper.js';
1113

1214
/**
1315
* @polymerMixin
16+
* @param {function(new:HTMLElement)} subclass
1417
*/
1518
export const DatePickerMixin = (subclass) =>
16-
class VaadinDatePickerMixin extends DelegateFocusMixin(InputMixin(KeyboardMixin(subclass))) {
19+
class VaadinDatePickerMixin extends ControllerMixin(DelegateFocusMixin(InputMixin(KeyboardMixin(subclass)))) {
1720
static get properties() {
1821
return {
1922
/**
@@ -419,6 +422,8 @@ export const DatePickerMixin = (subclass) =>
419422
this.open();
420423
}
421424
});
425+
426+
this.addController(new VirtualKeyboardController(this));
422427
}
423428

424429
/** @protected */

‎packages/date-picker/test/dropdown.test.js‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { aTimeout, click, down, fixtureSync, isIOS } from '@vaadin/testing-helpers';
2+
import { aTimeout, click, down, fixtureSync, isIOS, touchstart } from '@vaadin/testing-helpers';
3+
import { sendKeys } from '@web/test-runner-commands';
34
import '../src/vaadin-date-picker.js';
45
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
56
import { open, outsideClick } from './common.js';
@@ -56,6 +57,28 @@ describe('dropdown', () => {
5657
expect(document.activeElement).to.equal(input);
5758
});
5859

60+
describe('virtual keyboard', () => {
61+
it('should disable virtual keyboard on close', async () => {
62+
await open(datepicker);
63+
datepicker.close();
64+
expect(input.inputMode).to.equal('none');
65+
});
66+
67+
it('should re-enable virtual keyboard on touchstart', async () => {
68+
await open(datepicker);
69+
datepicker.close();
70+
touchstart(datepicker);
71+
expect(input.inputMode).to.equal('');
72+
});
73+
74+
it('should re-enable virtual keyboard on blur', async () => {
75+
await open(datepicker);
76+
datepicker.close();
77+
await sendKeys({ press: 'Tab' });
78+
expect(input.inputMode).to.equal('');
79+
});
80+
});
81+
5982
describe('sizing', () => {
6083
beforeEach(() => {
6184
const viewport = document.createElement('meta');
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2021 - 2022 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { ReactiveController } from 'lit';
7+
8+
/**
9+
* A controller which prevents the virtual keyboard from showing up on mobile devices
10+
* when the field's overlay is closed.
11+
*/
12+
export class VirtualKeyboardController implements ReactiveController {
13+
constructor(host: { inputElement?: HTMLElement; opened: boolean } & HTMLElement);
14+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2021 - 2022 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
7+
/**
8+
* A controller which prevents the virtual keyboard from showing up on mobile devices
9+
* when the field's overlay is closed.
10+
*/
11+
export class VirtualKeyboardController {
12+
/**
13+
* @param {{ inputElement?: HTMLElement; opened: boolean } & HTMLElement} host
14+
*/
15+
constructor(host) {
16+
this.host = host;
17+
18+
host.addEventListener('opened-changed', () => {
19+
if (!host.opened) {
20+
// Prevent opening the virtual keyboard when the input gets re-focused on dropdown close
21+
this.__setVirtualKeyboardEnabled(false);
22+
}
23+
});
24+
25+
// Re-enable virtual keyboard on blur, so it gets opened when the field is focused again
26+
host.addEventListener('blur', () => this.__setVirtualKeyboardEnabled(true));
27+
28+
// Re-enable the virtual keyboard whenever the field is touched
29+
host.addEventListener('touchstart', () => this.__setVirtualKeyboardEnabled(true));
30+
}
31+
32+
/** @private */
33+
__setVirtualKeyboardEnabled(value) {
34+
if (this.host.inputElement) {
35+
this.host.inputElement.inputMode = value ? '' : 'none';
36+
}
37+
}
38+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { fixtureSync, touchstart } from '@vaadin/testing-helpers';
3+
import { sendKeys } from '@web/test-runner-commands';
4+
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
5+
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
6+
import { VirtualKeyboardController } from '../src/virtual-keyboard-controller.js';
7+
8+
customElements.define(
9+
'virtual-keyboard-controller-element',
10+
class extends ControllerMixin(PolymerElement) {
11+
static get template() {
12+
return html`<slot></slot>`;
13+
}
14+
15+
constructor() {
16+
super();
17+
this.inputElement = document.createElement('input');
18+
this.appendChild(this.inputElement);
19+
}
20+
21+
ready() {
22+
super.ready();
23+
this.addController(new VirtualKeyboardController(this));
24+
}
25+
26+
open() {
27+
this.dispatchEvent(new CustomEvent('opened-changed', { detail: { value: true } }));
28+
}
29+
30+
close() {
31+
this.dispatchEvent(new CustomEvent('opened-changed', { detail: { value: false } }));
32+
}
33+
}
34+
);
35+
36+
describe('virtual-keyboard-controller', () => {
37+
let element, input;
38+
39+
beforeEach(() => {
40+
element = fixtureSync('<virtual-keyboard-controller-element></virtual-keyboard-controller-element>');
41+
input = element.inputElement;
42+
});
43+
44+
it('should disable virtual keyboard on close', async () => {
45+
element.open();
46+
element.close();
47+
expect(input.inputMode).to.equal('none');
48+
});
49+
50+
it('should re-enable virtual keyboard on touchstart', async () => {
51+
element.open();
52+
element.close();
53+
touchstart(element);
54+
expect(input.inputMode).to.equal('');
55+
});
56+
57+
it('should re-enable virtual keyboard on blur', async () => {
58+
element.open();
59+
element.close();
60+
element.tabIndex = 1;
61+
element.focus();
62+
await sendKeys({ press: 'Tab' });
63+
expect(input.inputMode).to.equal('');
64+
});
65+
});

0 commit comments

Comments
 (0)