Skip to content

Commit 90ca00f

Browse files
authored
fix: apply accent colours via css vars (#1182)
1 parent a851472 commit 90ca00f

File tree

4 files changed

+23
-45
lines changed

4 files changed

+23
-45
lines changed

app/assets/main.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
--accent: var(--accent-color, oklch(1 0 0));
2828
--accent-muted: var(--accent-color, oklch(0.922 0 0));
2929

30+
/* accent colors */
31+
--swatch-coral: oklch(0.704 0.177 14.75);
32+
--swatch-amber: oklch(0.828 0.165 84.429);
33+
--swatch-emerald: oklch(0.792 0.153 166.95);
34+
--swatch-sky: oklch(0.787 0.128 230.318);
35+
--swatch-violet: oklch(0.78 0.148 286.067);
36+
--swatch-magenta: oklch(0.78 0.15 330);
37+
3038
/* syntax highlighting colors */
3139
--syntax-fn: oklch(0.727 0.137 299.149);
3240
--syntax-str: oklch(0.829 0.088 252.458);
@@ -89,6 +97,14 @@
8997
--accent: var(--accent-color, oklch(0.145 0 0));
9098
--accent-muted: var(--accent-color, oklch(0.205 0 0));
9199

100+
/* accent colors */
101+
--swatch-coral: oklch(0.7 0.19 14.75);
102+
--swatch-amber: oklch(0.8 0.25 84.429);
103+
--swatch-emerald: oklch(0.7 0.17 166.95);
104+
--swatch-sky: oklch(0.7 0.15 230.318);
105+
--swatch-violet: oklch(0.7 0.17 286.067);
106+
--swatch-magenta: oklch(0.75 0.18 330);
107+
92108
--syntax-fn: oklch(0.502 0.188 294.988);
93109
--syntax-str: oklch(0.425 0.152 252);
94110
--syntax-kw: oklch(0.588 0.193 20.469);

app/components/Settings/AccentColorPicker.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ onPrehydrate(el => {
2424
v-for="color in accentColors"
2525
:key="color.id"
2626
class="size-6 rounded-full transition-transform duration-150 motion-safe:hover:scale-110 cursor-pointer has-[:checked]:(ring-2 ring-fg ring-offset-2 ring-offset-bg-subtle) has-[:focus-visible]:(ring-2 ring-fg ring-offset-2 ring-offset-bg-subtle)"
27-
:style="{ backgroundColor: color.value }"
27+
:style="{ backgroundColor: `var(--swatch-${color.id})` }"
2828
>
2929
<input
3030
type="radio"

app/composables/useSettings.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,13 @@ export function useAccentColor() {
9191

9292
function setAccentColor(id: AccentColorId | null) {
9393
if (id) {
94-
const isDark = colorMode.value === 'dark'
95-
const color = isDark ? ACCENT_COLORS.dark[id] : ACCENT_COLORS.light[id]
96-
document.documentElement.style.setProperty('--accent-color', color)
94+
document.documentElement.style.setProperty('--accent-color', `var(--swatch-${id})`)
9795
} else {
9896
document.documentElement.style.removeProperty('--accent-color')
9997
}
10098
settings.value.accentColorId = id
10199
}
102100

103-
// Update accent color when color mode changes
104-
watch(
105-
() => colorMode.value,
106-
() => {
107-
if (settings.value.accentColorId) {
108-
setAccentColor(settings.value.accentColorId)
109-
}
110-
},
111-
)
112-
113101
return {
114102
accentColors,
115103
selectedAccentColor: computed(() => settings.value.accentColorId),

app/utils/prehydrate.ts

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import type { ACCENT_COLORS } from '#shared/utils/constants'
2-
3-
type AccentColorId = keyof typeof ACCENT_COLORS.light // for both themes color names are same
4-
51
/**
62
* Initialize user preferences before hydration to prevent flash/layout shift.
73
* This sets CSS custom properties and data attributes that CSS can use
@@ -13,40 +9,18 @@ export function initPreferencesOnPrehydrate() {
139
// Callback is stringified by Nuxt - external variables won't be available.
1410
// All constants must be hardcoded inside the callback.
1511
onPrehydrate(() => {
16-
// Accent colors - hardcoded since ACCENT_COLORS can't be referenced
17-
18-
const colors = {
19-
light: {
20-
coral: 'oklch(0.70 0.19 14.75)',
21-
amber: 'oklch(0.8 0.25 84.429)',
22-
emerald: 'oklch(0.70 0.17 166.95)',
23-
sky: 'oklch(0.70 0.15 230.318)',
24-
violet: 'oklch(0.70 0.17 286.067)',
25-
magenta: 'oklch(0.75 0.18 330)',
26-
},
27-
dark: {
28-
coral: 'oklch(0.704 0.177 14.75)',
29-
amber: 'oklch(0.828 0.165 84.429)',
30-
emerald: 'oklch(0.792 0.153 166.95)',
31-
sky: 'oklch(0.787 0.128 230.318)',
32-
violet: 'oklch(0.78 0.148 286.067)',
33-
magenta: 'oklch(0.78 0.15 330)',
34-
},
35-
}
12+
// Valid accent color IDs (must match --swatch-* variables defined in main.css)
13+
const accentColorIds = new Set(['coral', 'amber', 'emerald', 'sky', 'violet', 'magenta'])
3614

3715
// Valid package manager IDs
3816
const validPMs = new Set(['npm', 'pnpm', 'yarn', 'bun', 'deno', 'vlt'])
3917

4018
// Read settings from localStorage
4119
const settings = JSON.parse(localStorage.getItem('npmx-settings') || '{}')
4220

43-
// Determine theme (default to 'dark')
44-
const theme = document.documentElement.dataset.theme === 'light' ? 'light' : 'dark'
45-
46-
// Apply accent color based on theme
47-
const accentColorId = settings.accentColorId as AccentColorId | undefined
48-
if (accentColorId && colors[theme][accentColorId]) {
49-
document.documentElement.style.setProperty('--accent-color', colors[theme][accentColorId])
21+
const accentColorId = settings.accentColorId
22+
if (accentColorId && accentColorIds.has(accentColorId)) {
23+
document.documentElement.style.setProperty('--accent-color', `var(--swatch-${accentColorId})`)
5024
}
5125

5226
// Apply background accent

0 commit comments

Comments
 (0)