Commit 37a3b6f879
37a3b6f879d8bf9fa04b7b92e97f93da3efa061e
parent: b7c93b1005
Unsigned
Alicia Sykes <alicia@omg.lol> · 2024-03-25 11:20 UTC
Adds theme switcher
Layout: unified · split
web/src/components/Hero.astro
+11
| @@ -1,7 +1,12 @@ |
| 1 | 1 | --- |
| 2 | 2 | import FontAwesome from "@components/form/FontAwesome.svelte" |
| 3 | import ThemeSwitcher from "@components/form/ThemeSwitcher.svelte" |
| 3 | 4 | --- |
| 4 | 5 | |
| 6 | <div class="theme-switcher"> |
| 7 | <ThemeSwitcher client:load /> |
| 8 | </div> |
| 9 | |
| 5 | 10 | <div class="hero"> |
| 6 | 11 | <h1>Awesome Privacy</h1> |
| 7 | 12 | <p class="intro"> |
| @@ -119,6 +124,12 @@ import FontAwesome from "@components/form/FontAwesome.svelte" |
| 119 | 124 | } |
| 120 | 125 | } |
| 121 | 126 | |
| 127 | .theme-switcher { |
| 128 | position: absolute; |
| 129 | right: 1rem; |
| 130 | top: 1rem; |
| 131 | } |
| 132 | |
| 122 | 133 | .top-right { |
| 123 | 134 | position: absolute; |
| 124 | 135 | top: 0; |
web/src/components/form/ThemeSwitcher.svelte
added
+100
| @@ -0,0 +1,100 @@ |
| 1 | <script lang="ts"> |
| 2 | import { onMount } from 'svelte'; |
| 3 | import { writable } from 'svelte/store'; |
| 4 | |
| 5 | // Define a store for the theme which reacts to changes |
| 6 | let theme = writable('dark'); |
| 7 | |
| 8 | // On component mount, check local storage for a theme setting |
| 9 | onMount(() => { |
| 10 | const storedTheme = localStorage.getItem('theme'); |
| 11 | theme.set(storedTheme || 'dark'); |
| 12 | applyTheme(storedTheme || 'dark'); |
| 13 | }); |
| 14 | |
| 15 | // Function to toggle theme between light and dark |
| 16 | function toggleTheme(): void { |
| 17 | theme.update((currentTheme) => { |
| 18 | const newTheme = currentTheme === 'dark' ? 'light' : 'dark'; |
| 19 | localStorage.setItem('theme', newTheme); |
| 20 | applyTheme(newTheme); |
| 21 | return newTheme; |
| 22 | }); |
| 23 | } |
| 24 | |
| 25 | // Function to apply the theme by setting the attribute on the <html> element |
| 26 | function applyTheme(selectedTheme: string): void { |
| 27 | document.documentElement.setAttribute('data-theme', selectedTheme); |
| 28 | } |
| 29 | </script> |
| 30 | |
| 31 | |
| 32 | <!-- svelte-ignore a11y-click-events-have-key-events --> |
| 33 | <!-- svelte-ignore a11y-no-static-element-interactions --> |
| 34 | <div class="theme-switcher" on:click={toggleTheme}> |
| 35 | <div class={`toggle ${$theme}`}> |
| 36 | <span class="theme-icon">🌘</span> |
| 37 | <span class="theme-icon">☀️</span> |
| 38 | </div> |
| 39 | </div> |
| 40 | |
| 41 | |
| 42 | <style lang="scss"> |
| 43 | .theme-switcher { |
| 44 | cursor: pointer; |
| 45 | display: inline-flex; |
| 46 | align-items: center; |
| 47 | justify-content: center; |
| 48 | border: 2px solid transparent; |
| 49 | border-radius: var(--curve-lg); |
| 50 | padding: 0.25rem; |
| 51 | background-color: rgba(255, 255, 255, 0.2); |
| 52 | transition: background-color 0.3s ease; |
| 53 | border: 2px solid var(--box-outline); |
| 54 | box-shadow: 3px 3px 0 var(--box-outline); |
| 55 | |
| 56 | &:hover { |
| 57 | background-color: rgba(255, 255, 255, 0.3); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | .toggle { |
| 62 | display: flex; |
| 63 | align-items: center; |
| 64 | justify-content: space-between; |
| 65 | width: 4rem; |
| 66 | height: 2rem; |
| 67 | background: var(--accent-fg); |
| 68 | border-radius: var(--curve-lg); |
| 69 | padding: 0.25rem; |
| 70 | position: relative; |
| 71 | transition: background 0.3s ease; |
| 72 | |
| 73 | &::before { |
| 74 | content: ''; |
| 75 | position: absolute; |
| 76 | top: 0.25rem; |
| 77 | left: 0.25rem; |
| 78 | width: 1.6rem; |
| 79 | height: 1.6rem; |
| 80 | border-radius: 50%; |
| 81 | background: var(--background); |
| 82 | opacity: 0.6; |
| 83 | transition: transform 0.3s ease; |
| 84 | } |
| 85 | |
| 86 | &.dark::before { |
| 87 | transform: translateX(0); |
| 88 | } |
| 89 | |
| 90 | &:not(.dark)::before { |
| 91 | transform: translateX(2.2rem); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | .theme-icon { |
| 96 | display: flex; |
| 97 | font-size: 1.5rem; |
| 98 | } |
| 99 | |
| 100 | </style> |
web/src/styles/values.css
+27 −1
| @@ -1,4 +1,5 @@ |
| 1 | | :root { |
| 1 | |
| 2 | html { |
| 2 | 3 | --accent: #f45397; |
| 3 | 4 | --accent-fg: #1e1f21; |
| 4 | 5 | |
| @@ -23,4 +24,29 @@ |
| 23 | 24 | --background-form: #19191c; |
| 24 | 25 | |
| 25 | 26 | --box-outline: #000; |
| 27 | |
| 28 | &[data-theme='light'] { |
| 29 | --accent: #f45397; |
| 30 | --accent-fg: #fff; |
| 31 | |
| 32 | --accent-2: #ffdf60; |
| 33 | --accent-3: #5f53f4; |
| 34 | --accent-4: #28dffd; |
| 35 | |
| 36 | --foreground: #13151a; |
| 37 | |
| 38 | --curve-sm: 4px; |
| 39 | --curve-md: 6px; |
| 40 | --curve-lg: 12px; |
| 41 | |
| 42 | --danger: #ff0048; |
| 43 | --success: #00ff64; |
| 44 | |
| 45 | --transparent-accent: #5f53f482; |
| 46 | |
| 47 | --background: #feecff; |
| 48 | --bg-gradient-comp-1: #feecff; |
| 49 | --bg-gradient-comp-2: #e1e4fb; |
| 50 | } |
| 26 | 51 | } |
| 52 | |