973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 1 | <script lang="ts"> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 2 | import { mediaUrl } from './api'; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 3 | import { initialsOf, senderHue } from './format'; |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 4 | import EmojiText from './EmojiText.svelte'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 5 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 6 | export let mxc: string | null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 7 | export let label: string; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 8 | export let size = 34; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 9 | /** 'circle' for people, 'rounded' for rooms/spaces */ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | export let shape: 'circle' | 'rounded' = 'circle'; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 11 | export let seed: string | null = null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 12 | |
7643577c76bb | atagen | 2026-10-25 07:37 UTC | 13 | const density = typeof window === 'undefined' ? 1 : Math.min(window.devicePixelRatio || 1, 3); |
7643577c76bb | atagen | 2026-10-25 07:37 UTC | 14 | // rounding up to a 32px step keeps the number of cached variants per avatar small |
7643577c76bb | atagen | 2026-10-25 07:37 UTC | 15 | $: src = mediaUrl(mxc, Math.ceil((size * density) / 32) * 32); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | let failed = false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | $: if (mxc) failed = false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | $: px = `${size}px`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | $: fontPx = `${Math.max(10, Math.round(size * 0.36))}px`; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 21 | $: hue = senderHue(seed ?? label ?? '#'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | </script> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | {#if src && !failed} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | <img |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | {src} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | alt="" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | aria-hidden="true" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | width={size} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | height={size} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 31 | loading="lazy" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 32 | class="mxc-avatar" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 33 | class:circle={shape === 'circle'} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | style={`width:${px};height:${px};`} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 35 | on:error={() => (failed = true)} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 36 | /> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 37 | {:else} |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 38 | <span class="mxc-fallback" class:circle={shape === 'circle'} aria-hidden="true" style={`width:${px};height:${px};font-size:${fontPx};background:hsl(${hue} 32% 30%);`}> |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 39 | <span><EmojiText text={initialsOf(label || '#')} /></span> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | </span> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | {/if} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 43 | <style> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 44 | .mxc-avatar { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 45 | flex: none; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 46 | object-fit: cover; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 47 | border-radius: var(--r-md); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 48 | background: var(--bg-soft); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 49 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 50 | .mxc-avatar.circle { border-radius: 50%; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 51 | .mxc-fallback { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 52 | flex: none; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 53 | display: inline-grid; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 54 | place-items: center; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 55 | border-radius: var(--r-md); |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 56 | color: #fff; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | font-weight: 700; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 58 | letter-spacing: 0.02em; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 60 | .mxc-fallback.circle { border-radius: 50%; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 61 | </style> |