Files
habitica/website/client/src/directives/resize.directive.js
T
Matteo Pagliazzi b44fbdcb14 old client structure
2019-10-01 11:44:38 +02:00

32 lines
653 B
JavaScript

import Vue from 'vue';
import _throttle from 'lodash/throttle';
import { emit } from './directive.common';
/**
* v-resize="throttleTimeout", @resized="callback()"
*/
const EVENT_NAME = 'resized';
export default {
bind (el, binding, vnode) {
el.handleWindowResize = _throttle(() => {
emit(vnode, EVENT_NAME, {
width: el.clientWidth,
height: el.clientHeight,
});
}, binding.value);
window.addEventListener('resize', el.handleWindowResize);
// send the first width
Vue.nextTick(el.handleWindowResize);
},
unbind (el) {
window.removeEventListener('resize', el.handleWindowResize);
},
};