mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 12:14:41 +01:00
docs: change favicon color on theme color change (#3917)
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
55
docs/app/composables/useFaviconFromTheme.ts
Normal file
55
docs/app/composables/useFaviconFromTheme.ts
Normal 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()
|
||||
})
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user