Skip to content

Commit abd953b

Browse files
authored
Support dynamic loading of special sponsors (#1838)
1 parent 4805fe6 commit abd953b

File tree

3 files changed

+54
-42
lines changed

3 files changed

+54
-42
lines changed

.vitepress/theme/components/Home.vue

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
<script setup lang="ts">
2+
import { onMounted } from 'vue';
23
import NewsLetter from './NewsLetter.vue'
4+
import { load, data, base } from './sponsors';
35
import SponsorsGroup from './SponsorsGroup.vue';
46
import VueMasteryModal from './VueMasteryModal.vue';
7+
8+
onMounted(async () => {
9+
await load()
10+
})
511
</script>
612

713
<template>
@@ -35,21 +41,20 @@ import VueMasteryModal from './VueMasteryModal.vue';
3541
</p>
3642
</section>
3743

38-
<!-- TODO make dynamic based on data -->
3944
<section id="special-sponsor">
4045
<span>Special Sponsor</span>
41-
<a href="https://www.dcloud.io/hbuilderx.html?hmsr=vue-en&hmpl=&hmcu=&hmkw=&hmci=">
42-
<picture>
43-
<source type="image/avif" srcset="/images/sponsors/hbuilder.avif" />
44-
<img
45-
alt="hbuilder logo"
46-
width="97"
47-
height="36"
48-
src="/images/sponsors/hbuilder.png"
49-
/>
50-
</picture>
51-
</a>
52-
<span>Advanced IDE for Vue</span>
46+
<template v-if="data && data.special">
47+
<template v-for="{ url, img, name, description } of data.special">
48+
<a :href="url" target="_blank" rel="sponsored noopener">
49+
<picture v-if="img.endsWith('png')">
50+
<source type="image/avif" :srcset="`${base}/images/${img.replace(/\.png$/, '.avif')}`" />
51+
<img :src="`${base}/images/${img}`" :alt="name" />
52+
</picture>
53+
<img v-else :src="`${base}/images/${img}`" :alt="name" />
54+
</a>
55+
<span v-if="description">{{ description }}</span>
56+
</template>
57+
</template>
5358
</section>
5459

5560
<section id="highlights" class="vt-box-container">
@@ -181,13 +186,14 @@ html:not(.dark) .accent,
181186
font-weight: 500;
182187
font-size: 13px;
183188
vertical-align: middle;
184-
margin: 0 24px;
189+
margin-right: 24px;
185190
}
186191
187192
#special-sponsor img {
188193
display: inline-block;
189194
vertical-align: middle;
190195
height: 36px;
196+
margin-right: 24px;
191197
}
192198
193199
.dark #special-sponsor img {
@@ -279,4 +285,4 @@ html:not(.dark) .accent,
279285
font-size: 36px;
280286
}
281287
}
282-
</style>
288+
</style>

.vitepress/theme/components/SponsorsGroup.vue

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
1-
<script lang="ts">
2-
interface Sponsor {
3-
url: string
4-
img: string
5-
name: string
6-
}
7-
8-
interface SponsorData {
9-
special: Sponsor[]
10-
platinum: Sponsor[]
11-
platinum_china: Sponsor[]
12-
gold: Sponsor[]
13-
silver: Sponsor[]
14-
bronze: Sponsor[]
15-
}
16-
17-
// shared data across instances so we load only once
18-
let data = $ref<SponsorData>()
19-
let pending = false
20-
21-
const base = `https://sponsors.vuejs.org`
22-
</script>
23-
241
<script setup lang="ts">
252
import { onMounted, onUnmounted } from 'vue'
3+
import { SponsorData, data, base, load } from './sponsors';
264
275
const { tier, placement = 'aside' } = defineProps<{
286
tier: keyof SponsorData
@@ -47,10 +25,7 @@ onMounted(async () => {
4725
onUnmounted(() => observer.disconnect())
4826
4927
// load data
50-
if (!pending) {
51-
pending = true
52-
data = await (await fetch(`${base}/data.json`)).json()
53-
}
28+
await load()
5429
})
5530
</script>
5631

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// shared data across instances so we load only once
2+
3+
import { ref } from 'vue'
4+
5+
export interface Sponsor {
6+
url: string
7+
img: string
8+
name: string
9+
description?: string
10+
}
11+
12+
export interface SponsorData {
13+
special: Sponsor[]
14+
platinum: Sponsor[]
15+
platinum_china: Sponsor[]
16+
gold: Sponsor[]
17+
silver: Sponsor[]
18+
bronze: Sponsor[]
19+
}
20+
21+
export const data = ref<SponsorData>()
22+
export const pending = ref<boolean>(false)
23+
24+
export const base = `https://sponsors.vuejs.org`
25+
26+
export const load = async () => {
27+
if (!pending.value) {
28+
pending.value = true
29+
data.value = await (await fetch(`${base}/data.json`)).json()
30+
}
31+
}

0 commit comments

Comments
 (0)