Skip to content

Commit

Permalink
docs: support using different sponsor images for dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 9, 2024
1 parent fd9de04 commit 504bc5f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
35 changes: 34 additions & 1 deletion docs/.vitepress/theme/composables/sponsor.ts
@@ -1,4 +1,4 @@
import { ref, onMounted } from 'vue'
import { ref, onMounted, onUnmounted } from 'vue'

interface Sponsors {
special: Sponsor[]
Expand All @@ -13,6 +13,10 @@ interface Sponsor {
name: string
img: string
url: string
/**
* Expects to also have an **inversed** image with `-dark` postfix.
*/

This comment has been minimized.

Copy link
@fatihhuylu0000

fatihhuylu0000 Apr 27, 2024

0x:/#123456789

hasDark?: true
}

// shared data across instances so we load only once.
Expand Down Expand Up @@ -58,12 +62,40 @@ const viteSponsors: Pick<Sponsors, 'special' | 'gold'> = {
name: 'Transloadit',
url: 'https://transloadit.com/?utm_source=vite&utm_medium=referral&utm_campaign=sponsorship&utm_content=website',
img: '/transloadit.svg',
hasDark: true,
},
],
}

function toggleDarkLogos() {
if (data.value) {
const isDark = document.documentElement.classList.contains('dark')
data.value.forEach(({ items }) => {
items.forEach((s: Sponsor) => {
if (s.hasDark) {
s.img = isDark
? s.img.replace(/(\.\w+)$/, '-dark$1')
: s.img.replace(/-dark(\.\w+)$/, '$1')
}
})
})
}
}

export function useSponsor() {
onMounted(async () => {
const ob = new MutationObserver((list) => {
for (const m of list) {
if (m.attributeName === 'class') {
toggleDarkLogos()
}
}
})
ob.observe(document.documentElement, { attributes: true })
onUnmounted(() => {
ob.disconnect()
})

if (data.value) {
return
}
Expand All @@ -72,6 +104,7 @@ export function useSponsor() {
const json = await result.json()

data.value = mapSponsors(json)
toggleDarkLogos()
})

return {
Expand Down
17 changes: 17 additions & 0 deletions docs/public/transloadit-dark.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

1 comment on commit 504bc5f

@fatihhuylu0000
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0x:/#123456789

Please sign in to comment.