Skip to content

Commit 754316b

Browse files
committed
refactor: use onPrehydrate for injecting accent colour var
1 parent c24d1c8 commit 754316b

3 files changed

Lines changed: 27 additions & 9 deletions

File tree

app/app.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { useEventListener } from '@vueuse/core'
44
const route = useRoute()
55
const router = useRouter()
66
7+
// Initialize accent color before hydration to prevent flash
8+
initAccentOnPrehydrate()
9+
710
const isHomepage = computed(() => route.path === '/')
811
912
useHead({

app/composables/useSettings.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,27 @@ export function useAccentColor() {
8080
setAccentColor,
8181
}
8282
}
83+
84+
/**
85+
* Applies accent color before hydration to prevent flash of default color.
86+
* Call this from app.vue to ensure accent color is applied on every page.
87+
*/
88+
export function initAccentOnPrehydrate() {
89+
// Callback is stringified by Nuxt - external variables won't be available.
90+
// Colors must be hardcoded since ACCENT_COLORS can't be referenced.
91+
onPrehydrate(() => {
92+
const colors: Record<AccentColorId, string> = {
93+
rose: '#e9aeba',
94+
amber: '#fbbf24',
95+
emerald: '#34d399',
96+
sky: '#38bdf8',
97+
violet: '#a78bfa',
98+
coral: '#fb7185',
99+
}
100+
const settings = JSON.parse(localStorage.getItem('npmx-settings') || '{}')
101+
const color = settings.accentColorId ? colors[settings.accentColorId as AccentColorId] : null
102+
if (color) {
103+
document.documentElement.style.setProperty('--accent-color', color)
104+
}
105+
})
106+
}

nuxt.config.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import { ACCENT_COLORS } from './shared/utils/constants'
2-
3-
const accentInitScript = `(function(){var s=JSON.parse(localStorage.getItem('npmx-settings'));if(s&&s.accentColorId){document.documentElement.style.setProperty('--accent-color',${JSON.stringify(ACCENT_COLORS)}[s.accentColorId])}})()`
4-
51
export default defineNuxtConfig({
62
modules: [
73
function (_, nuxt) {
@@ -50,11 +46,6 @@ export default defineNuxtConfig({
5046
href: '/opensearch.xml',
5147
},
5248
],
53-
script: [
54-
{
55-
innerHTML: accentInitScript,
56-
},
57-
],
5849
},
5950
},
6051

0 commit comments

Comments
 (0)