~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/spaces-bookclub

Book club: a Svelte demo app for atproto spaces (@atproto/lex-space)

0
0
main
58 lines · 1.4 KB Raw History
<script lang="ts">
  let {
    seed,
    label,
    size = 'md',
    shape = 'circle',
  }: {
    /** Stable input for the color, e.g. a DID */
    seed: string
    /** Text whose first letter is shown, e.g. a handle */
    label: string
    size?: 'sm' | 'md' | 'lg'
    shape?: 'circle' | 'square'
  } = $props()

  // A hue per seed, so each account or club keeps its color
  const hue = $derived(
    [...seed].reduce((hash, c) => (hash * 31 + c.charCodeAt(0)) >>> 0, 7) % 360,
  )
  const initial = $derived(label.replace(/^@/, '').charAt(0).toUpperCase())
</script>

<span class="avatar {size} {shape}" style:--hue={hue} aria-hidden="true">{initial}</span>

<style>
  .avatar {
    display: inline-grid;
    flex-shrink: 0;
    place-items: center;
    background: oklch(var(--avatar-bg-lightness) var(--avatar-chroma) var(--hue));
    color: oklch(var(--avatar-fg-lightness) var(--avatar-chroma) var(--hue));
    font-weight: var(--weight-semibold);
    line-height: 1;
    user-select: none;
  }
  .circle {
    border-radius: var(--radius-full);
  }
  .square {
    border-radius: var(--radius-md);
    font-family: var(--font-serif);
  }
  .sm {
    width: 1.5rem;
    height: 1.5rem;
    font-size: var(--text-xs);
  }
  .md {
    width: 2.25rem;
    height: 2.25rem;
    font-size: var(--text-sm);
  }
  .lg {
    width: 3rem;
    height: 3rem;
    font-size: var(--text-xl);
  }
</style>