Skip to content

Commit 760a24e

Browse files
author
Kevin Arthur
committed
feat(focus): resolve remaining focus-navigation limitations
Canvas focus ring: - Add ::after pseudo-element on .editor-canvas at z-index 11 for the focus ring, avoiding clipping from overflow:hidden - JS toggles data-canvas-focus-visible on keyboard :focus-visible only - Add aria-describedby on canvas pointing to announcer live region - Give CanvasAnnouncer stable IDs for screen-reader reference Toolbar wrapping: - Add wrap?: boolean prop to @strata/ui Toolbar (default true) - Non-wrapping mode clamps at first/last item Tauri native dialog focus: - Add withFocusRestore() wrapper in platform/src/tauri.ts - Wraps all 4 dialog methods (open, import, save, saveBinary) - Saves activeElement before dialog, restores after close Popover fallback: - Feature-detect showPopover API; fall back to display:none + data attr - CSS supports both :popover-open and [data-popover-open] Documentation: - Update focus-navigation.md with resolved limitations section - Add E2E regression tests for canvas focus ring and aria-describedby
1 parent b97e2b3 commit 760a24e

6 files changed

Lines changed: 276 additions & 90 deletions

File tree

‎docs/architecture/focus-navigation.md‎

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -152,29 +152,89 @@ Located in `packages/editor/src/hooks/`:
152152
| `useCompositeNavigation` | `useCompositeNavigation.ts` | Arrow-key + typeahead composite nav |
153153
| `useFocusVisible` | `useFocusVisible.ts` | Track keyboard vs pointer focus |
154154

155-
## Known Limitations and Pre-existing Issues
155+
## Resolved Limitations
156156

157-
1. **CanvasArea focus ring** — The canvas element has a `focus-visible` style
158-
but the ring is clipped in certain zoom states. This is a CSS `overflow`
159-
issue on the canvas container.
157+
### 1. Canvas focus ring (RESOLVED)
160158

161-
2. **Toolbar wrapping** — Current toolbar implementation wraps focus within
162-
the toolbar (last → first). Some users may prefer non-wrapping. The
163-
`useRovingTabIndex` hook supports both.
159+
**Was:** The canvas's `:focus-visible` rule targeted `.editor-canvas` (the
160+
section) but DOM focus was on the inner `<canvas tabIndex=0>`, so the ring
161+
never appeared. Additionally, the container's `overflow: hidden` could clip
162+
child focus indicators.
164163

