forked from keephq/keep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): dark mode detection (keephq#2739)
- Loading branch information
Showing
10 changed files
with
212 additions
and
93 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
This file was deleted.
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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const LOCALSTORAGE_THEME_KEY = "theme"; |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { LOCALSTORAGE_THEME_KEY } from "@/shared/constants"; | ||
import { | ||
ComputerDesktopIcon, | ||
MoonIcon, | ||
SunIcon, | ||
} from "@heroicons/react/20/solid"; | ||
import { useLocalStorage } from "utils/hooks/useLocalStorage"; | ||
import { DropdownMenu } from "@/components/ui/DropdownMenu/DropdownMenu"; | ||
import clsx from "clsx"; | ||
|
||
const THEMES = { | ||
light: { id: "light", icon: SunIcon, title: "Light" }, | ||
dark: { id: "dark", icon: MoonIcon, title: "Dark" }, | ||
system: { id: "system", icon: ComputerDesktopIcon, title: "System" }, | ||
}; | ||
|
||
export function ThemeControl({ className }: { className?: string }) { | ||
const [theme, setTheme] = useLocalStorage<string | null>( | ||
LOCALSTORAGE_THEME_KEY, | ||
null | ||
); | ||
|
||
const updateTheme = (theme: string) => { | ||
setTheme(theme === "system" ? null : theme); | ||
if (theme !== "system") { | ||
document.documentElement.classList[theme === "dark" ? "add" : "remove"]( | ||
"workaround-dark" | ||
); | ||
// If system theme is selected, <WatchUpdateTheme /> will handle the rest | ||
} | ||
}; | ||
|
||
const value = theme === null ? "system" : theme; | ||
|
||
return ( | ||
<DropdownMenu.Menu | ||
icon={() => ( | ||
<> | ||
<span className="workaround-dark-hidden"> | ||
<SunIcon className="w-4 h-4" /> | ||
</span> | ||
<span className="hidden workaround-dark-visible"> | ||
<MoonIcon className="w-4 h-4" /> | ||
</span> | ||
</> | ||
)} | ||
label="" | ||
className={clsx(value !== "system" && "text-tremor-brand", className)} | ||
> | ||
{Object.values(THEMES).map(({ id, icon: Icon, title }) => ( | ||
<DropdownMenu.Item | ||
key={id} | ||
icon={Icon} | ||
label={title} | ||
onClick={() => updateTheme(id)} | ||
className={clsx(id === value && "text-tremor-brand")} | ||
/> | ||
))} | ||
</DropdownMenu.Menu> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"use client"; | ||
|
||
import { LOCALSTORAGE_THEME_KEY } from "../../constants"; | ||
|
||
export const ThemeScript = () => { | ||
return ( | ||
<script | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
try { | ||
let theme = localStorage.getItem('keephq-${LOCALSTORAGE_THEME_KEY}'); | ||
if (theme) { | ||
theme = JSON.parse(theme); | ||
} | ||
if (!theme) { | ||
theme = window.matchMedia('(prefers-color-scheme: dark)').matches | ||
? 'dark' | ||
: 'light' | ||
} | ||
document.documentElement.classList[theme === "dark" ? "add" : "remove"]( | ||
"workaround-dark" | ||
); | ||
} catch (e) {} | ||
`, | ||
}} | ||
/> | ||
); | ||
}; |
Oops, something went wrong.