fix(editor): Task 8 round 2, cascade closes the sheet
This commit is contained in:
1 parent
579d8afff4
commit
791fc03a0d
7 files changed
+205
-10
No files matched your search
@@ -141,7 +141,7 @@ function useOwner(sheetId: string, tag: string): HistoryOwner {
|
||||
* or one under a tag already rotated (a sign-out): nobody would read that flag.
|
||||
*/
|
||||
function useUnflagged(nav: EditorNavigation, tag: string) {
|
||||
const confirmed = useLeaveConfirm(nav.guards)
|
||||
const confirmed = useLeaveConfirm(nav)
|
||||
return useCallback(
|
||||
() => confirmed() || useSessionTagStore.getState().held?.tag !== tag,
|
||||
[confirmed, tag],
|
||||
|
||||
@@ -10,6 +10,42 @@ import { useT } from '@/i18n/useT'
|
||||
const BOX =
|
||||
'space-y-2 rounded-lg border border-[var(--color-border)] bg-[var(--color-surface-strong)] p-3'
|
||||
|
||||
/** True when no open dialog lies over `host`: each open one is `host` or holds it. */
|
||||
const onTop = (host: Element) =>
|
||||
[...document.querySelectorAll('dialog[open]')].every(
|
||||
(dialog) => dialog === host || dialog.contains(host),
|
||||
)
|
||||
|
||||
/**
|
||||
* Focuses `target` once its dialog is on top: at once, or, when dialogs over it are
|
||||
* closing (a history jump that asks layer after layer, criterion 23), after the last one
|
||||
* has gone and restored its own focus (two microtasks after the DOM change; Chromium
|
||||
* fires `close` only once the dialog has left the DOM, so the DOM is what is watched).
|
||||
* Answers a cancel.
|
||||
*/
|
||||
function focusWhenOnTop(target: HTMLElement): () => void {
|
||||
const host = target.closest('dialog')
|
||||
if (!host || onTop(host)) {
|
||||
target.focus()
|
||||
return () => undefined
|
||||
}
|
||||
const watcher = new MutationObserver(() =>
|
||||
queueMicrotask(() =>
|
||||
queueMicrotask(() => {
|
||||
if (!onTop(host)) return
|
||||
watcher.disconnect()
|
||||
target.focus()
|
||||
}),
|
||||
),
|
||||
)
|
||||
watcher.observe(document.body, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
attributeFilter: ['open'],
|
||||
})
|
||||
return () => watcher.disconnect()
|
||||
}
|
||||
|
||||
export default function DiscardConfirm({
|
||||
onKeepTyping,
|
||||
onDiscard,
|
||||
@@ -19,7 +55,10 @@ export default function DiscardConfirm({
|
||||
}) {
|
||||
const t = useT()
|
||||
const keep = useRef<HTMLButtonElement>(null)
|
||||
useEffect(() => keep.current?.focus(), [])
|
||||
useEffect(() => {
|
||||
const button = keep.current
|
||||
return button ? focusWhenOnTop(button) : undefined
|
||||
}, [])
|
||||
return (
|
||||
<div
|
||||
role="group"
|
||||
|
||||
Reference in new issue
Block a user