Skip to content

Commit 9afdbe3

Browse files
feat: revert dark mode to @fisch0920/use-dark-mode
1 parent 4ebda1a commit 9afdbe3

File tree

10 files changed

+98
-41
lines changed

10 files changed

+98
-41
lines changed

components/Footer.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import * as React from 'react'
2-
import { useTheme } from 'next-themes'
32
import { FaTwitter } from '@react-icons/all-files/fa/FaTwitter'
43
import { FaZhihu } from '@react-icons/all-files/fa/FaZhihu'
54
import { FaGithub } from '@react-icons/all-files/fa/FaGithub'
65
import { FaLinkedin } from '@react-icons/all-files/fa/FaLinkedin'
76
import { IoSunnyOutline } from '@react-icons/all-files/io5/IoSunnyOutline'
87
import { IoMoonSharp } from '@react-icons/all-files/io5/IoMoonSharp'
8+
9+
import { useDarkMode } from 'lib/use-dark-mode'
910
import * as config from 'lib/config'
1011

1112
import styles from './styles.module.css'
@@ -14,14 +15,14 @@ import styles from './styles.module.css'
1415

1516
export const FooterImpl: React.FC = () => {
1617
const [hasMounted, setHasMounted] = React.useState(false)
17-
const { setTheme, resolvedTheme } = useTheme()
18+
const { isDarkMode, toggleDarkMode } = useDarkMode()
1819

1920
const onToggleDarkMode = React.useCallback(
2021
(e) => {
2122
e.preventDefault()
22-
setTheme(resolvedTheme === 'light' ? 'dark' : 'light')
23+
toggleDarkMode()
2324
},
24-
[resolvedTheme, setTheme]
25+
[toggleDarkMode]
2526
)
2627

2728
React.useEffect(() => {
@@ -41,7 +42,7 @@ export const FooterImpl: React.FC = () => {
4142
onClick={onToggleDarkMode}
4243
title='Toggle dark mode'
4344
>
44-
{resolvedTheme === 'dark' ? <IoMoonSharp /> : <IoSunnyOutline />}
45+
{isDarkMode ? <IoMoonSharp /> : <IoSunnyOutline />}
4546
</a>
4647
)}
4748
</div>

components/NotionPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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 { useTheme } from 'next-themes'
109
import { PageBlock } from 'notion-types'
1110

1211
import TweetEmbed from 'react-tweet-embed'
@@ -19,6 +18,7 @@ import { getBlockTitle, getPageProperty, formatDate } from 'notion-utils'
1918
import { mapPageUrl, getCanonicalPageUrl } from 'lib/map-page-url'
2019
import { mapImageUrl } from 'lib/map-image-url'
2120
import { searchNotion } from 'lib/search-notion'
21+
import { useDarkMode } from 'lib/use-dark-mode'
2222
import * as types from 'lib/types'
2323
import * as config from 'lib/config'
2424

@@ -177,8 +177,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
177177
// lite mode is for oembed
178178
const isLiteMode = lite === 'true'
179179

180-
const { resolvedTheme } = useTheme()
181-
const isDarkMode = resolvedTheme === 'dark'
180+
const { isDarkMode } = useDarkMode()
182181

183182
const siteMapPageUrl = React.useMemo(() => {
184183
const params: any = {}
@@ -267,6 +266,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
267266
styles.notion,
268267
pageId === site.rootNotionPageId && 'index-page'
269268
)}
269+
darkMode={isDarkMode}
270270
components={components}
271271
recordMap={recordMap}
272272
rootPageId={site.rootNotionPageId}

components/NotionPageHeader.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
import * as React from 'react'
22
import cs from 'classnames'
3-
import { useTheme } from 'next-themes'
43
import { IoSunnyOutline } from '@react-icons/all-files/io5/IoSunnyOutline'
54
import { IoMoonSharp } from '@react-icons/all-files/io5/IoMoonSharp'
65
import { Header, Breadcrumbs, Search, useNotionContext } from 'react-notion-x'
76
import * as types from 'notion-types'
87

8+
import { useDarkMode } from 'lib/use-dark-mode'
99
import { navigationStyle, navigationLinks, isSearchEnabled } from 'lib/config'
1010

1111
import styles from './styles.module.css'
1212

1313
const ToggleThemeButton = () => {
1414
const [hasMounted, setHasMounted] = React.useState(false)
15-
const { resolvedTheme, setTheme } = useTheme()
15+
const { isDarkMode, toggleDarkMode } = useDarkMode()
1616

1717
React.useEffect(() => {
1818
setHasMounted(true)
1919
}, [])
2020

2121
const onToggleTheme = React.useCallback(() => {
22-
setTheme(resolvedTheme === 'light' ? 'dark' : 'light')
23-
}, [resolvedTheme, setTheme])
22+
toggleDarkMode()
23+
}, [toggleDarkMode])
2424

2525
return (
2626
<div
2727
className={cs('breadcrumb', 'button', !hasMounted && styles.hidden)}
2828
onClick={onToggleTheme}
2929
>
30-
{hasMounted && resolvedTheme === 'dark' ? (
31-
<IoMoonSharp />
32-
) : (
33-
<IoSunnyOutline />
34-
)}
30+
{hasMounted && isDarkMode ? <IoMoonSharp /> : <IoSunnyOutline />}
3531
</div>
3632
)
3733
}

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

