commit 8bdb0a8437aae55ec6ee6cffd3ab01baa3b7c636
parent 3da28b34d42a7bfdd3c01301bdc4b408ab328932
Author: Florian Dold <dold@taler.net>
Date: Thu, 26 Nov 2026 04:13:06 +0100
wallet-webui: keep keyboard focus inside the mobile balance picker
Tab could move behind the open picker. Focus the selected balance on
open, let Escape close the picker, and return focus to the opener or the
selected desktop tab after a resize.
Diffstat:
3 files changed, 62 insertions(+), 3 deletions(-)
diff --git a/packages/taler-harness/src/integrationtests/test-wallet-web-ui-demo.ts b/packages/taler-harness/src/integrationtests/test-wallet-web-ui-demo.ts
@@ -105,6 +105,30 @@ export async function runWalletWebUiDemoTest(t: GlobalTestState) {
await page.getByText("Demo ยท fictional", { exact: true }).waitFor();
});
+ await stage.step("choose a balance with the keyboard", async (page) => {
+ const trigger = page.getByRole("button", { name: "Show all balances" });
+ await trigger.focus();
+ await page.keyboard.press("Enter");
+ const dialog = page.getByRole("dialog", { name: "Balances" });
+ await dialog.locator('button[aria-current="true"]:focus').waitFor();
+ for (const key of ["Tab", "Tab", "Shift+Tab"]) {
+ await page.keyboard.press(key);
+ await dialog.locator("button:focus").waitFor();
+ }
+ await page.keyboard.press("Escape");
+ await dialog.waitFor({ state: "hidden" });
+ t.assertDeepEqual(
+ await trigger.evaluate((el) => el === el.ownerDocument.activeElement),
+ true,
+ );
+ await trigger.press("Enter");
+ await dialog.locator('button[aria-current="true"]:focus').waitFor();
+ await page.setViewportSize({ width: 1024, height: 844 });
+ await dialog.waitFor({ state: "hidden" });
+ await page.locator('[role="tab"][aria-selected="true"]:focus').waitFor();
+ await page.setViewportSize({ width: 390, height: 844 });
+ });
+
await stage.step(
"shop with the selected balance on both layouts",
async (page) => {
diff --git a/packages/wallet-webui/src/screens/BalanceScreen.tsx b/packages/wallet-webui/src/screens/BalanceScreen.tsx
@@ -1,5 +1,6 @@
import { Amounts, ScopeType, type ScopeInfo } from "@gnu-taler/taler-util";
-import { useState } from "preact/hooks";
+import { useDialogFocus } from "@gnu-taler/web-util/browser";
+import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";
import type { TransactionHistoryView } from "../routes/transaction-model.js";
import { Button } from "../ui/Button.js";
import { Card } from "../ui/Card.js";
@@ -100,7 +101,7 @@ function BalanceWarning(props: { balance: BalanceView; compact?: boolean }) {
if (props.balance.warnings.length === 0) return null;
return (
<span
- class={`inline-flex min-w-0 items-center gap-1 text-warning ${props.compact ? "text-xs" : "text-sm"}`}
+ class={`inline-flex min-w-0 items-center gap-1 text-onWarningContainer ${props.compact ? "text-xs" : "text-sm"}`}
title={props.balance.warnings.join(". ")}
>
<WalletIcon name="warning" class="h-4 w-4 shrink-0" />
@@ -628,15 +629,36 @@ function BenefitAction(props: {
function BalanceListOverlay(props: {
balances: BalanceView[];
selectedScopeId: string;
+ returnFocus: () => HTMLElement | null;
onClose: () => void;
onSelect: (id: string) => void;
}) {
+ const root = useRef<HTMLElement>(null);
+ useDialogFocus({
+ root: () => root.current,
+ initialFocus: () =>
+ root.current?.querySelector<HTMLButtonElement>(
+ 'button[aria-current="true"]',
+ ) ?? root.current?.querySelector("button"),
+ returnFocus: props.returnFocus,
+ onEscape: props.onClose,
+ });
+ useLayoutEffect(() => {
+ const desktop = window.matchMedia("(min-width: 768px)");
+ const closeOnDesktop = () => {
+ if (desktop.matches) props.onClose();
+ };
+ desktop.addEventListener("change", closeOnDesktop);
+ closeOnDesktop();
+ return () => desktop.removeEventListener("change", closeOnDesktop);
+ }, [props.onClose]);
return (
<section
+ ref={root}
role="dialog"
aria-modal="true"
aria-labelledby="balance-list-title"
- class="fixed inset-0 z-40 overflow-y-auto bg-background px-6 pb-12 pt-[max(1rem,env(safe-area-inset-top))] text-onBackground md:hidden"
+ class="fixed inset-0 z-40 overflow-y-auto bg-background px-6 pb-12 pt-[max(1rem,env(safe-area-inset-top))] text-onBackground"
>
<header class="grid grid-cols-[3rem_1fr_3rem] items-center">
<button
@@ -720,6 +742,8 @@ export function BalanceScreen(props: {
onScan?: () => void;
onSettings?: () => void;
}) {
+ const balanceListTrigger = useRef<HTMLButtonElement>(null);
+ const desktopBalances = useRef<HTMLDivElement>(null);
const [showBalanceList, setShowBalanceList] = useState(false);
const balances = props.balances ?? [];
const selected =
@@ -817,6 +841,13 @@ export function BalanceScreen(props: {
<BalanceListOverlay
balances={balances}
selectedScopeId={selected.scopeId}
+ returnFocus={() =>
+ window.matchMedia("(min-width: 768px)").matches
+ ? (desktopBalances.current?.querySelector<HTMLButtonElement>(
+ '[aria-selected="true"]',
+ ) ?? null)
+ : balanceListTrigger.current
+ }
onClose={() => setShowBalanceList(false)}
onSelect={select}
/>
@@ -824,6 +855,7 @@ export function BalanceScreen(props: {
<div class="hidden space-y-6 md:block">
<div
+ ref={desktopBalances}
role="tablist"
aria-label={i18n.str`Wallet balances`}
class="flex gap-5 overflow-x-auto border-b-2 border-outline pb-4"
@@ -981,6 +1013,8 @@ export function BalanceScreen(props: {
>
<button
type="button"
+ ref={balanceListTrigger}
+ aria-haspopup="dialog"
onClick={() => setShowBalanceList(true)}
aria-label={i18n.str`Show all balances`}
class="rounded-full p-2 text-secondary hover:bg-secondaryContainer"
diff --git a/packages/wallet-webui/test/screens.test.tsx b/packages/wallet-webui/test/screens.test.tsx
@@ -1428,6 +1428,7 @@ test("Penpot balance view selects scopes, opens the balance list, and hands off
assert.equal(opened.at(-1), "txn:payment:recent");
await user.click(view.getByRole("button", { name: "Withdraw" }));
assert.equal(withdrawalCurrency, "CHF");
+ window.happyDOM.setWindowSize({ width: 390, height: 844 });
const result = await axe.run(window.document.body as unknown as Element);
assert.deepEqual(result.violations, []);
cleanup();