Skip to content

Commit

Permalink
feat: sets the right theme color (for iPhones) (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
filoozom authored Oct 28, 2023
1 parent 2702eaf commit 4cbffcc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<link rel="apple-touch-icon" href="/images/icons/icon-192x192.png" />
<link rel="manifest" href="/manifest.json" />
<meta name="msapplication-TileColor" content="#da532c" />
<meta name="theme-color" content="#ffffff" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0, viewport-fit=cover"
Expand Down
11 changes: 11 additions & 0 deletions src/lib/stores/is-system-dark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { browser } from '$app/environment'
import { readable } from 'svelte/store'

const isDarkQuery = browser && window.matchMedia('(prefers-color-scheme: dark)')

export const isSystemDark = !isDarkQuery
? readable(false)
: readable(isDarkQuery.matches, (set) => {
isDarkQuery.onchange = (event) => set(event.matches)
return () => (isDarkQuery.onchange = null)
})
11 changes: 10 additions & 1 deletion src/lib/stores/theme.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { getFromLocalStorage, saveToLocalStorage } from '$lib/adapters/utils'
import { writable, type Writable } from 'svelte/store'
import { z } from 'zod'
import { isSystemDark } from './is-system-dark'

const darkModeSchema = z.enum(['dark', 'light', 'system'])
export type DarkMode = z.infer<typeof darkModeSchema>

interface Theme {
darkMode: DarkMode
baseColor: string
isDarkMode: boolean
}

interface ThemeStore extends Writable<Theme> {
Expand All @@ -28,7 +30,14 @@ function createThemeStore(): ThemeStore {
} catch (error) {
// this is fine
}
const store = writable<Theme>({ darkMode, baseColor })
const store = writable<Theme>({ darkMode, baseColor, isDarkMode: false })

isSystemDark.subscribe((isSystemDark) => {
store.update((theme) => ({
...theme,
isDarkMode: isSystemDark && theme.darkMode === 'system',
}))
})

return {
...store,
Expand Down
4 changes: 1 addition & 3 deletions src/lib/utils/color.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getClosestColor, hexToRgb } from '@waku-objects/luminance'
import { browser } from '$app/environment'
import type { DarkMode } from '$lib/stores/theme'

interface Color {
name: string
Expand Down Expand Up @@ -70,10 +69,9 @@ const lightColorsVars: Color[] = [
},
]

export function changeColors(baseColor: string, darkMode: DarkMode, isSystemDark: boolean) {
export function changeColors(baseColor: string, isDarkMode: boolean) {
if (!browser) return

const isDarkMode = darkMode === 'dark' || (darkMode === 'system' && isSystemDark)
const colors = isDarkMode ? darkColorVars : lightColorsVars
const colorsToRemove = isDarkMode ? lightColorsVars : darkColorVars

Expand Down
14 changes: 5 additions & 9 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
let unsubscribeWalletStore: (() => void) | undefined = undefined
let unsubscribeExchangeStore: (() => void) | undefined = undefined
let loading = true
let error: string | undefined = undefined
let isDarkQuery: MediaQueryList
let isSystemDark: boolean | undefined
onMount(async () => {
unsubscribeWalletStore = walletStore.subscribe(({ wallet }) => {
Expand Down Expand Up @@ -60,11 +57,6 @@
})
}
loading = false
isDarkQuery = window.matchMedia('(prefers-color-scheme: dark)')
isDarkQuery.onchange = (event) => {
isSystemDark = event.matches
}
})
onDestroy(() => {
Expand All @@ -73,7 +65,7 @@
if (unsubscribeExchangeStore) unsubscribeExchangeStore()
})
$: changeColors($theme.baseColor, $theme.darkMode, isSystemDark ?? isDarkQuery?.matches)
$: changeColors($theme.baseColor, $theme.isDarkMode)
const resolveError =
(error: ErrorDescriptor, handler: () => Promise<void> | void) => async () => {
Expand All @@ -82,6 +74,10 @@
}
</script>

<svelte:head>
<meta name="theme-color" content={$theme.isDarkMode ? '#000000' : '#ffffff'} />
</svelte:head>

<div class="root">
{#if $errorStore.length > 0}
{@const error = $errorStore[0]}
Expand Down

1 comment on commit 4cbffcc

@vercel
Copy link

@vercel vercel bot commented on 4cbffcc Oct 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.