Skip to content

fix safari input jumping #4179

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

Merged
merged 1 commit into from
Dec 29, 2019

Conversation

tanhauhau
Copy link
Member

Fix #3449

When we dynamically set input.value = value, browser will reset the cursor to the end of the input. However, if the input.value === value, most browser (except safari) will not reset the cursor.

so even the example below, when editing from the middle of the input, only in safari, the input cursor will jump to the end of input

<script>
	let value = '';
	function onInput(e) {
		value = e.target.value;
	}
</script>

<input {value} on:input={onInput} />

To fix this, we have to check whether input.value === value before setting it:

if (dirty & /*name*/ 1 && input.value !== /*name*/ ctx[0]) {
    input.value = /*name*/ ctx[0];
}

and we've already done that for bindings:

<script>
	let value = '';
</script>

<input bind:value>

@Conduitry
Copy link
Member

Conduitry commented Dec 28, 2019

Nice, yeah, this is the type of solution I was hoping for. It makes sense now why this was still happening if that previous fix was only for two-way binding.

It looks like neither this PR nor #2511/#3208 handle <textarea>s though - is this something that is needed?

@tanhauhau
Copy link
Member Author

I tried textarea on desktop safari, it seems okay (not jumping) to me. 🙈
And we didn't check the case of contenteditable also....

@Conduitry Conduitry merged commit 7fb35dd into sveltejs:master Dec 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cursor misbehaviour in Safari's <input> controls
2 participants