Skip to content

Suggestion: Use 'next-themes' for theme switching #430

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

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion components/Faucet.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
removeQueryParams,
addQueryParams
} from '../utils'
import { useTheme } from './Layout/ThemeContext'
import { useTheme } from 'next-themes'
import { useRouter } from 'next/router'

import AddressInput from './UI/AddressInput'
Expand Down
2 changes: 1 addition & 1 deletion components/Home/PriceChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useState, useEffect } from 'react'
import { useTranslation } from 'next-i18next'
import dynamic from 'next/dynamic'

import { useTheme } from '../Layout/ThemeContext'
import { useTheme } from 'next-themes'
import { nativeCurrency } from '../../utils'

const Chart = dynamic(() => import('react-apexcharts'), { ssr: false })
Expand Down
2 changes: 1 addition & 1 deletion components/Layout/Ads.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useWidth, xahauNetwork } from '../../utils'
import { useTheme } from '../Layout/ThemeContext'
import { useTheme } from 'next-themes'
import { useEffect, useState } from 'react'
import { brandsBlock } from '../../styles/components/ads.module.scss'

Expand Down
15 changes: 7 additions & 8 deletions components/Layout/Header/Switch.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { useState, useEffect } from 'react'
import { useTheme } from '../ThemeContext'
import { useTheme } from 'next-themes'
import Image from 'next/image'

