-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_app.tsx
159 lines (147 loc) · 3.86 KB
/
_app.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React from "react";
import App from "next/app";
import Head from "next/head";
import { ThemeProvider } from "emotion-theming";
import { MDXProvider } from "@mdx-js/react";
import preset from "@rebass/preset";
import Layout from "../layouts/default";
import CodeBlock from "../components/CodeBlock";
import TitleText from "../components/TileText";
import { InlineCode } from "../components/InlineCode";
interface Colors {
text: string;
background: string;
primary: string;
secondary: string;
muted: string;
gray: string;
highlight: string; // hsla(205, 100%, 40%, 0.125)
}
/**
* Min and max are inclusive
* @returns random number
*/
function getRandomInt(floor: number, ceil: number) {
const min = Math.ceil(floor);
const max = Math.floor(ceil + 1);
return Math.floor(Math.random() * (max - min) + min);
}
export const ThemeRefresher = React.createContext({
canRefreshTheme: false,
refreshTheme: async () => {
console.error("No Theme to refresh!");
},
resetTheme: async () => {
console.error("No Theme to refresh!");
},
});
export function useRefreshTheme() {
return React.useContext(ThemeRefresher);
}
const components = {
code: CodeBlock,
inlineCode: InlineCode,
TitleText,
};
class MyApp extends App {
state = {
themes: [] as typeof preset[],
theme: preset,
};
setThemesFromColormindSet = async () => {
const themes = (await fetch("/static/colors.json").then((r) =>
r.json()
)) as Colors[];
this.setState({ themes });
};
refreshThemeColors = async (): Promise<void> => {
this.setState({
theme: {
...preset,
colors: {
...this.state.theme.colors,
...this.state.themes[getRandomInt(0, 99)],
},
},
});
};
resetThemeColors = async (): Promise<void> => {
this.setState({
theme: {
...preset,
colors: {
...preset.colors,
},
},
});
};
componentDidMount() {
this.setThemesFromColormindSet();
}
render(): JSX.Element {
const { Component, pageProps } = this.props;
const { theme } = this.state;
return (
<>
<Head>
<title>Ryan Jerue</title>
<meta name="theme-color" content={`${theme.colors.primary}`} />
</Head>
<ThemeRefresher.Provider
value={{
canRefreshTheme: this.state.themes.length > 0,
refreshTheme: this.refreshThemeColors,
resetTheme: this.resetThemeColors,
}}
>
<ThemeProvider theme={this.state.theme}>
<MDXProvider components={components}>
<Layout>
<Component {...pageProps} />
</Layout>
</MDXProvider>
<style jsx global>{`
body {
background-color: ${theme.colors.background};
font-family: "verdana";
}
code {
color: ${theme.colors.primary};
outline: 1px solid ${theme.colors.muted};
font-size: 0.9rem;
background-color: ${theme.colors.highlight};
padding: 0px 3px 1px;
margin: 0px -1px;
border-radius: 8px;
}
h1,
h2,
h3 {
color: ${theme.colors.primary};
}
h2:first-of-type {
margin-bottom: 2px;
}
h3,
h4,
h5,
h6 {
margin: 0;
}
a {
color: ${theme.colors.primary};
}
li {
padding-bottom: 4px;
}
li:last-of-type {
padding-bottom: 0px;
}
`}</style>
</ThemeProvider>
</ThemeRefresher.Provider>
</>
);
}
}
export default MyApp;