Skip to content

Commit facc4e3

Browse files
feat: fix dark mode; minor optimizations
1 parent 2f09c9d commit facc4e3

File tree

6 files changed

+84
-46
lines changed

6 files changed

+84
-46
lines changed

components/NotionPage.tsx

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { NotionRenderer } from 'react-notion-x'
1818
import { getBlockTitle, getPageProperty, formatDate } from 'notion-utils'
1919
import { mapPageUrl, getCanonicalPageUrl } from 'lib/map-page-url'
2020
import { mapImageUrl } from 'lib/map-image-url'
21-
import { getPageTweet } from 'lib/get-page-tweet'
2221
import { searchNotion } from 'lib/search-notion'
2322
import * as types from 'lib/types'
2423
import * as config from 'lib/config'
@@ -27,9 +26,8 @@ import * as config from 'lib/config'
2726
import { Loading } from './Loading'
2827
import { Page404 } from './Page404'
2928
import { PageHead } from './PageHead'
30-
import { PageActions } from './PageActions'
29+
import { PageAside } from './PageAside'
3130
import { Footer } from './Footer'
32-
import { PageSocial } from './PageSocial'
3331
import { NotionPageHeader } from './NotionPageHeader'
3432
import { GitHubShareButton } from './GitHubShareButton'
3533

@@ -180,6 +178,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
180178
const isLiteMode = lite === 'true'
181179

182180
const { resolvedTheme } = useTheme()
181+
const isDarkMode = resolvedTheme === 'dark'
183182

