Skip to content

Focus Trapping: canvas.set_onblur(fn) is respected, but focus() is not. #3003

Answered by Liamolucko
Zageron asked this question in Q&A
Discussion options

You must be logged in to vote

This looks to be a quirk of the web APIs themselves. This equivalent javascript has the same problem:

<canvas tabindex="0"></canvas>
<script>
    const canvas = document.querySelector("canvas");

    canvas.addEventListener("focus", () => console.log("focused"));
    canvas.addEventListener("blur", () => console.log("unfocused"));

    canvas.addEventListener("blur", () => canvas.focus());
</script>

It works if you add a timeout:

<canvas tabindex="0"></canvas>
<script>
    const canvas = document.querySelector("canvas");

    canvas.addEventListener("focus", () => console.log("focused"));
    canvas.addEventListener("blur", () => console.log("unfocused"));

    canvas.addEventListener("blur"

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Zageron
Comment options

Answer selected by Zageron
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants