0Pricing
Web Accessibility Academy · Lezione

Ripristinare il focus quando si chiude un livello

Riportare l'utente al punto in cui aveva interrotto

Ripristinare il focus quando si chiude un livello è una lezione Web Accessibility Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Web Accessibility Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Web Accessibility Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Layers Borrow Focus

When a modal, menu, or drawer opens, focus moves inside it. When it closes, you are responsible for returning focus to where the user was.

The Lost-Focus Problem

If a closed layer leaves focus on a removed element, it falls back to the page body. The user is stranded with no sense of place.

Remember the Trigger

The fix starts before opening: save a reference to the element that triggered the layer, usually document.activeElement.

const trigger = document.activeElement;

Restore on Close

When the layer closes, call focus() on that saved trigger so the user lands right back where they started.

trigger.focus();

The Full Round Trip

Open: store the trigger and focus the layer. Close: hide it and restore the trigger. That loop is the focus contract of any dismissible layer. 🔁

Native dialog Helps

The browser's dialog element restores focus to the opener automatically when you call close(), saving you the manual bookkeeping.

myDialog.close();

Handle Every Exit Path

Restore focus no matter how the layer closes: a Close button, the Escape key, or a click on the backdrop should all return the user.

When the Trigger Is Gone

If the trigger was removed while the layer was open, send focus to a sensible nearby element instead, never to nowhere.

Why This Matters So Much

Sighted mouse users barely notice, but for keyboard and screen reader users a missed focus return can make a feature unusable.

Test It Keyboard-Only

Open the layer with Enter, close it with Escape, and confirm the focus ring reappears on the button that opened it.

Make It a Habit

Treat focus return as part of building any overlay, not an afterthought. Wire it the moment you add open and close logic.

Quick Check

A custom modal closes. What is the correct destination for focus?

Recap

You can now close the loop: save the trigger on open, restore focus to it on every close path, and lean on native dialog when you can. 🎯

Domande Frequenti

La lezione «Ripristinare il focus quando si chiude un livello» è gratuita?

Sì — il testo completo di «Ripristinare il focus quando si chiude un livello» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Web Accessibility Academy, passa a CoddyKit PRO. Il corso Web Accessibility Academy include 4 lezioni in totale.

Cosa imparerò in «Ripristinare il focus quando si chiude un livello»?

Riportare l'utente al punto in cui aveva interrotto Eserciti Web Accessibility Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Web Accessibility Academy?

Non è richiesta alcuna esperienza precedente. Web Accessibility Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Ripristinare il focus quando si chiude un livello»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Web Accessibility Academy?

Sì. Ogni lezione Web Accessibility Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. tabindex 0, -1 e mai positivo
  2. Spostare il focus dopo un'azione
  3. Ripristinare il focus quando si chiude un livello
  4. Gestire il tabindex rotante nei widget
← Torna a Web Accessibility Academy