Skip to content

Commit 2180975

Browse files
feat: replace use-dark-mode with next-themes
1 parent b1ac72e commit 2180975

File tree

11 files changed

+4110
-4495
lines changed

11 files changed

+4110
-4495
lines changed

components/Footer.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import useDarkMode from '@fisch0920/use-dark-mode'
2+
import { useTheme } from 'next-themes'
33
import { FaTwitter } from '@react-icons/all-files/fa/FaTwitter'
44
import { FaZhihu } from '@react-icons/all-files/fa/FaZhihu'
55
import { FaGithub } from '@react-icons/all-files/fa/FaGithub'
@@ -14,14 +14,14 @@ import styles from './styles.module.css'
1414

1515
export const FooterImpl: React.FC = () => {
1616
const [hasMounted, setHasMounted] = React.useState(false)
17-
const darkMode = useDarkMode(false, { classNameDark: 'dark-mode' })
17+
const { setTheme, resolvedTheme } = useTheme()
1818

1919
const onToggleDarkMode = React.useCallback(
2020
(e) => {
2121
e.preventDefault()
22-
darkMode.toggle()
22+
setTheme(resolvedTheme === 'light' ? 'dark' : 'light')
2323
},
24-
[darkMode]
24+
[resolvedTheme, setTheme]
2525
)
2626

2727
React.useEffect(() => {
@@ -41,7 +41,7 @@ export const FooterImpl: React.FC = () => {
4141
onClick={onToggleDarkMode}
4242
title='Toggle dark mode'
4343
>
44-
{darkMode.value ? <IoMoonSharp /> : <IoSunnyOutline />}
44+
{resolvedTheme === 'dark' ? <IoMoonSharp /> : <IoSunnyOutline />}
4545
</a>
4646
)}
4747
</div>

components/NotionPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import cs from 'classnames'
66
import { useRouter } from 'next/router'
77
import { useSearchParam } from 'react-use'
88
import BodyClassName from 'react-body-classname'
9-
import useDarkMode from '@fisch0920/use-dark-mode'
9+
import { useTheme } from 'next-themes'
1010
import { PageBlock } from 'notion-types'
1111

1212
import { Tweet, TwitterContextProvider } from 'react-static-tweets'
@@ -189,7 +189,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
189189
// lite mode is for oembed
190190
const isLiteMode = lite === 'true'
191191

192-
const darkMode = useDarkMode(false, { classNameDark: 'dark-mode' })
192+
const { resolvedTheme } = useTheme()
193193

194194
const siteMapPageUrl = React.useMemo(() => {
195195
const params: any = {}
@@ -284,7 +284,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
284284
rootPageId={site.rootNotionPageId}
285285
rootDomain={site.domain}
286286
fullPage={!isLiteMode}
287-
darkMode={darkMode.value}
287+
darkMode={resolvedTheme === 'dark'}
288288
previewImages={!!recordMap.preview_images}
289289
showCollectionViewDropdown={false}
290290
showTableOfContents={showTableOfContents}

components/NotionPageHeader.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import cs from 'classnames'
3-
import useDarkMode from '@fisch0920/use-dark-mode'
3+
import { useTheme } from 'next-themes'
44
import { IoSunnyOutline } from '@react-icons/all-files/io5/IoSunnyOutline'
55
import { IoMoonSharp } from '@react-icons/all-files/io5/IoMoonSharp'
66

@@ -14,14 +14,18 @@ import styles from './styles.module.css'
1414
export const NotionPageHeader: React.FC<{
1515
block: types.CollectionViewPageBlock | types.PageBlock
1616
}> = ({ block }) => {
17-
const darkMode = useDarkMode(false, { classNameDark: 'dark-mode' })
1817
const [hasMounted, setHasMounted] = React.useState(false)
18+
const { resolvedTheme, setTheme } = useTheme()
1919
const { components, mapPageUrl } = useNotionContext()
2020

2121
React.useEffect(() => {
2222
setHasMounted(true)
2323
}, [])
2424

25+
const onToggleTheme = React.useCallback(() => {
26+
setTheme(resolvedTheme === 'light' ? 'dark' : 'light')
27+
}, [resolvedTheme, setTheme])
28+
2529
if (navigationStyle === 'default') {
2630
return <Header block={block} />
2731
}
@@ -62,8 +66,8 @@ export const NotionPageHeader: React.FC<{
6266
})
6367
.filter(Boolean)}
6468

65-
<div className={cs('breadcrumb', 'button')} onClick={darkMode.toggle}>
66-
{hasMounted && darkMode.value ? (
69+
<div className={cs('breadcrumb', 'button')} onClick={onToggleTheme}>
70+
{hasMounted && resolvedTheme === 'dark' ? (
6771
<IoMoonSharp />
6872
) : (
6973
<IoSunnyOutline />

components/PageSocial.module.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
fill: var(--bg-color);
6969
}
7070

71-
:global(.dark-mode) .action:hover svg {
71+
:global([data-theme='dark']) .action:hover svg {
7272
fill: var(--fg-color);
7373
}
7474

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"test:prettier": "prettier '**/*.{js,jsx,ts,tsx}' --check"
2626
},
2727
"dependencies": {
28-
"@fisch0920/use-dark-mode": "^2.4.0",
2928
"@keyvhq/core": "^1.6.9",
3029
"@keyvhq/redis": "^1.6.10",
3130
"@react-icons/all-files": "^4.1.0",
@@ -38,6 +37,7 @@
3837
"lqip-modern": "^1.2.0",
3938
"next": "^12.1.1",
4039
"next-api-og-image": "^2.2.1",
40+
"next-themes": "^0.1.1",
4141
"node-fetch": "^2.6.1",
4242
"notion-client": "^6.11.0",
4343
"notion-types": "^6.11.0",

pages/_app.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import 'styles/notion.css'
2323
import 'styles/prism-theme.css'
2424

2525
import * as React from 'react'
26-
import { useRouter } from 'next/router'
2726
import * as Fathom from 'fathom-client'
27+
import { useRouter } from 'next/router'
28+
import { ThemeProvider } from 'next-themes'
2829
import posthog from 'posthog-js'
2930

3031
import { bootstrap } from 'lib/bootstrap-client'
@@ -63,5 +64,9 @@ export default function App({ Component, pageProps }) {
6364
}
6465
}, [router.events])
6566

66-
return <Component {...pageProps} />
67+
return (
68+
<ThemeProvider disableTransitionOnChange>
69+
<Component {...pageProps} />
70+
</ThemeProvider>
71+
)
6772
}

pages/_document.tsx

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,6 @@ export default class MyDocument extends Document {
2020
</Head>
2121

2222
<body>
23-
<script
24-
dangerouslySetInnerHTML={{
25-
__html: `
26-
/** Inlined version of noflash.js from use-dark-mode */
27-
;(function () {
28-
var storageKey = 'darkMode'
29-
var classNameDark = 'dark-mode'
30-
var classNameLight = 'light-mode'
31-
32-
function setClassOnDocumentBody(darkMode) {
33-
document.body.classList.add(darkMode ? classNameDark : classNameLight)
34-
document.body.classList.remove(darkMode ? classNameLight : classNameDark)
35-
}
36-
37-
var preferDarkQuery = '(prefers-color-scheme: dark)'
38-
var mql = window.matchMedia(preferDarkQuery)
39-
var supportsColorSchemeQuery = mql.media === preferDarkQuery
40-
var localStorageTheme = null
41-
try {
42-
localStorageTheme = localStorage.getItem(storageKey)
43-
} catch (err) {}
44-
45-
var localStorageExists = localStorageTheme !== null
46-
if (localStorageExists) {
47-
localStorageTheme = JSON.parse(localStorageTheme)
48-
}
49-
50-
// Determine the source of truth
51-
if (localStorageExists) {
52-
// source of truth from localStorage
53-
setClassOnDocumentBody(localStorageTheme)
54-
} else if (supportsColorSchemeQuery) {
55-
// source of truth from system
56-
setClassOnDocumentBody(mql.matches)
57-
localStorage.setItem(storageKey, mql.matches)
58-
} else {
59-
// source of truth from document.body
60-
var isDarkMode = document.body.classList.contains(classNameDark)
61-
localStorage.setItem(storageKey, JSON.stringify(isDarkMode))
62-
}
63-
})();
64-
`
65-
}}
66-
/>
67-
6823
<Main />
6924

7025
<NextScript />

site.config.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ export default siteConfig({
4747

4848
// whether to use the default notion navigation style or a custom one with links to
4949
// important pages
50-
navigationStyle: 'default'
51-
// navigationStyle: 'custom',
52-
// navigationLinks: [
53-
// {
54-
// title: 'About',
55-
// pageId: 'f1199d37579b41cbabfc0b5174f4256a'
56-
// },
57-
// {
58-
// title: 'Contact',
59-
// pageId: '6a29ebcb935a4f0689fe661ab5f3b8d1'
60-
// }
61-
// ]
50+
// navigationStyle: 'default'
51+
navigationStyle: 'custom',
52+
navigationLinks: [
53+
{
54+
title: 'About',
55+
pageId: 'f1199d37579b41cbabfc0b5174f4256a'
56+
},
57+
{
58+
title: 'Contact',
59+
pageId: '6a29ebcb935a4f0689fe661ab5f3b8d1'
60+
}
61+
]
6262
})

styles/notion.css

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,15 @@
331331
);
332332
}
333333

334-
.dark-mode .notion-red_background,
335-
.dark-mode .notion-pink_background,
336-
.dark-mode .notion-blue_background,
337-
.dark-mode .notion-purple_background,
338-
.dark-mode .notion-teal_background,
339-
.dark-mode .notion-yellow_background,
340-
.dark-mode .notion-orange_background,
341-
.dark-mode .notion-brown_background,
342-
.dark-mode .notion-gray_background {
334+
[data-theme='dark'] .notion-red_background,
335+
[data-theme='dark'] .notion-pink_background,
336+
[data-theme='dark'] .notion-blue_background,
337+
[data-theme='dark'] .notion-purple_background,
338+
[data-theme='dark'] .notion-teal_background,
339+
[data-theme='dark'] .notion-yellow_background,
340+
[data-theme='dark'] .notion-orange_background,
341+
[data-theme='dark'] .notion-brown_background,
342+
[data-theme='dark'] .notion-gray_background {
343343
padding: 0;
344344
margin: 0;
345345
border-radius: 0;
@@ -361,15 +361,15 @@
361361
backdrop-filter: saturate(180%) blur(16px);
362362
}
363363

364-
.dark-mode .notion-header {
364+
[data-theme='dark'] .notion-header {
365365
background: transparent;
366366
box-shadow: inset 0 -1px 0 0 rgba(0, 0, 0, 0.1);
367367
backdrop-filter: saturate(180%) blur(8px);
368368
}
369369

370370
/* Workaround for Firefox not supporting backdrop-filter yet */
371371
@-moz-document url-prefix() {
372-
.dark-mode .notion-header {
372+
[data-theme='dark'] .notion-header {
373373
background: hsla(203, 8%, 20%, 0.8);
374374
}
375375
}

styles/prism-theme.css

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
padding: 1.5em !important;
88
}
99

10-
.dark-mode .notion-code {
10+
[data-theme='dark'] .notion-code {
1111
background-color: rgba(17, 24, 39, 1);
1212
border-color: rgba(55, 65, 81, 1);
1313
}
@@ -18,11 +18,11 @@
1818
background: none !important;
1919
padding: 0 !important;
2020
}
21-
.dark-mode .notion code {
21+
[data-theme='dark'] .notion code {
2222
color: rgba(229, 231, 235, 1);
2323
}
2424

25-
.dark-mode .notion .notion-inline-code {
25+
[data-theme='dark'] .notion .notion-inline-code {
2626
background: rgb(71, 76, 80) !important;
2727
color: #ff4081;
2828
padding: 0.2em 0.4em !important;
@@ -42,15 +42,15 @@
4242
.token.comment {
4343
color: #5b9b4c;
4444
}
45-
.dark-mode .token.cdata,
46-
.dark-mode .token.doctype,
47-
.dark-mode .token.prolog {
45+
[data-theme='dark'] .token.cdata,
46+
[data-theme='dark'] .token.doctype,
47+
[data-theme='dark'] .token.prolog {
4848
color: rgba(209, 213, 219, 1);
4949
}
5050
.token.punctuation {
5151
color: rgba(55, 65, 81, 1);
5252
}
53-
.dark-mode .token.punctuation {
53+
[data-theme='dark'] .token.punctuation {
5454
color: rgba(209, 213, 219, 1);
5555
}
5656
.token.boolean,
@@ -95,8 +95,8 @@ code[class*='language-'],
9595
pre[class*='language-'] {
9696
color: rgba(31, 41, 55, 1);
9797
}
98-
.dark-mode code[class*='language-'],
99-
.dark-mode pre[class*='language-'] {
98+
[data-theme='dark'] code[class*='language-'],
99+
[data-theme='dark'] pre[class*='language-'] {
100100
color: rgba(249, 250, 251, 1);
101101
}
102102
pre::-webkit-scrollbar {

0 commit comments

Comments
 (0)