Skip to content

Commit

Permalink
fix(Avatar): lazy load images
Browse files Browse the repository at this point in the history
  • Loading branch information
LeGmask committed Oct 30, 2023
1 parent 8238fa0 commit 0373cbb
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/lib/components/Avatar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
export let avatar: string;
export let first_name: string;
export let last_name: string;
let src;
let src: string;
function hashCode(str) { // java String#hashCode
function hashCode(str: string) { // java String#hashCode
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return hash;
}
function intToRGB(i) {
function intToRGB(i: number) {
let c = (i & 0x00FFFFFF)
.toString(16)
.toUpperCase();
Expand All @@ -28,7 +28,7 @@
* @param bgColor
* @returns {string}
*/
function getColorByBgColor(bgColor) {
function getColorByBgColor(bgColor: string) {
if (!bgColor) {
return '';
}
Expand Down Expand Up @@ -56,7 +56,7 @@
</script>

{#if avatar && src}
<img class="avatar" {src} alt="{first_name} {last_name}" {...$$restProps}/>
<img class="avatar" {src} alt="{first_name} {last_name}" loading="lazy" {...$$restProps}/>
{:else}
<div style="background-color: #{intToRGB(hashCode(first_name + last_name))}" class="avatar" {...$$restProps}>
<span style="color: {getColorByBgColor(intToRGB(hashCode(first_name + last_name)))}">
Expand Down

1 comment on commit 0373cbb

@vercel
Copy link

@vercel vercel bot commented on 0373cbb Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.