Skip to content

Commit e1076d7

Browse files
committed
refactor: migrate to @nuxtjs/color-mode
1 parent 321b653 commit e1076d7

7 files changed

Lines changed: 56 additions & 8 deletions

File tree

app/app.vue

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@ import { useEventListener } from '@vueuse/core'
33
44
const route = useRoute()
55
const router = useRouter()
6-
const { settings } = useSettings()
76
87
// Initialize accent color before hydration to prevent flash
98
initAccentOnPrehydrate()
109
1110
const isHomepage = computed(() => route.path === '/')
12-
const theme = settings.value.theme === 'dark' ? 'dark' : 'light'
1311
1412
useHead({
15-
htmlAttrs: {
16-
'data-theme': 'dark',
17-
},
1813
titleTemplate: titleChunk => {
1914
return titleChunk ? titleChunk : 'npmx - Better npm Package Browser'
2015
},

app/components/SettingsMenu.vue

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { onKeyStroke, onClickOutside } from '@vueuse/core'
33
44
const { settings } = useSettings()
55
const { locale, locales, setLocale } = useI18n()
6+
const colorMode = useColorMode()
67
78
const availableLocales = computed(() =>
89
locales.value.map(l => (typeof l === 'string' ? { code: l, name: l } : l)),
@@ -138,6 +139,32 @@ onKeyStroke(',', e => {
138139
</span>
139140
</button>
140141

142+
<!-- Theme selector -->
143+
<div class="pt-2 mt-2 border-t border-border">
144+
<div class="px-2 py-1">
145+
<label for="theme-select" class="text-xs text-fg-subtle uppercase tracking-wider">
146+
{{ $t('settings.theme') }}
147+
</label>
148+
</div>
149+
<div class="px-2 py-1">
150+
<select
151+
id="theme-select"
152+
:value="colorMode.preference"
153+
class="w-full bg-bg-muted border border-border rounded-md px-2 py-1.5 text-sm text-fg focus:outline-none focus:ring-2 focus:ring-fg/50 cursor-pointer"
154+
@change="
155+
colorMode.preference = ($event.target as HTMLSelectElement).value as
156+
| 'light'
157+
| 'dark'
158+
| 'system'
159+
"
160+
>
161+
<option value="system">{{ $t('settings.theme_system') }}</option>
162+
<option value="light">{{ $t('settings.theme_light') }}</option>
163+
<option value="dark">{{ $t('settings.theme_dark') }}</option>
164+
</select>
165+
</div>
166+
</div>
167+
141168
<!-- Language selector -->
142169
<div class="pt-2 mt-2 border-t border-border">
143170
<div class="px-2 py-1">

app/composables/useSettings.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ export interface AppSettings {
1414
includeTypesInInstall: boolean
1515
/** Accent color theme */
1616
accentColorId: AccentColorId | null
17-
/** Theme of site, default dark */
18-
theme: 'dark' | 'light'
1917
}
2018

2119
const DEFAULT_SETTINGS: AppSettings = {
2220
relativeDates: false,
2321
includeTypesInInstall: true,
24-
theme: 'dark',
2522
accentColorId: null,
2623
}
2724

i18n/locales/en.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
"settings": {
4444
"relative_dates": "Relative dates",
4545
"include_types": "Include {'@'}types in install",
46+
"theme": "Theme",
47+
"theme_light": "Light",
48+
"theme_dark": "Dark",
49+
"theme_system": "System",
4650
"language": "Language",
4751
"help_translate": "Help translate npmx"
4852
},

nuxt.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,16 @@ export default defineNuxtConfig({
3131
'@vite-pwa/nuxt',
3232
'@vueuse/nuxt',
3333
'@nuxtjs/i18n',
34+
'@nuxtjs/color-mode',
3435
],
3536

37+
colorMode: {
38+
preference: 'system',
39+
fallback: 'dark',
40+
dataValue: 'theme',
41+
storageKey: 'npmx-color-mode',
42+
},
43+
3644
css: ['vue-data-ui/style.css'],
3745

3846
devtools: { enabled: true },

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@nuxt/a11y": "1.0.0-alpha.1",
3232
"@nuxt/fonts": "^0.13.0",
3333
"@nuxt/scripts": "^0.13.2",
34+
"@nuxtjs/color-mode": "^4.0.0",
3435
"@nuxtjs/html-validator": "^2.1.0",
3536
"@nuxtjs/i18n": "10.2.1",
3637
"@shikijs/langs": "^3.21.0",

pnpm-lock.yaml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)