Skip to content

Commit 01c3343

Browse files
committed
Enhance documentation for Image and Video components; update transformation handling in snapshots
1 parent 19dd9c2 commit 01c3343

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

src/components/Image.tsx

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import { ImageKitContext } from "../provider/ImageKit";
55

66
export type IKImageProps = Omit<JSX.IntrinsicElements["img"], "src" | "srcSet" | "width"> & SrcProps & {
77
/**
8-
* Set to `false` to disable responsive srcset generation. By default, this is `true`.
8+
* Set to `false` to disable automatic responsive `srcSet` generation.
9+
* Defaults to `true`.
910
*/
1011
responsive?: boolean;
1112

1213
/**
13-
* The width of the image. This is used to calculate the srcset for responsive images. Provide in number or string format e.g. 100, "100".
14-
* If values like "100px" or "100%" are provided, srcset calculation will ignore the value of width and generate a much larger srcset to cater to all possible widths.
14+
* The intended display width of the image.
15+
*
16+
* - Accepts a number (e.g. `100`) or a numeric string (e.g. `"100"`).
17+
* - If you pass units such as `"100px"` or a percentage like `"100%"`, the value
18+
* is ignored when generating the `srcSet`. In that case, a broad range of
19+
* widths is produced to cover all possible viewport sizes.
1520
*/
1621
width?: number | `${number}`;
1722
}
@@ -32,30 +37,36 @@ function getInt(x: unknown): number {
3237

3338

3439
/**
35-
* The Image component is a wrapper around the React Image component. It supports all the features of the React Image component, along with additional features provided by ImageKit.
36-
*
40+
* A React wrapper around the native `<img>` element that adds ImageKit's
41+
* optimization and transformation capabilities.
42+
*
43+
* All native `<img>` props are supported, in addition to the ImageKit‑specific props.
44+
*
3745
* @example
3846
* ```jsx
3947
* import { Image } from "@imagekit/react";
48+
*
4049
* <Image
41-
* urlEndpoint="https://ik.imagekit.io/your_imagekit_id" // You can also set this in a parent ImageKitProvider component
42-
* src="/default-image.jpg" // The path to the image in your ImageKit account
43-
* alt="Default Image"
44-
* width={500}
45-
* height={500}
46-
* transformation={[{ width: 500, height: 500 }]} // Add ImageKit transformations
50+
* urlEndpoint="https://ik.imagekit.io/your_imagekit_id" // Can also be set on a parent <ImageKitProvider />
51+
* src="/default-image.jpg" // Path in your ImageKit account
52+
* alt="Default image"
53+
* width={500}
54+
* height={500}
55+
* transformation={[{ width: 500, height: 500 }]} // Optional ImageKit transformations
4756
* />
4857
* ```
4958
*/
5059
export const Image = (props: IKImageProps) => {
5160
const contextValues = useContext(ImageKitContext);
5261

53-
// Its important to extract the ImageKit specific props from the props, so that we can use the rest of the props as is in the img element
62+
// It's important to extract the ImageKit‑specific props so we can spread the
63+
// remaining props directly onto the `<img>` element.
5464
const { transformation = [], src = "", loading = "lazy", queryParameters, urlEndpoint, transformationPosition, sizes, responsive = true, ...nonIKParams } = {
5565
...contextValues, // Default values from context
5666
...props // Override with props
5767
};
5868

69+
// Fail fast in development if the mandatory urlEndpoint is missing.
5970
if (!urlEndpoint || urlEndpoint.trim() === "") {
6071
if (process.env.NODE_ENV !== "production") {
6172
console.error("urlEndpoint is neither provided in this component nor in the ImageKitContext.");

src/components/Video.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ export type IKVideoProps = Omit<JSX.IntrinsicElements["video"], "src"> & SrcProp
2525
export const Video = (props: IKVideoProps) => {
2626
const contextValues = useContext(ImageKitContext);
2727

28-
// Its important to extract the ImageKit specific props from the props, so that we can use the rest of the props as is in the video element
28+
// Extract ImageKitspecific props so we can spread the rest directly onto <video>.
2929
const { transformation = [], src = "", queryParameters, urlEndpoint, transformationPosition, ...nonIKParams } = {
3030
...contextValues, // Default values from context
3131
...props // Override with props
3232
};
3333

34+
// Fail fast in development if the mandatory urlEndpoint is missing.
3435
if (!urlEndpoint || urlEndpoint.trim() === "") {
3536
if (process.env.NODE_ENV !== "production") {
3637
console.error("urlEndpoint is neither provided in this component nor in the ImageKitContext.");
@@ -43,7 +44,8 @@ export const Video = (props: IKVideoProps) => {
4344
urlEndpoint,
4445
src,
4546
transformation: [...transformation], // Do not mutate original transformation array from the props
46-
queryParameters
47+
queryParameters,
48+
transformationPosition,
4749
});
4850

4951
return (

test-app/app/components/basic.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ export const Basic = () => {
192192
transformation={[{ height: 100, width: 100 }]}
193193
controls={true}
194194
/>
195+
196+
{/* transformationPosition="path" */}
197+
<Video
198+
src="sample-video.mp4"
199+
title="Video with path transformation"
200+
transformationPosition="path"
201+
height={300}
202+
width={300}
203+
transformation={[{ height: 100, width: 100 }]}
204+
/>
205+
195206
</ImageKitProvider>
196207
</div>
197208
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div class="container"><div><h1>Image</h1><img alt="Image without ImageKit provider" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max"><img alt="Image with ImageKit provider" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max"><img alt="Image with width not number, should produce larger srcset" width="300px" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 640w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-750,c-at_max 750w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-828,c-at_max 828w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1080,c-at_max 1080w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1200,c-at_max 1200w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1920,c-at_max 1920w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-2048,c-at_max 2048w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max 3840w" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max"><img alt="With transformation" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=h-100,w-100:w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?tr=h-100,w-100:w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?tr=h-100,w-100:w-640,c-at_max"><img alt="Image with queryParameters" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?version=v1&amp;tr=h-100,w-100:w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?version=v1&amp;tr=h-100,w-100:w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?version=v1&amp;tr=h-100,w-100:w-640,c-at_max"><img alt="Responsive image with sizes" height="300" width="300" loading="lazy" sizes="(max-width: 600px) 100vw, 50vw" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 384w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 640w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-750,c-at_max 750w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-828,c-at_max 828w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1080,c-at_max 1080w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1200,c-at_max 1200w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1920,c-at_max 1920w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-2048,c-at_max 2048w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max 3840w" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max"><img alt="Responsive image with sizes - should have very large srcset for all breakpoints" height="300" width="300" loading="lazy" sizes="300px" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-16,c-at_max 16w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-32,c-at_max 32w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-48,c-at_max 48w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-64,c-at_max 64w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-96,c-at_max 96w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-128,c-at_max 128w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-256,c-at_max 256w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 384w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 640w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-750,c-at_max 750w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-828,c-at_max 828w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1080,c-at_max 1080w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1200,c-at_max 1200w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1920,c-at_max 1920w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-2048,c-at_max 2048w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max 3840w" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max"><img alt="Image with urlEndpoint override" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo2/default-image.jpg?tr=h-100,w-100:w-384,c-at_max 1x, https://ik.imagekit.io/demo2/default-image.jpg?tr=h-100,w-100:w-640,c-at_max 2x" src="https://ik.imagekit.io/demo2/default-image.jpg?tr=h-100,w-100:w-640,c-at_max"><img alt="Image with className" class="custom-class" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max"><img alt="Image with lazy loading eager" height="300" width="300" loading="eager" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max"><img alt="Image with path transformation" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/tr:w-384,c-at_max/default-image.jpg 1x, https://ik.imagekit.io/demo/tr:w-640,c-at_max/default-image.jpg 2x" src="https://ik.imagekit.io/demo/tr:w-640,c-at_max/default-image.jpg"><img alt="Image with path transformation + custom transformations" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/tr:h-100,w-100:w-384,c-at_max/default-image.jpg 1x, https://ik.imagekit.io/demo/tr:h-100,w-100:w-640,c-at_max/default-image.jpg 2x" src="https://ik.imagekit.io/demo/tr:h-100,w-100:w-640,c-at_max/default-image.jpg"><img alt="path not respected with absolute url" height="300" width="300" loading="lazy" srcset="https://ik.imagekit/demo/default-image.jpg?tr=w-384,c-at_max 1x, https://ik.imagekit/demo/default-image.jpg?tr=w-640,c-at_max 2x" src="https://ik.imagekit/demo/default-image.jpg?tr=w-640,c-at_max"><img alt="Image with responsive off" height="300" width="300" loading="lazy" src="https://ik.imagekit.io/demo/default-image.jpg?tr=n-restrict-unnamed:w-3840,c-at_max"><h1>Video</h1><video title="Video without ImageKit provider" height="300" width="300" controls="" src="https://ik.imagekit.io/demo/sample-video.mp4"></video><video title="Video with ImageKit provider" height="300" width="300" controls="" src="https://ik.imagekit.io/demo/sample-video.mp4"></video><video title="Video with transformations" height="300" width="300" controls="" src="https://ik.imagekit.io/demo/sample-video.mp4?tr=h-100,w-100"></video><video title="Video with all props" height="300" width="300" controls="" autoplay="" loop="" playsinline="" preload="none" poster="https://ik.imagekit.io/demo/default-image.jpg" src="https://ik.imagekit.io/demo/sample-video.mp4"></video><video title="Video with urlEndpoint override" height="300" width="300" controls="" src="https://ik.imagekit.io/demo2/sample-video.mp4?tr=h-100,w-100"></video></div></div>
1+
<div class="container"><div><h1>Image</h1><img alt="Image without ImageKit provider" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max"><img alt="Image with ImageKit provider" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max"><img alt="Image with width not number, should produce larger srcset" width="300px" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 640w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-750,c-at_max 750w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-828,c-at_max 828w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1080,c-at_max 1080w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1200,c-at_max 1200w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1920,c-at_max 1920w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-2048,c-at_max 2048w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max 3840w" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max"><img alt="With transformation" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=h-100,w-100:w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?tr=h-100,w-100:w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?tr=h-100,w-100:w-640,c-at_max"><img alt="Image with queryParameters" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?version=v1&amp;tr=h-100,w-100:w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?version=v1&amp;tr=h-100,w-100:w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?version=v1&amp;tr=h-100,w-100:w-640,c-at_max"><img alt="Responsive image with sizes" height="300" width="300" loading="lazy" sizes="(max-width: 600px) 100vw, 50vw" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 384w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 640w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-750,c-at_max 750w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-828,c-at_max 828w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1080,c-at_max 1080w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1200,c-at_max 1200w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1920,c-at_max 1920w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-2048,c-at_max 2048w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max 3840w" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max"><img alt="Responsive image with sizes - should have very large srcset for all breakpoints" height="300" width="300" loading="lazy" sizes="300px" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-16,c-at_max 16w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-32,c-at_max 32w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-48,c-at_max 48w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-64,c-at_max 64w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-96,c-at_max 96w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-128,c-at_max 128w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-256,c-at_max 256w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 384w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 640w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-750,c-at_max 750w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-828,c-at_max 828w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1080,c-at_max 1080w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1200,c-at_max 1200w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-1920,c-at_max 1920w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-2048,c-at_max 2048w, https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max 3840w" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-3840,c-at_max"><img alt="Image with urlEndpoint override" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo2/default-image.jpg?tr=h-100,w-100:w-384,c-at_max 1x, https://ik.imagekit.io/demo2/default-image.jpg?tr=h-100,w-100:w-640,c-at_max 2x" src="https://ik.imagekit.io/demo2/default-image.jpg?tr=h-100,w-100:w-640,c-at_max"><img alt="Image with className" class="custom-class" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max"><img alt="Image with lazy loading eager" height="300" width="300" loading="eager" srcset="https://ik.imagekit.io/demo/default-image.jpg?tr=w-384,c-at_max 1x, https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max 2x" src="https://ik.imagekit.io/demo/default-image.jpg?tr=w-640,c-at_max"><img alt="Image with path transformation" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/tr:w-384,c-at_max/default-image.jpg 1x, https://ik.imagekit.io/demo/tr:w-640,c-at_max/default-image.jpg 2x" src="https://ik.imagekit.io/demo/tr:w-640,c-at_max/default-image.jpg"><img alt="Image with path transformation + custom transformations" height="300" width="300" loading="lazy" srcset="https://ik.imagekit.io/demo/tr:h-100,w-100:w-384,c-at_max/default-image.jpg 1x, https://ik.imagekit.io/demo/tr:h-100,w-100:w-640,c-at_max/default-image.jpg 2x" src="https://ik.imagekit.io/demo/tr:h-100,w-100:w-640,c-at_max/default-image.jpg"><img alt="path not respected with absolute url" height="300" width="300" loading="lazy" srcset="https://ik.imagekit/demo/default-image.jpg?tr=w-384,c-at_max 1x, https://ik.imagekit/demo/default-image.jpg?tr=w-640,c-at_max 2x" src="https://ik.imagekit/demo/default-image.jpg?tr=w-640,c-at_max"><img alt="Image with responsive off" height="300" width="300" loading="lazy" src="https://ik.imagekit.io/demo/default-image.jpg?tr=n-restrict-unnamed:w-3840,c-at_max"><h1>Video</h1><video title="Video without ImageKit provider" height="300" width="300" controls="" src="https://ik.imagekit.io/demo/sample-video.mp4"></video><video title="Video with ImageKit provider" height="300" width="300" controls="" src="https://ik.imagekit.io/demo/sample-video.mp4"></video><video title="Video with transformations" height="300" width="300" controls="" src="https://ik.imagekit.io/demo/sample-video.mp4?tr=h-100,w-100"></video><video title="Video with all props" height="300" width="300" controls="" autoplay="" loop="" playsinline="" preload="none" poster="https://ik.imagekit.io/demo/default-image.jpg" src="https://ik.imagekit.io/demo/sample-video.mp4"></video><video title="Video with urlEndpoint override" height="300" width="300" controls="" src="https://ik.imagekit.io/demo2/sample-video.mp4?tr=h-100,w-100"></video><video title="Video with path transformation" height="300" width="300" src="https://ik.imagekit.io/demo/tr:h-100,w-100/sample-video.mp4"></video></div></div>

0 commit comments

Comments
 (0)