Commit bc6404edaa

bc6404edaa5fc4b18cf95dc3e95cdfcd4833b707

parent: 5cbd851c3f

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-07-13 12:20 UTC

lazyicon

Layout: unified · split

docs/.vitepress/theme/components/extrepo/LazyIcon.vue added +39
@@ -0,0 +1,39 @@
1<script setup lang="ts">
2import { onBeforeUnmount, onMounted, ref } from 'vue'
3import { scheduleIconLoad } from './iconQueue'
4
5const props = defineProps<{
6 src?: string
7 alt: string
8}>()
9
10const rootRef = ref<HTMLElement>()
11const loaded = ref(false)
12let observer: IntersectionObserver | undefined
13
14onMounted(() => {
15 const el = rootRef.value
16 if (!el || !props.src) return
17 // Preload a bit before the card is actually visible so it's ready by the time it scrolls in.
18 observer = new IntersectionObserver((entries) => {
19 if (!entries.some(entry => entry.isIntersecting)) return
20 observer?.disconnect()
21 scheduleIconLoad(() => new Promise<void>((resolve) => {
22 const img = new Image()
23 img.onload = img.onerror = () => {
24 loaded.value = true
25 resolve()
26 }
27 img.src = props.src!
28 }))
29 }, { rootMargin: '200px' })
30 observer.observe(el)
31})
32
33onBeforeUnmount(() => observer?.disconnect())
34</script>
35
36<template>
37 <img v-if="loaded" ref="rootRef" :src="src" :alt="alt" />
38 <span v-else ref="rootRef" class="ext-site-icon-fallback" :class="{ 'i-lucide:globe': !src, 'ext-site-icon-loading': src }" />
39</template>
docs/.vitepress/theme/components/extrepo/SiteCell.vue +6 −2
@@ -1,5 +1,6 @@
11<script setup lang="ts">
22import { langFlagClass, ratingIcon, ratingTitle } from './helpers'
3import LazyIcon from './LazyIcon.vue'
34import type { RepoSite } from './types'
45
56defineProps<{
@@ -17,8 +18,7 @@ defineProps<{
1718 :rel="site.url ? 'noopener noreferrer' : undefined"
1819 >
1920 <div class="ext-site-row">
20 <img v-if="site.icon" class="ext-site-icon" :src="site.icon" :alt="site.name" loading="lazy" />
21 <span v-else class="ext-site-icon ext-site-icon-fallback i-lucide:globe" />
21 <LazyIcon class="ext-site-icon" :src="site.icon" :alt="site.name" />
2222 <span class="ext-site-name-text" :title="site.name">{{ site.name }}</span>
2323 <span v-if="site.rating !== 'safe'" class="ext-site-nsfw" :class="ratingIcon(site.rating)" :title="ratingTitle(site.rating)" />
2424 <span class="ext-site-lang" :class="langFlagClass(site.lang)" :title="site.lang" />
@@ -90,6 +90,10 @@ defineProps<{
9090 color: var(--vp-c-text-3);
9191}
9292
93.ext-site-icon-loading {
94 background-color: var(--vp-c-bg-mute, var(--vp-c-bg-soft));
95}
96
9397.ext-site-name-text {
9498 flex: 1;
9599 min-width: 0;
docs/.vitepress/theme/components/extrepo/SiteGroupCell.vue +6 −2
@@ -1,6 +1,7 @@
11<script setup lang="ts">
22import { computed } from 'vue'
33import { ratingIcon, ratingTitle } from './helpers'
4import LazyIcon from './LazyIcon.vue'
45import type { RepoSite } from './types'
56
67const props = defineProps<{
@@ -27,8 +28,7 @@ const highestRated = computed(() => props.sites.find(site => site.rating === 'ns
2728 @click="$emit('toggle')"
2829 >
2930 <div class="ext-site-row">
30 <img v-if="primary.icon" class="ext-site-icon" :src="primary.icon" :alt="primary.name" loading="lazy" />
31 <span v-else class="ext-site-icon ext-site-icon-fallback i-lucide:globe" />
31 <LazyIcon class="ext-site-icon" :src="primary.icon" :alt="primary.name" />
3232 <span class="ext-site-name-text" :title="primary.name">{{ primary.name }}</span>
3333 <span
3434 v-if="highestRated"
@@ -92,6 +92,10 @@ const highestRated = computed(() => props.sites.find(site => site.rating === 'ns
9292 color: var(--vp-c-text-3);
9393}
9494
95.ext-site-icon-loading {
96 background-color: var(--vp-c-bg-mute, var(--vp-c-bg-soft));
97}
98
9599.ext-site-name-text {
96100 flex: 1;
97101 min-width: 0;
docs/.vitepress/theme/components/extrepo/iconQueue.ts added +26
@@ -0,0 +1,26 @@
1const MAX_CONCURRENT_MOBILE = 6
2const MAX_CONCURRENT_DESKTOP = 24
3
4function maxConcurrent(): number {
5 if (typeof window === 'undefined') return MAX_CONCURRENT_DESKTOP
6 return window.matchMedia('(max-width: 767px)').matches ? MAX_CONCURRENT_MOBILE : MAX_CONCURRENT_DESKTOP
7}
8
9let active = 0
10const queue: (() => Promise<void>)[] = []
11
12function pump() {
13 while (active < maxConcurrent() && queue.length) {
14 const job = queue.shift()!
15 active++
16 job().finally(() => {
17 active--
18 pump()
19 })
20 }
21}
22
23export function scheduleIconLoad(load: () => Promise<void>) {
24 queue.push(load)
25 pump()
26}