fix and format

This commit is contained in:
ryanchristo 2023-11-03 17:25:05 -07:00
parent f45a2bcd3b
commit 818f984f7b
18 changed files with 268 additions and 254 deletions

View file

@ -1,45 +1,45 @@
'use client'
"use client";
import * as React from 'react'
import { createContext, useEffect, useState } from 'react'
import * as React from "react";
import { createContext, useEffect, useState } from "react";
export const cachedDarkKey = 'chora-web-dark'
export const cachedDarkKey = "chora-web-dark";
const initDarkTheme = () => {
let initDarkTheme = false
let initDarkTheme = false;
if (
typeof localStorage !== 'undefined' &&
typeof localStorage !== "undefined" &&
localStorage.getItem(cachedDarkKey)
) {
initDarkTheme = localStorage.getItem(cachedDarkKey) === 'true'
} else if (typeof window !== 'undefined') {
initDarkTheme = window.matchMedia('(prefers-color-scheme: dark)').matches
initDarkTheme = localStorage.getItem(cachedDarkKey) === "true";
} else if (typeof window !== "undefined") {
initDarkTheme = window.matchMedia("(prefers-color-scheme: dark)").matches;
}
return initDarkTheme
}
return initDarkTheme;
};
const ThemeContext = createContext<any>({})
const ThemeContext = createContext<any>({});
const ThemeContextProvider = (props: any) => {
const [darkTheme, setDarkTheme] = useState<boolean>(initDarkTheme())
const [darkTheme, setDarkTheme] = useState<boolean>(initDarkTheme());
useEffect(() => {
if (typeof window !== 'undefined' && typeof localStorage !== 'undefined') {
const root = window.document.documentElement
if (typeof window !== "undefined" && typeof localStorage !== "undefined") {
const root = window.document.documentElement;
if (darkTheme) {
root.classList.add('dark-theme')
root.style.backgroundColor = '#161616' // --background-secondary (dark-theme)
root.classList.add("dark-theme");
root.style.backgroundColor = "#161616"; // --background-secondary (dark-theme)
} else {
root.classList.remove('dark-theme')
root.style.backgroundColor = '#FAFAFA' // --background-secondary (light-theme)
root.classList.remove("dark-theme");
root.style.backgroundColor = "#FAFAFA"; // --background-secondary (light-theme)
}
localStorage.setItem(cachedDarkKey, darkTheme ? 'true' : 'false')
localStorage.setItem(cachedDarkKey, darkTheme ? "true" : "false");
}
}, [darkTheme])
}, [darkTheme]);
return (
<ThemeContext.Provider
@ -50,7 +50,7 @@ const ThemeContextProvider = (props: any) => {
>
{props.children}
</ThemeContext.Provider>
)
}
);
};
export { ThemeContext, ThemeContextProvider }
export { ThemeContext, ThemeContextProvider };