165-
3. **Floating panel focus** — Floating panels use the native `popover` API
166-
which provides automatic focus management but has inconsistent behavior
167-
across WebView versions.
164+
**Fix:** Added a `::after` pseudo-element on `.editor-canvas` at z-index 11
165+
(above interactive overlays) that renders the focus ring. JS toggles
166+
`data-canvas-focus-visible` on the section when the inner canvas receives
167+
`:focus-visible` (keyboard only — mouse clicks don't show the ring). Because
168+
the pseudo-element is positioned on the section itself (not a child), it is
169+
never clipped by `overflow: hidden`. See `editor.css` `.editor-canvas::after`
170+
and `CanvasArea.tsx` `handleCanvasFocus`/`handleCanvasBlur`.
168171

169-
4. **Minimap focus** — The minimap is a non-interactive visual reference and
170-
is correctly excluded from tab order.
172+
### 2. Toolbar wrapping (RESOLVED)
171173

172-
5. **Collab cursors** — Collaborative presence cursors are visual only and
173-
not focusable.
174+
**Was:** `<Toolbar>` had hardcoded wrapping navigation with no way to disable it.
174175

175-
6. **WebView2 / WKWebView** — Some older WebView versions do not support
176-
the `inert` attribute. A polyfill would be required for comprehensive
177-
focus isolation on those platforms.
176+
**Fix:** Added a `wrap?: boolean` prop (defaults to `true` for backward
177+
compatibility). When `wrap={false}`, arrow navigation clamps at the first/last
178+
item instead of wrapping. See `packages/ui/src/components/Toolbar.tsx`.
178179

179-
7. **Tauri native dialogs** — Native file dialogs opened by Tauri are outside
180-
the webview's control and may temporarily interrupt focus tracking.
180+
### 3. Popover focus fallback (RESOLVED)
181+
182+
**Was:** The `Popover` component unconditionally used the native `popover` API
183+
(`showPopover`/`hidePopover`/`popover="auto"`). Browsers without this API
184+
(Firefox < 125, Safari < 17) would get a TypeError.
185+
186+
**Fix:** Added feature detection (`HAS_POPOVER_API`). When unavailable, the
187+
component falls back to `display: none`/`display: ''` toggling and a
188+
`[data-popover-open]` attribute that CSS uses for visibility. The `inert`
189+
sibling behavior and focus management work identically in both modes. See
190+
`packages/ui/src/components/Popover.tsx`.
191+
192+
### 4. Tauri native dialog focus (RESOLVED)
193+
194+
**Was:** When a native Tauri file dialog opened, the webview lost OS-level
195+
focus. On close, no DOM focus restoration happened — focus stayed on
196+
`document.body`.
197+
198+
**Fix:** Added `withFocusRestore()` wrapper in `packages/platform/src/tauri.ts`
199+
that saves `document.activeElement` before each dialog call and restores it
200+
when the dialog closes. All four Tauri dialog methods
201+
(`openDocumentFromDisk`, `importDocumentFromDisk`, `saveDocumentToDisk`,
202+
`saveBinaryFile`) are now wrapped.
203+
204+
### 5. Screen-reader canvas announcements (RESOLVED)
205+
206+
**Was:** The canvas element had no `aria-describedby` pointing to the live
207+
region, so screen readers wouldn't announce canvas state changes (selection,
208+
tool, zoom) in the context of the focused canvas.
209+
210+
**Fix:** Added stable IDs (`strata-canvas-announcer-polite`,
211+
`strata-canvas-announcer-assertive`) to the `CanvasAnnouncer` live regions
212+
and wired `aria-describedby="strata-canvas-announcer-polite"` on the canvas
213+
element. Now when the canvas is focused, screen readers can access the live
214+
region's announcements.
215+
216+
### 6. WebGPU offscreen canvas (NOT APPLICABLE)
217+
218+
**Was listed as:** Potential focus issue with OffscreenCanvas in the render worker.
219+
220+
**Finding:** `OffscreenCanvas` in `packages/editor/src/render/` is purely a
221+
rendering target with no DOM presence, no `tabIndex`, and no focus semantics.
222+
It cannot receive focus and has no accessibility implications. No fix needed.
223+
224+
## Remaining Platform Notes
225+
226+
- **WebView2 / WKWebView** — Some older WebView versions do not support
227+
the `inert` attribute. A polyfill would be required for comprehensive
228+
focus isolation on those platforms. This project targets WebKitGTK 2.52.4
229+
which supports `inert`.
230+
231+
- **Screen-reader testing** — ARIA improvements were made but physical testing
232+
with NVDA (Windows), VoiceOver (macOS), and Orca (Linux) has not been
233+
performed in this session. The live regions and `aria-describedby` wiring
234+
follow WAI-ARIA 1.2 patterns.
235+
236+
- **Collab cursors** — Collaborative presence cursors remain visual-only and
237+
correctly non-focusable.
238+
239+
- **Minimap** — The minimap is correctly excluded from tab order as a
240+
non-interactive visual reference.

