-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
296 additions
and
72 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
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,37 @@ | ||
<script lang="ts"> | ||
import Fa from 'svelte-fa' | ||
import { faCloudSun, faMoon, faSun } from '@fortawesome/free-solid-svg-icons' | ||
import { Button, Tooltip } from 'sveltestrap' | ||
import { get } from 'svelte/store' | ||
import { currentTheme, setCurrentTheme } from 'theme' | ||
function toggle () { | ||
if (get(currentTheme) === 'auto') { | ||
setCurrentTheme('dark') | ||
} else if (get(currentTheme) === 'dark') { | ||
setCurrentTheme('light') | ||
} else { | ||
setCurrentTheme('auto') | ||
} | ||
} | ||
</script> | ||
|
||
<Button color="link" on:click={toggle} id="button" alt="Switch theme"> | ||
{#if $currentTheme === 'dark'} | ||
<Fa fw icon={faMoon} /> | ||
{:else if $currentTheme === 'light'} | ||
<Fa fw icon={faSun} /> | ||
{:else} | ||
<Fa fw icon={faCloudSun} /> | ||
{/if} | ||
</Button> | ||
<Tooltip target="button" animation> | ||
{#if $currentTheme === 'dark'} | ||
Dark theme | ||
{:else if $currentTheme === 'light'} | ||
Light theme | ||
{:else} | ||
Automatic theme | ||
{/if} | ||
</Tooltip> |
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
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,49 @@ | ||
import '@fontsource/work-sans' | ||
|
||
import { get, writable } from 'svelte/store' | ||
|
||
type ThemeFileName = 'dark'|'light' | ||
type ThemeName = ThemeFileName|'auto' | ||
|
||
const savedTheme = (localStorage.getItem('theme') ?? 'auto') as ThemeName | ||
export const currentTheme = writable(savedTheme) | ||
|
||
const styleElement = document.createElement('style') | ||
document.head.appendChild(styleElement) | ||
|
||
function loadThemeFile (name: ThemeFileName) { | ||
if (name === 'dark') { | ||
return import('./theme.dark.scss?inline') | ||
} | ||
return import('./theme.light.scss?inline') | ||
} | ||
|
||
async function loadTheme (name: ThemeFileName) { | ||
const theme = (await loadThemeFile(name)).default | ||
styleElement.innerHTML = theme | ||
} | ||
|
||
|
||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { | ||
if (get(currentTheme) === 'auto') { | ||
loadTheme(event.matches ? 'dark' : 'light') | ||
} | ||
}) | ||
|
||
|
||
export function setCurrentTheme (theme: ThemeName): void { | ||
localStorage.setItem('theme', theme) | ||
currentTheme.set(theme) | ||
if (theme === 'auto') { | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (window.matchMedia?.('(prefers-color-scheme: dark)').matches) { | ||
loadTheme('dark') | ||
} else { | ||
loadTheme('light') | ||
} | ||
} else { | ||
loadTheme(theme) | ||
} | ||
} | ||
|
||
setCurrentTheme(savedTheme) |
Oops, something went wrong.