How to don't close ModalContainer if "areYouSureModal" isOpen?
// If you know how to do it in better way - do it
const { isLoading } = useLoading()
const isAreYouSureModalOpen = useAreYouSureModalsStore.getState().isAreYouSureModalOpen
//correct way to add event listener to listen keydown
useEffect(() => {
document.addEventListener("keyup", handleKeyDown)
return () => document.removeEventListener("keyup", handleKeyDown)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoading])
/* onClose - close modal - show scrollbar */
function closeModal() {
onClose()
}
console.log(49, "isAreYouSureModalOpen - ", isAreYouSureModalOpen)
// Close modal on esc
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
if (event.key === "k" && (event.metaKey || event.ctrlKey)) {
//to prevent focus state on browser searchbar
// TODO - import ctrlK modal here and if user press ctrl+K close current modal and open ctrlK modal
event.preventDefault()
}
if (event.key === "Escape" && !isLoading && !isAreYouSureModalOpen) {
console.log(61, "are you sure modal closed - close ModalContainer")
closeModal()
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[isAreYouSureModalOpen, isLoading],
)
// If you know how to do it in better way - do it
const { isLoading } = useLoading()
const isAreYouSureModalOpen = useAreYouSureModalsStore.getState().isAreYouSureModalOpen
//correct way to add event listener to listen keydown
useEffect(() => {
document.addEventListener("keyup", handleKeyDown)
return () => document.removeEventListener("keyup", handleKeyDown)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoading])
/* onClose - close modal - show scrollbar */
function closeModal() {
onClose()
}
console.log(49, "isAreYouSureModalOpen - ", isAreYouSureModalOpen)
// Close modal on esc
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
if (event.key === "k" && (event.metaKey || event.ctrlKey)) {
//to prevent focus state on browser searchbar
// TODO - import ctrlK modal here and if user press ctrl+K close current modal and open ctrlK modal
event.preventDefault()
}
if (event.key === "Escape" && !isLoading && !isAreYouSureModalOpen) {
console.log(61, "are you sure modal closed - close ModalContainer")
closeModal()
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[isAreYouSureModalOpen, isLoading],
)
1 Reply
I put state inside handleKeyDown function
const isAreYouSureModalOpen = useAreYouSureModalsStore.getState().isAreYouSureModalOpen
const isAreYouSureModalOpen = useAreYouSureModalsStore.getState().isAreYouSureModalOpen