@@ -10,19 +10,43 @@ import {useLayoutEffect} from '@react-aria/utils';
1010import { useSpectrumContextProps } from './useSpectrumContextProps' ;
1111
1212export interface ImageProps extends UnsafeStyles , SlotProps {
13+ /** The URL of the image. */
1314 src ?: string ,
1415 // TODO
1516 // srcSet?: string,
1617 // sizes?: string,
18+ /** Accessible alt text for the image. */
1719 alt ?: string ,
20+ /**
21+ * Indicates if the fetching of the image must be done using a CORS request.
22+ * [See MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin).
23+ */
1824 crossOrigin ?: 'anonymous' | 'use-credentials' ,
25+ /**
26+ * Whether the browser should decode images synchronously or asynchronously.
27+ * [See MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#decoding).
28+ */
1929 decoding ?: 'async' | 'auto' | 'sync' ,
2030 // Only supported in React 19...
2131 // fetchPriority?: 'high' | 'low' | 'auto',
32+ /**
33+ * Whether the image should be loaded immediately or lazily when scrolled into view.
34+ * [See MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#loading).
35+ */
2236 loading ?: 'eager' | 'lazy' ,
37+ /**
38+ * A string indicating which referrer to use when fetching the resource.
39+ * [See MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#referrerpolicy).
40+ */
2341 referrerPolicy ?: HTMLAttributeReferrerPolicy ,
42+ /** Spectrum-defined styles, returned by the `style()` macro. */
2443 styles ?: StyleString ,
44+ /** A function that is called to render a fallback when the image fails to load. */
2545 renderError ?: ( ) => ReactNode ,
46+ /**
47+ * A group of images to coordinate between, matching the group passed to the `<ImageCoordinator>` component.
48+ * If not provided, the default image group is used.
49+ */
2650 group ?: ImageGroup
2751}
2852
@@ -84,11 +108,28 @@ function reducer(state: State, action: Action): State {
84108 }
85109}
86110
87- const imageStyles = style ( {
111+ const wrapperStyles = style ( {
88112 backgroundColor : 'gray-100' ,
89113 overflow : 'hidden'
90114} ) ;
91115
116+ const imgStyles = style ( {
117+ display : 'block' ,
118+ width : 'full' ,
119+ height : 'full' ,
120+ objectFit : '[inherit]' ,
121+ objectPosition : '[inherit]' ,
122+ opacity : {
123+ default : 0 ,
124+ isRevealed : 1
125+ } ,
126+ transition : {
127+ default : 'none' ,
128+ isTransitioning : 'opacity'
129+ } ,
130+ transitionDuration : 500
131+ } ) ;
132+
92133function Image ( props : ImageProps , domRef : ForwardedRef < HTMLDivElement > ) {
93134 [ props , domRef ] = useSpectrumContextProps ( props , domRef , ImageContext ) ;
94135
@@ -171,12 +212,12 @@ function Image(props: ImageProps, domRef: ForwardedRef<HTMLDivElement>) {
171212
172213 let errorState = ! isSkeleton && state === 'error' && renderError ?.( ) ;
173214 let isRevealed = state === 'revealed' && ! isSkeleton ;
174- let transition = isRevealed && loadTime > 200 ? 'opacity 500ms' : undefined ;
215+ let isTransitioning = isRevealed && loadTime > 200 ;
175216 return useMemo ( ( ) => hidden ? null : (
176217 < div
177218 ref = { domRef }
178219 style = { UNSAFE_style }
179- className = { UNSAFE_className + mergeStyles ( imageStyles , styles ) + ' ' + ( isAnimating ? loadingStyle : '' ) } >
220+ className = { UNSAFE_className + mergeStyles ( wrapperStyles , styles ) + ' ' + ( isAnimating ? loadingStyle : '' ) } >
180221 { errorState }
181222 { ! errorState && (
182223 < img
@@ -189,18 +230,10 @@ function Image(props: ImageProps, domRef: ForwardedRef<HTMLDivElement>) {
189230 ref = { imgRef }
190231 onLoad = { onLoad }
191232 onError = { onError }
192- style = { {
193- display : 'block' ,
194- width : '100%' ,
195- height : '100%' ,
196- objectFit : 'inherit' ,
197- objectPosition : 'inherit' ,
198- opacity : isRevealed ? 1 : 0 ,
199- transition
200- } } />
233+ className = { imgStyles ( { isRevealed, isTransitioning} ) } />
201234 ) }
202235 </ div >
203- ) , [ hidden , domRef , UNSAFE_style , UNSAFE_className , styles , isAnimating , errorState , src , alt , crossOrigin , decoding , loading , referrerPolicy , onLoad , onError , isRevealed , transition ] ) ;
236+ ) , [ hidden , domRef , UNSAFE_style , UNSAFE_className , styles , isAnimating , errorState , src , alt , crossOrigin , decoding , loading , referrerPolicy , onLoad , onError , isRevealed , isTransitioning ] ) ;
204237}
205238
206239const _Image = forwardRef ( Image ) ;
0 commit comments