1- import { ref , onMounted } from 'vue'
1+ import { ref , onMounted , onUnmounted } from 'vue'
22
33interface Sponsors {
44 special : Sponsor [ ]
@@ -13,6 +13,10 @@ interface Sponsor {
1313 name : string
1414 img : string
1515 url : string
16+ /**
17+ * Expects to also have an **inversed** image with `-dark` postfix.
18+ */
19+ hasDark ?: true
1620}
1721
1822// shared data across instances so we load only once.
@@ -58,12 +62,40 @@ const viteSponsors: Pick<Sponsors, 'special' | 'gold'> = {
5862 name : 'Transloadit' ,
5963 url : 'https://transloadit.com/?utm_source=vite&utm_medium=referral&utm_campaign=sponsorship&utm_content=website' ,
6064 img : '/transloadit.svg' ,
65+ hasDark : true ,
6166 } ,
6267 ] ,
6368}
6469
70+ function toggleDarkLogos ( ) {
71+ if ( data . value ) {
72+ const isDark = document . documentElement . classList . contains ( 'dark' )
73+ data . value . forEach ( ( { items } ) => {
74+ items . forEach ( ( s : Sponsor ) => {
75+ if ( s . hasDark ) {
76+ s . img = isDark
77+ ? s . img . replace ( / ( \. \w + ) $ / , '-dark$1' )
78+ : s . img . replace ( / - d a r k ( \. \w + ) $ / , '$1' )
79+ }
80+ } )
81+ } )
82+ }
83+ }
84+
6585export function useSponsor ( ) {
6686 onMounted ( async ( ) => {
87+ const ob = new MutationObserver ( ( list ) => {
88+ for ( const m of list ) {
89+ if ( m . attributeName === 'class' ) {
90+ toggleDarkLogos ( )
91+ }
92+ }
93+ } )
94+ ob . observe ( document . documentElement , { attributes : true } )
95+ onUnmounted ( ( ) => {
96+ ob . disconnect ( )
97+ } )
98+
6799 if ( data . value ) {
68100 return
69101 }
@@ -72,6 +104,7 @@ export function useSponsor() {
72104 const json = await result . json ( )
73105
74106 data . value = mapSponsors ( json )
107+ toggleDarkLogos ( )
75108 } )
76109
77110 return {
0 commit comments