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

Commit

Permalink
Rewrite cosmetics and theme preferences
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
brawaru committed Jun 16, 2024
1 parent bd49d6b commit cfa34f7
Show file tree
Hide file tree
Showing 21 changed files with 266 additions and 249 deletions.
5 changes: 3 additions & 2 deletions components/ui/charts/ChartDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
<div
:style="{
'--color-brand': isUsingProjectColors
? intToRgba(project.color, project.id, theme ?? undefined)
? intToRgba(project.color, project.id, $theme.active ?? undefined)
: getDefaultColor(project.id),
}"
class="legend__item__color"
Expand Down Expand Up @@ -309,7 +309,8 @@ import { UiChartsCompactChart as CompactChart, UiChartsChart as Chart } from '#c
import PaletteIcon from '~/assets/icons/palette.svg?component'
const router = useNativeRouter()
const theme = useTheme()
const $theme = useTheme()
const props = withDefaults(
defineProps<{
Expand Down
52 changes: 0 additions & 52 deletions composables/cosmetics.js

This file was deleted.

3 changes: 3 additions & 0 deletions composables/cosmetics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function useCosmetics() {
return useNuxtApp().$cosmetics
}
58 changes: 0 additions & 58 deletions composables/theme.js

This file was deleted.

3 changes: 3 additions & 0 deletions composables/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function useTheme() {
return useNuxtApp().$theme
}
22 changes: 9 additions & 13 deletions layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
:title="formatMessage(messages.changeTheme)"
@click="changeTheme"
>
<MoonIcon v-if="$colorMode.value === 'light'" aria-hidden="true" />
<MoonIcon v-if="$theme.active === 'light'" aria-hidden="true" />
<SunIcon v-else aria-hidden="true" />
</button>
<div
Expand Down Expand Up @@ -242,7 +242,7 @@
{{ formatMessage(commonMessages.settingsLabel) }}
</NuxtLink>
<button class="iconified-button" @click="changeTheme">
<MoonIcon v-if="$colorMode.value === 'light'" class="icon" />
<MoonIcon v-if="$theme.active === 'light'" class="icon" />
<SunIcon v-else class="icon" />
<span class="dropdown-item__text">
{{ formatMessage(messages.changeTheme) }}
Expand Down Expand Up @@ -403,7 +403,7 @@
{{ formatMessage(messages.getModrinthApp) }}
</nuxt-link>
<button class="iconified-button raised-button" @click="changeTheme">
<MoonIcon v-if="$colorMode.value === 'light'" aria-hidden="true" />
<MoonIcon v-if="$theme.active === 'light'" aria-hidden="true" />
<SunIcon v-else aria-hidden="true" />
{{ formatMessage(messages.changeTheme) }}
</button>
Expand Down Expand Up @@ -449,7 +449,6 @@ import ModalCreation from '~/components/ui/ModalCreation.vue'
import Avatar from '~/components/ui/Avatar.vue'
import { getProjectTypeMessage } from '~/utils/i18n-project-type.ts'
import { commonMessages } from '~/utils/common-messages.ts'
import { DARK_THEMES } from '~/composables/theme.js'
const { formatMessage } = useVIntl()
Expand Down Expand Up @@ -725,31 +724,28 @@ function runAnalytics() {
.catch(() => {})
})
}
function toggleMobileMenu() {
isMobileMenuOpen.value = !isMobileMenuOpen.value
if (isMobileMenuOpen.value) {
isBrowseMenuOpen.value = false
}
}
function toggleBrowseMenu() {
isBrowseMenuOpen.value = !isBrowseMenuOpen.value
if (isBrowseMenuOpen.value) {
isMobileMenuOpen.value = false
}
}
function changeTheme() {
updateTheme(
DARK_THEMES.includes(app.$colorMode.value)
? 'light'
: cosmetics.value.preferredDarkTheme ?? 'dark',
true
)
}
const $theme = useTheme()
const changeTheme = () => $theme.cycle()
function hideStagingBanner() {
cosmetics.value.hideStagingBanner = true
saveCosmetics()
}
</script>

Expand Down
9 changes: 9 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,15 @@ export default defineNuxtConfig({
devtools: {
enabled: true,
},
routeRules: {
'/**': {
headers: {
'Accept-CH': 'Sec-CH-Prefers-Color-Scheme',
Vary: 'Sec-CH-Prefers-Color-Scheme',
'Critical-CH': 'Sec-CH-Prefers-Color-Scheme',
},
},
},
})

function getApiUrl() {
Expand Down
2 changes: 1 addition & 1 deletion pages/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ useSeoMeta({
</div>
<div class="logo-banner">
<svg
v-if="$colorMode.value === 'light'"
v-if="$theme.active === 'light'"
viewBox="0 0 865 512"
fill="none"
xmlns="http://www.w3.org/2000/svg"
Expand Down
2 changes: 1 addition & 1 deletion pages/auth/reset-password.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
ref="turnstile"
v-model="token"
class="turnstile"
:options="{ theme: $colorMode.value === 'light' ? 'light' : 'dark' }"
:options="{ theme: $theme.active === 'light' ? 'light' : 'dark' }"
/>

<button class="btn btn-primary centered-btn" :disabled="!token" @click="recovery">
Expand Down
2 changes: 1 addition & 1 deletion pages/auth/sign-in.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
ref="turnstile"
v-model="token"
class="turnstile"
:options="{ theme: $colorMode.value === 'light' ? 'light' : 'dark' }"
:options="{ theme: $theme.active === 'light' ? 'light' : 'dark' }"
/>

<button
Expand Down
2 changes: 1 addition & 1 deletion pages/auth/sign-up.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
ref="turnstile"
v-model="token"
class="turnstile"
:options="{ theme: $colorMode.value === 'light' ? 'light' : 'dark' }"
:options="{ theme: $theme.active === 'light' ? 'light' : 'dark' }"
/>

<button
Expand Down
1 change: 0 additions & 1 deletion pages/collection/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,6 @@ function cycleSearchDisplayMode() {
cosmetics.value.searchDisplayMode.collection,
tags.value.projectViewModes
)
saveCosmetics()
}
let collection, refreshCollection, creator, projects, refreshProjects
Expand Down
4 changes: 2 additions & 2 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
<div class="blob-demonstration gradient-border">
<div class="launcher-view">
<img
v-if="$colorMode.value === 'light'"
v-if="$theme.active === 'light'"
src="https://cdn.modrinth.com/landing-new/launcher-light.webp"
alt="launcher graphic"
class="minecraft-screen"
Expand Down Expand Up @@ -407,7 +407,7 @@
</div>
<div class="logo-banner">
<svg
v-if="$colorMode.value === 'light'"
v-if="$theme.active === 'light'"
viewBox="0 0 865 512"
fill="none"
xmlns="http://www.w3.org/2000/svg"
Expand Down
1 change: 0 additions & 1 deletion pages/search/[searchProjectType].vue
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,6 @@ function cycleSearchDisplayMode() {
cosmetics.value.searchDisplayMode[projectType.value.id],
tags.value.projectViewModes
)
saveCosmetics()
setClosestMaxResults()
}
Expand Down
Loading

0 comments on commit cfa34f7

Please sign in to comment.