Last active
July 5, 2019 21:36
-
-
Save stevebrownlee/efea4ec6a9109c373e55cc53b140d057 to your computer and use it in GitHub Desktop.
React hook to control visibility of HTML dialogs
This file contains hidden or 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
import { useState } from "react" | |
const useModal = (selector) => { | |
const [modalIsOpen, setIsOpen] = useState(false) | |
function toggleDialog() { | |
setIsOpen(!modalIsOpen) | |
if (modalIsOpen) { | |
document.querySelector(`${selector}`).removeAttribute("open") | |
} else { | |
document.querySelector(`${selector}`).setAttribute("open", true) | |
} | |
} | |
return { toggleDialog, modalIsOpen } | |
} | |
export default useModal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment