2021-04-21 14:09:36 +02:00
|
|
|
<script>
|
2021-04-23 09:41:49 +02:00
|
|
|
import "@spectrum-css/avatar/dist/index-vars.css"
|
2023-11-20 21:52:29 +01:00
|
|
|
|
2021-05-05 10:43:06 +02:00
|
|
|
let sizes = new Map([
|
|
|
|
["XXS", "--spectrum-alias-avatar-size-50"],
|
|
|
|
["XS", "--spectrum-alias-avatar-size-75"],
|
|
|
|
["S", "--spectrum-alias-avatar-size-200"],
|
2022-06-24 15:55:25 +02:00
|
|
|
["M", "--spectrum-alias-avatar-size-400"],
|
2021-05-05 10:43:06 +02:00
|
|
|
["L", "--spectrum-alias-avatar-size-500"],
|
|
|
|
["XL", "--spectrum-alias-avatar-size-600"],
|
|
|
|
["XXL", "--spectrum-alias-avatar-size-700"],
|
|
|
|
])
|
|
|
|
export let size = "M"
|
2021-04-23 09:41:49 +02:00
|
|
|
export let url = ""
|
|
|
|
export let disabled = false
|
2021-05-21 14:16:54 +02:00
|
|
|
export let initials = "JD"
|
2023-05-26 10:24:53 +02:00
|
|
|
export let color = null
|
2022-06-24 15:55:25 +02:00
|
|
|
|
|
|
|
const DefaultColor = "#3aab87"
|
|
|
|
|
2023-05-26 10:24:53 +02:00
|
|
|
$: avatarColor = color || getColor(initials)
|
|
|
|
$: style = getStyle(size, avatarColor)
|
2022-06-24 15:55:25 +02:00
|
|
|
|
|
|
|
const getColor = initials => {
|
|
|
|
if (!initials?.length) {
|
|
|
|
return DefaultColor
|
|
|
|
}
|
|
|
|
const code = initials[0].toLowerCase().charCodeAt(0)
|
|
|
|
const hue = ((code % 26) / 26) * 360
|
|
|
|
return `hsl(${hue}, 50%, 50%)`
|
|
|
|
}
|
2023-05-26 10:24:53 +02:00
|
|
|
|
|
|
|
const getStyle = (sizeKey, color) => {
|
|
|
|
const size = `var(${sizes.get(sizeKey)})`
|
|
|
|
const fontSize = `calc(${size} / 2)`
|
|
|
|
return `width:${size}; height:${size}; font-size:${fontSize}; background:${color};`
|
|
|
|
}
|
2021-04-21 14:09:36 +02:00
|
|
|
</script>
|
|
|
|
|
2021-05-05 09:12:25 +02:00
|
|
|
{#if url}
|
|
|
|
<img
|
|
|
|
class:is-disabled={disabled}
|
|
|
|
class="spectrum-Avatar"
|
|
|
|
src={url}
|
|
|
|
alt="Avatar"
|
2021-05-05 10:43:06 +02:00
|
|
|
style="width: var({sizes.get(size)}); height: var({sizes.get(size)});"
|
2021-05-05 09:12:25 +02:00
|
|
|
/>
|
|
|
|
{:else}
|
2023-05-26 10:24:53 +02:00
|
|
|
<div class="spectrum-Avatar" class:is-disabled={disabled} {style}>
|
2021-05-21 14:16:54 +02:00
|
|
|
{initials || ""}
|
2021-05-05 10:43:06 +02:00
|
|
|
</div>
|
2021-05-05 09:12:25 +02:00
|
|
|
{/if}
|
|
|
|
|
|
|
|
<style>
|
|
|
|
div {
|
|
|
|
color: white;
|
|
|
|
display: grid;
|
|
|
|
place-items: center;
|
2021-05-25 11:11:41 +02:00
|
|
|
font-weight: 600;
|
2021-05-05 09:12:25 +02:00
|
|
|
border-radius: 50%;
|
2021-05-05 10:44:50 +02:00
|
|
|
overflow: hidden;
|
2021-05-05 11:00:04 +02:00
|
|
|
user-select: none;
|
2021-05-21 14:16:54 +02:00
|
|
|
text-transform: uppercase;
|
2023-01-18 14:56:53 +01:00
|
|
|
flex-shrink: 0;
|
2021-05-05 09:12:25 +02:00
|
|
|
}
|
|
|
|
</style>
|