lib/use-dark-mode.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import useDarkModeImpl from '@fisch0920/use-dark-mode'
2+
3+
export function useDarkMode() {
4+
const darkMode = useDarkModeImpl(false, { classNameDark: 'dark-mode' })
5+
6+
return {
7+
isDarkMode: darkMode.value,
8+
toggleDarkMode: darkMode.toggle
9+
}
10+
}

package.json

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

pages/_app.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import * as React from 'react'
2323
import * as Fathom from 'fathom-client'
2424
import type { AppProps } from 'next/app'
2525
import { useRouter } from 'next/router'
26-
import { ThemeProvider } from 'next-themes'
2726
import posthog from 'posthog-js'
2827

2928
import { bootstrap } from 'lib/bootstrap-client'
@@ -68,9 +67,5 @@ export default function App({ Component, pageProps }: AppProps) {
6867
}
6968
}, [router.events])
7069

71-
return (
72-
<ThemeProvider disableTransitionOnChange>
73-
<Component {...pageProps} />
74-
</ThemeProvider>
75-
)
70+
return <Component {...pageProps} />
7671
}

pages/_document.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,46 @@ 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+
function setClassOnDocumentBody(darkMode) {
32+
document.body.classList.add(darkMode ? classNameDark : classNameLight)
33+
document.body.classList.remove(darkMode ? classNameLight : classNameDark)
34+
}
35+
var preferDarkQuery = '(prefers-color-scheme: dark)'
36+
var mql = window.matchMedia(preferDarkQuery)
37+
var supportsColorSchemeQuery = mql.media === preferDarkQuery
38+
var localStorageTheme = null
39+
try {
40+
localStorageTheme = localStorage.getItem(storageKey)
41+
} catch (err) {}
42+
var localStorageExists = localStorageTheme !== null
43+
if (localStorageExists) {
44+
localStorageTheme = JSON.parse(localStorageTheme)
45+
}
46+
// Determine the source of truth
47+
if (localStorageExists) {
48+
// source of truth from localStorage
49+
setClassOnDocumentBody(localStorageTheme)
50+
} else if (supportsColorSchemeQuery) {
51+
// source of truth from system
52+
setClassOnDocumentBody(mql.matches)
53+
localStorage.setItem(storageKey, mql.matches)
54+
} else {
55+
// source of truth from document.body
56+
var isDarkMode = document.body.classList.contains(classNameDark)
57+
localStorage.setItem(storageKey, JSON.stringify(isDarkMode))
58+
}
59+
})();
60+
`
61+
}}
62+
/>
2363
<Main />
2464

2565
<NextScript />

styles/notion.css

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,15 @@
327327
}
328328

329329
/* disable highlighting in dark mode */
330-
[data-theme='dark'] .notion-red_background,
331-
[data-theme='dark'] .notion-pink_background,
332-
[data-theme='dark'] .notion-blue_background,
333-
[data-theme='dark'] .notion-purple_background,
334-
[data-theme='dark'] .notion-teal_background,
335-
[data-theme='dark'] .notion-yellow_background,
336-
[data-theme='dark'] .notion-orange_background,
337-
[data-theme='dark'] .notion-brown_background,
338-
[data-theme='dark'] .notion-gray_background {
330+
.dark-mode .notion-red_background,
331+
.dark-mode .notion-pink_background,
332+
.dark-mode .notion-blue_background,
333+
.dark-mode .notion-purple_background,
334+
.dark-mode .notion-teal_background,
335+
.dark-mode .notion-yellow_background,
336+
.dark-mode .notion-orange_background,
337+
.dark-mode .notion-brown_background,
338+
.dark-mode .notion-gray_background {
339339
padding: 0;
340340
margin: 0;
341341
border-radius: 0;
@@ -357,15 +357,15 @@
357357
backdrop-filter: saturate(180%) blur(16px);
358358
}
359359

360-
[data-theme='dark'] .notion-header {
360+
.dark-mode .notion-header {
361361
background: transparent;
362362
box-shadow: inset 0 -1px 0 0 rgba(0, 0, 0, 0.1);
363363
backdrop-filter: saturate(180%) blur(8px);
364364
}
365365

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

yarn.lock

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
resolved "https://registry.npmjs.org/@fisch0920/medium-zoom/-/medium-zoom-1.0.7.tgz"
3030
integrity sha512-hPUrgVM/QvsZdZzDTPyL1C1mOtEw03RqTLmK7ZlJ8S/64u4O4O5BvPvjB/9kyLtE6iVaS9UDRAMSwmM9uh2JIw==
3131

32+
"@fisch0920/use-dark-mode@^2.4.0":
33+
version "2.4.0"
34+
resolved "https://registry.yarnpkg.com/@fisch0920/use-dark-mode/-/use-dark-mode-2.4.0.tgz#92ba90c313a37099879a7b0405280933f9105cdc"
35+
integrity sha512-R5onciu81CCOxWVIJcRjp1PfSfBI5fIfxYR2jqydc0nuGWrn4ZB4ciDXiv36PNX5mFIbAgPfjUF0Z5lhSI62TA==
36+
dependencies:
37+
"@use-it/event-listener" "^0.1.2"
38+
use-persisted-state "^0.3.0"
39+
3240
"@humanwhocodes/config-array@^0.9.2":
3341
version "0.9.5"
3442
resolved "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz"
@@ -612,6 +620,11 @@
612620
"@typescript-eslint/types" "5.18.0"
613621
eslint-visitor-keys "^3.0.0"
614622

623+
"@use-it/event-listener@^0.1.2":
624+
version "0.1.7"
625+
resolved "https://registry.yarnpkg.com/@use-it/event-listener/-/event-listener-0.1.7.tgz#443a9b6df87f2f2961b74d42997ce723a7078623"
626+
integrity sha512-hgfExDzUU9uTRTPDCpw2s9jWTxcxmpJya3fK5ADpf5VDpSy8WYwY/kh28XE0tUcbsljeP8wfan48QvAQTSSa3Q==
627+
615628
"@xobotyi/scrollbar-width@^1.9.5":
616629
version "1.9.5"
617630
resolved "https://registry.npmjs.org/@xobotyi/scrollbar-width/-/scrollbar-width-1.9.5.tgz"
@@ -2368,11 +2381,6 @@ next-api-og-image@^2.2.1:
23682381
puppeteer-core "^10.4.0"
23692382
twemoji "^13.1.0"
23702383

2371-
next-themes@^0.1.1:
2372-
version "0.1.1"
2373-
resolved "https://registry.yarnpkg.com/next-themes/-/next-themes-0.1.1.tgz#122113a458bf1d1be5ffed66778ab924c106f82a"
2374-
integrity sha512-Iqxt6rhS/KfK/iHJ0tfFjTcdLEAI0AgwFuAFrMwLOPK5e+MI3I+fzyvBoS+VaOS+NldUiazurhgwYhrfV0VXsQ==
2375-
23762384
next@^12.1.1:
23772385
version "12.1.3"
23782386
resolved "https://registry.npmjs.org/next/-/next-12.1.3.tgz"
@@ -3763,6 +3771,13 @@ use-callback-ref@^1.2.3:
37633771
resolved "https://registry.npmjs.org/use-callback-ref/-/use-callback-ref-1.2.5.tgz"
37643772
integrity sha512-gN3vgMISAgacF7sqsLPByqoePooY3n2emTH59Ur5d/M8eg4WTWu1xp8i8DHjohftIyEx0S08RiYxbffr4j8Peg==
37653773

3774+
use-persisted-state@^0.3.0:
3775+
version "0.3.3"
3776+
resolved "https://registry.yarnpkg.com/use-persisted-state/-/use-persisted-state-0.3.3.tgz#5e0f2236967cec7c34de33abc07ae6818e7c7451"
3777+
integrity sha512-pCNlvYC8+XjRxwnIut4teGC9f2p9aD88R8OGseQGZa2dvqG/h1vEGk1vRE1IZG0Vf161UDpn+NlW4+UGubQflQ==
3778+
dependencies:
3779+
"@use-it/event-listener" "^0.1.2"
3780+
37663781
use-sidecar@^1.0.1:
37673782
version "1.0.5"
37683783
resolved "https://registry.npmjs.org/use-sidecar/-/use-sidecar-1.0.5.tgz"

0 commit comments

Comments
 (0)