docs: change favicon color on theme color change (#3917)

This commit is contained in:
Soryn
2025-04-16 17:50:23 +03:00
committed by GitHub
parent 47cdc2e1d8
commit 02b6b38a56
3 changed files with 61 additions and 2 deletions

View File

@@ -23,7 +23,7 @@ useHead({
{ key: 'theme-color', name: 'theme-color', content: color }
],
link: [
{ rel: 'icon', type: 'image/svg+xml', href: '/icon.svg' },
// { rel: 'icon', type: 'image/svg+xml', href: '/icon.svg' },
{ rel: 'canonical', href: `https://ui.nuxt.com${withoutTrailingSlash(route.path)}` }
],
style: [
@@ -40,6 +40,8 @@ useServerSeoMeta({
twitterCard: 'summary_large_image'
})
useFaviconFromTheme()
const { frameworks, modules } = useSharedData()
const { mappedNavigation, filteredNavigation } = useContentNavigation(navigation)

View File

@@ -0,0 +1,55 @@
import { useColorMode } from '@vueuse/core'
import { onMounted, watch } from 'vue'
import FaviconSvg from 'public/icon.svg?raw'
export function useFaviconFromTheme() {
const colorMode = useColorMode()
function generateFaviconSvg(color: string) {
const parser = new DOMParser()
const doc = parser.parseFromString(FaviconSvg, 'image/svg+xml')
const svg = doc.documentElement
svg.querySelectorAll('path').forEach((path) => {
path.setAttribute('fill', color)
})
return new XMLSerializer().serializeToString(svg)
}
function updateFavicon() {
const root = document.documentElement
const color = getComputedStyle(root).getPropertyValue('--ui-primary').trim() || '#00DC82'
const svg = generateFaviconSvg(color)
const encoded = `data:image/svg+xml,${encodeURIComponent(svg)}`
useFavicon(encoded)
}
function setupMutationObserver() {
const styleTag = document.getElementById('nuxt-ui-colors')
if (!styleTag) return
const observer = new MutationObserver(() => {
updateFavicon()
})
observer.observe(styleTag, {
characterData: true,
subtree: true,
childList: true
})
}
onMounted(() => {
watch(colorMode, () => {
updateFavicon()
}, {
immediate: true,
flush: 'post'
})
setupMutationObserver()
})
}

View File

@@ -26,7 +26,7 @@ useHead({
{ key: 'theme-color', name: 'theme-color', content: color }
],
link: [
{ rel: 'icon', type: 'image/svg+xml', href: '/icon.svg' }
// { rel: 'icon', type: 'image/svg+xml', href: '/icon.svg' }
],
style: [
{ innerHTML: radius, id: 'nuxt-ui-radius', tagPriority: -2 },
@@ -47,6 +47,8 @@ useServerSeoMeta({
twitterCard: 'summary_large_image'
})
useFaviconFromTheme()
const { frameworks, modules } = useSharedData()
const { mappedNavigation, filteredNavigation } = useContentNavigation(navigation)