fix(a11y): first-run screens are dialogs; the page behind them is inert

The terms screen was a plain div, rendered last in the page. Focus stayed
where it was, and Tab walked through the whole hidden page before reaching
its buttons. The human check had role="dialog", but neither moved focus
nor kept it.

Both now:
- report themselves to lib/overlays.svelte.ts;
- are role="dialog" with aria-modal, labelled by an h2 (each used to add a
  second h1);
- move focus into themselves on mount (focusOnMount).

The layout makes everything behind them inert: the bare pages' wrapper,
the skip link and the shell. BackToTop moves inside the shell to go inert
with it.

Ctrl/Cmd+K stands down while they are open. It could open the palette
UNDER the gate (z-50 against z-[60]) and trap focus there, out of sight.

Also:
- BackToTop is inert while hidden: it only faded out, so it stayed a Tab
  stop. Its JS smooth scroll follows prefers-reduced-motion.
- The command palette's search box gets a focus ring, drawn on its row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
poli0981andClaude Opus 5.5 committed 2026-11-09 09:03:58 +07:00
1 parent e827ac83a7
commit 3423500494
7 files changed
+113 -15

No files matched your search

+9 -1
View File
@@ -23,10 +23,18 @@
<!-- bottom-4/right-4 plus the safe-area insets, so in the Android app the
button clears the navigation bar (see the utilities in styles/theme.css). -->
<!-- inert while hidden: it only fades out, so it stayed a Tab stop and a
button a screen reader read out. A JS smooth scroll is not covered by
the CSS reduced-motion rule, so it asks the system itself. -->
<button
type="button"
aria-label={i18n.t("common.backToTop")}
onclick={() => scroller?.scrollTo({ top: 0, behavior: "smooth" })}
inert={!visible || undefined}
onclick={() =>
scroller?.scrollTo({
top: 0,
behavior: matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth",
})}
class={cn(
"fixed z-20 grid size-10 place-items-center rounded-full border",
"bg-card text-muted-foreground shadow-lg transition-all hover:text-foreground",
+4 -2
View File
@@ -136,7 +136,9 @@
>
<Dialog.Title class="sr-only">{t("cmdk.title")}</Dialog.Title>
<div class="flex items-center gap-2 border-b px-3">
<!-- The focus ring is the row's, inset: an outline on the input itself would be
cut off by the dialog's edge, and the input used to have none at all. -->
<div class="flex items-center gap-2 border-b px-3 has-[:focus-visible]:ring-2 has-[:focus-visible]:ring-ring has-[:focus-visible]:ring-inset">
<Search class="size-4 shrink-0 text-muted-foreground" />
<!-- svelte-ignore a11y_autofocus -->
<input
@@ -150,7 +152,7 @@
aria-controls="cmdk-list"
aria-activedescendant={activeId}
aria-autocomplete="list"
class="h-12 w-full bg-transparent text-sm outline-none placeholder:text-muted-foreground"
class="h-12 w-full bg-transparent text-sm focus-visible:outline-none placeholder:text-muted-foreground"
/>
</div>
+36 -6
View File
@@ -13,6 +13,7 @@
import { isUngatedPath } from "../gates";
import { closeTab, leaveSite } from "../leave-site";
import { pwa } from "../pwa-state.svelte";
import { overlays, focusOnMount } from "../overlays.svelte";
import Button from "../ui/Button.svelte";
const t = i18n.t;
@@ -38,6 +39,14 @@
// dialog into every page's HTML.
const open = $derived(consent.hydrated && !consent.accepted && !exempt);
// The layout makes the page behind inert while this is open (overlays.svelte.ts).
$effect(() => {
overlays.consent = open;
return () => {
overlays.consent = false;
};
});
/**
* REVIEW mode: some documents changed since this reader accepted them, and
* only those are shown, as a diff. Before per-document hashing the only
@@ -142,9 +151,16 @@
<!-- nothing: the page underneath is already rendered -->
{:else if leaving !== "idle"}
<div class="fixed inset-0 z-[60] flex overflow-auto bg-background/95 p-safe-or-4 backdrop-blur-sm">
<div class="m-auto w-full max-w-md rounded-lg border bg-card p-8 text-center">
<div
role="dialog"
aria-modal="true"
aria-labelledby="consent-title"
tabindex="-1"
{@attach focusOnMount}
class="m-auto w-full max-w-md rounded-lg border bg-card p-8 text-center focus:outline-none"
>
<House class="mx-auto mb-4 size-10 text-muted-foreground" />
<h1 class="mb-2 text-xl font-semibold">{t("consent.leaveNowhereTitle")}</h1>
<h2 id="consent-title" class="mb-2 text-xl font-semibold">{t("consent.leaveNowhereTitle")}</h2>
<p role="status" class="text-sm text-muted-foreground">
{leaving === "refused" ? t("consent.leaveRefused") : t("consent.leaveNowhereBody")}
</p>
@@ -161,9 +177,16 @@
</div>
{:else if declined}
<div class="fixed inset-0 z-[60] flex overflow-auto bg-background/95 p-safe-or-4 backdrop-blur-sm">
<div class="m-auto w-full max-w-md rounded-lg border border-warning/40 bg-warning/5 p-8 text-center">
<div
role="dialog"
aria-modal="true"
aria-labelledby="consent-title"
tabindex="-1"
{@attach focusOnMount}
class="m-auto w-full max-w-md rounded-lg border border-warning/40 bg-warning/5 p-8 text-center focus:outline-none"
>
<ShieldCheck class="mx-auto mb-4 size-10 text-warning" />
<h1 class="mb-2 text-xl font-semibold">{t("consent.declinedTitle")}</h1>
<h2 id="consent-title" class="mb-2 text-xl font-semibold">{t("consent.declinedTitle")}</h2>
<p class="text-sm text-muted-foreground">{t("consent.declinedBody")}</p>
<div class="mt-6 flex flex-wrap justify-center gap-2">
<Button variant="outline" onclick={() => (declined = false)}>
@@ -180,7 +203,14 @@
</div>
{:else}
<div class="fixed inset-0 z-[60] flex overflow-auto bg-background/95 p-safe-or-4 backdrop-blur-sm">
<div class="m-auto w-full max-w-lg rounded-xl border bg-card p-6 shadow-xl sm:p-8">
<div
role="dialog"
aria-modal="true"
aria-labelledby="consent-title"
tabindex="-1"
{@attach focusOnMount}
class="m-auto w-full max-w-lg rounded-xl border bg-card p-6 shadow-xl focus:outline-none sm:p-8"
>
<div class="mb-5 flex items-start gap-3">
<span class="grid size-10 shrink-0 place-items-center rounded-lg bg-primary/15 text-primary">
{#if reviewing}
@@ -190,7 +220,7 @@
{/if}
</span>
<div>
<h1 class="text-xl font-semibold">{reviewing ? t("consent.changedTitle") : t("consent.title")}</h1>
<h2 id="consent-title" class="text-xl font-semibold">{reviewing ? t("consent.changedTitle") : t("consent.title")}</h2>
<p class="mt-1 text-sm text-muted-foreground">
{reviewing ? t("consent.changedIntro") : t("consent.intro")}
</p>
+13 -2
View File
@@ -11,6 +11,7 @@
import { legalDocSlug } from "../legal";
import { HUMAN_CHECK_ACTION, HUMAN_CHECK_PATH, type HumanCheckReply } from "../../../shared/human-check";
import Button from "../ui/Button.svelte";
import { overlays, focusOnMount } from "../overlays.svelte";
/**
* The Turnstile human check - WEB ONLY, after the consent gate.
@@ -31,6 +32,14 @@
consent.accepted && humanCheck.hydrated && !humanCheck.cleared && !isUngatedPath(page.url.pathname),
);
// The layout makes the page behind inert while this is open (overlays.svelte.ts).
$effect(() => {
overlays.humanCheck = open;
return () => {
overlays.humanCheck = false;
};
});
type Status = "loading" | "ready" | "confirming" | "failed" | "blocked" | "unsupported";
let status = $state<Status>("loading");
let code = $state<string | null>(null);
@@ -166,14 +175,16 @@
aria-modal="true"
aria-labelledby="human-check-title"
aria-describedby="human-check-intro"
class="m-auto w-full max-w-md rounded-xl border bg-card p-6 shadow-xl sm:p-8"
tabindex="-1"
{@attach focusOnMount}
class="m-auto w-full max-w-md rounded-xl border bg-card p-6 shadow-xl focus:outline-none sm:p-8"
>
<div class="mb-5 flex items-start gap-3">
<span class="grid size-10 shrink-0 place-items-center rounded-lg bg-primary/15 text-primary">
<ShieldCheck class="size-5" />
</span>
<div>
<h1 id="human-check-title" class="text-xl font-semibold">{t("humanCheck.title")}</h1>
<h2 id="human-check-title" class="text-xl font-semibold">{t("humanCheck.title")}</h2>
<p id="human-check-intro" class="mt-1 text-sm text-muted-foreground">{t("humanCheck.intro")}</p>
</div>
</div>
+33
View File
@@ -0,0 +1,33 @@
/**
* Whether a first-run overlay (the consent gate, the human check) is on screen.
*
* Each overlay reports itself from an $effect, so the condition stays in the
* component that decides to show itself. The root layout makes the page
* underneath `inert` while one is open: out of the Tab order and out of the
* accessibility tree, so keyboard focus and a screen reader stay in the
* overlay. Before this, Tab walked through the whole hidden page before it
* reached the gate's buttons. The Ctrl/Cmd+K shortcut stands down too: the
* palette would open UNDER the gate (z-50 against z-[60]) and trap focus where
* nobody can see it.
*
* Never true during prerender: both overlays wait until storage has been read.
*/
class Overlays {
consent = $state(false);
humanCheck = $state(false);
get open(): boolean {
return this.consent || this.humanCheck;
}
}
export const overlays = new Overlays();
/**
* Attachment for an overlay's dialog element: focus it as it mounts, so the
* keyboard and a screen reader start inside it, on its label. The element
* carries tabindex="-1" (focusable from script, not a Tab stop of its own).
*/
export function focusOnMount(node: HTMLElement): void {
node.focus({ preventScroll: true });
}
+5
View File
@@ -1,3 +1,5 @@
import { overlays } from "./overlays.svelte";
/**
* Command-palette open state, kept out of the component so anything can open
* it — the topbar button, the keyboard shortcut, an empty-state prompt — without
@@ -40,6 +42,9 @@ export function installPaletteShortcut(): () => void {
function onKeydown(e: KeyboardEvent) {
if (e.isComposing || e.keyCode === 229) return;
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "k") {
// Not under a first-run overlay: the palette would open beneath it and
// trap focus out of sight (overlays.svelte.ts).
if (overlays.open) return;
e.preventDefault();
palette.toggle();
return;
+13 -4
View File
@@ -11,6 +11,7 @@
import { humanCheck } from "#lib/human-check-state.svelte.js";
import { appReady } from "#lib/app-ready.js";
import { installPaletteShortcut } from "#lib/palette.svelte.js";
import { overlays } from "#lib/overlays.svelte.js";
import { games, installGamesRevalidation } from "#lib/games.svelte.js";
import { upgradeLegacyHashUrl } from "#lib/legacy-url.js";
import { recoverFallbackRoute } from "#lib/fallback-route.js";
@@ -208,16 +209,21 @@
{#if pwaInfo}{@html pwaInfo.webManifest.linkTag}{/if}
</svelte:head>
<!-- inert while the consent gate or the human check is open: the page
behind leaves the Tab order and the accessibility tree, so keyboard
focus and screen readers stay in the overlay (lib/overlays.svelte.ts).
Never set during prerender. -->
{#if bare}
{@render children()}
<!-- display:contents: a wrapper for `inert` that takes no part in the layout. -->
<div class="contents" inert={overlays.open || undefined}>{@render children()}</div>
{:else}
<a href="#main" class="skip-link">{i18n.t("common.skipToContent")}</a>
<a href="#main" class="skip-link" inert={overlays.open || undefined}>{i18n.t("common.skipToContent")}</a>
<!-- The safe-area padding keeps the shell out from under the Android status
bar (and a landscape cutout); <main> takes the bottom inset itself, so
the page scrolls behind the navigation bar but its last line clears it.
See the safe-area utilities in styles/theme.css. -->
<div class="flex h-dvh overflow-hidden pt-safe pl-safe pr-safe">
<div class="flex h-dvh overflow-hidden pt-safe pl-safe pr-safe" inert={overlays.open || undefined}>
<aside class="hidden w-60 shrink-0 border-r bg-card/40 lg:block">
<Sidebar />
</aside>
@@ -253,10 +259,13 @@
</div>
</main>
</div>
<!-- Fixed-position, so where it sits in the DOM only decides what is
inert with it: inside the shell, it goes inert with the page. -->
<BackToTop scroller={main} />
</div>
<CommandPalette />
<BackToTop scroller={main} />
{/if}
<!-- An OVERLAY, not a replacement for the page.