imaskjs icon indicating copy to clipboard operation
imaskjs copied to clipboard

CustomElements. Error cursor position on input

Open shushlyakov opened this issue 8 years ago • 6 comments

When using web components, the cursor moves to the end. http://jsfiddle.net/et59ds31/

shushlyakov avatar Nov 16 '18 05:11 shushlyakov

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;
  }

boikovdv avatar Nov 16 '18 10:11 boikovdv

@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));

uNmAnNeR avatar Nov 16 '18 10:11 uNmAnNeR

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 avatar Sep 10 '19 14:09 nachiomdq

@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);

shushlyakov avatar Sep 11 '19 11:09 shushlyakov

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);

trazek avatar Jan 13 '21 21:01 trazek

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?

pmi24 avatar Oct 30 '24 16:10 pmi24