Skip to content

Commit

Permalink
Refactor code for enabling style sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
dbelokon committed Jan 30, 2022
1 parent 446413a commit 8234a1b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
14 changes: 14 additions & 0 deletions src/web/src/hooks/use-preferred-theme.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect } from 'react';
import { useLocalStorage, useMedia } from 'react-use';

/**
Expand All @@ -10,6 +11,19 @@ export default function usePreferredTheme() {
'preference:theme',
isDarkThemePreferred ? 'dark' : 'light'
);
useEffect(() => {
const lightStyleSheet = (document.querySelector('#light-stylesheet') as HTMLStyleElement).sheet;

if (lightStyleSheet !== null) {
lightStyleSheet.disabled = preferredTheme === 'dark';
}

const darkStyleSheet = (document.querySelector('#dark-stylesheet') as HTMLStyleElement).sheet;

if (darkStyleSheet !== null) {
darkStyleSheet.disabled = preferredTheme === 'light';
}
}, [preferredTheme]);

return [preferredTheme, setPreferredTheme] as const;
}
18 changes: 1 addition & 17 deletions src/web/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,11 @@ const App = ({ Component, pageProps }: AppProps) => {
if (jssStyles) {
jssStyles.parentElement?.removeChild(jssStyles);
}

// Enable the stylesheet for the syntax highlighting depending on the theme
if (preferredTheme === 'dark') {
document.querySelector('#light-stylesheet')?.setAttribute('disabled', 'disabled');
} else {
document.querySelector('#dark-stylesheet')?.setAttribute('disabled', 'disabled');
}
});

// Switch the active theme, and also store it for next load
// We also disable the current stylesheet and enable the other one.
const toggleTheme = () => {
if (preferredTheme === 'dark') {
document.querySelector('#light-stylesheet')?.removeAttribute('disabled');
document.querySelector('#dark-stylesheet')?.setAttribute('disabled', 'disabled');
setPreferredTheme('light');
} else {
document.querySelector('#dark-stylesheet')?.removeAttribute('disabled');
document.querySelector('#light-stylesheet')?.setAttribute('disabled', 'disabled');
setPreferredTheme('dark');
}
setPreferredTheme(preferredTheme === 'dark' ? 'light' : 'dark');
};

return (
Expand Down
4 changes: 2 additions & 2 deletions src/web/src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class MyDocument extends Document {
<meta name="twitter:image" content={image} />
<meta name="twitter:image:alt" content={imageAlt} />

<link rel="stylesheet" href="/styles/github.css" id="light-stylesheet" />
<link rel="stylesheet" href="/styles/github-dark.css" id="dark-stylesheet" />
<link rel="stylesheet" href="/styles/github.min.css" id="light-stylesheet" />
<link rel="stylesheet" href="/styles/github-dark.min.css" id="dark-stylesheet" />
</Head>
<body>
<Main />
Expand Down

0 comments on commit 8234a1b

Please sign in to comment.