Security News
cURL Project and Go Security Teams Reject CVSS as Broken
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
@component-driven/react-focus-within
Advanced tools
The missing JS API of the CSS `:focus-within` for React
FocusWithin is a component that allows detecting if one of its children has focus. It can be considered as a missing JS API for focus-within
.
FocusWithin will fire onFocus
once one of its children will receive focus. Similarly onBlur
is going to be fired once focus is left all its children.
Open developer console to see log messages.
<FocusWithin
onFocus={() => {
console.log('Received focus')
}}
onBlur={() => {
console.log('Lost focus')
}}
>
<input type="text" placeholder="Click to activate first input" />
<input type="text" placeholder="Use Tab to activate next input" />
<button>A button to try focus</button>
</FocusWithin>
If you want to react to the focus change, use function as a children pattern. When function is used as children, you must provide the ref
prop.
<FocusWithin
onFocus={() => {
console.log('Received focus')
}}
onBlur={() => {
console.log('Lost focus')
}}
>
{({ focused, getRef }) => (
<form>
<fieldset
ref={getRef}
style={{ borderColor: focused ? 'blue' : '#999' }}
>
<label htmlFor="input1">First input</label>
<input
id="input1"
type="text"
placeholder="Click to activate first input"
/>
<label htmlFor="input2">First input</label>
<input
id="input2"
type="text"
placeholder="Use Tab to activate next input"
/>
<button type="submit">Submit</button>
<p style={{ color: focused ? 'danger' : 'text' }}>
{focused ? 'Focus is inside' : 'No focus here'}
</p>
</fieldset>
</form>
)}
</FocusWithin>
If you're using a CSS-in-JS library like styled-components you need to pass a ref using ref
prop. You can use getRef
function from the parameters.
;({ focused: Boolean, getRef: Function }) => React.Element
const styled = require('styled-components').default
const StyledBox = styled('div')`
padding: 20px;
border: 1px solid;
border-color: ${props =>
props.focused ? 'palevioletred' : '#999'};
& > * + * {
margin-left: 20px;
}
`
;<FocusWithin
onFocus={() => {
console.log('Received focus')
}}
onBlur={() => {
console.log('Lost focus')
}}
>
{({ focused, getRef }) => (
<StyledBox ref={getRef} focused={focused}>
<input
type="text"
placeholder="Click to activate first input"
/>
<input
type="text"
placeholder="Use Tab to activate next input"
/>
<button>A button to try focus</button>
</StyledBox>
)}
</FocusWithin>
Note: It's recommended to use :focus-within
selector instead of interpoaltions whenever possible.
Sometimes it's needed to focus the container node programmatically. You can use the public method focus
. Note that tabIndex={-1}
needs to be set on non-interactive elements to make them receive focus.
const ref = React.createRef()
;<div>
<FocusWithin ref={ref}>
{({ focused, getRef }) => (
<span tabIndex={-1} ref={getRef}>
{focused ? 'Focused' : 'Not focused'}
</span>
)}
</FocusWithin>
<button
onClick={() => {
ref.current.focus()
}}
>
Focus the span
</button>
</div>
import { useRef, useState, useEffect } from 'react'
const firstInput = useRef(null)
const [enabled, setEnabled] = useState(false)
useEffect(() => {
if (enabled) {
firstInput.current.focus()
}
}, [enabled])
;<FocusWithin
onBlur={() => {
enabled && firstInput.current.focus()
}}
>
<fieldset>
<input
type="text"
placeholder="Click to activate first input"
ref={firstInput}
/>
<input type="text" placeholder="Use Tab to activate next input" />
<button
type="submit"
onClick={() => {
setEnabled(!enabled)
}}
>
{enabled ? 'Disable' : 'Enable'} focus trap
</button>
</fieldset>
</FocusWithin>
FAQs
The missing JS API of the CSS `:focus-within` for React
The npm package @component-driven/react-focus-within receives a total of 19 weekly downloads. As such, @component-driven/react-focus-within popularity was classified as not popular.
We found that @component-driven/react-focus-within 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
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.