-
Notifications
You must be signed in to change notification settings - Fork 0
/
modal-context-provider.tsx
48 lines (37 loc) · 1.04 KB
/
modal-context-provider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use client'
import { createContext, PropsWithChildren, useState } from 'react'
export const ModalContext = createContext({
openState: {} as OpenState,
isOpen: (name: string) => false as boolean,
openModal: (name: string) => {},
closeModal: (name: string) => {},
})
interface OpenState {
[key: string]: boolean
}
export const ModalContextProvider = ({ children }: PropsWithChildren) => {
const [openState, setOpenState] = useState<OpenState>({})
const isOpen = (name: string) => {
return openState[name] || false
}
const openModal = (name: string) => {
setOpenState((state) => {
const newState = { ...state }
newState[name] = true
return newState
})
}
const closeModal = (name: string) => {
setOpenState((state) => {
const newState = { ...state }
newState[name] = false
return newState
})
}
return (
<ModalContext.Provider value={{ openState, isOpen, openModal, closeModal }}>
{children}
</ModalContext.Provider>
)
}
export default ModalContextProvider