982cdb63eb
Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com> Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
19 lines
716 B
TypeScript
19 lines
716 B
TypeScript
import type { RefObject } from "react";
|
|
|
|
/**
|
|
* Builds a Radix `onCloseAutoFocus` handler that returns keyboard focus to
|
|
* `focusReturnRef` on close. Radix's own default restores focus to whatever
|
|
* element had it when the dialog opened, which has nothing valid to return to
|
|
* when the dialog was opened from a menu item that has already unmounted
|
|
* (e.g. the task actions menu closes on entry activation before its dialog
|
|
* opens).
|
|
*/
|
|
export function createFocusReturnHandler(focusReturnRef?: RefObject<HTMLElement | null>) {
|
|
return (event: Event) => {
|
|
const target = focusReturnRef?.current;
|
|
if (!target || !document.contains(target)) return;
|
|
event.preventDefault();
|
|
target.focus();
|
|
};
|
|
}
|