Skip to content

Commit 0016708

Browse files
authored
feat(Keyboard): pre-load keyboards (#538)
* create/format all keyboards on _update and toggle smoothly * remove setSmooth - not needed * update snapshot and tests * create keyboards once and remove unused func * allow user to set x pos * add number format to KeyboardNumbers story * support theme swapping, format patching, and storybook arg updates * fix existing prop name typo
1 parent 108266e commit 0016708

File tree

5 files changed

+58624
-1729
lines changed

5 files changed

+58624
-1729
lines changed

packages/@lightningjs/ui-components/src/components/Keyboard/Keyboard.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,31 @@ export default class Keyboard extends Base {
6969
}
7070

7171
_update() {
72-
if (!this._currentFormat) {
72+
if (!this._currentFormat || this._shouldUpdateKeyboards) {
7373
this._currentFormat = this.defaultFormat;
7474
}
7575
if (this.centerKeyboard) {
76-
this.x = (this.style.screenW - this.w) / 2 - this.style.marginX;
77-
} else {
76+
this.x = this.centeredXPos;
77+
} else if (this.x === this.centeredXPos && !this.centerKeyboard) {
78+
// if the keyboard was centered before but now should not be
7879
this.x = 0;
79-
}
80-
if (this._formatsChanged || this.shouldUpdateTheme) {
81-
this._createFormat(this._currentFormat);
82-
this._refocus();
83-
this._formatsChanged = false;
84-
this.shouldUpdateTheme = false;
8580
} else {
86-
this._formatKeys();
81+
this.x == null && (this.x = 0); // if x is undefined or null set it to 0, otherwise do not overwrite x pos
8782
}
83+
this._shouldUpdateKeyboards && this._createKeyboardsFromFormats();
84+
this._formatKeys();
8885
}
8986

90-
_createFormat(keyboard) {
91-
const format = this.formats[keyboard];
92-
if (format) {
93-
const keyboardData = this._formatKeyboardData(format);
94-
this._createKeyboard(keyboard, this._createRows(keyboardData, keyboard));
95-
}
87+
_createKeyboardsFromFormats() {
88+
this.childList.clear(); // if new formats patched in, remove keyboards created from the previous formats
89+
Object.keys(this.formats).forEach(key => {
90+
const format = this.formats[key];
91+
if (format) {
92+
const keyboardData = this._formatKeyboardData(format);
93+
this._createKeyboard(key, this._createRows(keyboardData, key));
94+
}
95+
});
96+
this._formatsChanged = false;
9697
}
9798

9899
_createKeyboard(key, rows = []) {
@@ -110,7 +111,8 @@ export default class Keyboard extends Base {
110111
},
111112
autoResizeWidth: true,
112113
autoResizeHeight: true,
113-
neverScroll: true
114+
neverScroll: true,
115+
alpha: key === capitalize(this._currentFormat) ? 1 : 0.001
114116
}
115117
});
116118
}
@@ -199,6 +201,7 @@ export default class Keyboard extends Base {
199201
const element = this.tag(capitalize(format));
200202
if (element) {
201203
element.patch({
204+
alpha: format === this._currentFormat ? 1 : 0.001,
202205
style: {
203206
itemSpacing: this.style.keySpacing
204207
}
@@ -221,11 +224,10 @@ export default class Keyboard extends Base {
221224
$toggleKeyboard(next) {
222225
const nextKeyboard = capitalize(next);
223226
if (next !== this._currentFormat) {
224-
this._createFormat(next);
225227
const nextKeyboardTag = this.tag(nextKeyboard);
226228

227229
this.selectKeyOn(nextKeyboardTag);
228-
this._currentKeyboard.alpha = 0;
230+
this._currentKeyboard.alpha = 0.001;
229231
nextKeyboardTag.alpha = 1;
230232
this._currentFormat = next;
231233
}
@@ -268,6 +270,14 @@ export default class Keyboard extends Base {
268270
return formats;
269271
}
270272

273+
get centeredXPos() {
274+
return (this.style.screenW - this.w) / 2 - this.style.marginX;
275+
}
276+
277+
get _shouldUpdateKeyboards() {
278+
return this.shouldUpdateTheme || this._formatsChanged;
279+
}
280+
271281
set defaultFormat(format) {
272282
this._defaultFormat = format;
273283
}

packages/@lightningjs/ui-components/src/components/Keyboard/Keyboard.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ All of the following examples will yield the same result.
9393
| name | type | required | default | description |
9494
| ------------- | ------------- | -------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
9595
| columnCount | number | false | 11 | number of columns across the keyboard if passing a flat array |
96-
| centeKeyboard | bool | false | false | center the keyboard within it's set width (must set the w property of Keyboard) |
96+
| centerKeyboard | bool | false | false | center the keyboard within it's set width (must set the w property of Keyboard) |
9797
| centerKeys | bool | false | false | center the keys within it's set width (must set the w property of Keyboard) |
9898
| defaultFormat | string | true | undefined | default format of the keyboard to be shown. should be a key of `formats`. |
9999
| formats | object | true | undefined | object containing arrays that represent different formats that the keyboard can be presented in. These arrays can contain strings or objects. |

packages/@lightningjs/ui-components/src/components/Keyboard/Keyboard.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ describe('Keyboard', () => {
173173

174174
it('should toggle to a different format', () => {
175175
keyboard.$toggleKeyboard('symbols');
176-
expect(keyboard.tag('Lowercase').alpha).toEqual(0);
176+
expect(keyboard.tag('Lowercase').alpha).toEqual(0.001);
177177
expect(keyboard.tag('Symbols').alpha).toEqual(1);
178178
});
179179

@@ -187,7 +187,7 @@ describe('Keyboard', () => {
187187
});
188188
return keyboard._whenEnabled.then(() => {
189189
keyboard.$toggleKeyboard('num');
190-
expect(keyboard.tag('Abc').alpha).toEqual(0);
190+
expect(keyboard.tag('Abc').alpha).toEqual(0.001);
191191
expect(keyboard.tag('Num').alpha).toEqual(1);
192192
});
193193
});

packages/@lightningjs/ui-components/src/components/Keyboard/KeyboardNumbers.stories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ KeyboardNumbers.argTypes = {
6060
defaultFormat: {
6161
description: 'Select the format of dialpad',
6262
control: 'radio',
63-
options: ['dialpad', 'dialpadExtended'],
63+
options: ['dialpad', 'dialpadExtended', 'numbers'],
6464
table: {
6565
defaultValue: { summary: 'undefined' }
6666
}

0 commit comments

Comments
 (0)