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: Input field: keyboard hides on mobile devices #106 #107

Merged
merged 1 commit into from
Oct 20, 2023
Merged
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
81 changes: 54 additions & 27 deletions src/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ export class Input extends Container

protected activation = false;
protected readonly options: InputOptions;
protected _keyboard: HTMLInputElement;
protected input: HTMLInputElement;

protected handleActivationBinding = this.handleActivation.bind(this);
protected onKeyDownBinding = this.onKeyDown.bind(this);
protected onKeyUpBinding = this.onKeyUp.bind(this);
protected stopEditingBinding = this.stopEditing.bind(this);

/** Fires when input loses focus. */
onEnter: Signal<(text: string) => void>;
Expand Down Expand Up @@ -90,7 +91,7 @@ export class Input extends Container
{
window.addEventListener('click', this.handleActivationBinding);

window.addEventListener('keydown', this.onKeyDownBinding);
window.addEventListener('keyup', this.onKeyUpBinding);
}

this.onEnter = new Signal();
Expand All @@ -108,7 +109,7 @@ export class Input extends Container
}
}

protected onKeyDown(e: KeyboardEvent)
protected onKeyUp(e: KeyboardEvent)
{
const key = e.key;

Expand Down Expand Up @@ -232,33 +233,59 @@ export class Input extends Container

if (utils.isMobile.any)
{
this._keyboard = document.createElement('input');
this.createInputField();
}

document.body.appendChild(this._keyboard);
this._keyboard.style.position = 'fixed';
this._keyboard.style.left = '-1000px';
this.align();
}

this._keyboard.oninput = () =>
{
let value = this._keyboard.value;
protected createInputField()
{
if (this.input)
{
this.input.removeEventListener('blur', this.stopEditingBinding);
this.input.removeEventListener('keyup', this.onKeyUpBinding);

this.input?.blur();
this.input?.remove();
this.input = null;
}

const maxLength = this.options.maxLength;
const input: HTMLInputElement = document.createElement('input');

if (maxLength && value.length > this.options.maxLength)
{
value = value.substring(0, maxLength);
this._keyboard.value = value;
}
document.body.appendChild(input);

this.value = value;
input.setAttribute('inputmode', 'decimal');

this.onChange.emit(this.value);
};
input.style.position = 'fixed';
input.style.left = `${this.getGlobalPosition().x}px`;
input.style.top = `${this.getGlobalPosition().y}px`;
input.style.opacity = '0.0000001';
input.style.width = `${this._bg.width}px`;
input.style.height = `${this._bg.height}px`;
input.style.border = 'none';
input.style.outline = 'none';
input.style.background = 'white';

this._keyboard.focus();
this._keyboard.click();
this._keyboard.value = this.value;
// This hack fixes instant hiding keyboard on mobile after showing it
if (utils.isMobile.android.device)
{
setTimeout(() =>
{
input.focus();
input.click();
}, 100);
}
else
{
input.focus();
input.click();
}

input.addEventListener('blur', this.stopEditingBinding);
input.addEventListener('keyup', this.onKeyUpBinding);

this.input = input;

this.align();
}
Expand Down Expand Up @@ -291,9 +318,9 @@ export class Input extends Container

if (utils.isMobile.any)
{
this._keyboard?.blur();
this._keyboard?.remove();
this._keyboard = null;
this.input?.blur();
this.input?.remove();
this.input = null;
}

this.align();
Expand Down Expand Up @@ -445,7 +472,7 @@ export class Input extends Container
{
window.removeEventListener('click', this.handleActivationBinding);

window.removeEventListener('keydown', this.onKeyDownBinding);
window.removeEventListener('keyup', this.onKeyUpBinding);
}

super.destroy(options);
Expand Down
Loading