-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-browser.js
108 lines (99 loc) · 2.45 KB
/
gatsby-browser.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*global FB*/
import "tailwindcss/dist/base.min.css"
// polyfill
import "intersection-observer"
import smoothscroll from "smoothscroll-polyfill"
import React, { useState, useEffect, useCallback } from "react"
import ThemeContext from "./src/lib/context/ThemContext"
import metaConfig from "./gatsby-meta-config"
// kick off the polyfill!
smoothscroll.polyfill()
export const onInitialClientRender = () => {
if (metaConfig.share.facebookAppId) {
window.fbAsyncInit = function () {
FB.init({
appId: metaConfig.share.facebookAppId,
xfbml: true,
version: "v3.2",
})
FB.AppEvents.logPageView()
}
;(function (d, s, id) {
var js,
fjs = d.getElementsByTagName(s)[0]
if (d.getElementById(id)) {
return
}
js = d.createElement(s)
js.id = id
js.src = "https://connect.facebook.net/en_US/sdk.js"
fjs.parentNode.insertBefore(js, fjs)
})(document, "script", "facebook-jssdk")
}
}
const Wrapper = ({ element }) => {
const [isDarkMode, setDarkMode] = useState(false)
const toggleThemMode = () => {
setDarkMode(!isDarkMode)
if (!isDarkMode) {
localStorage.setItem("isDarkMode", "true")
} else {
localStorage.setItem("isDarkMode", "false")
}
}
const printCopyright = useCallback(() => {
console.log(
"%c%s",
`
background: linear-gradient(
90deg,
#7f7fd5, #86a8e7
);
padding: 0.5rem;
border-radius: 0.25rem;
color: #FFFFFF;
font-size: 2.25rem;
`,
"Imron Rosyadi"
)
console.log(
"%c%s",
`
padding: 0.25rem;
border-radius: 0.25rem;
background-color: #edf2f7;
color: #3737B9;
font-size: 1.5rem;
`,
"©irosyadi using JaeSeoKim Theme"
)
console.log(
"%c%s",
`
font-size: 1.25rem;
`,
"http://jaeseokim.github.io/"
)
}, [])
useEffect(() => {
if (window) {
try {
const mode = localStorage.getItem("isDarkMode")
if (mode === "true") {
setDarkMode(true)
} else {
setDarkMode(false)
}
} catch (error) {
localStorage.setItem("isDarkMode", "false")
}
printCopyright()
}
}, [printCopyright])
return (
<ThemeContext.Provider value={{ isDarkMode, toggleThemMode }}>
{element}
</ThemeContext.Provider>
)
}
export const wrapRootElement = ({ element }) => <Wrapper element={element} />