Skip to content

Commit

Permalink
docs: Fix the example by using React hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Okonetchnikov authored and okonet committed Apr 21, 2020
1 parent 33d4d3d commit d5edad2
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/FocusWithin.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,19 @@ const ref = React.createRef()
## Naïve focus trap implementation

```jsx
const firstInput = React.createRef()
initialState = {
enabled: false
}
import { useRef, useState, useEffect } from 'react'

const firstInput = useRef(null)
const [enabled, setEnabled] = useState(false)

useEffect(() => {
if (enabled) {
firstInput.current.focus()
}
}, [enabled])
;<FocusWithin
onBlur={() => {
state.enabled && firstInput.current.focus()
enabled && firstInput.current.focus()
}}
>
<fieldset>
Expand All @@ -154,15 +160,10 @@ initialState = {
<button
type="submit"
onClick={() => {
setState({
enabled: !state.enabled
})
if (!state.enabled) {
firstInput.current.focus()
}
setEnabled(!enabled)
}}
>
{state.enabled ? 'Disable' : 'Enable'} focus trap
{enabled ? 'Disable' : 'Enable'} focus trap
</button>
</fieldset>
</FocusWithin>
Expand Down

0 comments on commit d5edad2

Please sign in to comment.