Archived
UI: Modal-Basis + Confirm-Infrastruktur + Feierabend-Tagesabschluss
Fundament für Paket B (UI/UX-Härtung):
- Modal: barrierearmer Basis-Dialog (role/aria-modal, Fokus-Trap, Escape,
Fokus-Rückgabe, Body-Scroll-Lock, Portal). Basis für alle App-Dialoge.
- ConfirmProvider/useConfirm: imperative Bestaetigung (await confirm({...}))
statt window.confirm(); wird als Naechstes an die Loeschpfade gehaengt.
- Feierabend-Dialog: Tagesabschluss mit Zusammenfassung (erledigt/offen/
verworfen) + Hinweis auf offene/unreflektierte ToDos und fehlende Reflexion.
Check-Out feuert erst nach Bestaetigung; bleibt reversibel.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
7285ed9cae
commit
00b611fa3b
4 files changed
+325
-29
No files matched your search
@@ -0,0 +1,69 @@
|
||||
import { createContext, useCallback, useContext, useRef, useState, type ReactNode } from 'react';
|
||||
import { Modal } from './Modal';
|
||||
import { Button } from './Button';
|
||||
|
||||
export type ConfirmOptions = {
|
||||
title: string;
|
||||
message?: ReactNode;
|
||||
confirmLabel?: string;
|
||||
cancelLabel?: string;
|
||||
destructive?: boolean;
|
||||
};
|
||||
|
||||
type ConfirmFn = (opts: ConfirmOptions) => Promise<boolean>;
|
||||
|
||||
const ConfirmContext = createContext<ConfirmFn | null>(null);
|
||||
|
||||
/**
|
||||
* Imperative Bestätigung: `const confirm = useConfirm(); if (await confirm({...})) …`.
|
||||
* Ersetzt window.confirm() durch einen gestylten, barrierearmen Dialog.
|
||||
*/
|
||||
export function useConfirm(): ConfirmFn {
|
||||
const ctx = useContext(ConfirmContext);
|
||||
if (!ctx) throw new Error('useConfirm muss innerhalb von <ConfirmProvider> stehen.');
|
||||
return ctx;
|
||||
}
|
||||
|
||||
export function ConfirmProvider({ children }: { children: ReactNode }) {
|
||||
const [opts, setOpts] = useState<ConfirmOptions | null>(null);
|
||||
const resolver = useRef<((v: boolean) => void) | null>(null);
|
||||
|
||||
const confirm = useCallback<ConfirmFn>((options) => {
|
||||
setOpts(options);
|
||||
return new Promise<boolean>((resolve) => {
|
||||
resolver.current = resolve;
|
||||
});
|
||||
}, []);
|
||||
|
||||
const settle = useCallback((result: boolean) => {
|
||||
resolver.current?.(result);
|
||||
resolver.current = null;
|
||||
setOpts(null);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<ConfirmContext.Provider value={confirm}>
|
||||
{children}
|
||||
<Modal open={opts !== null} onClose={() => settle(false)} title={opts?.title} size="sm">
|
||||
{opts && (
|
||||
<>
|
||||
{opts.message != null && (
|
||||
<div className="mt-2 text-sm text-[var(--color-ink-muted)]">{opts.message}</div>
|
||||
)}
|
||||
<div className="mt-5 flex justify-end gap-2">
|
||||
<Button variant="ghost" onClick={() => settle(false)}>
|
||||
{opts.cancelLabel ?? 'Abbrechen'}
|
||||
</Button>
|
||||
<Button
|
||||
variant={opts.destructive ? 'destructive' : 'primary'}
|
||||
onClick={() => settle(true)}
|
||||
>
|
||||
{opts.confirmLabel ?? 'OK'}
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</Modal>
|
||||
</ConfirmContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import { useEffect, useId, useRef, type ReactNode } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
|
||||
type Size = 'sm' | 'md' | 'lg';
|
||||
|
||||
const maxWidth: Record<Size, string> = {
|
||||
sm: 'max-w-sm',
|
||||
md: 'max-w-md',
|
||||
lg: 'max-w-2xl',
|
||||
};
|
||||
|
||||
/**
|
||||
* Barrierearmer Basis-Dialog: role=dialog/aria-modal, Fokus in den Dialog beim
|
||||
* Öffnen, Fokus-Trap (Tab zyklisch), Escape zum Schließen, Fokus-Rückgabe ans
|
||||
* vorher fokussierte Element, Body-Scroll-Lock. Klick aufs Overlay schließt.
|
||||
* Alle App-Dialoge sollen hierauf aufsetzen.
|
||||
*/
|
||||
export function Modal({
|
||||
open,
|
||||
onClose,
|
||||
title,
|
||||
children,
|
||||
size = 'md',
|
||||
}: {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
title?: ReactNode;
|
||||
children: ReactNode;
|
||||
size?: Size;
|
||||
}) {
|
||||
const panelRef = useRef<HTMLDivElement>(null);
|
||||
const restoreFocus = useRef<HTMLElement | null>(null);
|
||||
const titleId = useId();
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
restoreFocus.current = document.activeElement as HTMLElement | null;
|
||||
|
||||
const panel = panelRef.current;
|
||||
const focusableSel =
|
||||
'input:not([disabled]), textarea:not([disabled]), select:not([disabled]), button:not([disabled]), a[href], [tabindex]:not([tabindex="-1"])';
|
||||
const first = panel?.querySelector<HTMLElement>(focusableSel);
|
||||
(first ?? panel)?.focus();
|
||||
|
||||
function onKey(e: KeyboardEvent) {
|
||||
if (e.key === 'Escape') {
|
||||
e.preventDefault();
|
||||
onClose();
|
||||
return;
|
||||
}
|
||||
if (e.key !== 'Tab' || !panel) return;
|
||||
const nodes = Array.from(panel.querySelectorAll<HTMLElement>(focusableSel));
|
||||
if (nodes.length === 0) {
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
const firstEl = nodes[0]!;
|
||||
const lastEl = nodes[nodes.length - 1]!;
|
||||
if (e.shiftKey && document.activeElement === firstEl) {
|
||||
e.preventDefault();
|
||||
lastEl.focus();
|
||||
} else if (!e.shiftKey && document.activeElement === lastEl) {
|
||||
e.preventDefault();
|
||||
firstEl.focus();
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('keydown', onKey);
|
||||
const prevOverflow = document.body.style.overflow;
|
||||
document.body.style.overflow = 'hidden';
|
||||
return () => {
|
||||
document.removeEventListener('keydown', onKey);
|
||||
document.body.style.overflow = prevOverflow;
|
||||
restoreFocus.current?.focus?.();
|
||||
};
|
||||
}, [open, onClose]);
|
||||
|
||||
if (!open) return null;
|
||||
|
||||
return createPortal(
|
||||
// Overlay-Klick schließt (reiner Maus-Komfort); Tastatur/Screenreader nutzen
|
||||
// Escape und den Abbrechen-Button — daher ist die A11y-Regel hier ein false-positive.
|
||||
// Overlay-Klick (nur direkt aufs Overlay, nicht aufs Panel) schließt — reiner
|
||||
// Maus-Komfort; Tastatur/Screenreader nutzen Escape und den Abbrechen-Button.
|
||||
<div
|
||||
className="fixed inset-0 z-50 flex items-center justify-center bg-black/30 p-4"
|
||||
onClick={(e) => {
|
||||
if (e.target === e.currentTarget) onClose();
|
||||
}}
|
||||
>
|
||||
<div
|
||||
ref={panelRef}
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-labelledby={title != null ? titleId : undefined}
|
||||
tabIndex={-1}
|
||||
className={`w-full ${maxWidth[size]} rounded-[var(--radius-xl)] bg-[var(--color-bg)] p-6 shadow-[var(--shadow-lg)] focus:outline-none`}
|
||||
>
|
||||
{title != null && (
|
||||
<h2 id={titleId} className="text-xl font-semibold">
|
||||
{title}
|
||||
</h2>
|
||||
)}
|
||||
{children}
|
||||
</div>
|
||||
</div>,
|
||||
document.body,
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user