Created
June 5, 2019 19:58
-
-
Save ryanflorence/cce0739f0371752a8abde0d50b6c0b07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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