atagen

malice

public
Star0
Watch0
ed3363baaf0183507124a2397eaa37604e1a0fa0
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/src/lib/MxcAvatar.svelteSvelte59 lines1.5 KB
1
<script lang="ts">
2
  import { mediaUrl } from './api';
3
  import { initialsOf, senderHue } from './format';
4
  import EmojiText from './EmojiText.svelte';
5
6
  export let mxc: string | null;
7
  export let label: string;
8
  export let size = 34;
9
  /** 'circle' for people, 'rounded' for rooms/spaces */
10
  export let shape: 'circle' | 'rounded' = 'circle';
11
  export let seed: string | null = null;
12
13
  $: src = mediaUrl(mxc);
14
15
  let failed = false;
16
  $: if (mxc) failed = false;
17
  $: px = `${size}px`;
18
  $: fontPx = `${Math.max(10, Math.round(size * 0.36))}px`;
19
  $: hue = senderHue(seed ?? label ?? '#');
20
</script>
21
22
{#if src && !failed}
23
  <img
24
    {src}
25
    alt=""
26
    aria-hidden="true"
27
    width={size}
28
    height={size}
29
    loading="lazy"
30
    class="mxc-avatar"
31
    class:circle={shape === 'circle'}
32
    style={`width:${px};height:${px};`}
33
    on:error={() => (failed = true)}
34
  />
35
{:else}
36
  <span class="mxc-fallback" class:circle={shape === 'circle'} aria-hidden="true" style={`width:${px};height:${px};font-size:${fontPx};background:hsl(${hue} 32% 30%);`}>
37
    <span><EmojiText text={initialsOf(label || '#')} /></span>
38
  </span>
39
{/if}
40
41
<style>
42
  .mxc-avatar {
43
    flex: none;
44
    object-fit: cover;
45
    border-radius: var(--r-md);
46
    background: var(--bg-soft);
47
  }
48
  .mxc-avatar.circle { border-radius: 50%; }
49
  .mxc-fallback {
50
    flex: none;
51
    display: inline-grid;
52
    place-items: center;
53
    border-radius: var(--r-md);
54
    color: #fff;
55
    font-weight: 700;
56
    letter-spacing: 0.02em;
57
  }
58
  .mxc-fallback.circle { border-radius: 50%; }
59
</style>