
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
@benjamminj/use-focus-trap
Advanced tools
React hook that traps focus within a DOM element. Gives you full control of rendering the container and the triggering element.
React hook that traps focus within a DOM element. Gives you full control of rendering the container and the triggering element.
Sometimes when you're building complex UIs you'll need to make sure that user focus stays within a certain element. For example, when you have a modal, you want to keep the user from exiting the modal when they press Tab / Shift + Tab.
This React hook allows you to create a focus trap and attach it to any DOM element.
When the focus trap is enabled, it will:
Tab on the last item in the trap.Shift + Tab on the first item in the trap.npm install @benjamminj/use-focus-trap
Or, if you prefer yarn:
yarn add @benjamminj/use-focus-trap
By default, useFocusTrap will trap any incoming tab events after it is rendered.
import { useFocusTrap } from '@benjamminj/use-focus-trap';
const App = () => {
const focusTrapRef = useFocusTrap();
return (
<div>
{/* Focus will be trapped inside of this div */}
<div ref={focusTrapRef}>
<button>item #1</button>
<button>item #2</button>
<button>item #3</button>
</div>
{/* This button will not receive focus while the trap is active */}
<button>outside!</button>
</div>
);
};
You can also manually control whether the focus trap is enabled or disabled.
import { useFocusTrap } from '@benjamminj/use-focus-trap';
const App = () => {
const [isEnabled, setIsEnabled] = useState(false);
const focusTrapRef = useFocusTrap({ enabled: isEnabled });
return (
<div>
<button onClick={() => setIsEnabled(true)}>enable</button>
<div></div>
</div>
);
};
You can have the trap automatically shift focus to an element of your choosing upon closing the trap.
import { useFocusTrap } from '@benjamminj/use-focus-trap';
const App = () => {
const [isEnabled, setIsEnabled] = useState(false);
const triggerRef = useRef(null);
const focusTrapRef = useFocusTrap({
enabled: isEnabled,
trigger: triggerRef,
});
return (
<div>
<button onClick={() => setIsEnabled(true)} ref={triggerRef}>
enable
</button>
<div ref={focusTrapRef}>
<button>inner 1</button>
<button>inner 2</button>
</div>
</div>
);
};
useFocusTrap is written in TypeScript and accepts 1 generic parameter. You can use this to customize the type of ref that the hook returns.
const App = () => {
const focusTrapRef = useFocusTrap<HTMLDivElement>();
return (
<div>
<div ref={focusTrapRef}>
<button>inside trap 1</button>
<button>inside trap 2</button>
</div>
<button>outside</button>
</div>
);
};
Any PRs and issues are welcome!
FAQs
React hook that traps focus within a DOM element. Gives you full control of rendering the container and the triggering element.
We found that @benjamminj/use-focus-trap demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.