forked from beancount/fava
-
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: add light/dark mode switch (beancount#1874)
- Loading branch information
Showing
4 changed files
with
191 additions
and
125 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 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,79 @@ | ||
<script lang="ts"> | ||
import { themeStore } from "../stores/theme"; | ||
const icons = { | ||
light: `<svg viewBox="0 0 24 24" width="16" height="16"> | ||
<path fill="currentColor" d="M12 7c-2.76 0-5 2.24-5 5s2.24 5 5 5 5-2.24 5-5-2.24-5-5-5zM2 13h2c.55 0 1-.45 1-1s-.45-1-1-1H2c-.55 0-1 .45-1 1s.45 1 1 1zm18 0h2c.55 0 1-.45 1-1s-.45-1-1-1h-2c-.55 0-1 .45-1 1s.45 1 1 1zM11 2v2c0 .55.45 1 1 1s1-.45 1-1V2c0-.55-.45-1-1-1s-1 .45-1 1zm0 18v2c0 .55.45 1 1 1s1-.45 1-1v-2c0-.55-.45-1-1-1s-1 .45-1 1zM5.99 4.58a.996.996 0 0 0-1.41 0 .996.996 0 0 0 0 1.41l1.06 1.06c.39.39 1.03.39 1.41 0s.39-1.03 0-1.41L5.99 4.58zm12.37 12.37a.996.996 0 0 0-1.41 0 .996.996 0 0 0 0 1.41l1.06 1.06c.39.39 1.03.39 1.41 0a.996.996 0 0 0 0-1.41l-1.06-1.06zm1.06-10.96a.996.996 0 0 0 0-1.41.996.996 0 0 0-1.41 0l-1.06 1.06c-.39.39-.39 1.03 0 1.41s1.03.39 1.41 0l1.06-1.06zM7.05 18.36a.996.996 0 0 0 0-1.41.996.996 0 0 0-1.41 0l-1.06 1.06c-.39.39-.39 1.03 0 1.41s1.03.39 1.41 0l1.06-1.06z"/> | ||
</svg>`, | ||
dark: `<svg viewBox="0 0 24 24" width="16" height="16"> | ||
<path fill="currentColor" d="M9.37 5.51A7.35 7.35 0 0 0 9.1 7.5c0 4.08 3.32 7.4 7.4 7.4.68 0 1.35-.09 1.99-.27A7.014 7.014 0 0 1 12 19c-3.86 0-7-3.14-7-7 0-2.93 1.81-5.45 4.37-6.49zM12 3a9 9 0 1 0 9 9c0-.46-.04-.92-.1-1.36a5.389 5.389 0 0 1-4.4 2.26 5.403 5.403 0 0 1-3.14-9.8c-.44-.06-.9-.1-1.36-.1z"/> | ||
</svg>` | ||
}; | ||
</script> | ||
|
||
<div class="theme-switch"> | ||
{#each [...themeStore.values()] as [option, name]} | ||
<button | ||
class="theme-button" | ||
class:active={$themeStore === option} | ||
on:click={() => themeStore.set(option)} | ||
title={name} | ||
> | ||
{@html icons[option]} | ||
</button> | ||
{/each} | ||
</div> | ||
|
||
<style> | ||
.theme-switch { | ||
display: flex; | ||
gap: 2px; | ||
padding: 3px; | ||
margin-right: 0.5em; | ||
background: var(--background-darker); | ||
border-radius: 8px; | ||
} | ||
.theme-button { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
width: 32px; | ||
height: 32px; | ||
padding: 0; | ||
color: var(--text-color-lighter); | ||
cursor: pointer; | ||
background: transparent; | ||
border: none; | ||
border-radius: 6px; | ||
transition: all 0.2s ease; | ||
} | ||
.theme-button:hover { | ||
color: var(--text-color); | ||
background: var(--background); | ||
} | ||
.theme-button.active { | ||
color: var(--header-color); | ||
background: var(--header-background); | ||
} | ||
/* Ensure SVG icons inherit color properly */ | ||
.theme-button :global(svg) { | ||
width: 18px; | ||
height: 18px; | ||
} | ||
@media (width <= 768px) { | ||
.theme-button { | ||
width: 28px; | ||
height: 28px; | ||
} | ||
.theme-button :global(svg) { | ||
width: 16px; | ||
height: 16px; | ||
} | ||
} | ||
</style> |
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,27 @@ | ||
import { constants } from "../lib/validation"; | ||
import { localStorageSyncedStore } from "../lib/store"; | ||
import type { ValidationT } from "../lib/validation"; | ||
|
||
const theme_validator = constants("light", "dark"); | ||
type Theme = ValidationT<typeof theme_validator>; | ||
|
||
export const themeStore = localStorageSyncedStore<Theme>( | ||
"theme", | ||
theme_validator, | ||
() => "dark", // Set dark as default | ||
() => [ | ||
["light", "Light"], | ||
["dark", "Dark"], | ||
], | ||
); | ||
|
||
// Apply theme class to document | ||
themeStore.subscribe((theme: Theme) => { | ||
if (theme === "light") { | ||
document.documentElement.classList.remove("dark-theme"); | ||
document.documentElement.classList.add("light-theme"); | ||
} else { | ||
document.documentElement.classList.remove("light-theme"); | ||
document.documentElement.classList.add("dark-theme"); | ||
} | ||
}); |