Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit cfa34f7

Browse files
committed
Rewrite cosmetics and theme preferences
- Cosmetics and theme preferences are now only stored in cookies instead of mix of both the cookies and the state. - Theme preference now supports client hints. This means the server can render the page in correct theme for the client if the client passes that information (any browser other than Firefox), which avoids annoying flash. - Hacky Nitro plugin for theme has been removed. It's functionality should be fully provided by the theme plugin with much cleaner code that uses Nuxt's built-ins. - All pages and components should be updated to use the new plugins.
1 parent bd49d6b commit cfa34f7

File tree

21 files changed

+266
-249
lines changed

21 files changed

+266
-249
lines changed

components/ui/charts/ChartDisplay.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
<div
161161
:style="{
162162
'--color-brand': isUsingProjectColors
163-
? intToRgba(project.color, project.id, theme ?? undefined)
163+
? intToRgba(project.color, project.id, $theme.active ?? undefined)
164164
: getDefaultColor(project.id),
165165
}"
166166
class="legend__item__color"
@@ -309,7 +309,8 @@ import { UiChartsCompactChart as CompactChart, UiChartsChart as Chart } from '#c
309309
import PaletteIcon from '~/assets/icons/palette.svg?component'
310310
311311
const router = useNativeRouter()
312-
const theme = useTheme()
312+
313+
const $theme = useTheme()
313314
314315
const props = withDefaults(
315316
defineProps<{

composables/cosmetics.js

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

composables/cosmetics.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function useCosmetics() {
2+
return useNuxtApp().$cosmetics
3+
}

composables/theme.js

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

composables/theme.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function useTheme() {
2+
return useNuxtApp().$theme
3+
}

layouts/default.vue

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
:title="formatMessage(messages.changeTheme)"
6363
@click="changeTheme"
6464
>
65-
<MoonIcon v-if="$colorMode.value === 'light'" aria-hidden="true" />
65+
<MoonIcon v-if="$theme.active === 'light'" aria-hidden="true" />
6666
<SunIcon v-else aria-hidden="true" />
6767
</button>
6868
<div
@@ -242,7 +242,7 @@
242242
{{ formatMessage(commonMessages.settingsLabel) }}
243243
</NuxtLink>
244244
<button class="iconified-button" @click="changeTheme">
245-
<MoonIcon v-if="$colorMode.value === 'light'" class="icon" />
245+
<MoonIcon v-if="$theme.active === 'light'" class="icon" />
246246
<SunIcon v-else class="icon" />
247247
<span class="dropdown-item__text">
248248
{{ formatMessage(messages.changeTheme) }}
@@ -403,7 +403,7 @@
403403
{{ formatMessage(messages.getModrinthApp) }}
404404
</nuxt-link>
405405
<button class="iconified-button raised-button" @click="changeTheme">
406-
<MoonIcon v-if="$colorMode.value === 'light'" aria-hidden="true" />
406+
<MoonIcon v-if="$theme.active === 'light'" aria-hidden="true" />
407407
<SunIcon v-else aria-hidden="true" />
408408
{{ formatMessage(messages.changeTheme) }}
409409
</button>
@@ -449,7 +449,6 @@ import ModalCreation from '~/components/ui/ModalCreation.vue'
449449
import Avatar from '~/components/ui/Avatar.vue'
450450
import { getProjectTypeMessage } from '~/utils/i18n-project-type.ts'
451451
import { commonMessages } from '~/utils/common-messages.ts'
452-
import { DARK_THEMES } from '~/composables/theme.js'
453452
454453
const { formatMessage } = useVIntl()
455454
@@ -725,31 +724,28 @@ function runAnalytics() {
725724
.catch(() => {})
726725
})
727726
}
727+
728728
function toggleMobileMenu() {
729729
isMobileMenuOpen.value = !isMobileMenuOpen.value
730730
if (isMobileMenuOpen.value) {
731731
isBrowseMenuOpen.value = false
732732
}
733733
}
734+
734735
function toggleBrowseMenu() {
735736
isBrowseMenuOpen.value = !isBrowseMenuOpen.value
736737
737738
if (isBrowseMenuOpen.value) {
738739
isMobileMenuOpen.value = false
739740
}
740741
}
741-
function changeTheme() {
742-
updateTheme(
743-
DARK_THEMES.includes(app.$colorMode.value)
744-
? 'light'
745-
: cosmetics.value.preferredDarkTheme ?? 'dark',
746-
true
747-
)
748-
}
742+
743+
const $theme = useTheme()
744+
745+
const changeTheme = () => $theme.cycle()
749746
750747
function hideStagingBanner() {
751748
cosmetics.value.hideStagingBanner = true
752-
saveCosmetics()
753749
}
754750
</script>
755751

nuxt.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,15 @@ export default defineNuxtConfig({
370370
devtools: {
371371
enabled: true,
372372
},
373+
routeRules: {
374+
'/**': {
375+
headers: {
376+
'Accept-CH': 'Sec-CH-Prefers-Color-Scheme',
377+
Vary: 'Sec-CH-Prefers-Color-Scheme',
378+
'Critical-CH': 'Sec-CH-Prefers-Color-Scheme',
379+
},
380+
},
381+
},
373382
})
374383

375384
function getApiUrl() {

pages/app.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ useSeoMeta({
896896
</div>
897897
<div class="logo-banner">
898898
<svg
899-
v-if="$colorMode.value === 'light'"
899+
v-if="$theme.active === 'light'"
900900
viewBox="0 0 865 512"
901901
fill="none"
902902
xmlns="http://www.w3.org/2000/svg"

pages/auth/reset-password.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
ref="turnstile"
2727
v-model="token"
2828
class="turnstile"
29-
:options="{ theme: $colorMode.value === 'light' ? 'light' : 'dark' }"
29+
:options="{ theme: $theme.active === 'light' ? 'light' : 'dark' }"
3030
/>
3131

3232
<button class="btn btn-primary centered-btn" :disabled="!token" @click="recovery">

pages/auth/sign-in.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
ref="turnstile"
8686
v-model="token"
8787
class="turnstile"
88-
:options="{ theme: $colorMode.value === 'light' ? 'light' : 'dark' }"
88+
:options="{ theme: $theme.active === 'light' ? 'light' : 'dark' }"
8989
/>
9090

9191
<button

0 commit comments

Comments
 (0)