Skip to content

Commit

Permalink
Fixes a bug in which certain keypresses would be prevented when perfo…
Browse files Browse the repository at this point in the history
…rmed outside the pop-up. They only need to be prevented while the form is open.
  • Loading branch information
coddingtonbear committed Feb 12, 2024
1 parent c838b7f commit d840769
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,6 @@ if (!document.getElementById(ROOT_CONTAINER_ID)) {
},
};

function preventBrowserFromStealingKeypress(event: KeyboardEvent) {
if (event.code !== "Escape") {
event.stopPropagation();
}
}
document.addEventListener(
"keydown",
preventBrowserFromStealingKeypress,
true
);

interface Props {
sandbox: HTMLIFrameElement;
}
Expand Down Expand Up @@ -406,10 +395,34 @@ if (!document.getElementById(ROOT_CONTAINER_ID)) {
const [previewContext, setPreviewContext] =
React.useState<Record<string, any>>();

function preventBrowserFromStealingKeypress(event: KeyboardEvent) {
if (event.code !== "Escape") {
event.stopPropagation();
}
}

useEffect(() => {
if (popupFormDisplayed) {
updatePreviewContext();
document.addEventListener(
"keydown",
preventBrowserFromStealingKeypress,
true
);
} else {
document.removeEventListener(
"keydown",
preventBrowserFromStealingKeypress,
true
);
}
return () => {
document.removeEventListener(
"keydown",
preventBrowserFromStealingKeypress,
true
);
};
}, [popupFormDisplayed]);

async function updatePreviewContext(): Promise<void> {
Expand Down

0 comments on commit d840769

Please sign in to comment.