CustomElements. Error cursor position on input
When using web components, the cursor moves to the end. http://jsfiddle.net/et59ds31/
The bug in isActive method in HTMLMaskElement.
document.activeElement points to top level custom element instead native input.
In case of using shadowDom the method must search activeElement in shadowRoot recursively. For example:
get isActive (): boolean {
let active = document.activeElement;
while (active && active.shadowRoot && active.shadowRoot.activeElement){
active = active.shadowRoot.activeElement;
}
return this.input === active;
}
@shushlyakov @boikovdv hello, guys!
You don't need to hack around. Implement your ShadowHTMLMaskElement same way as it's done in HTMLMaskElement. Inherit from HTMLMaskElement or MaskElement which one is more suitable. Then wrap shadow element and pass it to IMask:
const imask = new IMask(new ShadowHTMLMaskElement(nativeShadowElement));
Hi, i know that this post is a little bit older... but @uNmAnNeR you could explain a little bit how could we do ShadowHTMLMaskElement ?
Thanks1
@nachiomdq
class ShadowHTMLMaskElement extends IMask.HTMLMaskElement {
get isActive() {
let active = document.activeElement;
while (active && active.shadowRoot && active.shadowRoot.activeElement) {
active = active.shadowRoot.activeElement;
}
return this.input === active;
}
}
const mask = new IMask(new ShadowHTMLMaskElement(element), maskOptions);
I am have a web component created with StencilJS and tried this solution. I actually still have issues. Tried both scoped and shadow settings. I notice the cursor still wants to jump to the end and I manually move it back to the right place. I also notice the cursor will get stuck.
Tried this for the scoped setting:
class ShadowHTMLMaskElement extends IMask.HTMLMaskElement {
get isActive() {
let active = document.activeElement;
return this.input.querySelector('.native-input') === active;
}
}
...
export const phone = {
mask: '(000) 000-0000',
lazy: false,
placeholderChar: '_',
};
...
this.mask = IMask(this.input, phone);
I can confirm there are still issues with this. Or, as this was posted some time ago, maybe this workaround does not work anymore?
Using a custom implementation like suggested:
class ShadowHTMLMaskElement extends IMask.HTMLMaskElement {
get isActive() {
[...]
}
}
var shadowElement = new ShadowHTMLMaskElement(shadowPart);
throws the following error:
imask.js:300
Uncaught TypeError: Cannot read properties of undefined (reading 'length')
at get selectionStart (imask.js:300:55)
at get selectionStart (imask.js:659:77)
at InputMask.alignCursorFriendly (imask.js:770:22)
at InputMask._onFocus (imask.js:836:18)
(Script: https://unpkg.com/[email protected]/dist/imask.js)
because this.value is null inside the class.
Changing the isActive() getter directly inside of HTMLMaskElement also has not the desired effect.
Even when using and comparing the correct shadow elements so the getter returns true, the caret still jumps to the right after each key press.
Has anyone made this work, eventually, that can help me figure this out?