web/src/components/things/ServiceIcon.astro

130 lines · 3468 bytes

  1---
  2import { formatLink } from '@utils/parse-markdown';
  3
  4interface Props {
  5	name: string;
  6	url: string;
  7	icon?: string;
  8	size?: number;
  9	eager?: boolean; // Set on above-the-fold icons, so they aren't deferred
 10}
 11
 12const { name, url, icon, size = 40, eager = false } = Astro.props;
 13const src = icon || `https://icon.horse/icon/${formatLink(url)}`;
 14const horseSrc = `https://icon.horse/icon/${formatLink(url)}`;
 15const iconSize = Math.round(size * 0.45);
 16---
 17
 18<span class="service-icon-wrap" style={`width:${size}px;height:${size}px`}>
 19	<svg
 20		class="icon-state loading"
 21		xmlns="http://www.w3.org/2000/svg"
 22		viewBox="0 0 640 640"
 23	>
 24		<path
 25			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"
 26		></path>
 27	</svg>
 28	<svg
 29		class="icon-state broken"
 30		xmlns="http://www.w3.org/2000/svg"
 31		width={iconSize}
 32		height={iconSize}
 33		viewBox="0 0 320 512"
 34	>
 35		<path
 36			fill="currentColor"
 37			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"
 38		></path>
 39	</svg>
 40	<img
 41		width={size}
 42		height={size}
 43		loading={eager ? 'eager' : 'lazy'}
 44		decoding="async"
 45		class="service-icon"
 46		alt={name}
 47		data-horse={horseSrc}
 48		src={src}
 49		onload="this.parentNode.classList.add('loaded')"
 50		onerror={`if(!this.src.includes('icon.horse')){this.src=this.dataset.horse}else{this.remove();this.parentNode?.classList.add('broken')}`}
 51	/>
 52</span>
 53
 54<script>
 55	document.addEventListener('astro:page-load', () => {
 56		const obs = new IntersectionObserver((entries) => {
 57			entries.forEach((e) => {
 58				if (!e.isIntersecting) return;
 59				obs.unobserve(e.target);
 60				setTimeout(() => {
 61					const wrap = e.target as HTMLElement;
 62					if (
 63						wrap.classList.contains('loaded') ||
 64						wrap.classList.contains('broken')
 65					)
 66						return;
 67					const img = wrap.querySelector('img');
 68					if (img) img.remove();
 69					wrap.classList.add('broken');
 70				}, 8000);
 71			});
 72		});
 73		document
 74			.querySelectorAll('.service-icon-wrap')
 75			.forEach((el) => obs.observe(el));
 76	});
 77</script>
 78
 79<style is:global>
 80	.service-icon-wrap {
 81		display: inline-flex;
 82		align-items: center;
 83		justify-content: center;
 84		flex-shrink: 0;
 85		border-radius: var(--curve-sm);
 86		position: relative;
 87		&:not(.loaded) {
 88			border: var(--border-heavy);
 89			box-shadow: var(--shadow-xs);
 90			background: var(--accent-3-translucent);
 91		}
 92		.icon-state {
 93			color: var(--accent-3-text);
 94		}
 95		.icon-state.broken {
 96			display: none;
 97		}
 98		.icon-state.loading {
 99			animation: spin 1s linear infinite;
100			animation-timing-function: cubic-bezier(0, 0, 0.2, 0);
101			fill: var(--accent-3);
102			opacity: 0.5;
103		}
104		.service-icon {
105			position: absolute;
106			inset: 0;
107			object-fit: contain;
108			border-radius: var(--curve-sm);
109			font-size: 0;
110		}
111		&.loaded {
112			.icon-state {
113				display: none;
114			}
115		}
116		&.broken {
117			.icon-state.loading {
118				display: none;
119			}
120			.icon-state.broken {
121				display: block;
122			}
123		}
124	}
125	@keyframes spin {
126		to {
127			transform: rotate(360deg);
128		}
129	}
130</style>