forked from compsoc-edinburgh/comp-soc.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimatedBackgroundContext.tsx
41 lines (34 loc) · 1013 Bytes
/
AnimatedBackgroundContext.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
'use client'
import { createContext, useContext, useState, ReactNode } from 'react'
interface AnimatedBackgroundContextProps {
isActive: boolean
toggleBackground: () => void
}
const AnimatedBackgroundContext = createContext<
AnimatedBackgroundContextProps | undefined
>(undefined)
export const useAnimatedBackground = (): AnimatedBackgroundContextProps => {
const context = useContext(AnimatedBackgroundContext)
if (!context) {
throw new Error(
'useAnimatedBackground must be used within an AnimatedBackgroundProvider'
)
}
return context
}
interface AnimatedBackgroundProviderProps {
children: ReactNode
}
export const AnimatedBackgroundProvider = ({
children,
}: AnimatedBackgroundProviderProps) => {
const [isActive, setIsActive] = useState(false)
const toggleBackground = () => {
setIsActive(!isActive)
}
return (
<AnimatedBackgroundContext.Provider value={{ isActive, toggleBackground }}>
{children}
</AnimatedBackgroundContext.Provider>
)
}