vant/src/image-preview/function-call.tsx
neverland 140fa0c180
chore: improve withInstall typing (#9354)
* chore: improve withInstall typing

* chore: upd

* fix: missing entry
2021-08-30 20:10:14 +08:00

80 lines
1.7 KiB
TypeScript

import { extend, inBrowser, withInstall, ComponentInstance } from '../utils';
import { mountComponent, usePopupState } from '../utils/mount-component';
import VanImagePreview from './ImagePreview';
import type { App } from 'vue';
import type { ImagePreviewOptions } from './types';
let instance: ComponentInstance;
const defaultConfig: ImagePreviewOptions = {
loop: true,
images: [],
maxZoom: 3,
minZoom: 1 / 3,
onScale: undefined,
onClose: undefined,
onChange: undefined,
teleport: 'body',
className: '',
showIndex: true,
closeable: false,
closeIcon: 'clear',
transition: undefined,
beforeClose: undefined,
overlayStyle: undefined,
startPosition: 0,
swipeDuration: 300,
showIndicators: false,
closeOnPopstate: true,
closeIconPosition: 'top-right',
};
function initInstance() {
({ instance } = mountComponent({
setup() {
const { state, toggle } = usePopupState();
const onClosed = () => {
(state as any).images = [];
};
return () => (
<VanImagePreview
{...state}
{...{
onClosed,
'onUpdate:show': toggle,
}}
/>
);
},
}));
}
const ImagePreview = (
images: string[] | ImagePreviewOptions,
startPosition = 0
) => {
/* istanbul ignore if */
if (!inBrowser) {
return;
}
if (!instance) {
initInstance();
}
const options = Array.isArray(images) ? { images, startPosition } : images;
instance.open(extend({}, defaultConfig, options));
return instance;
};
ImagePreview.Component = withInstall(VanImagePreview);
ImagePreview.install = (app: App) => {
app.use(ImagePreview.Component);
};
export { ImagePreview };