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; |
d07bca5da4ae | atagen | 2026-10-26 03:48 UTC | 12 | export let eager = false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | |
7643577c76bb | atagen | 2026-10-25 07:37 UTC | 14 | const density = typeof window === 'undefined' ? 1 : Math.min(window.devicePixelRatio || 1, 3); |
7643577c76bb | atagen | 2026-10-25 07:37 UTC | 15 | // rounding up to a 32px step keeps the number of cached variants per avatar small |
7643577c76bb | atagen | 2026-10-25 07:37 UTC | 16 | $: src = mediaUrl(mxc, Math.ceil((size * density) / 32) * 32); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | let failed = false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | $: if (mxc) failed = false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | $: px = `${size}px`; |
d07bca5da4ae | atagen | 2026-10-26 03:48 UTC | 21 | $: compact = size < 24; |
d07bca5da4ae | atagen | 2026-10-26 03:48 UTC | 22 | $: fontPx = `${compact ? Math.round(size * 0.56) : Math.max(10, Math.round(size * 0.36))}px`; |
d07bca5da4ae | atagen | 2026-10-26 03:48 UTC | 23 | $: initials = initialsOf(label || '#'); |
d07bca5da4ae | atagen | 2026-10-26 03:48 UTC | 24 | $: shown = compact ? ([...new Intl.Segmenter().segment(initials)][0]?.segment ?? '#') : initials; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 25 | $: hue = senderHue(seed ?? label ?? '#'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | </script> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | {#if src && !failed} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | <img |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | {src} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 31 | alt="" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 32 | aria-hidden="true" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 33 | width={size} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | height={size} |
d07bca5da4ae | atagen | 2026-10-26 03:48 UTC | 35 | loading={eager ? 'eager' : 'lazy'} |
d07bca5da4ae | atagen | 2026-10-26 03:48 UTC | 36 | decoding="async" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 37 | class="mxc-avatar" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 38 | class:circle={shape === 'circle'} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 39 | style={`width:${px};height:${px};`} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | on:error={() => (failed = true)} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | /> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | {:else} |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 43 | <span class="mxc-fallback" class:circle={shape === 'circle'} aria-hidden="true" style={`width:${px};height:${px};font-size:${fontPx};background:hsl(${hue} 32% 30%);`}> |
d07bca5da4ae | atagen | 2026-10-26 03:48 UTC | 44 | <span><EmojiText text={shown} /></span> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 45 | </span> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 46 | {/if} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 47 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 48 | <style> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 49 | .mxc-avatar { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 50 | flex: none; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 51 | object-fit: cover; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 52 | border-radius: var(--r-md); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 53 | background: var(--bg-soft); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 54 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 55 | .mxc-avatar.circle { border-radius: 50%; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 56 | .mxc-fallback { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | flex: none; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 58 | display: inline-grid; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | place-items: center; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 60 | border-radius: var(--r-md); |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 61 | color: #fff; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 62 | font-weight: 700; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 63 | letter-spacing: 0.02em; |
d07bca5da4ae | atagen | 2026-10-26 03:48 UTC | 64 | line-height: 1; |
d07bca5da4ae | atagen | 2026-10-26 03:48 UTC | 65 | overflow: hidden; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 66 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 67 | .mxc-fallback.circle { border-radius: 50%; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | </style> |