Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding optional custom logo to header with darkmode support #325

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions components/NotionPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import { IoSunnyOutline } from '@react-icons/all-files/io5/IoSunnyOutline'
import { IoMoonSharp } from '@react-icons/all-files/io5/IoMoonSharp'
import { Header, Breadcrumbs, Search, useNotionContext } from 'react-notion-x'
import * as types from 'notion-types'
import Image from 'next/image'

import { useDarkMode } from 'lib/use-dark-mode'
import { navigationStyle, navigationLinks, isSearchEnabled } from 'lib/config'
import {
navigationStyle,
navigationLinks,
isSearchEnabled,
customHeaderLogo,
customHeaderLogoDark,
rootNotionPageId
} from 'lib/config'

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

Expand All @@ -32,6 +40,48 @@ const ToggleThemeButton = () => {
)
}

export const HeaderLogo: React.FC<{
block: types.CollectionViewPageBlock | types.PageBlock
}> = ({ block }) => {
const { components, mapPageUrl } = useNotionContext()

const { isDarkMode } = useDarkMode()

const LinkWrapper = (props: {
currentPage: string
children: JSX.Element
}) => {
if (props.currentPage !== '/') {
return (
<components.PageLink
href={mapPageUrl(rootNotionPageId)}
key={0}
className={cs(styles.navLink, 'breadcrumb', 'button')}
>
{props.children}
</components.PageLink>
)
}
return <div style={{ padding: 12, display: 'flex' }}>{props.children}</div>
}

return (
<LinkWrapper currentPage={mapPageUrl(block.id)}>
<Image
src={
isDarkMode && customHeaderLogoDark
? customHeaderLogoDark
: customHeaderLogo
}
width='100%'
height='100%'
objectFit='contain'
alt='logo'
/>
</LinkWrapper>
)
}

export const NotionPageHeader: React.FC<{
block: types.CollectionViewPageBlock | types.PageBlock
}> = ({ block }) => {
Expand All @@ -44,7 +94,11 @@ export const NotionPageHeader: React.FC<{
return (
<header className='notion-header'>
<div className='notion-nav-header'>
<Breadcrumbs block={block} rootOnly={true} />
{customHeaderLogo ? (
<HeaderLogo block={block} />
) : (
<Breadcrumbs block={block} rootOnly={true} />
)}

<div className='notion-nav-header-rhs breadcrumbs'>
{navigationLinks
Expand Down
10 changes: 10 additions & 0 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ export const linkedin: string | null = getSiteConfig('linkedin', null)
export const newsletter: string | null = getSiteConfig('newsletter', null)
export const zhihu: string | null = getSiteConfig('zhihu', null)

// default logo values
export const customHeaderLogo: string | null = getSiteConfig(
'customHeaderLogo',
null
)
export const customHeaderLogoDark: string | null = getSiteConfig(
'customHeaderLogoDark',
null
)

// default notion values for site-wide consistency (optional; may be overridden on a per-page basis)
export const defaultPageIcon: string | null = getSiteConfig(
'defaultPageIcon',
Expand Down
3 changes: 3 additions & 0 deletions lib/site-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export interface SiteConfig {
youtube?: string
zhihu?: string

customHeaderLogo?: string | null
customHeaderLogoDark?: string | null

defaultPageIcon?: string | null
defaultPageCover?: string | null
defaultPageCoverPosition?: number | null
Expand Down
4 changes: 4 additions & 0 deletions site.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export default siteConfig({
// newsletter: '#', // optional newsletter URL
// youtube: '#', // optional youtube channel name or `channel/UCGbXXXXXXXXXXXXXXXXXXXXXX`

// Custom logo - this will replace the breadcrumbs - Feature requires navigationStyle: 'custom'
// customHeaderLogo: 'https://transitivebullsh.it/logo.png',
// customHeaderLogoDark: 'https://transitivebullsh.it/logo-dark.png', // optional

// default notion icon and cover images for site-wide consistency (optional)
// page-specific values will override these site-wide defaults
defaultPageIcon: null,
Expand Down