
Product
A Fresh Look for the Socket Dashboard
We’ve redesigned the Socket dashboard with simpler navigation, less visual clutter, and a cleaner UI that highlights what really matters.
solid-focus-trap
Advanced tools
SolidJS utility that traps focus inside a given DOM element.
import createFocusTrap from 'solid-focus-trap'
const DialogContent: Component<{
open: boolean
}> = (props) => {
const [contentRef, setContentRef] = createSignal<HTMLElement | null>(null)
createFocusTrap({
element: contentRef,
enabled: () => props.open, // default = true
observeChanges: true, // default
restoreFocus: true, // default
})
return (
<Show when={props.open()}>
<div ref={setContentRef}>Dialog</div>
</Show>
)
}
The first focusable element within the focus trap element will be focused initially. When the trap is disabled, the focus will be restored to the element that was focused before the trap was enabled.
This example shows how to customize the initial focus element so that the focus moves to a specific element when the trap gets enabled.
const DialogContent: Component<{
open: boolean
}> = (props) => {
const [contentRef, setContentRef] = createSignal<HTMLElement | null>(null)
const [initialFocusRef, setInitialFocusRef] =
createSignal<HTMLElement | null>(null)
createFocusTrap({
element: contentRef,
enabled: () => props.open,
initialFocusElement: initialFocusRef,
})
return (
<Show when={props.open()}>
<div ref={setContentRef}>Dialog</div>
<button>Close</button>
<input ref={setInitialFocusRef} />
</Show>
)
}
This utility is from the maintainers of corvu, a collection of unstyled, accessible and customizable UI primitives for SolidJS. It is also documented in the corvu docs under Focus Trap.
FAQs
SolidJS utility that traps focus inside a given DOM element.
The npm package solid-focus-trap receives a total of 2,343 weekly downloads. As such, solid-focus-trap popularity was classified as popular.
We found that solid-focus-trap demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Product
We’ve redesigned the Socket dashboard with simpler navigation, less visual clutter, and a cleaner UI that highlights what really matters.
Industry Insights
Terry O’Daniel, Head of Security at Amplitude, shares insights on building high-impact security teams, aligning with engineering, and why AI gives defenders a fighting chance.
Security News
MCP spec updated with structured tool output, stronger OAuth 2.1 security, resource indicators, and protocol cleanups for safer, more reliable AI workflows.