From 8ef4aa794e056ae2045caa534c4c0524cf1fb259 Mon Sep 17 00:00:00 2001 From: Jonghyeon Ko Date: Sun, 15 Nov 2026 23:53:13 +0900 Subject: [PATCH] fix(maintainers): make avatar loading states visible --- src/components/MaintainerAvatarImage.tsx | 65 ++++++++++++++++++++++++ src/components/MaintainerCard.tsx | 20 +++----- src/components/MaintainersMap.tsx | 37 +++++--------- 3 files changed, 84 insertions(+), 38 deletions(-) create mode 100644 src/components/MaintainerAvatarImage.tsx diff --git a/src/components/MaintainerAvatarImage.tsx b/src/components/MaintainerAvatarImage.tsx new file mode 100644 index 000000000..d4f7c1cd5 --- /dev/null +++ b/src/components/MaintainerAvatarImage.tsx @@ -0,0 +1,65 @@ +import type { ComponentProps } from 'react' +import { twMerge } from 'tailwind-merge' + +type MaintainerAvatarImageProps = Pick< + ComponentProps<'img'>, + 'className' | 'width' | 'height' +> & { + src: string + alt: string + defer?: boolean +} + +function revealImage(image: HTMLImageElement) { + void image.decode().then( + () => { + image.dataset.loaded = '' + }, + () => { + image.dataset.failed = '' + }, + ) +} + +function checkCachedImage(image: HTMLImageElement | null) { + if (!image?.getAttribute('src') || !image.complete) return + if (image.naturalWidth) revealImage(image) + else image.dataset.failed = '' +} + +export function MaintainerAvatarImage({ + src, + alt, + defer = false, + className, + width, + height, +}: MaintainerAvatarImageProps) { + return ( + <> + {alt} revealImage(event.currentTarget)} + onError={(event) => { + event.currentTarget.dataset.failed = '' + }} + className={twMerge( + 'peer block size-full object-cover opacity-0 transition-opacity duration-300 data-[loaded]:opacity-100 motion-reduce:transition-none [@media(scripting:none)]:opacity-100', + className, + )} + /> +