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

It's moving cursor end of the mask on first focus #916

Open
fatihturan opened this issue Jul 23, 2023 · 4 comments
Open

It's moving cursor end of the mask on first focus #916

fatihturan opened this issue Jul 23, 2023 · 4 comments
Labels

Comments

@fatihturan
Copy link

fatihturan commented Jul 23, 2023

Describe the bug
When you click on the input as first time, it's moving the cursor to end of mask characters and can't write anything in the input.

See screen recording below:

Screen.Recording.2023-07-23.at.15.38.59.mov

To Reproduce

  1. Enter the sample form in this web site
  2. Click on the Phone input
  3. Try to write something.
  4. If you click outside and re-click on the input, it's working as expected.

Expected behavior
It must move the cursor at the start and it would allow to write in the input.

Environment:

  • OS: macOS Ventura 13.4.1
  • Browser: Safari
  • Version: 16.5.2
  • IMask version: I'm using it from https://unpkg.com/imask. So it's probably latest version.
  • Framework/plugin version if used: Static web site.

Additional context
I added two event listener that updates the lazy option to false when you focus and set it true when you lost the focus on the input. Because I want to show default placeholder until users fill the input.

    const element = document.getElementById('phone');
    const maskOptions = {
        mask: '(000) 000-0000',
    };
    const mask = IMask(element, maskOptions);

    phone.addEventListener('focus', function() {
        mask.updateOptions({
            lazy: false
        });
    })

    phone.addEventListener('blur', function() {
        mask.updateOptions({
            lazy: true
        });
    })
@dakota-kallas
Copy link

I had the same issue. It is because the masking characters are being placed on the screen before the on click event is being registered. The only way for me to get around it was to put a wait timer on the focus/blur event listeners for 1ms, so it gave time for the onclick event to happen first. Something like this:

phone.addEventListener('focus', function() { setTimeout(() => { mask.updateOptions({ lazy: false }); }, 1); })

It's definitly a workaround, but it would be nice if this was fixed natively for the plugin. I breifly mentioned the issue in #900 but to no avail.

@fatihturan
Copy link
Author

Hi @dakota-kallas,

Unfortunately it didn't help to solve the issue. The issue still persist.

Screen.Recording.2023-07-26.at.09.43.13.mov

You can see it live from here.

@gterras
Copy link

gterras commented Aug 20, 2023

Same problem on a mask with spaces, I can confirm waiting for the DOM to be updated does the trick. In Svelte :

const focus = async (el) => tick().then(() => el.focus())
<input use:focus ... />

@mallchel
Copy link

In my case, the selectionEnd in safari becomes to 0 seems like on its own. and in time of select method in imask/esm/controls/mask-element.js we have early return because end === this.selectionEnd but caret in UI is still in the end 🤯

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants