File tree 3 files changed +15
-5
lines changed
packages/bits-ui/src/lib/bits/avatar
sites/docs/src/lib/components/demos
3 files changed +15
-5
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " bits-ui " : patch
3
+ ---
4
+
5
+ fix: Avatar should render fallback if src is updated and is ` null ` /` undefined `
Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ class AvatarRootState {
41
41
} ) ;
42
42
}
43
43
44
- loadImage ( src : string , crossorigin ?: CrossOrigin , referrerPolicy ?: ReferrerPolicy ) {
44
+ loadImage = ( src : string , crossorigin ?: CrossOrigin , referrerPolicy ?: ReferrerPolicy ) => {
45
45
let imageTimerId : number ;
46
46
const image = new Image ( ) ;
47
47
@@ -59,9 +59,9 @@ class AvatarRootState {
59
59
this . loadingStatus . current = "error" ;
60
60
} ;
61
61
return ( ) => {
62
- clearTimeout ( imageTimerId ) ;
62
+ window . clearTimeout ( imageTimerId ) ;
63
63
} ;
64
- }
64
+ } ;
65
65
66
66
props = $derived . by (
67
67
( ) =>
@@ -107,7 +107,10 @@ class AvatarImageState {
107
107
} ) ;
108
108
109
109
$effect . pre ( ( ) => {
110
- if ( ! this . #src. current ) return ;
110
+ if ( ! this . #src. current ) {
111
+ this . #root. loadingStatus . current = "error" ;
112
+ return ;
113
+ }
111
114
// dependency on crossorigin
112
115
this . #crossOrigin. current ;
113
116
untrack ( ( ) =>
Original file line number Diff line number Diff line change 2
2
import { Avatar } from " bits-ui" ;
3
3
4
4
let loadingStatus = $state <Avatar .RootProps [" loadingStatus" ]>(" loading" );
5
+ let src = $state <string | null >(" /avatar-1.png" );
5
6
</script >
6
7
8
+ <button onclick ={() => (src = null )}> Remove src </button >
7
9
<Avatar .Root
8
10
bind:loadingStatus
9
11
class ="h-12 w-12 rounded-full border {loadingStatus === ' loaded'
13
15
<div
14
16
class =" flex h-full w-full items-center justify-center overflow-hidden rounded-full border-2 border-transparent"
15
17
>
16
- <Avatar .Image src = " /avatar-1.png " alt =" @huntabyte" />
18
+ <Avatar .Image { src } alt =" @huntabyte" />
17
19
<Avatar .Fallback class =" border border-muted" >HB</Avatar .Fallback >
18
20
</div >
19
21
</Avatar .Root >
You can’t perform that action at this time.
0 commit comments