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: improved refocus behavior on window exit and then return #2528

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading