| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { Icon } from '@wordpress/components'; |
| 5 |
import { page } from '@wordpress/icons'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Internal dependencies |
| 9 |
*/ |
| 10 |
import { HydratedPost } from '../../providers/base-wordpress-provider'; |
| 11 |
|
| 12 |
/** |
| 13 |
* Defines the props structure for Thumbnail. |
| 14 |
* |
| 15 |
* @since 3.18.0 |
| 16 |
*/ |
| 17 |
interface ThumbnailProps { |
| 18 |
post?: HydratedPost; |
| 19 |
imageUrl?: string; |
| 20 |
icon?: React.JSX.Element; |
| 21 |
size?: number; |
| 22 |
className?: string; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Thumbnail component that displays either a post's thumbnail, a custom image, or a fallback icon. |
| 27 |
* |
| 28 |
* @since 3.18.0 |
| 29 |
* |
| 30 |
* @param {ThumbnailProps} props The component's props. |
| 31 |
*/ |
| 32 |
export const Thumbnail = ( { |
| 33 |
post, |
| 34 |
imageUrl, |
| 35 |
icon = page, |
| 36 |
size = 100, |
| 37 |
className = '', |
| 38 |
}: Readonly<ThumbnailProps> ): React.JSX.Element => { |
| 39 |
const thumbnailUrl = post?.thumbnail ?? imageUrl; |
| 40 |
const altText = post?.title.rendered ?? ''; |
| 41 |
|
| 42 |
return ( |
| 43 |
<div className={ `parsely-thumbnail ${ className }` } style={ { width: size, height: size } }> |
| 44 |
{ thumbnailUrl ? ( |
| 45 |
<img |
| 46 |
src={ thumbnailUrl } |
| 47 |
alt={ altText } |
| 48 |
width={ size } |
| 49 |
height={ size } |
| 50 |
loading="lazy" |
| 51 |
aria-hidden={ altText === '' } |
| 52 |
/> |
| 53 |
) : ( |
| 54 |
<div className="parsely-thumbnail-icon-container"> |
| 55 |
<Icon icon={ icon } size={ size } /> |
| 56 |
</div> |
| 57 |
) } |
| 58 |
</div> |
| 59 |
); |
| 60 |
}; |
| 61 |
|