-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContainer.tsx
79 lines (74 loc) · 2.21 KB
/
Container.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React, { useEffect, useRef, useState } from "react"
import ResizeObserver from "resize-observer-polyfill"
import useAppContext from "~/hooks/useAppContext"
import Loading from "./components/Loading"
import { editorFonts } from "./constants/fonts"
import { getFonts } from "./store/slices/fonts/actions"
import { useAppDispatch } from "./store/store"
function Container({ children }: { children: React.ReactNode }) {
const containerRef = useRef<HTMLDivElement>(null)
const { isMobile, setIsMobile } = useAppContext()
const [loaded, setLoaded] = useState(false)
const dispatch = useAppDispatch()
const updateMediaQuery = (value: number) => {
if (!isMobile && value >= 800) {
setIsMobile(false)
} else if (!isMobile && value < 800) {
setIsMobile(true)
} else {
setIsMobile(false)
}
}
useEffect(() => {
const containerElement = containerRef.current!
const containerWidth = containerElement.clientWidth
updateMediaQuery(containerWidth)
const resizeObserver = new ResizeObserver((entries) => {
const { width = containerWidth } = (entries[0] && entries[0].contentRect) || {}
updateMediaQuery(width)
})
resizeObserver.observe(containerElement)
return () => {
if (containerElement) {
resizeObserver.unobserve(containerElement)
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
useEffect(() => {
dispatch(getFonts())
loadFonts()
setTimeout(() => {
setLoaded(true)
}, 1000)
}, [])
const loadFonts = () => {
const promisesList = editorFonts.map((font) => {
// @ts-ignore
return new FontFace(font.name, `url(${font.url})`, font.options).load().catch((err) => err)
})
Promise.all(promisesList)
.then((res) => {
res.forEach((uniqueFont) => {
if (uniqueFont && uniqueFont.family) {
document.fonts.add(uniqueFont)
}
})
})
.catch((err) => console.log({ err }))
}
return (
<div
ref={containerRef}
style={{
flex: 1,
display: "flex",
height: "100vh",
width: "100vw",
}}
>
{loaded ? <>{children} </> : <Loading />}
</div>
)
}
export default Container