Programmatic Focus & activeElement Playground

Open the duty sheet on the teacher’s board: where focus goes as the sheet opens and closes, how to give it back to its opener, focus() on a plain div…

Why it matters

Every accessible modal, menu and command palette stands on focus that is handled right. Get it wrong and a keyboard user is left behind the modal, or their focus falls to <body> once the modal closes and they have to start Tabbing from the top again.

The key idea

Focus only moves when your code moves it

Common mistakes

Try it in this playground