Skip to content

Commit e03d133

Browse files
committed
Add Video component and update package structure for improved functionality
1 parent e70af93 commit e03d133

File tree

6 files changed

+127
-48
lines changed

6 files changed

+127
-48
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@imagekit/vue",
33
"version": "4.0.0-beta.1",
4-
"main": "dist/index.cjs",
4+
"main": "dist/index.cjs.js",
55
"module": "dist/index.es.js",
66
"types": "dist/index.d.ts",
77
"files": [

src/components/Image.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ const imgData = computed(() => {
5050
} = merged.value;
5151
5252
if (!urlEndpoint || urlEndpoint.trim() === "") {
53-
console.error("urlEndpoint is neither provided in this component nor in the ImageKitContext.");
53+
console.error("urlEndpoint is neither provided in this component nor in any parent ImageKitProvider.");
5454
return { src, loading };
5555
}
5656
5757
if (responsive === false) {
5858
const normalSrc = buildSrc({
5959
src,
60-
transformation,
60+
transformation: [...transformation],
6161
queryParameters,
6262
urlEndpoint,
6363
transformationPosition,
@@ -70,7 +70,7 @@ const imgData = computed(() => {
7070
7171
const { src: newSrc, srcSet } = getResponsiveImageAttributes({
7272
src,
73-
transformation,
73+
transformation: [...transformation],
7474
width: isNaN(widthInt) ? undefined : widthInt,
7575
sizes,
7676
queryParameters,

src/components/Video.vue

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<script setup lang="ts">
2+
import { buildSrc } from "@imagekit/javascript";
3+
import { computed, inject, useAttrs } from "vue";
4+
import type { IKVideoProps, ImageKitProviderProps } from "../interface";
5+
import { ImageKitContextKey } from "../provider/keys";
6+
7+
const props = defineProps<IKVideoProps>();
8+
const context = inject<ImageKitProviderProps>(ImageKitContextKey, {});
9+
10+
function mergeDefined<T extends Record<string, any>>(base: ImageKitProviderProps, override: IKVideoProps): T {
11+
const out = { ...base } as Record<string, any>;
12+
for (const [k, v] of Object.entries(override)) {
13+
if (v !== undefined) out[k] = v;
14+
}
15+
return out as T;
16+
}
17+
18+
const merged = computed(() => mergeDefined(context, props));
19+
20+
const videoData = computed(() => {
21+
const {
22+
src = "",
23+
transformation = [],
24+
queryParameters,
25+
urlEndpoint,
26+
transformationPosition,
27+
} = merged.value;
28+
29+
if (!urlEndpoint || urlEndpoint.trim() === "") {
30+
console.error("urlEndpoint is neither provided in this component nor in any parent ImageKitProvider.");
31+
return { src };
32+
}
33+
34+
const finalSrc = buildSrc({
35+
urlEndpoint,
36+
src,
37+
transformation: [...transformation],
38+
queryParameters,
39+
transformationPosition,
40+
});
41+
42+
return { src: finalSrc };
43+
});
44+
45+
/* ------------------------------------------------------------------ */
46+
/* forward non-ImageKit attrs */
47+
/* ------------------------------------------------------------------ */
48+
49+
const IK_KEYS = [
50+
"src",
51+
"transformation",
52+
"queryParameters",
53+
"urlEndpoint",
54+
"transformationPosition",
55+
] as const;
56+
57+
const attrs = useAttrs();
58+
const nonIKAttrs = computed(() => {
59+
const obj: Record<string, unknown> = {};
60+
for (const [k, v] of Object.entries(attrs)) {
61+
if (!IK_KEYS.includes(k as (typeof IK_KEYS)[number])) obj[k] = v;
62+
}
63+
return obj;
64+
});
65+
</script>
66+
67+
<template>
68+
<video v-bind="nonIKAttrs" :src="videoData.src" />
69+
</template>

src/index.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
import Image from './components/Image.vue'
2-
import ImageKitProvider from './provider/ImageKitProvider.vue'
3-
import type { SrcOptions, Transformation, UploadOptions, UploadResponse, GetImageAttributesOptions, ResponsiveImageAttributes } from '@imagekit/javascript';
1+
import type { GetImageAttributesOptions, ResponsiveImageAttributes, SrcOptions, Transformation, UploadOptions, UploadResponse } from '@imagekit/javascript';
2+
import { buildSrc, buildTransformationString, getResponsiveImageAttributes, ImageKitAbortError, ImageKitInvalidRequestError, ImageKitServerError, ImageKitUploadNetworkError, upload } from '@imagekit/javascript';
3+
import Image from './components/Image.vue';
4+
import Video from './components/Video.vue';
5+
import ImageKitProvider from './provider/ImageKitProvider.vue';
6+
import { ImageKitContextKey } from './provider/keys';
47

5-
import type { ImageKitProviderProps, IKImageProps } from './interface'
6-
export { Image, ImageKitProvider }
8+
import type { IKImageProps, IKVideoProps, ImageKitProviderProps } from './interface';
9+
10+
export { buildSrc, buildTransformationString, getResponsiveImageAttributes, Image, ImageKitAbortError, ImageKitContextKey, ImageKitInvalidRequestError, ImageKitProvider, ImageKitServerError, ImageKitUploadNetworkError, upload, Video };
711

812
export type {
13+
GetImageAttributesOptions, IKImageProps, IKVideoProps,
14+
// Vue 3 specific
15+
ImageKitProviderProps, ResponsiveImageAttributes,
16+
// JS SDK types
917
SrcOptions,
1018
Transformation,
1119
UploadOptions,
12-
UploadResponse,
13-
GetImageAttributesOptions,
14-
ResponsiveImageAttributes,
15-
16-
ImageKitProviderProps, IKImageProps
17-
}
20+
UploadResponse
21+
};

src/interface/index.ts

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11
import type { GetImageAttributesOptions, SrcOptions } from '@imagekit/javascript';
22

3-
export type IKImageProps = Pick<GetImageAttributesOptions, "deviceBreakpoints" | "imageBreakpoints"> & {
3+
export type IKImageProps = Pick<GetImageAttributesOptions, "deviceBreakpoints" | "imageBreakpoints"> & CommonProps & {
4+
/**
5+
* Set to `false` to disable automatic responsive `srcSet` generation.
6+
* Defaults to `true`.
7+
*/
8+
responsive?: Boolean;
9+
10+
/**
11+
* The intended display width of the image.
12+
*
13+
* - Accepts a number (e.g. `100`) or a numeric string (e.g. `"100"`).
14+
* - If you pass units such as `"100px"` or a percentage like `"100%"`, the value
15+
* is ignored when generating the `srcSet`. In that case, a broad range of
16+
* widths is produced to cover all possible viewport sizes.
17+
*/
18+
width?: number | `${number}`;
19+
20+
/**
21+
* Define the sizes of the image at different breakpoints. Used by the browser to choose the most appropriate size from the generated srcset.
22+
*/
23+
sizes?: string;
24+
25+
/**
26+
* Controls when the image should start loading.
27+
*
28+
* * `lazy` - Defer loading the image until it reaches a calculated distance from the viewport.
29+
* * `eager` - Load the image immediately, regardless of its position in the page.
30+
*
31+
* `lazy` is the default value.
32+
*/
33+
loading?: "lazy" | "eager";
34+
}
35+
36+
type CommonProps = {
437
/**
538
* Accepts a relative or absolute path of the resource. If a relative path is provided, it is appended to the `urlEndpoint`.
639
* If an absolute path is provided, `urlEndpoint` is ignored.
@@ -14,8 +47,9 @@ export type IKImageProps = Pick<GetImageAttributesOptions, "deviceBreakpoints" |
1447
*
1548
* @example
1649
* ```jsx
17-
* import { ImageKitProvider, Image } from "@imagekit/react";
50+
* import { ImageKitProvider, Image, Video } from "@imagekit/vue";
1851
* <ImageKitProvider urlEndpoint="https://ik.imagekit.io/your_imagekit_id">
52+
* <Video src="/video.mp4" />
1953
* <Image src="/default-image.jpg" />
2054
* </ImageKitProvider>
2155
* ```
@@ -41,37 +75,8 @@ export type IKImageProps = Pick<GetImageAttributesOptions, "deviceBreakpoints" |
4175
* If you want to add the transformation string in the path of the URL, set this to `path`, final URL will look like `https://ik.imagekit.io/your_imagekit_id/tr:w-100,h-100/default-image.jpg`.
4276
*/
4377
transformationPosition?: "path" | "query";
44-
45-
/**
46-
* Set to `false` to disable automatic responsive `srcSet` generation.
47-
* Defaults to `true`.
48-
*/
49-
responsive?: Boolean;
50-
51-
/**
52-
* The intended display width of the image.
53-
*
54-
* - Accepts a number (e.g. `100`) or a numeric string (e.g. `"100"`).
55-
* - If you pass units such as `"100px"` or a percentage like `"100%"`, the value
56-
* is ignored when generating the `srcSet`. In that case, a broad range of
57-
* widths is produced to cover all possible viewport sizes.
58-
*/
59-
width?: number | `${number}`;
60-
61-
/**
62-
* Define the sizes of the image at different breakpoints. Used by the browser to choose the most appropriate size from the generated srcset.
63-
*/
64-
sizes?: string;
65-
66-
/**
67-
* Controls when the image should start loading.
68-
*
69-
* * `lazy` - Defer loading the image until it reaches a calculated distance from the viewport.
70-
* * `eager` - Load the image immediately, regardless of its position in the page.
71-
*
72-
* `lazy` is the default value.
73-
*/
74-
loading?: "lazy" | "eager";
7578
}
7679

80+
export type IKVideoProps = CommonProps;
81+
7782
export type ImageKitProviderProps = Pick<IKImageProps, "urlEndpoint" | "transformationPosition">

vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default defineConfig({
1717
lib: {
1818
entry: resolve(__dirname, 'src/index.ts'),
1919
name: 'VueImagekit',
20+
formats: ['es', 'umd', "cjs"],
2021
fileName: format => `index.${format}.js`
2122
},
2223
rollupOptions: {

0 commit comments

Comments
 (0)