Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@component-driven/react-focus-within
Advanced tools
The missing JS API of the CSS `:focus-within` for React
Readme
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 2 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.