Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Oct 22, 2023
1 parent 8989ece commit 4070e64
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 17 deletions.
11 changes: 0 additions & 11 deletions src/app/main.tsx

This file was deleted.

7 changes: 7 additions & 0 deletions src/app/root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { type ReactNode } from 'react'
import { useApplyCssTransition } from './hooks/useApplyCssTransition.js'

export function Root({ children }: { children: ReactNode }) {
useApplyCssTransition()
return <div className="vocs">{children}</div>
}
12 changes: 7 additions & 5 deletions src/app/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { routes as routes_virtual } from 'virtual:routes'
import { A } from './components/A.js'
import { CodeGroup } from './components/CodeGroup.js'
import { FrontmatterHead } from './components/FrontmatterHead.js'
import { Main } from './main.js'
import { Root } from './root.js'

const components: MDXComponents = {
a: A,
Expand All @@ -26,11 +26,13 @@ export const routes = routes_virtual.map((route_virtual) => ({
<>
{head && <Helmet>{head}</Helmet>}
{frontmatter && <FrontmatterHead frontmatter={frontmatter} />}
<Main>
<route.default components={components} />
</Main>
<Root>
<article>
<route.default components={components} />
</article>
</Root>
</>
),
} satisfies RouteObject
},
}))
})) satisfies RouteObject[]
13 changes: 13 additions & 0 deletions src/app/utils/initialize-theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)')

const storedTheme = localStorage.getItem('vocs.theme')
const theme = storedTheme || 'dark'

if (theme === 'dark') document.documentElement.classList.add('dark')

if (!storedTheme)
// Update the theme if the user changes their OS preference
darkModeMediaQuery.addEventListener('change', ({ matches: isDark }) => {
if (isDark) document.documentElement.classList.add('dark')
else document.documentElement.classList.remove('dark')
})
15 changes: 15 additions & 0 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,19 @@ export async function build({ outDir = 'dist', ssr = false }: BuildParameters =
},
root: __dirname,
})

// initialize theme script
await vite.build({
build: {
lib: {
formats: ['iife'],
name: 'theme',
entry: [resolve(__dirname, './app/utils/initialize-theme.ts')],
},
minify: true,
outDir: resolve(outDir, ssr ? 'client' : ''),
emptyOutDir: false,
},
configFile: undefined,
})
}
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="/app/utils/initialize-theme.ts"></script>
<link href="/styles/index.css" rel="stylesheet">
<!--head-->
</head>
Expand Down
5 changes: 4 additions & 1 deletion src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export async function prerender(args: PrerenderParameters = {}) {
// Prerender each route.
for (const route of routes) {
const { head, body } = await mod.prerender(route)
const html = template.replace('<!--body-->', body).replace('<!--head-->', head)
const html = template
.replace('<!--body-->', body)
.replace('<!--head-->', head)
.replace('/app/utils/initialize-theme.ts', '/initialize-theme.iife.js')
const filePath = `${route.endsWith('/') ? `${route}index` : route}.html`.replace(/^\//, '')
const path = resolve(outDir_resolved, filePath)
const pathDir = dirname(path)
Expand Down

0 comments on commit 4070e64

Please sign in to comment.