Skip to content

Commit

Permalink
fix: fix focus on window exit and then return
Browse files Browse the repository at this point in the history
Now correctly handles case where window is still visible (previoulsy only work when tab was entirely hidden). Additionaly, a click off of the math field will now cancel the refocus similar to a standard input element.
  • Loading branch information
mgreminger committed Sep 25, 2024
1 parent b0f6477 commit d6f8340
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/editor-mathfield/mathfield-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1716,26 +1716,20 @@ If you are using Vue, this may be because you are using the runtime-only build o
// don't do that for custom elements, so we do it ourselves. @futureweb
//

// Wait for the window/document visibility to change
// Wait for the window focus to change
// (the mathfield gets blurred before the window)
const controller = new AbortController();
const signal = controller.signal;
document.addEventListener(
'visibilitychange',
window.addEventListener(
'blur',
() => {
if (document.visibilityState === 'hidden') {
document.addEventListener(
'visibilitychange',
() => {
if (
isValidMathfield(this) &&
document.visibilityState === 'visible'
)
this.focus({ preventScroll: true });
},
{ once: true, signal }
);
}
window.addEventListener(
'focus',
() => {
if (isValidMathfield(this)) this.focus({ preventScroll: true });
},
{ once: true, signal }
);
},
{ once: true, signal }
);
Expand All @@ -1745,6 +1739,12 @@ If you are using Vue, this may be because you are using the runtime-only build o
document.addEventListener('focusin', () => controller.abort(), {
once: true,
});

// If user clicks anywhere, cancel the refocus (covers case where
// click doesn't cause an element to come into focus)
document.addEventListener('click', () => controller.abort(), {
once: true,
});
}
}

Expand Down

0 comments on commit d6f8340

Please sign in to comment.