Skip to content

Commit 690b29c

Browse files
feat: added support for imgProps and videoProps
1 parent 4771324 commit 690b29c

5 files changed

+59
-43
lines changed
+19-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react';
12
import type { Meta, StoryObj } from '@storybook/react';
23
import { CloudinaryImage } from './CloudinaryImage';
34
import { Cloudinary } from '@cloudinary/url-gen';
@@ -6,7 +7,8 @@ import { scale } from '@cloudinary/url-gen/actions/resize';
67
import { format, quality } from '@cloudinary/url-gen/actions/delivery';
78

89
const meta: Meta<typeof CloudinaryImage> = {
9-
component: CloudinaryImage
10+
component: CloudinaryImage,
11+
tags: ['autodocs']
1012
};
1113

1214
export default meta;
@@ -28,21 +30,22 @@ export const VersionV3: Story = {
2830
}
2931
};
3032

31-
const cloudinary = new Cloudinary({
32-
cloud: {
33-
cloudName: 'demo'
34-
}
35-
});
36-
const cloudinaryImageObject = cloudinary
37-
.image('front_face')
38-
.effect(backgroundRemoval())
39-
.effect(sepia())
40-
.resize(scale().height(333))
41-
.delivery(format('auto'))
42-
.delivery(quality('auto'));
43-
4433
export const VersionV2: Story = {
45-
args: {
46-
cldImg: cloudinaryImageObject
34+
render: () => {
35+
const cloudinary = new Cloudinary({
36+
cloud: {
37+
cloudName: 'demo'
38+
}
39+
});
40+
41+
const cloudinaryImageObject = cloudinary
42+
.image('front_face')
43+
.effect(backgroundRemoval())
44+
.effect(sepia())
45+
.resize(scale().height(333))
46+
.delivery(format('auto'))
47+
.delivery(quality('auto'));
48+
49+
return <CloudinaryImage cldImg={cloudinaryImageObject} alt='' />
4750
}
4851
}

packages/react/src/CloudinaryImage.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,24 @@ type ImageV3Props = {
4949
src: string;
5050
alt: string;
5151
children?: never;
52+
imgProps?: React.HTMLProps<HTMLImageElement>
5253
} & ImageTransformationProps;
5354

5455
interface ImageV2Props {
5556
src?: never;
57+
alt: string;
5658
cldImg: UrlGenCloudinaryImage;
5759
children?: never;
60+
imgProps?: React.HTMLProps<HTMLImageElement>
5861
}
5962

6063
export type CloudinaryImageProps = ImageV3Props | ImageV2Props;
6164

6265
export const CloudinaryImage = forwardRef<HTMLImageElement, CloudinaryImageProps>(
6366
(props, ref) => {
6467
if (props.cldImg) {
65-
const { cldImg, ...rest } = props;
66-
return <img src={cldImg.toURL()} {...rest} ref={ref} />;
68+
const { cldImg, alt, imgProps, ...rest } = props;
69+
return <img {...imgProps} src={cldImg.toURL()} alt={alt} {...rest} ref={ref} />;
6770
}
6871
const transformationMap = {
6972
format: parseFormat,
@@ -84,12 +87,12 @@ export const CloudinaryImage = forwardRef<HTMLImageElement, CloudinaryImageProps
8487
roundCorners: parseRoundCorners,
8588
opacity: parseOpacity
8689
} satisfies TransformationMap<ImageTransformationProps>;
87-
const { src, alt, children, cldImg, ...rest } = props;
90+
const { src, alt, children, cldImg, imgProps, ...rest } = props;
8891
const { baseCloudUrl, assetPath } = parseCloudinaryUrlToParts(src);
8992
const transformationString = parsePropsToTransformationString({
9093
transformationProps: rest,
9194
transformationMap
9295
});
9396

94-
return <img src={`${baseCloudUrl}/${transformationString}/${assetPath}`} alt={alt} ref={ref} />;
97+
return <img {...imgProps} src={`${baseCloudUrl}/${transformationString}/${assetPath}`} alt={alt} ref={ref} />;
9598
});
+29-20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react';
12
import type { Meta, StoryObj } from '@storybook/react';
23
import { CloudinaryVideo } from './CloudinaryVideo';
34
import { Cloudinary } from '@cloudinary/url-gen';
@@ -11,13 +12,14 @@ import { vcl31 } from '@cloudinary/url-gen/qualifiers/videoCodecLevel';
1112
import { scale } from '@cloudinary/url-gen/actions/resize';
1213

1314
const meta: Meta<typeof CloudinaryVideo> = {
14-
component: CloudinaryVideo
15+
component: CloudinaryVideo,
16+
tags: ['autodocs']
1517
};
1618

1719
export default meta;
1820
type Story = StoryObj<typeof CloudinaryVideo>;
1921

20-
export const VersionV3: Story = {
22+
export const Version_v3: Story = {
2123
args: {
2224
src: 'https://res.cloudinary.com/demo/video/upload/dog.mp4',
2325
resize: { height: 333 },
@@ -38,24 +40,31 @@ const cloudinary = new Cloudinary({
3840
cloudName: 'demo'
3941
}
4042
});
41-
const cloudinaryVideoObject = cloudinary.video('dog.mp4')
42-
.effect(blackwhite())
43-
.resize(scale().height(333))
44-
.videoEdit(trim().duration('1p'))
45-
.roundCorners(byRadius(20))
46-
.transcode(
47-
videoCodec(
48-
h264()
49-
.profile(baseline())
50-
.level(vcl31())
51-
)
52-
);
5343

54-
export const VersionV2: Story = {
55-
args: {
56-
cldVid: cloudinaryVideoObject,
57-
loop: true,
58-
autoPlay: true,
59-
muted: true
44+
export const Version_v2: Story = {
45+
render: () => {
46+
const cloudinaryVideoObject = cloudinary.video('dog.mp4')
47+
.effect(blackwhite())
48+
.resize(scale().height(333))
49+
.videoEdit(trim().duration('1p'))
50+
.roundCorners(byRadius(20))
51+
.transcode(
52+
videoCodec(
53+
h264()
54+
.profile(baseline())
55+
.level(vcl31())
56+
)
57+
);
58+
59+
return (
60+
<CloudinaryVideo
61+
cldVid={cloudinaryVideoObject}
62+
videoProps={{
63+
loop: true,
64+
autoPlay: true,
65+
muted: true
66+
}}
67+
/>
68+
)
6069
}
6170
}

packages/react/src/CloudinaryVideo.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef, ReactNode } from 'react';
1+
import React, { forwardRef } from 'react';
22
import { type CloudinaryVideo as UrlGenCloudinaryVideo } from '@cloudinary/url-gen/assets/CloudinaryVideo';
33
import { parseCloudinaryUrlToParts } from './parseCloudinaryUrlToParts';
44
import { parseFormat } from './transformationParsers/parseFormat';
@@ -58,9 +58,10 @@ type VideoV3Props = {
5858
children?: never;
5959
} & VideoTransformationProps;
6060

61-
interface VideoV2Props extends React.HTMLProps<HTMLVideoElement> {
61+
interface VideoV2Props {
6262
src?: never;
6363
cldVid: UrlGenCloudinaryVideo;
64+
videoProps?: React.HTMLProps<HTMLVideoElement>
6465
children?: never;
6566
}
6667

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
"@cloudinary/vue": ["vue/src"],
1616
"@cloudinary/*": ["*/src"],
1717
"*": ["node_modules"]
18-
},
18+
},
1919
"exclude": ["node_modules", "**/node_modules", "**/e2e", "**/dist", "**/build", "**/coverage"]
2020
}

0 commit comments

Comments
 (0)