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

[DOC][UI]: Fix issues on light/dark theme #3380

Open
wants to merge 2 commits into
base: master
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
16 changes: 16 additions & 0 deletions docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"gh-pages": "^5.0.0",
"next": "12.2.1",
"next-plausible": "^3.2.0",
"next-themes": "^0.4.3",
"postcss-focus-visible": "^6.0.4",
"postcss-import": "^14.1.0",
"prism-react-renderer": "^1.3.5",
Expand Down
6 changes: 5 additions & 1 deletion docs/src/components/Fence.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Fragment } from 'react'
import Highlight, { defaultProps } from 'prism-react-renderer'
import { useTheme } from 'next-themes'
import lightTheme from 'prism-react-renderer/themes/github'


export function Fence({ children, language }) {
const { resolvedTheme } = useTheme()
return (
<Highlight
{...defaultProps}
code={children.trimEnd()}
language={language}
theme={undefined}
theme={resolvedTheme !== 'dark' ? lightTheme: undefined }
>
{({ className, style, tokens, getTokenProps }) => (
<pre className={className} style={style}>
Expand Down
35 changes: 20 additions & 15 deletions docs/src/components/ThemeSelector.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from 'react'
import { useTheme } from 'next-themes'
import { Listbox } from '@headlessui/react'
import clsx from 'clsx'

Expand Down Expand Up @@ -53,31 +54,35 @@ function SystemIcon(props) {
}

export function ThemeSelector(props) {
let [selectedTheme, setSelectedTheme] = useState()

let { theme, setTheme } = useTheme()
const [mounted, setMounted] = useState(false)
useEffect(() => {
if (selectedTheme) {
document.documentElement.setAttribute('data-theme', selectedTheme.value)
} else {
setSelectedTheme(
themes.find(
(theme) =>
theme.value === document.documentElement.getAttribute('data-theme')
)
)
if (theme) {
document.documentElement.setAttribute('data-theme', theme)
}
}, [selectedTheme])
}, [theme])

// useEffect only runs on the client, so now we can safely show the UI
useEffect(() => {
setMounted(true)
}, [])

if (!mounted) {
return null
}

return (
<Listbox
as="div"
value={selectedTheme}
onChange={setSelectedTheme}
value={themes.find((t) => t.value === theme) }
onChange={(theme) => {
setTheme(theme.value)
}}
{...props}
>
<Listbox.Label className="sr-only">Theme</Listbox.Label>
<Listbox.Button className="flex h-6 w-6 items-center justify-center rounded-lg shadow-md shadow-black/5 ring-1 ring-black/5 dark:bg-slate-700 dark:ring-inset dark:ring-white/5">
<span className="sr-only">{selectedTheme?.name}</span>
<span className="sr-only">{theme}</span>
<LightIcon className="hidden h-4 w-4 fill-sky-400 [[data-theme=light]_&]:block" />
<DarkIcon className="hidden h-4 w-4 fill-sky-400 [[data-theme=dark]_&]:block" />
<LightIcon className="hidden h-4 w-4 fill-slate-400 [:not(.dark)[data-theme=system]_&]:block" />
Expand Down
6 changes: 3 additions & 3 deletions docs/src/pages/_app.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Head from 'next/head'
import { slugifyWithCounter } from '@sindresorhus/slugify'
import PlausibleProvider from 'next-plausible'

import { ThemeProvider } from 'next-themes'
import Prism from 'prism-react-renderer/prism'
;(typeof global !== 'undefined' ? global : window).Prism = Prism

Expand Down Expand Up @@ -155,7 +155,7 @@ export default function App({ Component, pageProps }) {
: []

return (
<>
<ThemeProvider attribute="class" defaultTheme="system">
<PlausibleProvider domain="anchor-lang.com" trackOutboundLinks={true}>
<Head>
<title>{pageTitle}</title>
Expand Down Expand Up @@ -189,6 +189,6 @@ export default function App({ Component, pageProps }) {
<Component {...pageProps} />
</Layout>
</PlausibleProvider>
</>
</ThemeProvider>
)
}
52 changes: 0 additions & 52 deletions docs/src/pages/_document.jsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,9 @@
import { Head, Html, Main, NextScript } from 'next/document'

const themeScript = `
let mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')

function updateTheme(savedTheme) {
let theme = 'system'
try {
if (!savedTheme) {
savedTheme = window.localStorage.theme
}
if (savedTheme === 'dark') {
theme = 'dark'
document.documentElement.classList.add('dark')
} else if (savedTheme === 'light') {
theme = 'light'
document.documentElement.classList.remove('dark')
} else if (mediaQuery.matches) {
document.documentElement.classList.add('dark')
} else {
document.documentElement.classList.remove('dark')
}
} catch {
theme = 'light'
document.documentElement.classList.remove('dark')
}
return theme
}

function updateThemeWithoutTransitions(savedTheme) {
updateTheme(savedTheme)
document.documentElement.classList.add('[&_*]:!transition-none')
window.setTimeout(() => {
document.documentElement.classList.remove('[&_*]:!transition-none')
}, 0)
}

document.documentElement.setAttribute('data-theme', updateTheme())

new MutationObserver(([{ oldValue }]) => {
let newValue = document.documentElement.getAttribute('data-theme')
if (newValue !== oldValue) {
try {
window.localStorage.setItem('theme', newValue)
} catch {}
updateThemeWithoutTransitions(newValue)
}
}).observe(document.documentElement, { attributeFilter: ['data-theme'], attributeOldValue: true })

mediaQuery.addEventListener('change', updateThemeWithoutTransitions)
window.addEventListener('storage', updateThemeWithoutTransitions)
`

export default function Document() {
return (
<Html className="antialiased [font-feature-settings:'ss01']" lang="en">
<Head>
<script dangerouslySetInnerHTML={{ __html: themeScript }} />
</Head>
<body className="bg-white dark:bg-slate-900">
<Main />
Expand Down