Mitarbeiter:innen, Organisationsstruktur, Planstellen, Eintritt, Austritt, Versetzung, Befoerderung, Karenz, On- und Offboarding, Berichte, Massenimport und ein lueckenloser Nachweis jeder Aenderung. Next.js 16 mit React 19 und TypeScript, PostgreSQL 17 ueber Kysely, Anmeldung ueber Microsoft Entra ID. Die Fachlogik liegt in der Datenbank: rund dreissig PL/pgSQL-Funktionen fuehren eine Aenderung, ihren Historieneintrag und ihren Protokolleintrag in einer Transaktion aus. Die Zugriffsschranke ist Row Level Security, nicht die Oberflaeche. Betrieb auf Windows Server ohne Docker: ein fertiges Paket, ein Windows-Dienst, der IIS als HTTPS-Vorschaltung und zwei geplante Aufgaben. Siehe deploy/windows/README.md.
91 lines
3.3 KiB
TypeScript
91 lines
3.3 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef, type RefObject } from "react";
|
|
|
|
// Everything a dialog owes the keyboard, in one place so Modal and SlideOver
|
|
// cannot drift apart. Both previously handled only Escape: focus stayed on
|
|
// whatever was behind the overlay, Tab walked straight out of the dialog into
|
|
// the page underneath, and closing left focus on <body> — so the next Tab
|
|
// started again from the top of the document.
|
|
const FOCUSABLE = [
|
|
"a[href]",
|
|
"button:not([disabled])",
|
|
"input:not([disabled]):not([type='hidden'])",
|
|
"select:not([disabled])",
|
|
"textarea:not([disabled])",
|
|
"[tabindex]:not([tabindex='-1'])",
|
|
].join(",");
|
|
|
|
function focusableWithin(container: HTMLElement): HTMLElement[] {
|
|
return [...container.querySelectorAll<HTMLElement>(FOCUSABLE)].filter(
|
|
(el) => el.offsetParent !== null || el.getClientRects().length > 0
|
|
);
|
|
}
|
|
|
|
export function useDialogFocus(open: boolean, onClose: () => void, containerRef: RefObject<HTMLElement | null>) {
|
|
const restoreToRef = useRef<HTMLElement | null>(null);
|
|
|
|
// onClose wird von den meisten Aufrufern bei jedem Rendern neu erzeugt.
|
|
// Stünde es in den Abhängigkeiten, liefe dieser Effekt nach jedem Rendern
|
|
// erneut — und sein Aufräumen holt den Fokus dorthin zurück, wo er vor dem
|
|
// Öffnen war. In einem Dialog mit Eingabefeldern hiesse das: nach dem
|
|
// ersten Zeichen springt der Fokus auf den Knopf, der ihn geöffnet hat, und
|
|
// der Rest der Eingabe geht ins Leere. Über eine Referenz bleibt der
|
|
// aktuelle Rückruf erreichbar, ohne dass der Effekt daran hängt.
|
|
const schliessen = useRef(onClose);
|
|
useEffect(() => {
|
|
schliessen.current = onClose;
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
const container = containerRef.current;
|
|
restoreToRef.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
|
|
// First field rather than the close button: the point of opening these is
|
|
// to fill them in. autoFocus on a child wins, since it has already run.
|
|
if (container && !container.contains(document.activeElement)) {
|
|
const [first] = focusableWithin(container);
|
|
(first ?? container).focus();
|
|
}
|
|
|
|
function onKeyDown(e: KeyboardEvent) {
|
|
if (e.key === "Escape") {
|
|
schliessen.current();
|
|
return;
|
|
}
|
|
if (e.key !== "Tab" || !container) return;
|
|
|
|
const focusable = focusableWithin(container);
|
|
if (focusable.length === 0) {
|
|
e.preventDefault();
|
|
return;
|
|
}
|
|
const first = focusable[0];
|
|
const last = focusable[focusable.length - 1];
|
|
const active = document.activeElement;
|
|
|
|
// Wrap around at both ends, and pull focus back in if it somehow
|
|
// escaped (a click on the backdrop, say).
|
|
if (!container.contains(active)) {
|
|
e.preventDefault();
|
|
(e.shiftKey ? last : first).focus();
|
|
} else if (e.shiftKey && active === first) {
|
|
e.preventDefault();
|
|
last.focus();
|
|
} else if (!e.shiftKey && active === last) {
|
|
e.preventDefault();
|
|
first.focus();
|
|
}
|
|
}
|
|
|
|
window.addEventListener("keydown", onKeyDown);
|
|
return () => {
|
|
window.removeEventListener("keydown", onKeyDown);
|
|
// Back to whatever opened the dialog, so the next Tab continues from
|
|
// there instead of restarting at the top of the page.
|
|
restoreToRef.current?.focus();
|
|
};
|
|
}, [open, containerRef]);
|
|
}
|