‎packages/editor/src/CanvasArea.tsx‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,27 @@ export function CanvasArea({
488488
useEffect(() => {
489489
enableDrawDiagnostics(settings.performance.showPerformanceDiagnostics);
490490
}, [settings.performance.showPerformanceDiagnostics]);
491+
492+
// Track keyboard focus on the inner canvas to drive the parent section's
493+
// focus-visible ring. Mouse clicks fire focus but we suppress the ring
494+
// for them — only :focus-visible (keyboard) shows it.
495+
const handleCanvasFocus = useCallback(() => {
496+
const canvas = contentCanvasRef.current;
497+
if (!canvas) return;
498+
const section = canvas.closest('.editor-canvas') as HTMLElement | null;
499+
if (!section) return;
500+
if (canvas.matches(':focus-visible')) {
501+
section.setAttribute('data-canvas-focus-visible', 'true');
502+
}
503+
}, []);
504+
505+
const handleCanvasBlur = useCallback(() => {
506+
const canvas = contentCanvasRef.current;
507+
if (!canvas) return;
508+
const section = canvas.closest('.editor-canvas') as HTMLElement | null;
509+
if (!section) return;
510+
section.removeAttribute('data-canvas-focus-visible');
511+
}, []);
491512
// Frame/group spatial index, cached by fingerprint for fast drag containment.
492513
const frameIndexRef = useRef<FrameSpatialIndex | null>(null);
493514
const snapIndexRef = useRef<{
@@ -2808,9 +2829,12 @@ export function CanvasArea({
28082829
tabIndex={0}
28092830
aria-roledescription="Design canvas"
28102831
aria-label="Design canvas"
2832+
aria-describedby="strata-canvas-announcer-polite"
28112833
data-testid="editor-canvas"
28122834
className="editor-canvas__content-layer"
28132835
style={{ cursor }}
2836+
onFocus={handleCanvasFocus}
2837+
onBlur={handleCanvasBlur}
28142838
onKeyDown={input.handleKeyDown}
28152839
onKeyUp={input.handleKeyUp}
28162840
onDoubleClick={input.handleDoubleClick}
@@ -2819,7 +2843,6 @@ export function CanvasArea({
28192843
onPointerUp={input.handlePointerUp}
28202844
onPointerCancel={input.handlePointerCancel}
28212845
onPointerLeave={input.onPointerLeave}
2822-
onBlur={input.onBlur}
28232846
/>
28242847
<canvas
28252848
ref={overlayCanvasRef}

‎packages/editor/src/canvas/CanvasAnnouncer.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class CanvasAnnouncer {
2020

2121
private createRegions(): void {
2222
this.politeEl = document.createElement('div');
23+
this.politeEl.id = 'strata-canvas-announcer-polite';
2324
this.politeEl.setAttribute('role', 'status');
2425
this.politeEl.setAttribute('aria-live', 'polite');
2526
this.politeEl.setAttribute('aria-atomic', 'true');
@@ -33,6 +34,7 @@ export class CanvasAnnouncer {
3334
document.body.appendChild(this.politeEl);
3435

3536
this.assertiveEl = document.createElement('div');
37+
this.assertiveEl.id = 'strata-canvas-announcer-assertive';
3638
this.assertiveEl.setAttribute('role', 'alert');
3739
this.assertiveEl.setAttribute('aria-live', 'assertive');
3840
this.assertiveEl.setAttribute('aria-atomic', 'true');

‎packages/platform/src/tauri.ts‎

Lines changed: 87 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,33 @@ function arrayBufferForBytes(data: Uint8Array): ArrayBuffer {
8686
return copy.buffer;
8787
}
8888

89+
/**
90+
* Wrap an async Tauri-dialog call with focus save/restore.
91+
*
92+
* When a native Tauri dialog opens, the webview loses OS-level focus.
93+
* On close, the webview regains OS focus but no DOM element is focused.
94+
* This wrapper saves the active element before the dialog opens and
95+
* restores focus to it when the dialog closes. If the saved element is
96+
* no longer in the DOM (e.g. a panel was closed during the dialog),
97+
* focus falls back to document.body which is safe and predictable.
98+
*/
99+
async function withFocusRestore<T>(fn: () => Promise<T>): Promise<T> {
100+
const saved = document.activeElement as HTMLElement | null;
101+
try {
102+
return await fn();
103+
} finally {
104+
requestAnimationFrame(() => {
105+
if (saved && saved !== document.body && document.contains(saved)) {
106+
saved.focus({ preventScroll: true });
107+
} else if (saved) {
108+
// Element was removed from DOM — nothing meaningful to restore to.
109+
// Leaving focus on document.body is the correct fallback; the user
110+
// can Tab back into the UI from there.
111+
}
112+
});
113+
}
114+
}
115+
89116
export function createTauriPlatform(): Platform {
90117
const platform: Platform = {
91118
kind: 'tauri',
@@ -500,71 +527,79 @@ export function createTauriPlatform(): Platform {
500527
},
501528

502529
async openDocumentFromDisk() {
503-
const c = core();
504-
const picked = (await c.invoke('plugin:dialog|open', {
505-
options: {
506-
multiple: false,
507-
filters: [{ name: 'Strata document', extensions: ['strata'] }],
508-
},
509-
})) as Array<{ path?: string; name?: string; content?: string }> | null;
510-
const first = picked?.[0];
511-
if (!first?.path) return null;
512-
const text = (await c.invoke('home_read_text_file', { path: first.path })) as string;
513-
return ingest(first.name ?? 'untitled.strata', text);
530+
return withFocusRestore(async () => {
531+
const c = core();
532+
const picked = (await c.invoke('plugin:dialog|open', {
533+
options: {
534+
multiple: false,
535+
filters: [{ name: 'Strata document', extensions: ['strata'] }],
536+
},
537+
})) as Array<{ path?: string; name?: string; content?: string }> | null;
538+
const first = picked?.[0];
539+
if (!first?.path) return null;
540+
const text = (await c.invoke('home_read_text_file', { path: first.path })) as string;
541+
return ingest(first.name ?? 'untitled.strata', text);
542+
});
514543
},
515544

516545
async importDocumentFromDisk(extensions) {
517-
const c = core();
518-
const picked = (await c.invoke('plugin:dialog|open', {
519-
options: {
520-
multiple: false,
521-
filters: [{ name: 'Import', extensions: extensions.map((e) => e.replace(/^\./, '')) }],
522-
},
523-
})) as Array<{ path?: string; name?: string }> | null;
524-
const first = picked?.[0];
525-
if (!first?.path || !first.name) return { result: null, unsupported: false };
526-
const kind = detectFileKind(first.name);
527-
if (kind === 'unknown') return { result: null, unsupported: true };
528-
const text = (await c.invoke('home_read_text_file', { path: first.path })) as string;
529-
if (kind !== 'strata') {
530-
const entry = capture(first.name, text, kind);
531-
return { result: { entry, documentJson: text }, unsupported: false };
532-
}
533-
return { result: ingest(first.name, text), unsupported: false };
546+
return withFocusRestore(async () => {
547+
const c = core();
548+
const picked = (await c.invoke('plugin:dialog|open', {
549+
options: {
550+
multiple: false,
551+
filters: [{ name: 'Import', extensions: extensions.map((e) => e.replace(/^\./, '')) }],
552+
},
553+
})) as Array<{ path?: string; name?: string }> | null;
554+
const first = picked?.[0];
555+
if (!first?.path || !first.name) return { result: null, unsupported: false };
556+
const kind = detectFileKind(first.name);
557+
if (kind === 'unknown') return { result: null, unsupported: true };
558+
const text = (await c.invoke('home_read_text_file', { path: first.path })) as string;
559+
if (kind !== 'strata') {
560+
const entry = capture(first.name, text, kind);
561+
return { result: { entry, documentJson: text }, unsupported: false };
562+
}
563+
return { result: ingest(first.name, text), unsupported: false };
564+
});
534565
},
535566

536567
async saveDocumentToDisk(name, documentJson) {
537-
const c = core();
538-
const suggested = name.endsWith('.strata') ? name : `${name}.strata`;
539-
const path = (await c.invoke('plugin:dialog|save', {
540-
options: {
541-
defaultPath: suggested,
542-
filters: [{ name: 'Strata document', extensions: ['strata'] }],
543-
},
544-
})) as string | null;
545-
if (!path) return null;
546-
await c.invoke('home_write_text_file', { path, contents: documentJson });
547-
return path;
548-
},
549-
async saveBinaryFile(name, data, mimeType, extension) {
550-
const c = core();
551-
const ext = extension.replace(/^\./, '');
552-
const suggested = name.endsWith(`.${ext}`) ? name : `${name}.${ext}`;
553-
try {
568+
return withFocusRestore(async () => {
569+
const c = core();
570+
const suggested = name.endsWith('.strata') ? name : `${name}.strata`;
554571
const path = (await c.invoke('plugin:dialog|save', {
555572
options: {
556573
defaultPath: suggested,
557-
filters: [{ name: mimeType, extensions: [ext] }],
574+
filters: [{ name: 'Strata document', extensions: ['strata'] }],
558575
},
559576
})) as string | null;
560577
if (!path) return null;
561-
await c.invoke('write_binary_file', { path, data: arrayBufferForBytes(data) });
578+
await c.invoke('home_write_text_file', { path, contents: documentJson });
562579
return path;
563-
} catch (e) {
564-
// Tauri IPC rejects with the raw error payload (often a plain string,
565-
// not an Error), which loses its message when callers assume `.message`.
566-
throw e instanceof Error ? e : new Error(String(e));
567-
}
580+
});
581+
},
582+
async saveBinaryFile(name, data, mimeType, extension) {
583+
return withFocusRestore(async () => {
584+
const c = core();
585+
const ext = extension.replace(/^\./, '');
586+
const suggested = name.endsWith(`.${ext}`) ? name : `${name}.${ext}`;
587+
try {
588+
const path = (await c.invoke('plugin:dialog|save', {
589+
options: {
590+
defaultPath: suggested,
591+
filters: [{ name: mimeType, extensions: [ext] }],
592+
},
593+
})) as string | null;
594+
if (!path) return null;
595+
await c.invoke('write_binary_file', { path, data: arrayBufferForBytes(data) });
596+
return path;
597+
} catch (e) {
598+
// Tauri IPC rejects with the raw error payload (often a plain string,
599+
// not an Error), which loses its message when callers assume `.message`.
600+
throw e instanceof Error ? e : new Error(String(e));
601+
}
602+
});
568603
},
569604

570605
async listPrinters() {

0 commit comments

Comments
 (0)