Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Created June 5, 2019 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanflorence/cce0739f0371752a8abde0d50b6c0b07 to your computer and use it in GitHub Desktop.
Save ryanflorence/cce0739f0371752a8abde0d50b6c0b07 to your computer and use it in GitHub Desktop.
function ModalExampleFocusManagement({ label="Hamburger" }) {
const [isOpen, setIsOpen] = useState(false)
const triggerRef = useRef()
const headingRef = useRef()
const justOpenedRef = useRef(false)
useEffect(() => {
if (justOpenedRef.current) {
justOpenedRef.current = false
headingRef.current.focus()
}
})
return (
<div>
<button onClick={() => {
justOpenedRef.current = true
setIsOpen(true)
}} ref={triggerRef}>trigger</button>
{isOpen && (
<div>
<h2 ref={headingRef}>...</h2>
</div>
)}
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment