Skip to content

Commit 04cf466

Browse files
committed
refactor: combine with other settings
1 parent c937b8c commit 04cf466

5 files changed

Lines changed: 39 additions & 50 deletions

File tree

app/components/Settings/BgThemePicker.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<script setup lang="ts">
2-
import { useBackgroundTheme } from '~/composables/useBackgroundTheme'
3-
42
const { backgroundThemes, selectedBackgroundTheme, setBackgroundTheme } = useBackgroundTheme()
53
64
onPrehydrate(el => {
7-
const id = localStorage.getItem('npmx-background-theme')
5+
const settings = JSON.parse(localStorage.getItem('npmx-settings') || '{}')
6+
const id = settings.preferredBackgroundTheme
87
if (id) {
98
const input = el.querySelector<HTMLInputElement>(`input[value="${id || 'neutral'}"]`)
109
if (input) {

app/composables/useBackgroundTheme.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

app/composables/useSettings.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import type { RemovableRef } from '@vueuse/core'
22
import { useLocalStorage } from '@vueuse/core'
33
import { ACCENT_COLORS } from '#shared/utils/constants'
44
import type { LocaleObject } from '@nuxtjs/i18n'
5+
import { BACKGROUND_THEMES } from '#shared/utils/constants'
6+
7+
type BackgroundThemeId = keyof typeof BACKGROUND_THEMES
58

69
type AccentColorId = keyof typeof ACCENT_COLORS
710

@@ -15,6 +18,8 @@ export interface AppSettings {
1518
includeTypesInInstall: boolean
1619
/** Accent color theme */
1720
accentColorId: AccentColorId | null
21+
/** Preferred background shade */
22+
preferredBackgroundTheme: BackgroundThemeId | null
1823
/** Hide platform-specific packages (e.g., @scope/pkg-linux-x64) from search results */
1924
hidePlatformPackages: boolean
2025
/** User-selected locale */
@@ -30,6 +35,7 @@ const DEFAULT_SETTINGS: AppSettings = {
3035
accentColorId: null,
3136
hidePlatformPackages: true,
3237
selectedLocale: null,
38+
preferredBackgroundTheme: null,
3339
sidebar: {
3440
collapsed: [],
3541
},
@@ -93,3 +99,28 @@ export function useAccentColor() {
9399
setAccentColor,
94100
}
95101
}
102+
103+
export function useBackgroundTheme() {
104+
const backgroundThemes = Object.entries(BACKGROUND_THEMES).map(([id, value]) => ({
105+
id: id as BackgroundThemeId,
106+
name: id,
107+
value,
108+
}))
109+
110+
const { settings } = useSettings()
111+
112+
function setBackgroundTheme(id: BackgroundThemeId | null) {
113+
if (id) {
114+
document.documentElement.dataset.bgTheme = id
115+
} else {
116+
document.documentElement.removeAttribute('data-bg-theme')
117+
}
118+
settings.value.preferredBackgroundTheme = id
119+
}
120+
121+
return {
122+
backgroundThemes,
123+
selectedBackgroundTheme: computed(() => settings.value.preferredBackgroundTheme),
124+
setBackgroundTheme,
125+
}
126+
}

app/utils/prehydrate.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export function initPreferencesOnPrehydrate() {
3535
document.documentElement.style.setProperty('--accent-color', color)
3636
}
3737

38+
// Apply background accent
39+
const preferredBackgroundTheme = settings.preferredBackgroundTheme
40+
if (preferredBackgroundTheme) {
41+
document.documentElement.dataset.bgTheme = preferredBackgroundTheme
42+
}
43+
3844
// Read and apply package manager preference
3945
const storedPM = localStorage.getItem('npmx-pm')
4046
// Parse the stored value (it's stored as a JSON string by useLocalStorage)

nuxt.config.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,6 @@ export default defineNuxtConfig({
7373
href: '/opensearch.xml',
7474
},
7575
],
76-
script: [
77-
{
78-
innerHTML: `
79-
(function () {
80-
const preferredBackgroundTheme = localStorage.getItem('npmx-background-theme')
81-
if (preferredBackgroundTheme) {
82-
document.documentElement.dataset.bgTheme = preferredBackgroundTheme
83-
}
84-
})()
85-
`,
86-
},
87-
],
8876
},
8977
},
9078

0 commit comments

Comments
 (0)