Rewrite cosmetics and theme preferences (#1328)

- 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.

- For the future, the theme plugin now supports additional light themes.
  Light theme preferences are currently not stored, but this can easily
  be fixed if the need arises.

- 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.

- Compared to the previous attempt, this commit has been improved to be
  more robust in cases where the theme cookie contains invalid values.

Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
Sasha Sorokin
2024-07-31 21:42:55 +02:00
committed by GitHub
parent 984da93a7b
commit 1e0d7c7e28
25 changed files with 379 additions and 252 deletions

View File

@@ -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"

View File

@@ -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">

View File

@@ -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

View File

@@ -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

View File

@@ -487,7 +487,6 @@ function cycleSearchDisplayMode() {
cosmetics.value.searchDisplayMode.collection,
tags.value.projectViewModes,
);
saveCosmetics();
}
let collection, refreshCollection, creator, projects, refreshProjects;

View File

@@ -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"
@@ -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"

View File

@@ -763,7 +763,6 @@ function cycleSearchDisplayMode() {
cosmetics.value.searchDisplayMode[projectType.value.id],
tags.value.projectViewModes,
);
saveCosmetics();
setClosestMaxResults();
}

View File

@@ -21,7 +21,7 @@
v-for="option in themeOptions"
:key="option"
class="preview-radio button-base"
:class="{ selected: theme.preference === option }"
:class="{ selected: theme.preferred === option }"
@click="() => updateColorTheme(option)"
>
<div class="preview" :class="`${option === 'system' ? systemTheme : option}-mode`">
@@ -32,16 +32,16 @@
</div>
</div>
<div class="label">
<RadioButtonChecked v-if="theme.preference === option" class="radio" />
<RadioButtonChecked v-if="theme.preferred === option" class="radio" />
<RadioButtonIcon v-else class="radio" />
{{ colorTheme[option] ? formatMessage(colorTheme[option]) : option }}
<SunIcon
v-if="'light' === option"
v-if="theme.preferences.light === option"
v-tooltip="formatMessage(colorTheme.preferredLight)"
class="theme-icon"
/>
<MoonIcon
v-else-if="(cosmetics.preferredDarkTheme ?? 'dark') === option"
v-else-if="theme.preferences.dark === option"
v-tooltip="formatMessage(colorTheme.preferredDark)"
class="theme-icon"
/>
@@ -153,7 +153,6 @@
v-model="cosmetics.advancedRendering"
class="switch stylized-toggle"
type="checkbox"
@change="saveCosmetics"
/>
</div>
<div class="adjacent-input small">
@@ -170,7 +169,6 @@
v-model="cosmetics.externalLinksNewTab"
class="switch stylized-toggle"
type="checkbox"
@change="saveCosmetics"
/>
</div>
<div class="adjacent-input small">
@@ -187,7 +185,6 @@
v-model="cosmetics.hideModrinthAppPromos"
class="switch stylized-toggle"
type="checkbox"
@change="saveCosmetics"
/>
</div>
<div class="adjacent-input small">
@@ -204,7 +201,6 @@
v-model="cosmetics.searchLayout"
class="switch stylized-toggle"
type="checkbox"
@change="saveCosmetics"
/>
</div>
<div class="adjacent-input small">
@@ -221,19 +217,19 @@
v-model="cosmetics.projectLayout"
class="switch stylized-toggle"
type="checkbox"
@change="saveCosmetics"
/>
</div>
</section>
</div>
</template>
<script setup>
import { CodeIcon, RadioButtonIcon, RadioButtonChecked, SunIcon, MoonIcon } from "@modrinth/assets";
<script setup lang="ts">
import { CodeIcon, MoonIcon, RadioButtonChecked, RadioButtonIcon, SunIcon } from "@modrinth/assets";
import { Button } from "@modrinth/ui";
import { formatProjectType } from "~/plugins/shorthands.js";
import MessageBanner from "~/components/ui/MessageBanner.vue";
import { DARK_THEMES } from "~/composables/theme.js";
import type { DisplayLocation } from "~/plugins/cosmetics";
import { formatProjectType } from "~/plugins/shorthands.js";
import { isDarkTheme, type Theme } from "~/plugins/theme/index.ts";
useHead({
title: "Display settings - Modrinth",
@@ -330,6 +326,10 @@ const projectListLayouts = defineMessages({
id: "settings.display.project-list-layouts.user",
defaultMessage: "User profile pages",
},
collection: {
id: "settings.display.project-list.layouts.collection",
defaultMessage: "Collection",
},
});
const toggleFeatures = defineMessages({
@@ -390,45 +390,41 @@ const cosmetics = useCosmetics();
const flags = useFeatureFlags();
const tags = useTags();
const systemTheme = ref("light");
const theme = useTheme();
// On the server the value of native theme can be 'unknown'. To hydrate
// correctly, we need to make sure we aren't using 'unknown' and values between
// server and client renders are in sync.
const serverSystemTheme = useState(() => {
const theme_ = theme.native;
if (theme_ === "unknown") return "light";
return theme_;
});
const systemTheme = useMountedValue((mounted): Theme => {
const systemTheme_ = mounted ? theme.native : serverSystemTheme.value;
return systemTheme_ === "light" ? theme.preferences.light : theme.preferences.dark;
});
const themeOptions = computed(() => {
const options = ["system", "light", "dark", "oled"];
if (flags.value.developerMode || theme.value.preference === "retro") {
const options: ("system" | Theme)[] = ["system", "light", "dark", "oled"];
if (flags.value.developerMode || theme.preferred === "retro") {
options.push("retro");
}
return options;
});
onMounted(() => {
updateSystemTheme();
window.matchMedia("(prefers-color-scheme: light)").addEventListener("change", (event) => {
setSystemTheme(event.matches);
});
});
function updateSystemTheme() {
const query = window.matchMedia("(prefers-color-scheme: light)");
setSystemTheme(query.matches);
}
function setSystemTheme(light) {
if (light) {
systemTheme.value = "light";
} else {
systemTheme.value = cosmetics.value.preferredDarkTheme ?? "dark";
function updateColorTheme(value: Theme | "system") {
if (value !== "system") {
if (isDarkTheme(value)) {
theme.preferences.dark = value;
} else {
theme.preferences.light = value;
}
}
}
function updateColorTheme(value) {
if (DARK_THEMES.includes(value)) {
cosmetics.value.preferredDarkTheme = value;
saveCosmetics();
updateSystemTheme();
}
updateTheme(value, true);
theme.preferred = value;
}
function disableDeveloperMode() {
@@ -445,16 +441,18 @@ function disableDeveloperMode() {
const listTypes = computed(() => {
const types = tags.value.projectTypes.map((type) => {
return {
id: type.id,
id: type.id as DisplayLocation,
name: formatProjectType(type.id) + "s",
display: "the " + formatProjectType(type.id).toLowerCase() + "s search page",
};
});
types.push({
id: "user",
id: "user" as DisplayLocation,
name: "User profiles",
display: "user pages",
});
return types;
});
</script>

View File

@@ -489,7 +489,6 @@ function cycleSearchDisplayMode() {
cosmetics.value.searchDisplayMode.user,
tags.value.projectViewModes,
);
saveCosmetics();
}
</script>
<script>