Skip to content

Commit

Permalink
refactor(dark-model): Clean up code (remix-run#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
EarthlingDavey authored May 31, 2024
1 parent e973dc2 commit 1dc167b
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions dark-mode/app/utils/theme-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,34 @@ const clientThemeCode = `
// a theme, then I'll know what you want in the future and you'll not see this
// script anymore.
;(() => {
const theme = window.matchMedia(${JSON.stringify(prefersDarkMQ)}).matches
? 'dark'
: 'light';
const theme = window.matchMedia('${prefersDarkMQ}').matches
? '${Theme.DARK}'
: '${Theme.LIGHT}';
const cl = document.documentElement.classList;
const themeAlreadyApplied = cl.contains('light') || cl.contains('dark');
if (themeAlreadyApplied) {
if (
cl.contains('${Theme.LIGHT}') || cl.contains('${Theme.DARK}')
) {
// The theme is already applied...
// this script shouldn't exist if the theme is already applied!
console.warn(
"Hi there, could you let me know you're seeing this message? Thanks!",
);
console.warn("See theme-provider.tsx>clientThemeCode>cl.contains");
// Hi there, could you let me know you're seeing this console.warn? Thanks!
} else {
cl.add(theme);
}
const meta = document.querySelector('meta[name=color-scheme]');
if (meta) {
if (theme === 'dark') {
meta.content = 'dark light';
} else if (theme === 'light') {
meta.content = 'light dark';
}
meta.content = theme === '${Theme.DARK}' ? 'dark light' : 'light dark';
} else {
console.warn(
"Hey, could you let me know you're seeing this message? Thanks!",
);
console.warn("See theme-provider.tsx>clientThemeCode>meta");
// Hey, could you let me know you're seeing this console.warn? Thanks!
}
})();
`;
`
// Remove double slash comments & replace excess white space with a single space.
.replace(/((?<=[^:])\/\/.*|\s)+/g, ' ')
.trim();

const themeStylesCode = `
/* default light, but app-preference is "dark" */
Expand Down

0 comments on commit 1dc167b

Please sign in to comment.