export default function Switch() {
const [rendered, setRendered] = useState(false)
const { theme, toggleTheme } = useTheme()
const [mounted, setMounted] = useState(false)
const { theme, setTheme } = useTheme()

useEffect(() => {
setRendered(true)
}, [])
// When mounted on client, now we can show the UI
useEffect(() => setMounted(true), [])

if (!rendered) return null
if (!mounted) return null

const switchOnClick = () => {
toggleTheme()
setTheme(theme === 'dark' ? 'light' : 'dark')
}

return (
Expand Down
10 changes: 8 additions & 2 deletions components/Layout/LogoSmall.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import Logo from '../../public/images/logo-small.svg'
import { server, xahauNetwork } from '../../utils'
import { useTheme } from './ThemeContext'
import { useTheme } from 'next-themes'
import { useState, useEffect } from 'react'

export default function LogoSmall({ width, height, color, dependOnTheme }) {
const { theme } = useTheme()
const [mounted, setMounted] = useState(false)

useEffect(() => {
setMounted(true)
}, [])

if (server.includes('bithomp')) {
return (
Expand Down Expand Up @@ -44,7 +50,7 @@ export default function LogoSmall({ width, height, color, dependOnTheme }) {
}

let fill = color || xahauNetwork ? '#ffcc53' : '#4ba8b6'
if (dependOnTheme && theme === 'light' && xahauNetwork) {
if (dependOnTheme && mounted && theme === 'light' && xahauNetwork) {
fill = '#0E233F'
}
return <Logo width={width} height={height} fill={fill} />
Expand Down
22 changes: 0 additions & 22 deletions components/Layout/ThemeContext.js

This file was deleted.

13 changes: 7 additions & 6 deletions components/Receipt.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { useTranslation } from 'next-i18next'
import { useTheme } from 'next-themes'

import { fullDateAndTime } from '../utils/format'

import { useTheme } from './Layout/ThemeContext'

import XahauExplorerLogo from '../public/images/xahauexplorer/longDark.svg'
import XrplExplorerLogo from '../public/images/xrplexplorer/longDark.svg'

import { nativeCurrency, xahauNetwork } from '../utils'

export default function Receipt({ item, details }) {
const { t } = useTranslation()
const { theme } = useTheme()
const { theme, setTheme } = useTheme()

if (!details) {
return
}

const onPrint = () => {
if (theme === 'dark') {
global.window.__setPreferredTheme('light')
window.print()
global.window.__setPreferredTheme('dark')
setTheme('light')
setTimeout(() => {
window.print()
setTheme('dark')
}, 100)
} else {
window.print()
}
Expand Down
4 changes: 2 additions & 2 deletions components/SimpleChart.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useTranslation } from 'next-i18next'
import dynamic from 'next/dynamic'
import { useTheme } from 'next-themes'

import { useTheme } from './Layout/ThemeContext'
import { niceNumber } from '../utils/format'

const Chart = dynamic(() => import('react-apexcharts'), { ssr: false })
Expand All @@ -22,7 +22,7 @@ const locales = {
}
}

export default function PriceChart({ data, combined, currency }) {
export default function SimpleChart({ data, combined, currency }) {
const { i18n } = useTranslation()
const { theme } = useTheme()

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"next-i18next": "^13.2.1",
"next-qrcode": "^2.5.1",
"next-seo": "^5.15.0",
"next-themes": "^0.4.6",
"nprogress": "^0.2.0",
"postcss": "^8.5.3",
"react": "^18.3.1",
Expand Down
4 changes: 2 additions & 2 deletions pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import '../styles/globals.css'
import '../styles/ui.scss'
import '../styles/components/nprogress.css'

import { ThemeProvider } from '../components/Layout/ThemeContext'
import { ThemeProvider } from 'next-themes'
import { fetchCurrentFiatRate } from '../utils/common'

const Header = dynamic(() => import('../components/Layout/Header'), { ssr: true })
Expand Down Expand Up @@ -164,7 +164,7 @@ const MyApp = ({ Component, pageProps }) => {
<meta charSet="utf-8" />
</Head>
<IsSsrMobileContext.Provider value={pageProps.isSsrMobile}>
<ThemeProvider>
<ThemeProvider attribute="class" storageKey="theme" enableSystem={false} defaultTheme="light">
<div
className="body"
data-network={network}
Expand Down
50 changes: 1 addition & 49 deletions pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,55 +31,7 @@ class MyDocument extends Document {
<link rel="apple-touch-icon" href={this.props.logoPath + '/apple-touch-icon.png'} />
<link rel="manifest" href="/manifest.json" />
</Head>
<body className={this.props.cookieTheme} data-networkname={process.env.NEXT_PUBLIC_NETWORK_NAME}>
<script
dangerouslySetInnerHTML={{
__html: `(function () {
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function setTheme(newTheme) {
document.body.className = newTheme;
window.__theme = newTheme;
window.__onThemeChange(newTheme);
}
window.__onThemeChange = function () {};
window.__setPreferredTheme = function (newTheme) {
setTheme(newTheme);
try {
let domain = window.location.hostname;
document.cookie = "theme=" + JSON.stringify(window.__theme) + ";path=/;domain=" + encodeURI(domain) + ";max-age=31536000";
} catch (err) {}
};
let preferredTheme;
try {
preferredTheme = JSON.parse(getCookie("theme"));
} catch (err) {}
const darkQuery = window.matchMedia("(prefers-color-scheme: dark)");
if (!preferredTheme) {
darkQuery.addListener(function (event) {
window.__setPreferredTheme(event.matches ? "dark" : "light");
});
preferredTheme = darkQuery.matches ? "dark" : "light";
window.__setPreferredTheme(preferredTheme);
}
setTheme(preferredTheme);
})();
`
}}
/>
<body data-networkname={process.env.NEXT_PUBLIC_NETWORK_NAME}>
<Main />
<NextScript />
</body>
Expand Down
13 changes: 7 additions & 6 deletions pages/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { useTranslation } from 'next-i18next'
import { useEffect, useState } from 'react'
import { Turnstile } from '@marsidev/react-turnstile'
import { useTheme } from '../../components/Layout/ThemeContext'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { useState, useEffect } from 'react'
import { useTheme } from 'next-themes'
import Link from 'next/link'
import Mailto from 'react-protected-mailto'
import { Turnstile } from '@marsidev/react-turnstile'

import SEO from '../../components/SEO'
import AdminTabs from '../../components/Tabs/AdminTabs'
import CheckBox from '../../components/UI/CheckBox'

import { isEmailValid, turnstileSupportedLanguages, useWidth } from '../../utils'
import { getIsSsrMobile } from '../../utils/mobile'
import AdminTabs from '../../components/Tabs/AdminTabs'

import { isEmailValid, turnstileSupportedLanguages, useWidth } from '../../utils'
import { axiosAdmin } from '../../utils/axios'

export const getServerSideProps = async (context) => {
Expand Down
2 changes: 1 addition & 1 deletion pages/amendment/[amendmentName].js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Image from 'next/image'
import SEO from '../../components/SEO'
import CopyButton from '../../components/UI/CopyButton'
import ReactCountryFlag from 'react-country-flag'
import { useTheme } from '../../components/Layout/ThemeContext'
import { useTheme } from 'next-themes'
import VerifiedIcon from '../../public/images/verified.svg'

export async function getServerSideProps(context) {
Expand Down
2 changes: 1 addition & 1 deletion pages/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useTranslation, Trans } from 'next-i18next'
import { useState, useEffect, memo } from 'react'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import ReactCountryFlag from 'react-country-flag'
import { useTheme } from '../components/Layout/ThemeContext'
import { useTheme } from 'next-themes'

import SEO from '../components/SEO'
import CheckBox from '../components/UI/CheckBox'
Expand Down
11 changes: 6 additions & 5 deletions styles/variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ body {
--card-bg: #fff;
--card-text: #000;
}
body.dark {
.dark body {
--background-header: #031B21;
--background-block: #031B21;
--background-main: #090909;
Expand Down Expand Up @@ -63,12 +63,13 @@ body {
--card-text: #fff;
}

body[data-networkname*="xahau"] {
body[data-networkname*="xahau"] {
--background-header: #0E233F;
--background-block: #0E233F;
--background-footer: #061322;
&.dark {
}

.dark body[data-networkname*="xahau"] {
--background-header: #061322;
--background-block: #061322;
}
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6933,6 +6933,11 @@ next-seo@^5.15.0:
resolved "https://registry.yarnpkg.com/next-seo/-/next-seo-5.15.0.tgz#b1a90508599774982909ea44803323c6fb7b50f4"
integrity sha512-LGbcY91yDKGMb7YI+28n3g+RuChUkt6pXNpa8FkfKkEmNiJkeRDEXTnnjVtwT9FmMhG6NH8qwHTelGrlYm9rgg==

next-themes@^0.4.6:
version "0.4.6"
resolved "https://registry.yarnpkg.com/next-themes/-/next-themes-0.4.6.tgz#8d7e92d03b8fea6582892a50a928c9b23502e8b6"
integrity sha512-pZvgD5L0IEvX5/9GWyHMf3m8BKiVQwsCMHfoFosXtXBMnaS0ZnIJ9ST4b4NqLVKDEm8QBxoNNGNaBv2JNF6XNA==

[email protected]:
version "14.2.29"
resolved "https://registry.yarnpkg.com/next/-/next-14.2.29.tgz#f67610f8368ef863065b3b791e23b9198f0df615"
Expand Down