Skip to content

Commit

Permalink
fix: use next-themes for the legacy website (nodejs#6004)
Browse files Browse the repository at this point in the history
  • Loading branch information
ovflowd authored Oct 15, 2023
1 parent 407145e commit 57401ec
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 48 deletions.
4 changes: 4 additions & 0 deletions components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import classNames from 'classnames';
import Image from 'next/image';
import { useTheme } from 'next-themes';
import { useIntl } from 'react-intl';

import { useLocale } from '@/hooks/useLocale';
Expand All @@ -13,6 +14,7 @@ const Header = () => {
const { navigationItems } = useNavigation();
const { formatMessage } = useIntl();
const { asPath, basePath } = useRouter();
const { theme, setTheme } = useTheme();

const getLinkClassName = (href: string) =>
classNames({ active: isCurrentLocaleRoute(href, href !== '/') });
Expand Down Expand Up @@ -57,6 +59,7 @@ const Header = () => {
type="button"
title={toggleDarkMode}
aria-label={toggleDarkMode}
onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}
>
<Image
priority
Expand All @@ -66,6 +69,7 @@ const Header = () => {
src={`${basePath}/static/images/light-mode.svg`}
alt="Dark Theme Switcher"
/>

<Image
priority
width="28"
Expand Down
5 changes: 5 additions & 0 deletions next.constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,8 @@ export const EXTERNAL_LINKS_SITEMAP = [
'https://trademark-list.openjsf.org/',
'https://www.linuxfoundation.org/cookies',
];

/**
* The `localStorage` key to store the theme choice of `next-themes`
*/
export const THEME_LOCAL_STORAGE_KEY = 'theme';
5 changes: 3 additions & 2 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BlogDataProvider } from '@/providers/blogDataProvider';
import { LocaleProvider } from '@/providers/localeProvider';
import { NodeReleasesProvider } from '@/providers/nodeReleasesProvider';
import { SiteProvider } from '@/providers/siteProvider';
import { ThemeProvider } from '@/providers/themeProvider';

import '@/styles/old/index.css';

Expand All @@ -17,7 +18,7 @@ const sourceSans = Source_Sans_3({
});

const App = ({ Component, pageProps }: AppProps) => (
<>
<ThemeProvider>
<LocaleProvider>
<SiteProvider>
<NodeReleasesProvider>
Expand All @@ -37,7 +38,7 @@ const App = ({ Component, pageProps }: AppProps) => (
}
`}
</style>
</>
</ThemeProvider>
);

export default App;
15 changes: 10 additions & 5 deletions providers/themeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { ThemeProvider as NextThemeProvider } from 'next-themes';
import type { FC, PropsWithChildren } from 'react';

import { THEME_LOCAL_STORAGE_KEY } from '@/next.constants.mjs';

export const ThemeProvider: FC<PropsWithChildren> = ({ children }) => (
<>
<NextThemeProvider enableSystem={true} enableColorScheme={true}>
{children}
</NextThemeProvider>
</>
<NextThemeProvider
attribute="data-theme"
storageKey={THEME_LOCAL_STORAGE_KEY}
enableSystem={true}
enableColorScheme={true}
>
{children}
</NextThemeProvider>
);
40 changes: 0 additions & 40 deletions public/static/js/legacyMain.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
const themeAttr = 'data-theme';

const setTheme = theme => {
document.querySelector('html').setAttribute(themeAttr, theme);
window.localStorage.setItem('theme', theme);
};

const preferredColorScheme = window.matchMedia('(prefers-color-scheme: dark)');

const getTheme = () => window.localStorage.getItem('theme');

const listenLanguagePickerButton = () => {
const langPickerTogglerElement = document.querySelector(
'.lang-picker-toggler'
Expand All @@ -28,25 +17,6 @@ const listenLanguagePickerButton = () => {
langPickerTogglerElement.addEventListener('click', toggleFunction);
};

const watchThemeChanges = () =>
preferredColorScheme.addEventListener(
'change',
event => getTheme() || setTheme(event.matches ? 'dark' : 'light')
);

const listenThemeToggleButton = () => {
const darkThemeSwitcherElement = document.querySelector(
'.dark-theme-switcher'
);

darkThemeSwitcherElement.addEventListener('click', () => {
const currentTheme =
getTheme() || (preferredColorScheme.matches ? 'dark' : 'light');

setTheme(currentTheme === 'light' ? 'dark' : 'light');
});
};

const listenScrollToTopButton = () => {
const scrollToTop = document.querySelector('#scroll-to-top');

Expand All @@ -67,19 +37,9 @@ const listenScrollToTopButton = () => {
});
};

const setCurrentTheme = () =>
setTheme(getTheme() || (preferredColorScheme.matches ? 'dark' : 'light'));

const startLegacyApp = () => {
setCurrentTheme();

watchThemeChanges();

listenLanguagePickerButton();
listenThemeToggleButton();
listenScrollToTopButton();
};

setCurrentTheme();

window.startLegacyApp = startLegacyApp;
1 change: 0 additions & 1 deletion styles/old/layout/dark-theme.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
html[data-theme='dark'] {
background-color: $dark-black;
color: $white;
color-scheme: dark;

.dark-theme-switcher {
img.light-image {
Expand Down

0 comments on commit 57401ec

Please sign in to comment.