feat(use): add useWindowSize

This commit is contained in:
chenjiahan 2020-09-14 17:48:14 +08:00
parent ea846033f7
commit 4fa602bc77
2 changed files with 19 additions and 0 deletions

View File

@ -1,4 +1,5 @@
export { useToggle } from './useToggle'; export { useToggle } from './useToggle';
export { useClickAway } from './useClickAway'; export { useClickAway } from './useClickAway';
export { useWindowSize } from './useWindowSize';
export { useScrollParent } from './useScrollParent'; export { useScrollParent } from './useScrollParent';
export { useEventListener } from './useEventListener'; export { useEventListener } from './useEventListener';

View File

@ -0,0 +1,18 @@
import { ref } from 'vue';
import { inBrowser } from '../shared';
import { useEventListener } from '../useEventListener';
export function useWindowSize(initialWidth = 0, initialHeight = 0) {
const width = ref(inBrowser ? window.innerWidth : initialWidth);
const height = ref(inBrowser ? window.innerHeight : initialHeight);
const onResize = () => {
width.value = window.innerWidth;
height.value = window.innerHeight;
};
useEventListener('resize', onResize);
useEventListener('orientationchange', onResize);
return { width, height };
}