fix: add classes for picture tag in media component (#11605)

Sometimes I need to add some classes to the `picture` tag of Media
component. in this case I need to do this:
```
<Media
    resource={content.image}
    className="w-full h-full [&>picture]:w-full"  // <<< follow this
    imgClassName="w-full h-full object-cover"
/>
```

So I added an additional props `pictureClassName` for the picture tag.
Now I can do this:
```
<Media
    resource={content.image}
    className="w-full h-full"
    pictureClassName="w-full h-full" // <<< follow this
    imgClassName="w-full h-full object-cover"
/>
```
NOTE: I've encountered situations where I needed to add classes to the
`picture` tag, not just for `w-full h-full`. To handle this, I had to
update the Media component. I believe this would be a valuable
improvement to the Media component.
This commit is contained in:
Md. Tajmirul Islam Akhand
2025-03-14 05:29:03 +06:00
committed by GitHub
parent 5e3d07bf44
commit d66cdbd4f8
2 changed files with 3 additions and 1 deletions

View File

@@ -21,6 +21,7 @@ export const ImageMedia: React.FC<MediaProps> = (props) => {
const { const {
alt: altFromProps, alt: altFromProps,
fill, fill,
pictureClassName,
imgClassName, imgClassName,
priority, priority,
resource, resource,
@@ -56,7 +57,7 @@ export const ImageMedia: React.FC<MediaProps> = (props) => {
.join(', ') .join(', ')
return ( return (
<picture> <picture className={cn(pictureClassName)}>
<NextImage <NextImage
alt={alt || ''} alt={alt || ''}
className={cn(imgClassName)} className={cn(imgClassName)}

View File

@@ -8,6 +8,7 @@ export interface Props {
className?: string className?: string
fill?: boolean // for NextImage only fill?: boolean // for NextImage only
htmlElement?: ElementType | null htmlElement?: ElementType | null
pictureClassName?: string
imgClassName?: string imgClassName?: string
onClick?: () => void onClick?: () => void
onLoad?: () => void onLoad?: () => void