Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(editor): transient surfaces restore focus; escape everywhere

yhhb06fb3a47a8d
diff --git a/desktop/src/components/Menu.tsx b/desktop/src/components/Menu.tsx
index 7cf5bcbe5..bebf5ca16 100644
--- a/desktop/src/components/Menu.tsx
+++ b/desktop/src/components/Menu.tsx
@@ -127,6 +127,21 @@ export function ContextMenu({
}) {
const ref = useRef<HTMLDivElement>(null);
const [pos, setPos] = useState({ left: x, top: y });
+ // Who had focus when the menu opened (captured at first render, before the
+ // seed-focus effect steals it) — keyboard focus returns there on close.
+ const opener = useRef<HTMLElement | null>(document.activeElement as HTMLElement | null);
+
+ // Restore focus to the opener — unless the dismissal itself moved focus
+ // somewhere real (outside-press on another control must keep that focus).
+ useEffect(
+ () => () => {
+ const cur = document.activeElement;
+ const stillOurs = cur == null || cur === document.body || (ref.current?.contains(cur) ?? false);
+ const el = opener.current;
+ if (stillOurs && el && document.contains(el)) el.focus();
+ },
+ [],
+ );
// Measure the rendered menu and pull it back inside the viewport (runs before
// paint, so the clamped position is the first thing shown — no flash off-edge).
diff --git a/desktop/src/components/Popover.tsx b/desktop/src/components/Popover.tsx
index 5308625ee..409b32c5c 100644
--- a/desktop/src/components/Popover.tsx
+++ b/desktop/src/components/Popover.tsx
@@ -28,6 +28,19 @@ export function Popover({
}) {
const ref = useRef<HTMLDivElement>(null);
const [pos, setPos] = useState<{ left: number; top: number }>({ left: anchor.left, top: anchor.bottom + 2 });
+ // Who had focus when the popover opened (usually the trigger) — keyboard focus
+ // returns there on close, unless the dismissal itself focused another control.
+ const opener = useRef<HTMLElement | null>(document.activeElement as HTMLElement | null);
+
+ useEffect(
+ () => () => {
+ const cur = document.activeElement;
+ const stillOurs = cur == null || cur === document.body || (ref.current?.contains(cur) ?? false);
+ const el = opener.current;
+ if (stillOurs && el && document.contains(el)) el.focus();
+ },
+ [],
+ );
// Measure then clamp before paint so the first frame is already on-screen.
useLayoutEffect(() => {
diff --git a/desktop/src/panels/ContentBrowser.tsx b/desktop/src/panels/ContentBrowser.tsx
index 3478a4481..9db6d9979 100644
--- a/desktop/src/panels/ContentBrowser.tsx
+++ b/desktop/src/panels/ContentBrowser.tsx
@@ -841,12 +841,13 @@ export function ContentBrowser() {
{crumbs.map((c, i) => (
<Fragment key={c.path}>
{i > 0 && <span className="sep">›</span>}
- <span
+ <button
+ type="button"
className={`cr${i === crumbs.length - 1 ? ' cur' : ''}`}
onClick={() => go(c.path)}
>
{c.name}
- </span>
+ </button>
</Fragment>
))}
</div>
diff --git a/desktop/src/panels/TilemapPainter.tsx b/desktop/src/panels/TilemapPainter.tsx
index bd535c783..93a828901 100644
--- a/desktop/src/panels/TilemapPainter.tsx
+++ b/desktop/src/panels/TilemapPainter.tsx
@@ -192,12 +192,20 @@ export function TilemapPainter() {
};
// Dismiss the add-tileset menu on any outside click (the menu + its opener stop
- // their own pointerdown so those don't self-close it).
+ // their own pointerdown so those don't self-close it) — and on Escape, like
+ // every other transient surface.
useEffect(() => {
if (!addOpen) return;
const close = () => setAddOpen(false);
+ const onKey = (e: KeyboardEvent) => {
+ if (e.key === 'Escape') setAddOpen(false);
+ };
window.addEventListener('pointerdown', close);
- return () => window.removeEventListener('pointerdown', close);
+ window.addEventListener('keydown', onKey);
+ return () => {
+ window.removeEventListener('pointerdown', close);
+ window.removeEventListener('keydown', onKey);
+ };
}, [addOpen]);
// Re-render the layer strip on any model change (add/remove/rename/hide/lock).
diff --git a/desktop/src/panels/TilesetEditor.tsx b/desktop/src/panels/TilesetEditor.tsx
index d9b1f2138..ec5dc45e7 100644
--- a/desktop/src/panels/TilesetEditor.tsx
+++ b/desktop/src/panels/TilesetEditor.tsx
@@ -63,15 +63,15 @@ function GridField(props: { label: string; value: number; min?: number; onCommit
}
// Peering zones in the cell's 3×3 grid; center (membership) is handled separately.
-const ZONES: { gx: number; gy: number; bit: number; corner: boolean }[] = [
- { gx: 1, gy: 0, bit: TB_N, corner: false },
- { gx: 2, gy: 0, bit: TB_NE, corner: true },
- { gx: 2, gy: 1, bit: TB_E, corner: false },
- { gx: 2, gy: 2, bit: TB_SE, corner: true },
- { gx: 1, gy: 2, bit: TB_S, corner: false },
- { gx: 0, gy: 2, bit: TB_SW, corner: true },
- { gx: 0, gy: 1, bit: TB_W, corner: false },
- { gx: 0, gy: 0, bit: TB_NW, corner: true },
+const ZONES: { gx: number; gy: number; bit: number; corner: boolean; dir: string }[] = [
+ { gx: 1, gy: 0, bit: TB_N, corner: false, dir: 'north' },
+ { gx: 2, gy: 0, bit: TB_NE, corner: true, dir: 'north-east' },
+ { gx: 2, gy: 1, bit: TB_E, corner: false, dir: 'east' },
+ { gx: 2, gy: 2, bit: TB_SE, corner: true, dir: 'south-east' },
+ { gx: 1, gy: 2, bit: TB_S, corner: false, dir: 'south' },
+ { gx: 0, gy: 2, bit: TB_SW, corner: true, dir: 'south-west' },
+ { gx: 0, gy: 1, bit: TB_W, corner: false, dir: 'west' },
+ { gx: 0, gy: 0, bit: TB_NW, corner: true, dir: 'north-west' },
];
/** A focused per-tile collision-polygon editor: a magnified tile + click-to-add /
@@ -103,6 +103,15 @@ function PolygonEditor(props: {
commit([...pts, [Math.max(0, Math.min(tw, px)), Math.max(0, Math.min(th, py))]]);
};
+ // Escape closes, like every other transient surface.
+ useEffect(() => {
+ const onKey = (e: KeyboardEvent) => {
+ if (e.key === 'Escape') onClose();
+ };
+ window.addEventListener('keydown', onKey);
+ return () => window.removeEventListener('keydown', onKey);
+ }, [onClose]);
+
return (
<div className="ts-pe-backdrop" onPointerDown={(e) => { if (e.target === e.currentTarget) onClose(); }}>
<div className="ts-pe">
@@ -271,6 +280,8 @@ export function TilesetEditor() {
type="button"
className={'ts-zone' + (on ? ' is-on' : '') + (z.corner ? ' is-corner' : '')}
style={{ left: `${z.gx * 33.34}%`, top: `${z.gy * 33.34}%` }}
+ aria-label={`${z.dir} peering zone`}
+ title={`Peer with the ${z.dir} neighbor`}
onClick={() => toggleBit(id, z.bit)}
/>
);