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

Fix Safari x-mask bug that causes focus trap #2852

Merged
merged 1 commit into from Apr 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions packages/mask/src/index.js
Expand Up @@ -39,9 +39,11 @@ export default function (Alpine) {
}

el.addEventListener('input', () => processInputValue(el))
el.addEventListener('blur', () => processInputValue(el))
// Don't "restoreCursorPosition" on "blur", because Safari
// will re-focus the input and cause a focus trap.
el.addEventListener('blur', () => processInputValue(el, false))

function processInputValue (el) {
function processInputValue (el, shouldRestoreCursor = true) {
let input = el.value

let template = templateFn(input)
Expand All @@ -51,12 +53,18 @@ export default function (Alpine) {
return lastInputValue = el.value
}

// When an input element's value is set, it moves the cursor to the end
// therefore we need to track, estimate, and restore the cursor after
// a change was made.
restoreCursorPosition(el, template, () => {
lastInputValue = el.value = formatInput(input, template)
})
let setInput = () => { lastInputValque = el.value = formatInput(input, template) }

if (shouldRestoreCursor) {
// When an input element's value is set, it moves the cursor to the end
// therefore we need to track, estimate, and restore the cursor after
// a change was made.
restoreCursorPosition(el, template, () => {
setInput()
})
} else {
setInput()
}
}

function formatInput(input, template) {
Expand Down