|
1 | 1 | import type { JSX } from 'solid-js'; |
2 | | -import { createEffect, createSignal, splitProps } from 'solid-js'; |
| 2 | +import { createEffect, createSignal, on, Show, splitProps } from 'solid-js'; |
3 | 3 | import steveSrc from '../../../assets/images/steve.png'; |
4 | 4 |
|
5 | 5 | type PlayerHeadProps = JSX.IntrinsicElements['img'] & { |
6 | 6 | uuid: string | null | undefined; |
7 | 7 | onError?: () => void; |
8 | 8 | }; |
9 | 9 |
|
10 | | -function crafatar(uuid: string) { |
| 10 | +function headSrc(uuid: string) { |
11 | 11 | return `https://crafatar.com/avatars/${uuid}?size=48`; |
12 | 12 | } |
13 | 13 |
|
14 | 14 | function PlayerHead(props: PlayerHeadProps) { |
15 | 15 | const [split, rest] = splitProps(props, ['uuid', 'class']); |
16 | | - const [src, setSrc] = createSignal(steveSrc); |
| 16 | + const [isLoaded, setLoaded] = createSignal(false); |
17 | 17 |
|
18 | | - createEffect(() => { |
19 | | - if (split.uuid) |
20 | | - setSrc(crafatar(split.uuid)); |
21 | | - }); |
22 | | - |
23 | | - const onError = () => { |
24 | | - setSrc(steveSrc); |
25 | | - props.onError && props.onError(); |
26 | | - }; |
| 18 | + createEffect(on(() => props.uuid, () => setLoaded(false))); |
27 | 19 |
|
28 | 20 | return ( |
29 | | - <img |
30 | | - class={`image-render-pixel ${split.class}`} |
31 | | - onError={onError} |
32 | | - src={src()} |
33 | | - {...rest} |
34 | | - /> |
| 21 | + <> |
| 22 | + <Show |
| 23 | + children={( |
| 24 | + <img |
| 25 | + class={`image-render-pixel ${isLoaded() ? '' : 'hidden'} ${split.class}`} |
| 26 | + onError={() => props.onError && props.onError()} |
| 27 | + onLoad={() => setLoaded(true)} |
| 28 | + src={headSrc(split.uuid!)} |
| 29 | + {...rest} |
| 30 | + /> |
| 31 | + )} |
| 32 | + fallback={( |
| 33 | + <img |
| 34 | + class={`image-render-pixel ${split.class}`} |
| 35 | + src={steveSrc} |
| 36 | + {...rest} |
| 37 | + /> |
| 38 | + )} |
| 39 | + when={props.uuid !== null && props.uuid !== undefined} |
| 40 | + /> |
| 41 | + </> |
35 | 42 | ); |
36 | 43 | } |
37 | 44 |
|
|
0 commit comments