mirror of
https://github.com/youzan/vant.git
synced 2025-10-16 08:00:34 +00:00
feat(use): add useWindowSize
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
export { useToggle } from './useToggle';
|
||||
export { useClickAway } from './useClickAway';
|
||||
export { useWindowSize } from './useWindowSize';
|
||||
export { useScrollParent } from './useScrollParent';
|
||||
export { useEventListener } from './useEventListener';
|
||||
|
18
packages/vant-use/src/useWindowSize/index.ts
Normal file
18
packages/vant-use/src/useWindowSize/index.ts
Normal 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 };
|
||||
}
|
Reference in New Issue
Block a user