From eafb640077dc3123b3ab2243e6b05b282daff1da Mon Sep 17 00:00:00 2001 From: moebiusmania Date: Fri, 16 Aug 2024 10:51:16 +0200 Subject: [PATCH] feat: revert theme switcher to be client side --- README.md | 2 + app.vue | 97 +++++++++++++++++++++----------------- components/Header.vue | 6 +-- components/PostPreview.vue | 2 +- middleware/theme.global.ts | 15 ------ nuxt.config.ts | 4 +- 6 files changed, 61 insertions(+), 65 deletions(-) delete mode 100644 middleware/theme.global.ts diff --git a/README.md b/README.md index 69ee43a14..031beb03c 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ The contents are **my own personal thoughts and opinions**. Images and videos th - [rss](https://www.npmjs.com/package/rss) - utility to help create the RSS `.xml` file - [sitemap](https://www.npmjs.com/package/sitemap) - utility to help create the sitemap `.xml` file +I'm also integrating with [Nuxt Studio](https://nuxt.studio/) to play a bit with **visual editing**, which comes pretty handy when editing/writing content from devices like the iPad Mini. + ## Creating a new post With an NPM script: diff --git a/app.vue b/app.vue index 0d675f4c0..f3da2b4e5 100644 --- a/app.vue +++ b/app.vue @@ -2,63 +2,72 @@ import type { Ref } from "vue"; import { SITE_TITLE, SITE_DESCRIPTION } from "@/utils/config"; -const route = useRoute() - const themes: { [k: string]: string } = { dark: "sunset", light: "corporate", } -const current: string = route.query.theme as string; +const current: Ref = ref("light"); + +const theme: Ref = ref(themes[current.value] || themes.light); -const theme: Ref = ref(themes[current] || themes.light); +const changeTheme = () => { + current.value = current.value === "dark" ? "light" : "dark"; + theme.value = themes[current.value] || themes.light; + // updateHead() + updateDocument(current.value); +} -const updateHead = () => { - const htmlAttrs = { +const updateDocument = (update: string) => { + if (document) { + document.documentElement.setAttribute("data-theme", theme.value); + document.documentElement.className = update; + } +} + + +useHead({ + title: SITE_TITLE, + meta: [ + { name: "title", content: SITE_TITLE }, + { name: "description", content: SITE_DESCRIPTION }, + ], + // script: [ + // { + // src: "https://platform.twitter.com/widgets.js", + // crossorigin: "anonymous", + // defer: true, + // async: true, + // }, + // ], + link: [ + { rel: "alternate", type: "application/rss+xml", href: "/rss.xml" }, + { rel: "icon", type: "image/png", href: "/static/favicons/favicon.ico" }, + { + rel: "icon", + type: "image/png", + sizes: "16x16", + href: "/static/favicons/favicon-16x16.png", + }, + { + rel: "icon", + type: "image/png", + sizes: "32x32", + href: "/static/favicons/favicon-32x32.png", + }, + ], + htmlAttrs: { lang: "it-IT", "data-theme": theme.value, - class: theme.value === themes.dark ? "dark" : "light", - }; - - return useHead({ - title: SITE_TITLE, - meta: [ - { name: "title", content: SITE_TITLE }, - { name: "description", content: SITE_DESCRIPTION }, - ], - // script: [ - // { - // src: "https://platform.twitter.com/widgets.js", - // crossorigin: "anonymous", - // defer: true, - // async: true, - // }, - // ], - link: [ - { rel: "alternate", type: "application/rss+xml", href: "/rss.xml" }, - { rel: "icon", type: "image/png", href: "/static/favicons/favicon.ico" }, - { - rel: "icon", - type: "image/png", - sizes: "16x16", - href: "/static/favicons/favicon-16x16.png", - }, - { - rel: "icon", - type: "image/png", - sizes: "32x32", - href: "/static/favicons/favicon-32x32.png", - }, - ], - htmlAttrs, - }); -}; + class: document && document.documentElement.classList.contains("dark") ? "dark" : "light", + } +}); -updateHead(); +//updateDocument(current.value);