flotilla/src/lib/components/Avatar.svelte

30 lines
829 B
Svelte
Raw Normal View History

2024-08-15 21:30:55 +00:00
<script lang="ts">
2025-01-15 19:42:54 +00:00
import {onMount} from "svelte"
2024-08-15 21:30:55 +00:00
import Icon from "@lib/components/Icon.svelte"
const {src = "", size = 7, icon = "user-rounded", style = "", ...restProps} = $props()
2025-01-15 18:56:56 +00:00
2025-01-15 19:42:54 +00:00
let element: HTMLElement
2025-02-04 00:37:14 +00:00
const rem = $derived(size * 4)
2025-01-15 19:42:54 +00:00
onMount(() => {
if (src) {
const image = new Image()
image.addEventListener("error", () => {
element.querySelector(".hidden")?.classList.remove("hidden")
})
image.src = src
}
})
2024-08-15 21:30:55 +00:00
</script>
2025-01-15 18:56:56 +00:00
<div
2025-01-15 19:42:54 +00:00
bind:this={element}
2025-02-04 00:37:14 +00:00
class="{restProps.class} relative !flex shrink-0 items-center justify-center overflow-hidden rounded-full bg-cover bg-center"
style="width: {rem}px; height: {rem}px; min-width: {rem}px; background-image: url({src}); {style}">
2025-01-15 19:42:54 +00:00
<Icon {icon} class={src ? "hidden" : ""} size={Math.round(size * 0.8)} />
2025-01-15 18:56:56 +00:00
</div>