@ryvora/react-focus-guards
v2.0.0
Published
๐โโ๏ธ Invisible focus guards for robust focus trapping in modals, dialogs, and more.
Maintainers
Readme
Focus Guards ๐โโ๏ธ
Salutations, Focus Guardian! ๐ก๏ธ
The focus-guards module is a nifty utility, often used internally by other components (like FocusScope), to help trap focus within a specific area of your page. It achieves this by adding hidden, focusable elements at the beginning and end of a section.
Think of them as invisible sentinels standing guard at the edges of a focus trap, ensuring the focus doesn't escape! ๐ฐ
How They Work
When focus tries to leave the "guarded" area by tabbing, it hits one of these guards. The guard then programmatically redirects the focus back into the area (e.g., to the last or first focusable element within the trap).
This is crucial for accessibility in components like modals or dialogs, where focus should remain within the component as long as it's active.
Conceptual Usage (Usually Internal)
import { useFocusGuards } from '@ryvora/react-focus-guards'; // Or a component that uses it
function MyFocusTrappedArea({ children }) {
// This hook would typically add the guard elements around the children
// or be part of a <FocusScope> component.
useFocusGuards();
return (
<div>
{/* FocusGuard at the start (conceptually) */}
{children}
{/* FocusGuard at the end (conceptually) */}
</div>
);
}Keep that focus on lockdown! ๐ Your users will thank you!
