Skip to content

Commit

Permalink
Rewrite cosmetics and theme preferences (#1292)
Browse files Browse the repository at this point in the history
- Cosmetics and theme preferences are now only stored in cookies instead
  of a combination of both cookies and state.

- The theme plugin now supports client hints. This allows the server
  to render a page using the client-preferred theme provided it supplies
  this information (any browser other than Firefox), helping to avoid an
  annoying flash while the page is hydrating.

- The previous workaround using the Nitro plugin has been removed. Its
  functionality is now handled by the Nuxt theme plugin with cleaner
  code.

- All pages and components now use the new plugins.
  • Loading branch information
brawaru authored Jul 13, 2024
1 parent ce42502 commit 8704d3a
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 248 deletions.
8 changes: 8 additions & 0 deletions apps/frontend/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ export default defineNuxtConfig({
autoprefixer: {},
},
},
routeRules: {
"/**": {
headers: {
"Accept-CH": "Sec-CH-Prefers-Color-Scheme",
"Critical-CH": "Sec-CH-Prefers-Color-Scheme",
},
},
},
compatibilityDate: "2024-07-03",
});

Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/components/ui/charts/ChartDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,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
52 changes: 0 additions & 52 deletions apps/frontend/src/composables/cosmetics.js

This file was deleted.

7 changes: 7 additions & 0 deletions apps/frontend/src/composables/nuxt-accessors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function useTheme() {
return useNuxtApp().$theme;
}

export function useCosmetics() {
return useNuxtApp().$cosmetics;
}
58 changes: 0 additions & 58 deletions apps/frontend/src/composables/theme.js

This file was deleted.

14 changes: 14 additions & 0 deletions apps/frontend/src/composables/vue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Creates a computed reference that uses a provide getter function called with an argument representing the current mount state of the component.
* @param getter A getter function that will run with `mounted` argument representing whether or not the component is mounted.
* @returns A computed reference that changes when component becomes mounted or unmounted.
*/
export function useMountedValue<T>(getter: (isMounted: boolean) => T) {
const mounted = ref(getCurrentInstance()?.isMounted ?? false);

onMounted(() => (mounted.value = true));

onUnmounted(() => (mounted.value = false));

return computed(() => getter(mounted.value));
}
18 changes: 5 additions & 13 deletions apps/frontend/src/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 @@ -738,18 +737,11 @@ function toggleBrowseMenu() {
isMobileMenuOpen.value = false;
}
}
function changeTheme() {
updateTheme(
DARK_THEMES.includes(app.$colorMode.value)
? "light"
: cosmetics.value.preferredDarkTheme ?? "dark",
true,
);
}
const { cycle: changeTheme } = useTheme();
function hideStagingBanner() {
cosmetics.value.hideStagingBanner = true;
saveCosmetics();
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/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 apps/frontend/src/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 apps/frontend/src/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 apps/frontend/src/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 apps/frontend/src/pages/collection/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,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 apps/frontend/src/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 apps/frontend/src/pages/search/[searchProjectType].vue
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,6 @@ function cycleSearchDisplayMode() {
cosmetics.value.searchDisplayMode[projectType.value.id],
tags.value.projectViewModes,
);
saveCosmetics();
setClosestMaxResults();
}

Expand Down
Loading

0 comments on commit 8704d3a

Please sign in to comment.