184183
const siteMapPageUrl = React.useMemo(() => {
185184
const params: any = {}
@@ -189,14 +188,30 @@ export const NotionPage: React.FC<types.PageProps> = ({
189188
return mapPageUrl(site, recordMap, searchParams)
190189
}, [site, recordMap, lite])
191190

191+
const keys = Object.keys(recordMap?.block || {})
192+
const block = recordMap?.block?.[keys[0]]?.value
193+
194+
// const isRootPage =
195+
// parsePageId(block?.id) === parsePageId(site?.rootNotionPageId)
196+
const isBlogPost =
197+
block?.type === 'page' && block?.parent_table === 'collection'
198+
const showTableOfContents = !!isBlogPost
199+
const minTableOfContentsItems = 3
200+
201+
const pageAside = React.useMemo(
202+
() => (
203+
<PageAside block={block} recordMap={recordMap} isBlogPost={isBlogPost} />
204+
),
205+
[block, recordMap, isBlogPost]
206+
)
207+
208+
const footer = React.useMemo(() => <Footer />, [])
209+
192210
if (router.isFallback) {
193211
return <Loading />
194212
}
195213

196-
const keys = Object.keys(recordMap?.block || {})
197-
const block = recordMap?.block?.[keys[0]]?.value
198-
199-
if (error || !site || !keys.length || !block) {
214+
if (error || !site || !block) {
200215
return <Page404 site={site} pageId={pageId} error={error} />
201216
}
202217

@@ -221,13 +236,6 @@ export const NotionPage: React.FC<types.PageProps> = ({
221236
const canonicalPageUrl =
222237
!config.isDev && getCanonicalPageUrl(site, recordMap)(pageId)
223238

224-
// const isRootPage =
225-
// parsePageId(block.id) === parsePageId(site.rootNotionPageId)
226-
const isBlogPost =
227-
block.type === 'page' && block.parent_table === 'collection'
228-
const showTableOfContents = !!isBlogPost
229-
const minTableOfContentsItems = 3
230-
231239
const socialImage = mapImageUrl(
232240
getPageProperty<string>('Social Image', block, recordMap) ||
233241
(block as PageBlock).format?.page_cover ||
@@ -239,18 +247,6 @@ export const NotionPage: React.FC<types.PageProps> = ({
239247
getPageProperty<string>('Description', block, recordMap) ||
240248
config.description
241249

242-
let pageAside: React.ReactNode = null
243-
244-
// only display comments and page actions on blog post pages
245-
if (isBlogPost) {
246-
const tweet = getPageTweet(block, recordMap)
247-
if (tweet) {
248-
pageAside = <PageActions tweet={tweet} />
249-
}
250-
} else {
251-
pageAside = <PageSocial />
252-
}
253-
254250
return (
255251
<>
256252
<PageHead
@@ -263,6 +259,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
263259
/>
264260

265261
{isLiteMode && <BodyClassName className='notion-lite' />}
262+
{isDarkMode && <BodyClassName className='dark-mode' />}
266263

267264
<NotionRenderer
268265
bodyClassName={cs(
@@ -274,7 +271,6 @@ export const NotionPage: React.FC<types.PageProps> = ({
274271
rootPageId={site.rootNotionPageId}
275272
rootDomain={site.domain}
276273
fullPage={!isLiteMode}
277-
darkMode={resolvedTheme === 'dark'}
278274
previewImages={!!recordMap.preview_images}
279275
showCollectionViewDropdown={false}
280276
showTableOfContents={showTableOfContents}
@@ -286,7 +282,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
286282
mapImageUrl={mapImageUrl}
287283
searchNotion={config.isSearchEnabled ? searchNotion : null}
288284
pageAside={pageAside}
289-
footer={<Footer />}
285+
footer={footer}
290286
/>
291287

292288
<GitHubShareButton />

components/NotionPageHeader.tsx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@ import cs from 'classnames'
33
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'
6-
76
import { Header, Breadcrumbs, Search, useNotionContext } from 'react-notion-x'
7+
import * as types from 'notion-types'
88

9-
import * as types from 'lib/types'
109
import { navigationStyle, navigationLinks, isSearchEnabled } from 'lib/config'
1110

1211
import styles from './styles.module.css'
1312

14-
export const NotionPageHeader: React.FC<{
15-
block: types.CollectionViewPageBlock | types.PageBlock
16-
}> = ({ block }) => {
13+
const ToggleThemeButton = () => {
1714
const [hasMounted, setHasMounted] = React.useState(false)
1815
const { resolvedTheme, setTheme } = useTheme()
19-
const { components, mapPageUrl } = useNotionContext()
2016

2117
React.useEffect(() => {
2218
setHasMounted(true)
@@ -26,6 +22,25 @@ export const NotionPageHeader: React.FC<{
2622
setTheme(resolvedTheme === 'light' ? 'dark' : 'light')
2723
}, [resolvedTheme, setTheme])
2824

25+
return (
26+
<div
27+
className={cs('breadcrumb', 'button', !hasMounted && styles.hidden)}
28+
onClick={onToggleTheme}
29+
>
30+
{hasMounted && resolvedTheme === 'dark' ? (
31+
<IoMoonSharp />
32+
) : (
33+
<IoSunnyOutline />
34+
)}
35+
</div>
36+
)
37+
}
38+
39+
export const NotionPageHeader: React.FC<{
40+
block: types.CollectionViewPageBlock | types.PageBlock
41+
}> = ({ block }) => {
42+
const { components, mapPageUrl } = useNotionContext()
43+
2944
if (navigationStyle === 'default') {
3045
return <Header block={block} />
3146
}
@@ -66,13 +81,7 @@ export const NotionPageHeader: React.FC<{
6681
})
6782
.filter(Boolean)}
6883

69-
<div className={cs('breadcrumb', 'button')} onClick={onToggleTheme}>
70-
{hasMounted && resolvedTheme === 'dark' ? (
71-
<IoMoonSharp />
72-
) : (
73-
<IoSunnyOutline />
74-
)}
75-
</div>
84+
<ToggleThemeButton />
7685

7786
{isSearchEnabled && <Search block={block} title={null} />}
7887
</div>

components/PageAside.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as React from 'react'
2+
import { Block, ExtendedRecordMap } from 'notion-types'
3+
4+
import { PageActions } from './PageActions'
5+
import { PageSocial } from './PageSocial'
6+
7+
import { getPageTweet } from 'lib/get-page-tweet'
8+
9+
export const PageAside: React.FC<{
10+
block: Block
11+
recordMap: ExtendedRecordMap
12+
isBlogPost: boolean
13+
}> = ({ block, recordMap, isBlogPost }) => {
14+
if (!block) {
15+
return null
16+
}
17+
18+
// only display comments and page actions on blog post pages
19+
if (isBlogPost) {
20+
const tweet = getPageTweet(block, recordMap)
21+
if (!tweet) {
22+
return null
23+
}
24+
25+
return <PageActions tweet={tweet} />
26+
}
27+
28+
return <PageSocial />
29+
}

components/styles.module.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,7 @@
201201
animation: octocat-wave 560ms ease-in-out;
202202
}
203203
}
204+
205+
.hidden {
206+
visibility: hidden;
207+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"react": "^17.0.2",
4949
"react-body-classname": "^1.3.1",
5050
"react-dom": "^17.0.2",
51-
"react-notion-x": "^6.12.2",
51+
"react-notion-x": "^6.12.3",
5252
"react-tweet-embed": "^2.0.0",
5353
"react-use": "^17.3.2"
5454
},

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2973,10 +2973,10 @@ react-modal@^3.14.3:
29732973
react-lifecycles-compat "^3.0.0"
29742974
warning "^4.0.3"
29752975

2976-
react-notion-x@^6.12.2:
2977-
version "6.12.2"
2978-
resolved "https://registry.yarnpkg.com/react-notion-x/-/react-notion-x-6.12.2.tgz#7850da7ddcbdff9114dcfa424a1620deb6476a00"
2979-
integrity sha512-JrUPQrkSshYXkYh9pcDq17bV+shsh2KKznDRIuqa1ZPAVEkpLHhVrN+xghU/7a/jQNAWlXlIfUAahtkMM6HshQ==
2976+
react-notion-x@^6.12.3:
2977+
version "6.12.3"
2978+
resolved "https://registry.yarnpkg.com/react-notion-x/-/react-notion-x-6.12.3.tgz#273c9344185c454ae3fefc89a3bb1dcb8d49eff9"
2979+
integrity sha512-NTTxaQ2OL9DwEihxlMC3SBa6+VipysAe5zVMEzrN2xnEI7ug0K2FPLvGR/uXnvbFQkLZRqIATQkhtqAiFvyNrA==
29802980
dependencies:
29812981
"@fisch0920/medium-zoom" "^1.0.7"
29822982
"@matejmazur/react-katex" "^3.1.3"

0 commit comments

Comments
 (0)