-
Notifications
You must be signed in to change notification settings - Fork 2
/
root.tsx
228 lines (206 loc) · 5.6 KB
/
root.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* eslint-disable no-use-before-define */
// eslint-disable-next-line no-use-before-define
import * as React from 'react'
import {AnimatePresence, motion} from 'framer-motion'
import {
useOutlet,
useLoaderData,
Scripts,
useLocation,
Meta,
Links,
ScrollRestoration,
LiveReload,
useCatch,
} from '@remix-run/react'
import type {LinksFunction, LoaderFunction, MetaFunction} from '@remix-run/node'
import global from './styles/global.css'
import tailwindUrl from './styles/tailwind.css'
import Layout from './components/Layout'
import {
NonFlashOfWrongThemeEls,
Theme,
ThemeProvider,
useTheme,
} from './utils/themeProvider'
import {getThemeSession} from './utils/theme.server'
export const links: LinksFunction = () => [
{rel: 'stylesheet', href: global},
{rel: 'stylesheet', href: tailwindUrl},
]
export type LoaderData = {
theme: Theme | null
}
export const loader: LoaderFunction = async ({request}) => {
const themeSession = await getThemeSession(request)
const data: LoaderData = {
theme: themeSession.getTheme(),
}
return data
}
export const meta: MetaFunction = () => ({
'twitter:card': 'summary',
'twitter:image': 'https://thomasledoux.be/me.jpeg',
'og:image': 'https://thomasledoux.be/me.jpeg',
})
/**
* The root module's default export is a component that renders the current
* route via the `<Outlet />` component. Think of this as the global layout
* component for your app.
*/
export default function App() {
const outlet = useOutlet()
const data = useLoaderData<LoaderData>()
return (
<ThemeProvider specifiedTheme={data.theme}>
<Document>
<Scripts />
<link rel="preconnect" href="https://www.google-analytics.com" />
<script
async
src="https://www.googletagmanager.com/gtag/js?id=UA-125864873-1"
/>
<script
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: `window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-125864873-1');`,
}}
/>
<Layout>
<AnimatePresence exitBeforeEnter initial={false}>
<motion.main
key={useLocation().pathname}
initial={{x: '-10%', opacity: 0}}
animate={{x: '0', opacity: 1}}
exit={{y: '-10%', opacity: 0}}
transition={{duration: 0.3}}
>
{outlet}
</motion.main>
</AnimatePresence>
</Layout>
</Document>
</ThemeProvider>
)
}
function Document({
children,
title,
}: {
children: React.ReactNode
title?: string
}) {
const data = useLoaderData<LoaderData>()
const [theme] = useTheme()
return (
<html lang="en" className={theme ?? ''}>
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<NonFlashOfWrongThemeEls ssrTheme={Boolean(data.theme)} />
{title ? <title>{title}</title> : null}
<Meta />
<Links />
</head>
<body className="wrapper bg-white dark:bg-lightgrey flex flex-col">
{children}
{/* eslint-disable-next-line no-use-before-define */}
<RouteChangeAnnouncement />
<ScrollRestoration />
{process.env.NODE_ENV === 'development' && <LiveReload />}
</body>
</html>
)
}
export function CatchBoundary() {
const caught = useCatch()
let message
switch (caught.status) {
case 404:
message = (
<p>Oops! Looks like you tried to visit a page that does not exist.</p>
)
break
default:
throw new Error(caught.data || caught.statusText)
}
return (
<Document title={`${caught.status} ${caught.statusText}`}>
<Layout>
<>
<h1>
{caught.status}: {caught.statusText}
</h1>
{message}
</>
</Layout>
</Document>
)
}
export function ErrorBoundary({error}: {error: Error}) {
console.error(error)
return (
<Document title="Error!">
<Layout>
<div>
<h1>There was an error</h1>
<p>{error.message}</p>
<hr />
<p>Please try again later</p>
</div>
</Layout>
</Document>
)
}
/**
* Provides an alert for screen reader users when the route changes.
*/
const RouteChangeAnnouncement = React.memo(() => {
const [hydrated, setHydrated] = React.useState(false)
const [innerHtml, setInnerHtml] = React.useState('')
const location = useLocation()
React.useEffect(() => {
setHydrated(true)
}, [])
const firstRenderRef = React.useRef(true)
React.useEffect(() => {
// Skip the first render because we don't want an announcement on the
// initial page load.
if (firstRenderRef.current) {
firstRenderRef.current = false
return
}
const pageTitle = location.pathname === '/' ? 'Home page' : document.title
setInnerHtml(`Navigated to ${pageTitle}`)
}, [location.pathname])
// Render nothing on the server. The live region provides no value unless
// scripts are loaded and the browser takes over normal routing.
if (!hydrated) {
return null
}
return (
<div
aria-live="assertive"
aria-atomic
id="route-change-region"
style={{
border: '0',
clipPath: 'inset(100%)',
clip: 'rect(0 0 0 0)',
height: '1px',
margin: '-1px',
overflow: 'hidden',
padding: '0',
position: 'absolute',
width: '1px',
whiteSpace: 'nowrap',
wordWrap: 'normal',
}}
>
{innerHtml}
</div>
)
})