Skip to content

Commit

Permalink
Add Vue image component
Browse files Browse the repository at this point in the history
  • Loading branch information
Siilwyn committed Jul 23, 2024
1 parent c940700 commit c62120b
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ jobs:

- name: Publish package
run: npm publish --provenance
working-directory: ./image-vue
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
62 changes: 62 additions & 0 deletions image-vue/Image.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { defineComponent, h } from "vue";
import type { ImageLoader } from "../core/types.mjs";
import { generateSrcSet } from "../core/generate-src-set.mjs";

export type { ImageLoader } from "../core/types.mjs";

export type ImageProps = {
src: string;
alt: string;
width: number;
height: number;
loading: "eager" | "lazy";
sizes?: string;
srcset?: string;
decoding?: "async" | "auto" | "sync";
loader: ImageLoader;
// Quality passed to the loader
quality?: number;
};

export const Image = defineComponent(
(props: ImageProps) => {
const loader = props.loader !== undefined
? props.loader
: ({ src }: { src: string }) => src;
const quality = props.quality ?? 45;

return () =>
h("img", {
src: props.src,
alt: props.alt,
width: props.width,
height: props.height,
loading: props.loading,
sizes: props.sizes,
srcset: props.srcset ||
generateSrcSet({
loader,
src: props.src,
width: props.width,
quality,
sizes: props.sizes,
}),
decoding: props.decoding ?? "async",
quality,
});
},
{
props: [
"src",
"alt",
"width",
"height",
"loading",
"sizes",
"srcset",
"decoding",
"loader",
"quality",
],
},
);
34 changes: 34 additions & 0 deletions image-vue/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@voorhoede/image-vue",
"version": "0.0.0",
"description": "Optimized CDN image component",
"type": "module",
"scripts": {
"build": "tsc",
"prepare": "npm run build"
},
"dependencies": {
"vue": "^3.4.27"
},
"devDependencies": {
"@tsconfig/node-lts": "^20.1.3",
"typescript": "^5.4.5"
},
"files": [
"dist"
],
"exports": "./dist/image-vue-pure/Image.mjs",
"types": "./dist/image-vue-pure/Image.d.mts",
"repository": {
"type": "git",
"url": "git+https://github.com/voorhoede/open-source-project.git",
"directory": "image-vue"
},
"keywords": [
"vue",
"image",
"component"
],
"author": "De Voorhoede (https://www.voorhoede.nl/)",
"license": "ISC"
}
10 changes: 10 additions & 0 deletions image-vue/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@tsconfig/node-lts/tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "dist"
},
"files": [
"Image.mts"
]
}
Loading

0 comments on commit c62120b

Please sign in to comment.