Skip to content

Modals: corner close button and full-screen EEG viewer - #206

Merged
neuromechanist merged 4 commits into
mainfrom
feature/issue-199-modal-close-and-viewer
Jul 29, 2026
Merged

neuromechanist merged 4 commits into
mainfrom
feature/issue-199-modal-close-and-viewer

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Closes #199.

What changed

Part 1 — shared corner close button

Added src/components/DialogCloseButton.astro, a small reusable component (X icon + aria-label="Close" + a delegated document-level click handler that calls dialog.close() on the nearest ancestor <dialog>, or sets .open = false on the nearest ancestor <details>). Used it in:

  • ActionBar.astro's three dialogs ([data-download-dialog], [data-compute-dialog], [data-citations-dialog])
  • ProvenanceToggle.astro's role="dialog" popover

The X is an addition next to the existing bottom Close button (kept — it still reads as the natural end-of-content action) and Esc (unchanged, native <dialog> behavior; the shared component's script only listens for clicks). Keyboard reachable, visible :focus-visible ring using var(--color-focus) / var(--shadow-focus), theme-aware via tokens only.

Restructured the three ActionBar dialogs so the corner X stays pinned while long content scrolls underneath it: the outer <dialog> no longer scrolls itself (padding: 0; overflow: hidden), a new inner .compute-dialog__scroll / .cite-dialog__scroll wrapper does. Without this, the X would have the same "goes out of view on a long dialog" problem the issue opened with, just moved from the bottom to the top.

Part 2 — EEG viewer in a full-screen modal

Added src/components/EegViewerDialog.astro, a page-level dialog sized to min(70vw, 1400px) wide by min(70vh, 900px) tall (clamped for small/huge screens, block-size: fit-content so short states like the "unavailable" fallback don't leave a mostly-empty box). Same close-button/scroll-wrapper treatment as part 1, minus the bottom Close button — the viewer's own toolbar already fills the available height, so a redundant button below it would sit below the fold, the exact problem the corner X exists to fix.

src/pages/dataset/[id].astro's click handler now branches "eeg" preview buttons into a new openEegViewerModal() instead of the old inline per-row-slot toggle. The dynamic import (await import("../../lib/eeg-viewer/viewer")) still only fires on first open of a signal file — code-split behavior unchanged. Every existing failure path is preserved: zarrAvailable === "false" with its recorded zarrFailureReason + download fallback, the mount-failure catch + download fallback, and the aria-busy / data-loaded (undefined/"loading"/"true") state markers.

src/lib/eeg-viewer/viewer.ts: mountEegViewer() now returns Promise<(() => void) | undefined> (a teardown handle) instead of Promise<void>, and tears down any previous instance mounted into the slot at the very top of the function rather than only on the happy path — previously a re-open that failed early (store won't open, no channel groups, no 2D context) could strand the prior instance's ResizeObserver/MutationObserver/WebGL context under the DOM being overwritten.

render-dir-listing.ts: the eeg preview button no longer renders a per-row data-preview-slot (it mounts into the modal's one shared slot instead) and uses aria-haspopup="dialog" rather than aria-expanded, since it opens a dialog rather than toggling adjacent content.

Design decisions

Shared component vs. per-file duplication. AGENTS.md's scoped-style rule says siblings that each render their own row markup should duplicate layout CSS rather than reach for a shared stylesheet (BidsTree.astro/BidsDirChildren.astro). That rule is about siblings independently rendering similar-looking markup; it isn't a blanket ban on small reusable leaf components. DialogCloseButton.astro is exactly the normal Astro pattern for that case — one component owns both its markup and its own scoped style/script, and gets imported wherever needed. Using it three times on one page only registers one delegated click listener (Astro dedupes identical inline scripts by content), so there's no per-instance wiring cost either.

ConfirmDialog: no X. Judged this deliberately rather than applying the X uniformly. ConfirmDialog.astro already has a Cancel button that is behaviorally identical to what a corner X would do here — both just call dialog.close() without confirming (ConfirmDialog's close handler only proceeds when returnValue === "confirm", so neither X nor Cancel could be confused for the destructive action). Since there's no actual ambiguity hazard, but the dialog is small, single-purpose, and already has an explicit dismiss control front and center, adding a second one that does the same thing is clutter, not a fix. Left it out.

EEG viewer sizing. ~70% of the viewport is a cap (max-inline-size/max-block-size), not a fixed size, so short states (the "unavailable" fallback) shrink to content instead of leaving a big empty box — caught this exact issue while testing and fixed it with block-size: fit-content.

Concurrency guard changed shape, not intent. The old per-row data-loaded gate only had to protect against re-clicking the same row mid-fetch (each file had its own isolated slot). The shared modal slot can now have a different file opened before the first one's mount settles, so dataset/[id].astro tracks a monotonic eegModalSeq plus the current teardown handle: a superseded mount attempt (rapid reopen with a different file, or a close while a fetch is in flight) calls its own destroy() and — if the reason it was superseded was a close rather than a newer open — re-clears the slot, since destroy() only unwinds side effects (observers/listeners/GL context), not the DOM the mount already wrote. Verified this specific race by hand (see below).

What I verified in the browser

Used the puppeteer MCP tools against the dev server (Playwright's own Chromium wasn't installed locally and the download was too slow to block on; puppeteer gave full coverage instead) on a real dataset with real EEG recordings (nm000103, HBN-EEG NC) and a real mirrored dataset (on007753) for the ProvenanceToggle popover, in both light and dark theme (data-theme attribute):

  • All three ActionBar dialogs open/close via stub button, corner X, and confirmed closed dialogs are display: none (this caught the [open]-gating bug described above — a screenshot showed "Compute on this dataset" bleeding through behind the download dialog before the fix).
  • Scroll-pinning: shrank the viewport, scrolled the download dialog's content, confirmed the X stays in the corner instead of scrolling away.
  • ProvenanceToggle popover: opens via summary click, X closes it (details.open flips to false).
  • EEG modal: opened a real recording (129-channel resting-state EEG), confirmed the canvas renders at real (non-zero) dimensions sized to the modal, confirmed the status line reflects the actual loaded recording.
  • Reopening a different file: immediately shows "Loading viewer…" with zero stale canvas before the new mount resolves; final state after resolution matches the newly requested file with exactly one canvas (no duplicates).
  • Rapid double-open race (file A clicked, file B clicked 30ms later before A settles): final state is file B, one canvas, no orphaned instance.
  • Close while a mount is in flight (closed 40ms after clicking, well before the store fetch could finish): slot ends up empty with zero canvases — this caught the DOM-not-cleared bug described above and confirmed the fix.
  • zarrAvailable === "false" fallback path (forced via a synthetic dataset attribute): renders the recorded failure reason + download link inside the modal.
  • Both themes: light and dark screenshots of the download dialog and the EEG modal; corner X visible and legible in both.
  • bun run typecheck (0 errors), bun run test (907/907 passing, including two new/updated tests in render-dir-listing.test.ts for the eeg row's slot-less markup and aria-haspopup), bun run lint (clean).

Not verified: real keyboard-driven Esc-to-close (native <dialog> behavior — untouched by this change, and synthetic KeyboardEvents can't trigger the browser's trusted-event-only handling for it; didn't want to block on the slow Playwright Chromium download to confirm something I didn't modify). Real :focus-visible styling on Tab-driven keyboard focus, for the same synthetic-event limitation — confirmed the CSS declaration is present and matches the existing :focus-visible pattern used elsewhere in the codebase (SiteNotices.astro's dismiss button, ConfirmDialog.astro's verify input).

Left out

Nothing deliberately scoped out beyond the ConfirmDialog judgment call above.

Adds DialogCloseButton.astro, a small reusable component rendering an
X in the top-right corner with aria-label="Close", keyboard focus, and
a visible focus ring. Used by ActionBar's three dialogs and the
ProvenanceToggle popover, alongside the existing bottom Close button
(kept) and Esc (unchanged, native <dialog> behavior).

Restructures ActionBar's dialogs so the close button stays pinned
while content scrolls: the outer dialog no longer scrolls itself, an
inner .compute-dialog__scroll/.cite-dialog__scroll wrapper does.
Fixes a bug caught in manual testing where `display: flex` on the
bare dialog class selector outranked the browser's default
`dialog { display: none }` and left closed dialogs visible; gated
behind [open] instead.

ConfirmDialog intentionally does not get the X: its Cancel button is
already the same dismiss action a corner X would be (both just close
without confirming), so adding one would be redundant on a small,
single-purpose confirm box rather than fixing a real gap.
Moves the Zarr signal viewer from an inline slot cramped into a BIDS
tree row into EegViewerDialog.astro, a page-level dialog sized to
~70% of the viewport (clamped for small/huge screens) using the same
close-button treatment as the other dialogs, minus a redundant bottom
Close button since the viewer already fills the available height.

mountEegViewer() now returns a teardown handle instead of Promise<void>,
and tears down any previous instance in the slot at the very start of
the function (previously only on the happy path, which could strand a
prior instance's ResizeObserver/MutationObserver/WebGL context if a
re-open failed early). The page script tracks the handle plus a
monotonic sequence number so a superseded mount (reopen with a
different file, or a close mid-fetch) tears itself down instead of
writing stale content into the shared slot; closing the dialog clears
the slot and invalidates any mount still in flight.

The code-split dynamic import is unchanged — the viewer + zarrita
bundle still loads only when a signal file is first opened. Every
existing failure path (zarrAvailable=false with its recorded reason,
the mount-failure catch, the aria-busy/data-loaded state markers) is
preserved, just relocated from the per-row click handler into
openEegViewerModal().

render-dir-listing.ts: the eeg preview button no longer renders a
per-row preview slot (it mounts into the modal's shared slot instead)
and uses aria-haspopup="dialog" rather than aria-expanded, since it
opens a dialog rather than toggling adjacent content.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 29, 2026 •

Copy link
Copy Markdown

Deploying nemar-website with  Cloudflare Pages  Cloudflare Pages

Latest commit: 16991f0
Status: ✅  Deploy successful!
Preview URL: https://cda14a8c.nemar-website.pages.dev
Branch Preview URL: https://feature-issue-199-modal-clos.nemar-website.pages.dev

View logs

@neuromechanist

Copy link
Copy Markdown
Contributor Author

Both must-fix findings addressed, plus the Esc gap. Pushed as 2006f18.

1. Stale mount clobbering a live viewer

Confirmed the root cause reading the code: mountEegViewer's teardown at the top only sees what was in the slot when that call started, and the caller's supersede check ran after the mount had already written its DOM. The comment above slot.innerHTML = "" even stated the flawed assumption outright ("The previous instance (if any) was already torn down at the top of this function") — true for the call itself, false for a later call that started and finished during the await.

Implemented the suggested predicate. ViewerOptions gains isStale?: () => boolean, checked immediately after openRecording resolves and in the catch block — a superseded attempt returns before it can write "viewer unavailable" over a working viewer too. The caller passes isStale: () => seq !== eegModalSeq, so a superseded mount never reaches slot.innerHTML = "" and can't orphan the instance that legitimately owns the slot.

Updated the misleading comment to say what is actually guaranteed now.

2. WebGL context leak

dispose() in gl-trace.ts now calls canvas.getContext("webgl2")?.getExtension("WEBGL_lose_context")?.loseContext() after deleting the buffer/VAO/program. Your measurement (20 opens, 20 contexts, 4 already force-evicted by Chrome) is in the code comment so the next person doesn't read the loseContext() call as cargo-culting. Noted there too that the fallout is benign — createGlTraceRenderer returning null drops to the 2D path — but silently degrading to CPU rendering mid-session is precisely the failure nobody thinks to look for.

3. ProvenanceToggle Escape

Agreed it's pre-existing rather than a regression, and agreed it should be fixed here rather than deferred: the PR gives that popover a close button that makes it read as a dialog, so it should behave like one. Added a keydown handler closing any open [data-prov-toggle] on Escape, and returning focus to the <summary> chip that opened it — matching what native <dialog> does, and what a keyboard user needs to not lose their place.

Not changed

bun run typecheck 0 errors, bun run test 907/907, bun run lint clean.

Worth flagging: fix 1 is a genuine race, and I have not re-run your delayed-fetch reproduction against it. The logic is straightforward and the guard sits before every slot write, but if you still have that harness set up, re-running it would be the real confirmation.

@neuromechanist
neuromechanist merged commit 9d94e84 into main Jul 29, 2026
5 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-199-modal-close-and-viewer branch September 30, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modals: corner close button on every dialog, and open the EEG viewer in one

1 participant