Skip to content
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
29 changes: 29 additions & 0 deletions packages/repl/src/lib/Output/srcdoc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
}

(0, eval)(data.args.script);

// hand focus back to the editor if it's taken in an effect
// that runs immediately (for focuses in effects, this is
// more effective than listening for the focusin event)
Promise.resolve().then(() => {
if (document.activeElement !== null && document.activeElement !== document.body) {
send({ type: 'iframe_took_focus' });
}
});
}

if (action === 'catch_clicks') {
Expand Down Expand Up @@ -244,6 +253,26 @@
original(...args);
};
}

// Focus management
let can_focus = false;

window.addEventListener('pointerdown', (e) => (can_focus = true));
window.addEventListener('pointerup', (e) => (can_focus = false));
window.addEventListener('keydown', (e) => (can_focus = true));
window.addEventListener('keyup', (e) => (can_focus = false));

window.addEventListener('focusin', (e) => {
// if focusin happened as a result of a mouse/keyboard event, allow it
if (can_focus) return;

// if `e.target` is the `<body>` and there's a `relatedTarget`,
// assume the focusin was the result of a user navigation — allow it
if (e.target.tagName === 'BODY' && e.relatedTarget) return;

// otherwise, broadcast an event that causes the editor to reclaim focus
send({ type: 'iframe_took_focus' });
});
})();
</script>
</head>
Expand Down