feat: add diagnostics and telemetry runtime
This commit is contained in:
@@ -341,6 +341,7 @@ export const Dialog = forwardRef<HTMLDialogElement, DialogProps>(
|
||||
const { message } = useLocale();
|
||||
const dialogRef = useRef<HTMLDialogElement | null>(null);
|
||||
const previousFocusRef = useRef<HTMLElement | null>(null);
|
||||
const focusRestoreGenerationRef = useRef(0);
|
||||
const titleId = useId();
|
||||
const descriptionId = useId();
|
||||
useImperativeHandle(forwardedRef, () => dialogRef.current!, []);
|
||||
@@ -348,6 +349,8 @@ export const Dialog = forwardRef<HTMLDialogElement, DialogProps>(
|
||||
useEffect(() => {
|
||||
const dialog = dialogRef.current;
|
||||
if (!dialog) return;
|
||||
const generation = focusRestoreGenerationRef.current + 1;
|
||||
focusRestoreGenerationRef.current = generation;
|
||||
|
||||
if (open) {
|
||||
previousFocusRef.current =
|
||||
@@ -371,9 +374,19 @@ export const Dialog = forwardRef<HTMLDialogElement, DialogProps>(
|
||||
}
|
||||
const previousFocus = previousFocusRef.current;
|
||||
previousFocusRef.current = null;
|
||||
queueMicrotask(() => {
|
||||
if (previousFocus?.isConnected) previousFocus.focus();
|
||||
});
|
||||
const restoreFocus = () => {
|
||||
if (
|
||||
focusRestoreGenerationRef.current === generation &&
|
||||
previousFocus?.isConnected
|
||||
) {
|
||||
previousFocus.focus();
|
||||
}
|
||||
};
|
||||
if (typeof globalThis.requestAnimationFrame === "function") {
|
||||
const frame = globalThis.requestAnimationFrame(restoreFocus);
|
||||
return () => globalThis.cancelAnimationFrame(frame);
|
||||
}
|
||||
queueMicrotask(restoreFocus);
|
||||
}, [open]);
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user