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

Caret shifts to the last character when the "thousands" separator gets applied #66

Open
fmaclen opened this issue Oct 24, 2023 · 2 comments
Labels
1.0 Features that introduce breaking changes and require version bump bug Something isn't working

Comments

@fmaclen
Copy link
Owner

fmaclen commented Oct 24, 2023

Screen.Recording.2023-10-24.at.9.31.50.AM.mov
@fmaclen fmaclen added the bug Something isn't working label Oct 24, 2023
@fmaclen fmaclen mentioned this issue Jul 24, 2024
@fmaclen
Copy link
Owner Author

fmaclen commented Sep 20, 2024

After doing some research I think there's multiple issues at play here.

I was able to "fix" the caret shifting issue by implementing Svelte's await tick() to determine when the DOM has finished updating instead of retrying:

// Wait for Svelte DOM updates
await tick();

// Defer caret position update
const endCaretPosition =
	startCaretPosition + formattedValue.length - previousFormattedValueLength;

// Set the caret position after DOM updates
inputElement?.setSelectionRange(endCaretPosition, endCaretPosition);

This solution worked okay for Chrome and Firefox but it was causing an infinite loop in Safari (WebKit) where the on:blur event was getting triggered constantly until the browser crashes.

This is likely caused by calling setFormattedValue() from multiple places:

The fact that the data flows in so many different ways makes me think that our overall implementation needs to be re-thought from the ground up.

For example, I'm not sure we should listen to an on:blur event, looking at react-currency-field the formatting is always applied as the user types and not after clicking outside of the input, which feels like a nicer UX.

For reference, Bits-UI has WIP implementation of a NumberInput component written in Svelte 5 (which lacks a lot of our current features) and applies formatting after clicking outside of the input.

Maybe this is a good excuse to start re-writing the library with Svelte 5's new syntax 🤔

@GregoMac1
Copy link
Collaborator

Great progress! Personally, I think the formatting should occur everytime the user types a number or a punctuation sign, it feels more intuitive (even more if we wanted to use this input in a mobile environment).

@fmaclen fmaclen added the 1.0 Features that introduce breaking changes and require version bump label Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.0 Features that introduce breaking changes and require version bump bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants