Skip to content

Commit 5b99e42

Browse files
committed
fix(settings): current active color theme
1 parent 854ac6d commit 5b99e42

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/components/App.svelte

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
import settings from '../store/settings';
55
import { onMount } from 'svelte';
66
import { _ } from 'svelte-i18n';
7-
import Color from 'color';
87
import { fetchWordsList } from '../services/words';
98
import { loadSettings } from '../services/settings';
9+
import { colorSecondary } from '../ts/constants';
1010
1111
let wordsPromise = fetchWordsList();
1212
let lang = loadSettings().language;
1313
1414
onMount(() => {
1515
settings.subscribe(newSettings => {
1616
const { style } = document.documentElement;
17-
const white = Color('white');
1817
style.setProperty('--color-primary', newSettings.colorTheme.hex());
19-
style.setProperty('--color-secondary', white.hex());
20-
style.setProperty('--color-secondary-transparent', white.alpha(0.2).string());
18+
style.setProperty('--color-secondary', colorSecondary.hex());
19+
style.setProperty('--color-secondary-transparent', colorSecondary.alpha(0.2).string());
2120
if (newSettings.language !== lang) {
2221
lang = newSettings.language;
2322
wordsPromise = fetchWordsList();

src/components/Loader.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
width: 64px;
1414
height: 64px;
1515
border: 7px solid;
16-
border-color: white transparent white transparent;
16+
border-color: var(--color-secondary) transparent var(--color-secondary) transparent;
1717
border-radius: var(--radius-high);
1818
pointer-events: none;
1919
animation: spin 1s forwards infinite linear;

src/components/Settings.svelte

+16-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
import { fade } from 'svelte/transition';
44
import IoMdClose from 'svelte-icons/io/IoMdClose.svelte';
55
import IconButton from './shared/IconButton.svelte';
6-
import { defaultColors, LANGS } from '../ts/constants';
6+
import {
7+
colorSecondary,
8+
COLOR_HEX_REGEX,
9+
defaultColors,
10+
LANGS,
11+
MIN_CONTRAST,
12+
} from '../ts/constants';
713
import settings, { Settings } from '../store/settings';
814
import Icon from './shared/Icon.svelte';
915
import IoMdCheckmark from 'svelte-icons/io/IoMdCheckmark.svelte';
@@ -15,6 +21,8 @@
1521
let localSettings: Settings = loadSettings();
1622
1723
let colorInput = localSettings.colorTheme.hex();
24+
let colorErrorMessageKey: string | null;
25+
1826
$: colorErrorMessageKey = getColorErrorMessageKey(colorInput);
1927
2028
const dispatch = createEventDispatcher();
@@ -28,15 +36,13 @@
2836
};
2937
3038
const getColorErrorMessageKey = (colorStr: string) => {
31-
let color: Color;
32-
33-
try {
34-
color = Color(colorStr);
35-
} catch {
39+
if (!colorStr.match(COLOR_HEX_REGEX)) {
3640
return 'color-invalid';
3741
}
3842
39-
if (color.contrast(Color('#fff')) < 1.5) {
43+
const color = Color(colorStr);
44+
45+
if (color.contrast(colorSecondary) < MIN_CONTRAST) {
4046
return 'color-too-bright';
4147
}
4248
@@ -46,12 +52,11 @@
4652
const handleColorInputChange = () => {
4753
if (colorErrorMessageKey !== null) return;
4854
49-
updateColorTheme(localSettings.colorTheme);
55+
updateColorTheme(Color(colorInput));
5056
};
5157
5258
const updateColorTheme = (color: Color) => {
5359
setColorTheme(color);
54-
console.log(color.hex());
5560
colorInput = color.hex();
5661
};
5762
@@ -123,11 +128,11 @@
123128
</div>
124129
{#each defaultColors as color}
125130
<div
126-
class={`color ${localSettings.colorTheme === color ? 'color-active' : ''}`}
131+
class={`color ${color.hex() === colorInput ? 'color-active' : ''}`}
127132
style={`--color: ${color}`}
128133
on:click={() => updateColorTheme(color)}
129134
>
130-
{#if localSettings.colorTheme === color}
135+
{#if color.hex() === colorInput}
131136
<Icon icon={IoMdCheckmark} />
132137
{/if}
133138
</div>

src/ts/constants.ts

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import Color from 'color';
22

33
export const defaultColors = ['#3498db', '#313131', '#c77a99'].map(color => Color(color));
44

5+
export const colorSecondary = Color('white');
6+
7+
export const MIN_CONTRAST = 1.5;
8+
9+
export const COLOR_HEX_REGEX = /^#[0-9a-f]{3}([0-9a-f]{3})?$/i;
10+
511
export const LANGS = {
612
en: 'English',
713
fr: 'Français',

0 commit comments

Comments
 (0)