web/src/components/things/ServiceIcon.astro
128 lines · 3330 bytes
1---
2import { formatLink } from '@utils/parse-markdown';
3
4interface Props {
5 name: string;
6 url: string;
7 icon?: string;
8 size?: number;
9}
10
11const { name, url, icon, size = 40 } = Astro.props;
12const src = icon || `https://icon.horse/icon/${formatLink(url)}`;
13const horseSrc = `https://icon.horse/icon/${formatLink(url)}`;
14const iconSize = Math.round(size * 0.45);
15---
16
17<span class="service-icon-wrap" style={`width:${size}px;height:${size}px`}>
18 <svg
19 class="icon-state loading"
20 xmlns="http://www.w3.org/2000/svg"
21 viewBox="0 0 640 640"
22 >
23 <path
24 d="M320.3 93.7C322.8 76.2 339 64.1 356.5 66.6C480.6 84.4 576 191 576 320C576 353.5 569.5 385.6 557.8 415.1C551.2 431.5 532.6 439.5 516.2 432.9C499.8 426.3 491.8 407.7 498.4 391.3C507.2 369.3 512.1 345.3 512.1 320C512.1 223.3 440.6 143.3 347.6 129.9C330.1 127.4 318 111.2 320.5 93.7z"
25 ></path>
26 </svg>
27 <svg
28 class="icon-state broken"
29 xmlns="http://www.w3.org/2000/svg"
30 width={iconSize}
31 height={iconSize}
32 viewBox="0 0 320 512"
33 >
34 <path
35 fill="currentColor"
36 d="M80 160c0-35.3 28.7-64 64-64h32c35.3 0 64 28.7 64 64v3.6c0 21.8-11.1 42.1-29.4 53.8l-42.2 27.1A87.98 87.98 0 0 0 128 320v16c0 13.3 10.7 24 24 24s24-10.7 24-24v-16c0-17.4 8.8-33.7 23.5-43.1l42.2-27.1A111.97 111.97 0 0 0 288 163.6V160c0-61.9-50.1-112-112-112h-32C82.1 48 32 98.1 32 160c0 13.3 10.7 24 24 24s24-10.7 24-24zm80 288a32 32 0 1 0 0-64 32 32 0 1 0 0 64z"
37 ></path>
38 </svg>
39 <img
40 width={size}
41 height={size}
42 loading="lazy"
43 decoding="async"
44 class="service-icon"
45 alt={name}
46 data-horse={horseSrc}
47 src={src}
48 onload="this.parentNode.classList.add('loaded')"
49 onerror={`if(!this.src.includes('icon.horse')){this.src=this.dataset.horse}else{this.remove();this.parentNode?.classList.add('broken')}`}
50 />
51</span>
52
53<script>
54 document.addEventListener('astro:page-load', () => {
55 const obs = new IntersectionObserver((entries) => {
56 entries.forEach((e) => {
57 if (!e.isIntersecting) return;
58 obs.unobserve(e.target);
59 setTimeout(() => {
60 const wrap = e.target as HTMLElement;
61 if (
62 wrap.classList.contains('loaded') ||
63 wrap.classList.contains('broken')
64 )
65 return;
66 const img = wrap.querySelector('img');
67 if (img) img.remove();
68 wrap.classList.add('broken');
69 }, 8000);
70 });
71 });
72 document
73 .querySelectorAll('.service-icon-wrap')
74 .forEach((el) => obs.observe(el));
75 });
76</script>
77
78<style is:global>
79 .service-icon-wrap {
80 display: inline-flex;
81 align-items: center;
82 justify-content: center;
83 flex-shrink: 0;
84 border-radius: var(--curve-sm);
85 position: relative;
86 &:not(.loaded) {
87 border: var(--border-heavy);
88 box-shadow: var(--shadow-xs);
89 background: var(--accent-3-translucent);
90 }
91 .icon-state {
92 color: var(--accent-3);
93 }
94 .icon-state.broken {
95 display: none;
96 }
97 .icon-state.loading {
98 animation: spin 1s linear infinite;
99 animation-timing-function: cubic-bezier(0, 0, 0.2, 0);
100 fill: var(--accent-3);
101 opacity: 0.5;
102 }
103 .service-icon {
104 position: absolute;
105 inset: 0;
106 border-radius: var(--curve-sm);
107 font-size: 0;
108 }
109 &.loaded {
110 .icon-state {
111 display: none;
112 }
113 }
114 &.broken {
115 .icon-state.loading {
116 display: none;
117 }
118 .icon-state.broken {
119 display: block;
120 }
121 }
122 }
123 @keyframes spin {
124 to {
125 transform: rotate(360deg);
126 }
127 }
128</style>