|
1 |
| -import { component, computed, hasWindow, ref, render, state } from "../../../nice"; |
| 1 | +import { component, computed, hasWindow, ref, render, state, valueOf } from "../../../nice"; |
| 2 | +import { globalStore } from "../app"; |
2 | 3 | import { hexToRgb, rgbToHex, shiftHue } from "../lib/utils";
|
3 | 4 | import { Button } from "./button";
|
4 | 5 | import { ColourPicker } from "./colour-picker";
|
5 | 6 |
|
6 | 7 | import styles from './theme-widget.module.scss'
|
7 | 8 |
|
8 | 9 | export const ThemeWidget = component(() => {
|
9 |
| - const inputRef = ref<HTMLInputElement>(); |
10 |
| - const currentColor = state(hasWindow ? getComputedStyle(document.body).getPropertyValue('--theme-primary') : ''); |
11 |
| - const currentColorHex = computed(() => rgbToHex(currentColor.get()), [currentColor]); |
| 10 | + const currentColor = globalStore('theme'); |
12 | 11 | const colourPickerOpen = state(false);
|
13 | 12 |
|
14 |
| - const onColorChange = computed<MouseEvent>((e) => { |
15 |
| - const inputElement = e.target as HTMLInputElement; |
16 |
| - currentColor.set(hexToRgb(inputElement.value)); |
17 |
| - }); |
18 |
| - |
19 | 13 | computed(() => {
|
20 | 14 | if (hasWindow) {
|
21 | 15 | const body = document.querySelector('body');
|
22 |
| - body!.style.setProperty('--theme-primary', currentColor.get()); |
23 |
| - body!.style.setProperty('--theme-secondary', shiftHue(currentColor.get(), .2)); |
| 16 | + body!.style.setProperty('--theme-primary', hexToRgb(valueOf(currentColor))); |
| 17 | + body!.style.setProperty('--theme-secondary', shiftHue(hexToRgb(valueOf(currentColor)), .2)); |
24 | 18 | }
|
25 | 19 | }, [currentColor]);
|
26 | 20 |
|
| 21 | + const buttonLabel = computed(() => { |
| 22 | + return valueOf(colourPickerOpen) ? 'Close' : 'Choose Theme' as string; |
| 23 | + }, [colourPickerOpen]); |
| 24 | + |
27 | 25 | const onClick = computed<MouseEvent>(() => {
|
28 |
| - const input = inputRef.get(); |
29 |
| - if (!input) return; |
30 | 26 | colourPickerOpen.set(!colourPickerOpen.get());
|
31 | 27 | });
|
32 | 28 |
|
33 | 29 | const onChange = computed<string>((e) => {
|
34 |
| - console.log('On Change', e); |
35 |
| - currentColor.set(hexToRgb(e)); |
| 30 | + document.cookie = `theme=${e}`; |
| 31 | + currentColor.set(e); |
36 | 32 | colourPickerOpen.set(false);
|
37 | 33 | });
|
38 | 34 |
|
39 | 35 | return render`
|
40 | 36 | <div class="${styles.themeWidget}">
|
41 |
| - ${Button({ label: 'Change Theme', onClick })} |
42 |
| - <input type="color" ref=${inputRef} on-input=${onColorChange} set-value=${currentColorHex}> |
43 |
| - ${ColourPicker({ isOpen: colourPickerOpen, onChange: onChange, selected: currentColorHex })} |
| 37 | + ${Button({ label: buttonLabel, onClick })} |
| 38 | + ${ColourPicker({ isOpen: colourPickerOpen, onChange: onChange, selected: currentColor })} |
44 | 39 | </div>
|
45 | 40 | `
|
46 | 41 | });
|
0 commit comments