/
SponsorsGroup.vue
137 lines (124 loc) · 2.77 KB
/
SponsorsGroup.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<script lang="ts">
// shared data across instances so we load only once
let data = $ref<SponsorData>()
const base = `https://sponsors.vuejs.org`
const dataUrl = `${base}/vite.json`
</script>
<script setup lang="ts">
import { onMounted, onUnmounted } from 'vue'
interface Sponsor {
url: string
img: string
name: string
}
interface SponsorData {
special: Sponsor[]
platinum: Sponsor[]
platinum_china: Sponsor[]
gold: Sponsor[]
silver: Sponsor[]
bronze: Sponsor[]
}
const { tier, placement = 'aside' } = defineProps<{
tier: keyof SponsorData
placement?: 'aside' | 'page' | 'landing'
}>()
let container = $ref<HTMLElement>()
let visible = $ref(false)
onMounted(async () => {
// only render when entering view
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
visible = true
observer.disconnect()
}
},
{ rootMargin: '0px 0px 300px 0px' }
)
observer.observe(container)
onUnmounted(() => observer.disconnect())
// load data
if (!data) {
data = await (await fetch(dataUrl)).json()
}
})
</script>
<template>
<div
ref="container"
class="sponsor-container"
:class="[tier.startsWith('plat') ? 'platinum' : tier, placement]"
>
<template v-if="data && visible">
<a
v-for="{ url, img, name } of data[tier]"
class="sponsor-item"
:href="url"
target="_blank"
rel="sponsored noopener"
>
<picture v-if="img.endsWith('png')">
<source
type="image/avif"
:srcset="`${base}/images/${img.replace(/\.png$/, '.avif')}`"
/>
<img :src="`${base}/images/${img}`" :alt="name" />
</picture>
<img v-else :src="`${base}/images/${img}`" :alt="name" />
</a>
</template>
</div>
</template>
<style scoped>
.sponsor-container {
--max-width: 100%;
display: flex;
justify-content: space-evenly;
flex-wrap: wrap;
}
.sponsor-container.platinum {
--max-width: 260px;
}
.sponsor-container.gold {
--max-width: 160px;
}
.sponsor-container.silver {
--max-width: 140px;
}
.sponsor-item {
margin: 2px 0;
display: flex;
align-items: center;
border-radius: 2px;
transition: background-color 0.2s ease;
height: calc(var(--max-width) / 2 - 6px);
}
.sponsor-item.action {
font-size: 11px;
color: #999;
}
.sponsor-item img {
width: 100%;
max-width: calc(var(--max-width) - 30px);
max-height: calc(var(--max-width) / 2 - 20px);
margin: 10px 20px;
}
.special .sponsor-item {
height: 160px;
}
.special .sponsor-item img {
max-width: 300px;
max-height: 150px;
}
/* aside mode (on content pages) */
.sponsor-container.platinum.aside {
--max-width: 200px;
}
.sponsor-container.gold.aside {
--max-width: 124px;
}
.aside .sponsor-item {
margin: 0;
}
</style>