Skip to content

Commit

Permalink
feat: use vue 3 global and composition api in ix-source
Browse files Browse the repository at this point in the history
  • Loading branch information
luqven committed Jul 8, 2021
1 parent 3c3d626 commit 619ed84
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions src/plugins/vue-imgix/ix-source.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ensureVueImgixClientSingleton, IVueImgixClient } from './vue-imgix';
import Vue, { CreateElement } from 'vue';
import Component from 'vue-class-component';
import { defineComponent, h } from 'vue';

const IxSourceProps = Vue.extend({

const defaultAttributeMap = {
src: 'src',
srcset: 'srcset',
};

export const IxSource = defineComponent({
props: {
src: {
type: String,
Expand All @@ -11,39 +16,25 @@ const IxSourceProps = Vue.extend({
imgixParams: Object,
attributeConfig: Object,
},
});

const defaultAttributeMap = {
src: 'src',
srcset: 'srcset',
};
setup(props) {
const vueImgixSingleton: IVueImgixClient = ensureVueImgixClientSingleton();

@Component
export class IxSource extends IxSourceProps {
// Using !: here because we ensure it is set in created()
private vueImgixSingleton!: IVueImgixClient;

created() {
this.vueImgixSingleton = ensureVueImgixClientSingleton();
}

render(createElement: CreateElement) {
const imgixParamsFromAttributes = {};

const { srcset } = this.vueImgixSingleton.buildUrlObject(this.src, {
const { srcset } = vueImgixSingleton.buildUrlObject(props.src, {
...imgixParamsFromAttributes,
...this.imgixParams,
...props.imgixParams,
});

const attributeConfig = {
...defaultAttributeMap,
...this.attributeConfig,
...props.attributeConfig,
};

const childAttrs = {
[attributeConfig.srcset]: srcset,
};

return createElement('source', { attrs: childAttrs });
}
}
return () => h('source', childAttrs );
},
});

0 comments on commit 619ed84

Please sign in to comment.