diff --git a/CLAUDE.md b/CLAUDE.md
index 1d3d158..57200e6 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -2132,6 +2132,18 @@ frontend at `http://localhost:5173`.
`ConfirmDialog` by the `ConfirmHost` mounted once in `main.tsx`. `tone` is
`danger` (default, pink) or `primary`. Components that already hold dialog
state may still render `ConfirmDialog` directly.
+- **Every dialog is a ``** (`components/Modal.tsx`; decided 2026-11-02):
+ the portal, backdrop, `aria-modal`, Esc and outside click in ONE place —
+ never a hand-rolled backdrop or a per-dialog Esc listener. `onDismiss` is
+ what the dialog's Cancel / Close (or Hide, while a job runs) does, `null`
+ while that is unavailable (busy): Esc and the backdrop then do nothing. An
+ outside click closes only when the press AND the release are both on the
+ backdrop — a plain `onClick` fires on the common ancestor, so a text
+ selection dragged out of the dialog closed it. Esc reaches only the TOP
+ modal (by opening order) and never while a menu is open. `layer="nested"`
+ = `z-[60]` (a dialog opened from a dialog, QuitGuard). The one exception is
+ `RelocateDialog`'s `backdropDismiss={false}` (Esc only: "Not now" lasts
+ until the library is reopened).
## Theme — "dark liquid glass"
@@ -2156,7 +2168,7 @@ easy to break:
opened far from the cursor. `.glass`'s blur and tint therefore live on its
`::after` (z-index -1 under `isolation: isolate`), not on the panel.
- **Every overlay renders through `createPortal(…, document.body)`** — all
- dialogs, `ContextMenu` and `Toast`. Each `.glass` panel is a stacking context
+ dialogs (via `Modal`), `ContextMenu` and `Toast`. Each `.glass` panel is a stacking context
(`isolation`), so an overlay rendered INSIDE one can never rise above a
neighbouring panel, whatever its z-index: New Export (from the sidebar) sat
behind the library, and the deck's Auto Hotcues dialog behind both. One
diff --git a/frontend/src/components/AddFilesDialog.tsx b/frontend/src/components/AddFilesDialog.tsx
index 91ac891..4e031fd 100644
--- a/frontend/src/components/AddFilesDialog.tsx
+++ b/frontend/src/components/AddFilesDialog.tsx
@@ -1,10 +1,10 @@
-import { createPortal } from 'react-dom'
import { useEffect, useRef, useState, type ReactNode } from 'react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { api, isServerPath, type FolderAddPreview, type FolderAddRequest, type JobStatus, type TrackOrigin } from '../api'
import { useCaps } from '../lib/capabilities'
import { placedAudioSummary } from '../lib/platformCopy'
import { FolderPicker } from './FolderPicker'
+import { Modal } from './Modal'
/**
* Add browsed files to the collection — and, optionally, to a playlist or export.
@@ -253,7 +253,7 @@ export function AddFilesDialog({ trackIds, target: requested, note, onClose, onD
)
- return createPortal(
+ return (
<>
{browsing && (
setBrowsing(false)}
/>
)}
-
+
{title}
@@ -461,9 +458,8 @@ export function AddFilesDialog({ trackIds, target: requested, note, onClose, onD
)}
-
- >,
- document.body,
+
+ >
)
}
diff --git a/frontend/src/components/AnalyzeDialog.tsx b/frontend/src/components/AnalyzeDialog.tsx
index 854cea4..55c02f0 100644
--- a/frontend/src/components/AnalyzeDialog.tsx
+++ b/frontend/src/components/AnalyzeDialog.tsx
@@ -1,7 +1,7 @@
-import { createPortal } from 'react-dom'
import { useEffect, useState, type ReactNode } from 'react'
import { keepPreviousData, useQuery, useQueryClient } from '@tanstack/react-query'
import { api, type AnalyzeOptions, type AnalyzePreview, type TrackOrigin } from '../api'
+import { Modal } from './Modal'
interface Props {
/** The selected tracks. */
@@ -100,12 +100,6 @@ export function AnalyzeDialog({ ids, origin, gridWhy, keyWhy, gainWhy, preset, o
const nothingTicked = !!what && !(what.bpm || what.grid || what.key || what.loudness || what.gain)
const count = nothingTicked ? 0 : (p?.tracks_to_analyze ?? 0)
- useEffect(() => {
- const onKey = (e: KeyboardEvent) => e.key === 'Escape' && !busy && onClose()
- window.addEventListener('keydown', onKey)
- return () => window.removeEventListener('keydown', onKey)
- }, [busy, onClose])
-
const run = async () => {
if (!opts || busy || count === 0) return
setBusy(true)
@@ -186,13 +180,9 @@ export function AnalyzeDialog({ ids, origin, gridWhy, keyWhy, gainWhy, preset, o
if (p.grid_locked) gridLines.push(
{plural(p.grid_locked, 'locked grid')} — skipped
)
}
- return createPortal(
-
!busy && onClose()}
- >
-
e.stopPropagation()}>
+ return (
+
+
Analyze
{plural(ids.length, 'track')} selected
@@ -265,7 +255,6 @@ export function AnalyzeDialog({ ids, origin, gridWhy, keyWhy, gainWhy, preset, o
-
,
- document.body,
+
)
}
diff --git a/frontend/src/components/AppSettings.tsx b/frontend/src/components/AppSettings.tsx
index 7d3c4ad..d70830e 100644
--- a/frontend/src/components/AppSettings.tsx
+++ b/frontend/src/components/AppSettings.tsx
@@ -1,7 +1,7 @@
-import { createPortal } from 'react-dom'
import { useEffect, useState } from 'react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { api } from '../api'
+import { Modal } from './Modal'
/**
* Settings → Konduktor: preferences about the app itself rather than about a
@@ -42,12 +42,6 @@ export function AppSettings({ onClose }: { onClose: () => void }) {
const levelCeiling = useAppSetting('levelCeiling')
const [error, setError] = useState(null)
- useEffect(() => {
- const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose()
- window.addEventListener('keydown', onKey)
- return () => window.removeEventListener('keydown', onKey)
- }, [onClose])
-
const set = async (patch: Partial) => {
setError(null)
try {
@@ -57,13 +51,9 @@ export function AppSettings({ onClose }: { onClose: () => void }) {
}
}
- return createPortal(
-
-
e.stopPropagation()}>
+ return (
+
+
Konduktor settings
-
,
- document.body,
+
)
}
diff --git a/frontend/src/components/AutoCueDialog.tsx b/frontend/src/components/AutoCueDialog.tsx
index 8206eb2..06e4de2 100644
--- a/frontend/src/components/AutoCueDialog.tsx
+++ b/frontend/src/components/AutoCueDialog.tsx
@@ -1,4 +1,3 @@
-import { createPortal } from 'react-dom'
import { useEffect, useState } from 'react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import {
@@ -10,6 +9,7 @@ import {
type Track,
} from '../api'
import { formatDuration } from '../lib/format'
+import { Modal } from './Modal'
/**
* Auto Hotcues: bind each hotcue slot to a structural event plus an offset in
@@ -109,14 +109,6 @@ export function AutoCueDialog(props: Props) {
if (rows === null && (prefs.data || prefs.isError)) setRows(loadTemplate(prefs.data, slotCount))
}, [prefs.data, prefs.isError, rows, slotCount])
- useEffect(() => {
- const onKey = (e: KeyboardEvent) => {
- if (e.key === 'Escape' && !busy) onClose()
- }
- window.addEventListener('keydown', onKey)
- return () => window.removeEventListener('keydown', onKey)
- }, [busy, onClose])
-
const setRow = (i: number, patch: Partial) =>
setRows((rs) => rs && rs.map((r, j) => (j === i ? { ...r, ...patch } : r)))
const nudge = (i: number, by: number) =>
@@ -152,11 +144,10 @@ export function AutoCueDialog(props: Props) {
}
}
- return createPortal(
-
!busy && onClose()}>
+ return (
+
e.stopPropagation()}
>
Auto Hotcues
@@ -294,7 +285,6 @@ export function AutoCueDialog(props: Props) {
-
,
- document.body,
+
)
}
diff --git a/frontend/src/components/ConfirmDialog.tsx b/frontend/src/components/ConfirmDialog.tsx
index 4eb9bc6..b1ec4f1 100644
--- a/frontend/src/components/ConfirmDialog.tsx
+++ b/frontend/src/components/ConfirmDialog.tsx
@@ -1,5 +1,5 @@
-import { createPortal } from 'react-dom'
import { useEffect, useState, type ReactNode } from 'react'
+import { Modal } from './Modal'
export interface ConfirmRequest {
title: string
@@ -42,7 +42,6 @@ export function ConfirmDialog({ title, body, confirmLabel, tone = 'danger', altL
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
- if (e.key === 'Escape' && !busy) onClose()
// A focused button handles its own Enter — otherwise tabbing to Cancel
// and pressing Enter would run the destructive action.
if (e.key === 'Enter' && !(e.target instanceof HTMLButtonElement)) {
@@ -54,16 +53,9 @@ export function ConfirmDialog({ title, body, confirmLabel, tone = 'danger', altL
return () => window.removeEventListener('keydown', onKey)
})
- return createPortal(
-
!busy && onClose()}
- >
-
e.stopPropagation()}
- >
+ return (
+
+
{title}
@@ -101,7 +93,6 @@ export function ConfirmDialog({ title, body, confirmLabel, tone = 'danger', altL
-
,
- document.body,
+
)
}
diff --git a/frontend/src/components/ConvertStemsDialog.tsx b/frontend/src/components/ConvertStemsDialog.tsx
index f14a676..8f5e235 100644
--- a/frontend/src/components/ConvertStemsDialog.tsx
+++ b/frontend/src/components/ConvertStemsDialog.tsx
@@ -1,4 +1,3 @@
-import { createPortal } from 'react-dom'
import { useEffect, useMemo, useState, type ReactNode } from 'react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import {
@@ -12,6 +11,7 @@ import {
import { FolderPicker } from './FolderPicker'
import { useServerFolder } from './AddFilesDialog'
import { useCaps } from '../lib/capabilities'
+import { Modal } from './Modal'
/**
* Convert to Stems — one dialog (decided, see the stem-conversion discussion
@@ -224,7 +224,7 @@ export function ConvertStemsDialog({ trackIds, onClose, onStarted, onDownloading
)
const count = trackIds.length
- return createPortal(
+ return (
<>
{browsing && opts && (
setBrowsing(false)}
/>
)}
-
- >,
- document.body,
+
+ >
)
}
diff --git a/frontend/src/components/LocalLibrariesSettings.tsx b/frontend/src/components/LocalLibrariesSettings.tsx
index f7a1496..0a34f64 100644
--- a/frontend/src/components/LocalLibrariesSettings.tsx
+++ b/frontend/src/components/LocalLibrariesSettings.tsx
@@ -1,10 +1,10 @@
-import { createPortal } from 'react-dom'
-import { useEffect, useState } from 'react'
+import { useState } from 'react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { api, type NormalizeOptions } from '../api'
import { localLibraryProblem } from '../lib/platformCopy'
import { FolderPicker } from './FolderPicker'
import { LevelInput, useAppSetting } from './AppSettings'
+import { Modal } from './Modal'
/**
* Settings → Local libraries: where a "This computer" set is pushed.
@@ -32,12 +32,6 @@ export function LocalLibrariesSettings({ onClose }: { onClose: () => void }) {
const [picking, setPicking] = useState(null) // a platform, or 'audio'
const [error, setError] = useState(null)
- useEffect(() => {
- const onKey = (e: KeyboardEvent) => e.key === 'Escape' && !picking && onClose()
- window.addEventListener('keydown', onKey)
- return () => window.removeEventListener('keydown', onKey)
- }, [onClose, picking])
-
const p = prefs.data ?? {}
const overrides = (p.localLibraries as Record | undefined) ?? {}
const normalise = p.localNormalise === true
@@ -74,17 +68,13 @@ export function LocalLibrariesSettings({ onClose }: { onClose: () => void }) {
? d?.audio_root ?? ''
: (d?.libraries.find((l) => l.platform === picking)?.path ?? '').replace(/[\\/][^\\/]*$/, '')
- return createPortal(
+ return (
<>
{picking && (
setPicking(null)} />
)}
-
-
e.stopPropagation()}>
+
+
Local libraries
-
- >,
- document.body,
+
+ >
)
}
diff --git a/frontend/src/components/Modal.tsx b/frontend/src/components/Modal.tsx
new file mode 100644
index 0000000..0749fff
--- /dev/null
+++ b/frontend/src/components/Modal.tsx
@@ -0,0 +1,97 @@
+import { createPortal } from 'react-dom'
+import { useEffect, useRef, useState, type AriaRole, type ReactNode } from 'react'
+
+// Open modals by opening order (taken at first render, so a dialog opened
+// from another always ranks above it). Esc is answered by the top one only.
+const open = new Set()
+let opened = 0
+
+const menuOpen = () => document.querySelector('[role="menu"]') != null
+
+interface Props {
+ /** What Esc and a click on the backdrop do: the dialog's Cancel / Close.
+ * `null` while that is not available (a run in progress), so neither
+ * can close the dialog when its own Cancel could not. */
+ onDismiss: (() => void) | null
+ /** `false`: Esc only. For a dialog whose dismissal decides something a
+ * stray click should not (RelocateDialog). */
+ backdropDismiss?: boolean
+ /** `nested` for a dialog opened from a dialog or above every other one. */
+ layer?: 'dialog' | 'nested'
+ /** The backdrop's padding (default `p-6`). */
+ padding?: string
+ role?: AriaRole
+ 'aria-label'?: string
+ 'aria-labelledby'?: string
+ children: ReactNode
+}
+
+/** The ONE modal container: portal, dimmed backdrop, Esc and outside click.
+ *
+ * An outside click closes only when the press AND the release are both on
+ * the backdrop. A `click` alone fires on the nearest common ancestor, so a
+ * text selection started inside the dialog and released outside it used to
+ * land on the backdrop and close the dialog. */
+export function Modal({
+ onDismiss,
+ backdropDismiss = true,
+ layer = 'dialog',
+ padding = 'p-6',
+ role,
+ children,
+ ...aria
+}: Props) {
+ const [id] = useState(() => ++opened)
+ const dismissRef = useRef(onDismiss)
+ dismissRef.current = onDismiss
+
+ useEffect(() => {
+ open.add(id)
+ const onKey = (e: KeyboardEvent) => {
+ if (e.key !== 'Escape' || e.defaultPrevented) return
+ if (id !== Math.max(...open)) return
+ // A menu over the dialog closes itself on Esc; the dialog stays.
+ if (menuOpen()) return
+ const dismiss = dismissRef.current
+ if (!dismiss) return
+ e.preventDefault()
+ dismiss()
+ }
+ window.addEventListener('keydown', onKey)
+ return () => {
+ open.delete(id)
+ window.removeEventListener('keydown', onKey)
+ }
+ }, [id])
+
+ const pressed = useRef(false)
+ const released = useRef(false)
+
+ return createPortal(
+
{
+ // A press that closes an open menu closes only the menu.
+ pressed.current = e.target === e.currentTarget && e.button === 0 && !menuOpen()
+ released.current = false
+ }}
+ onPointerUp={(e) => {
+ released.current = e.target === e.currentTarget
+ }}
+ onClick={(e) => {
+ const outside = pressed.current && released.current && e.target === e.currentTarget
+ pressed.current = released.current = false
+ if (outside && backdropDismiss) dismissRef.current?.()
+ }}
+ >
+ {children}
+
,
- document.body,
+
)
}
diff --git a/frontend/src/components/QuitGuard.tsx b/frontend/src/components/QuitGuard.tsx
index 982b6c2..f8ebe52 100644
--- a/frontend/src/components/QuitGuard.tsx
+++ b/frontend/src/components/QuitGuard.tsx
@@ -1,8 +1,8 @@
-import { createPortal } from 'react-dom'
import { useEffect, useRef, useState } from 'react'
import { useQuery } from '@tanstack/react-query'
import { api } from '../api'
import { flushPrefs } from '../lib/prefs'
+import { Modal } from './Modal'
/**
* Ask before quitting with unsaved changes: Save · Discard · Cancel.
@@ -99,21 +99,9 @@ export function QuitGuard() {
}
}
- useEffect(() => {
- if (!asking) return
- const onKey = (e: KeyboardEvent) => {
- if (e.key === 'Escape' && !busy) void cancel()
- }
- window.addEventListener('keydown', onKey)
- return () => window.removeEventListener('keydown', onKey)
- })
-
if (!asking) return null
- return createPortal(
-
+ return (
+ void cancel()} layer="nested">
Save changes before quitting?
@@ -147,7 +135,6 @@ export function QuitGuard() {
-
,
- document.body,
+
)
}
diff --git a/frontend/src/components/RelocateDialog.tsx b/frontend/src/components/RelocateDialog.tsx
index d26b425..d454a75 100644
--- a/frontend/src/components/RelocateDialog.tsx
+++ b/frontend/src/components/RelocateDialog.tsx
@@ -1,6 +1,6 @@
-import { createPortal } from 'react-dom'
-import { useEffect, useState } from 'react'
+import { useState } from 'react'
import { api, type PathMapping, type RelocationVolume } from '../api'
+import { Modal } from './Modal'
interface Props {
volumes: RelocationVolume[]
@@ -51,21 +51,12 @@ export function RelocateDialog({ volumes, onAnswered, onOpenManual, onError }: P
}
}
- useEffect(() => {
- const onKey = (e: KeyboardEvent) => e.key === 'Escape' && !busy && answer([])
- window.addEventListener('keydown', onKey)
- return () => window.removeEventListener('keydown', onKey)
- })
-
const set = (root: string, to: string | null) => setChoice((c) => ({ ...c, [root]: to }))
- return createPortal(
- // No close on a backdrop click: dismissing lasts until the library is
- // reopened, which is too much for a stray click to decide.
-
+ return (
+ // Esc is "Not now", but a backdrop click is not: dismissing lasts until
+ // the library is reopened, which is too much for a stray click to decide.
+ void answer([])} backdropDismiss={false}>
Tracks not found
@@ -116,8 +107,7 @@ export function RelocateDialog({ volumes, onAnswered, onOpenManual, onError }: P
e.stopPropagation()}
>
{/* The sleeve, bled into the card behind everything: a soft wash of the
art's own colours. `filter` on this leaf is safe — nothing can open
@@ -166,8 +153,7 @@ export function TrackInfoDialog({ track, artUrl, onClose, onEditTags }: Props) {
)}
-
,
- document.body,
+
)
}
diff --git a/frontend/src/components/UpdateDialog.tsx b/frontend/src/components/UpdateDialog.tsx
index 133ef25..5978dac 100644
--- a/frontend/src/components/UpdateDialog.tsx
+++ b/frontend/src/components/UpdateDialog.tsx
@@ -1,7 +1,7 @@
-import { createPortal } from 'react-dom'
import { useEffect, useState } from 'react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { api } from '../api'
+import { Modal } from './Modal'
/**
* Startup update check against GitHub Releases.
@@ -285,14 +285,10 @@ export function UpdateCheck() {
const sections = newer.map((r) => ({ tag: r.tag_name, changes: whatsChanged(r.body) }))
const grouped = sections.length > 1
- return createPortal(
-
+ return (
+
e.stopPropagation()}
>
{title}
@@ -418,7 +414,6 @@ export function UpdateCheck() {
)}