feat(directive): add custom directive v-resize

This commit is contained in:
xiaoxian521
2021-09-03 14:46:35 +08:00
parent 0bd4f4ff68
commit 8b8491912a
9 changed files with 330 additions and 194 deletions

View File

@@ -0,0 +1,28 @@
import { Directive } from "vue";
import type { DirectiveBinding } from "vue";
import elementResizeDetectorMaker from "element-resize-detector";
import type { Erd } from "element-resize-detector";
import { emitter } from "/@/utils/mitt";
const erd: Erd = elementResizeDetectorMaker({
strategy: "scroll"
});
export const resize: Directive = {
mounted(el: HTMLElement, binding?: DirectiveBinding, vnode?: any) {
erd.listenTo(el, elem => {
const width = elem.offsetWidth;
const height = elem.offsetHeight;
if (binding?.instance) {
emitter.emit("resize", { detail: { width, height } });
} else {
vnode.el.dispatchEvent(
new CustomEvent("resize", { detail: { width, height } })
);
}
});
},
unmounted(el: HTMLElement) {
erd.uninstall(el);
}
};