Skip to content

Instantly share code, notes, and snippets.

@stevebrownlee
Last active July 5, 2019 21:36
Show Gist options
  • Save stevebrownlee/efea4ec6a9109c373e55cc53b140d057 to your computer and use it in GitHub Desktop.
Save stevebrownlee/efea4ec6a9109c373e55cc53b140d057 to your computer and use it in GitHub Desktop.
React hook to control visibility of HTML dialogs
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