-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: set app dark mode instead of renderer (#130)
* fix: set app dark mode instead of renderer Signed-off-by: Innei <[email protected]> * chore: remove jotai-dark Signed-off-by: Innei <[email protected]> --------- Signed-off-by: Innei <[email protected]>
- Loading branch information
Showing
11 changed files
with
86 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { useDark } from "@renderer/hooks/common" | ||
|
||
export const useShikiDefaultTheme = () => { | ||
const { isDark } = useDark() | ||
const isDark = useDark() | ||
|
||
return isDark ? "github-dark" : "github-light" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
import { useDark } from "@renderer/hooks/common" | ||
import { Toaster as Sonner } from "sonner" | ||
|
||
type ToasterProps = React.ComponentProps<typeof Sonner> | ||
|
||
const Toaster = ({ ...props }: ToasterProps) => { | ||
const { theme } = useDark() | ||
|
||
return ( | ||
<Sonner | ||
theme={theme as ToasterProps["theme"]} | ||
className="toaster group" | ||
toastOptions={{ | ||
classNames: { | ||
toast: "group toast group-[.toaster]:bg-theme-background group-[.toaster]:text-theme-foreground group-[.toaster]:border-border group-[.toaster]:shadow-lg", | ||
description: "group-[.toast]:text-muted-foreground", | ||
actionButton: "group-[.toast]:bg-primary group-[.toast]:text-primary-foreground", | ||
cancelButton: "group-[.toast]:bg-muted group-[.toast]:text-muted-foreground", | ||
}, | ||
}} | ||
{...props} | ||
/> | ||
) | ||
} | ||
const Toaster = ({ ...props }: ToasterProps) => ( | ||
<Sonner | ||
theme="system" | ||
className="toaster group" | ||
toastOptions={{ | ||
classNames: { | ||
toast: | ||
"group toast group-[.toaster]:bg-theme-background group-[.toaster]:text-theme-foreground group-[.toaster]:border-border group-[.toaster]:shadow-lg", | ||
description: "group-[.toast]:text-muted-foreground", | ||
actionButton: | ||
"group-[.toast]:bg-primary group-[.toast]:text-primary-foreground", | ||
cancelButton: | ||
"group-[.toast]:bg-muted group-[.toast]:text-muted-foreground", | ||
}, | ||
}} | ||
{...props} | ||
/> | ||
) | ||
|
||
export { Toaster } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,50 @@ | ||
import { useAtom } from "jotai" | ||
import { atomDark } from "jotai-dark" | ||
|
||
const isDarkAtom = atomDark({ | ||
storageKey: "theme", | ||
disableTransition: true, | ||
mode: "data-theme", | ||
}) | ||
import { jotaiStore } from "@renderer/lib/jotai" | ||
import { atom, useAtomValue } from "jotai" | ||
import { useLayoutEffect } from "react" | ||
import { useMediaQuery } from "usehooks-ts" | ||
|
||
const darkAtom = atom(false) | ||
export function useDark() { | ||
const [isDark, setIsDark] = useAtom(isDarkAtom) | ||
return { | ||
isDark, | ||
toggleDark: setIsDark as () => void, | ||
theme: (isDark ? "dark" : "light") as "dark" | "light", | ||
return useAtomValue(darkAtom) | ||
} | ||
|
||
export const useSyncDark = () => { | ||
const isDark = useMediaQuery("(prefers-color-scheme: dark)") | ||
|
||
useLayoutEffect(() => { | ||
jotaiStore.set(darkAtom, isDark) | ||
|
||
document.documentElement.dataset.theme = isDark ? "dark" : "light" | ||
disableTransition([ | ||
"[role=switch]>*", | ||
]) | ||
}, [isDark]) | ||
} | ||
|
||
function disableTransition(disableTransitionExclude: string[] = []) { | ||
const css = document.createElement("style") | ||
css.append( | ||
document.createTextNode( | ||
` | ||
*${disableTransitionExclude.map((s) => `:not(${s})`).join("")} { | ||
-webkit-transition: none !important; | ||
-moz-transition: none !important; | ||
-o-transition: none !important; | ||
-ms-transition: none !important; | ||
transition: none !important; | ||
} | ||
`, | ||
), | ||
) | ||
document.head.append(css) | ||
|
||
return () => { | ||
// Force restyle | ||
(() => window.getComputedStyle(document.body))() | ||
|
||
// Wait for next tick before removing | ||
setTimeout(() => { | ||
css.remove() | ||
}, 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 6 additions & 2 deletions
8
...r/src/providers/ui-setting-Initialize.tsx → ...enderer/src/providers/ui-setting-sync.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters