diff --git a/package.json b/package.json index a587ae1c5..89c22efc0 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "dependencies": { "@babel/runtime": "7.x", "@vant/icons": "1.3.0", - "@vant/use": "^0.0.3", + "@vant/use": "^0.0.4", "vue-lazyload": "1.2.3" }, "peerDependencies": { diff --git a/packages/vant-use/changelog.md b/packages/vant-use/changelog.md index bb18c1cdb..f2d1939f3 100644 --- a/packages/vant-use/changelog.md +++ b/packages/vant-use/changelog.md @@ -1,5 +1,11 @@ # 更新日志 +### v0.0.4 + +`2020-10-05` + +- 新增 `useRect` 方法 + ### v0.0.3 `2020-09-27` diff --git a/src/calendar/components/Month.js b/src/calendar/components/Month.js index fdcdfb8f0..70754aba0 100644 --- a/src/calendar/components/Month.js +++ b/src/calendar/components/Month.js @@ -16,7 +16,7 @@ import { import { useToggle } from '@vant/use'; import { useRefs } from '../../composition/use-refs'; import { useExpose } from '../../composition/use-expose'; -import { useRect, useHeight } from '../../composition/use-rect'; +import { useRect, useHeight } from '../../composition/use-height'; // Components import Day from './Day'; diff --git a/src/calendar/index.js b/src/calendar/index.js index 69cc13105..b46d41c59 100644 --- a/src/calendar/index.js +++ b/src/calendar/index.js @@ -17,8 +17,8 @@ import { } from './utils'; // Composition +import { useRect } from '@vant/use'; import { useRefs } from '../composition/use-refs'; -import { useRect } from '../composition/use-rect'; import { useExpose } from '../composition/use-expose'; // Components diff --git a/src/composition/use-rect.ts b/src/composition/use-height.ts similarity index 54% rename from src/composition/use-rect.ts rename to src/composition/use-height.ts index 090827f94..3120dd8e7 100644 --- a/src/composition/use-rect.ts +++ b/src/composition/use-height.ts @@ -1,8 +1,5 @@ -import { Ref, ref, unref, onMounted, nextTick } from 'vue'; - -export const useRect = (element: Element | Ref) => { - return unref(element).getBoundingClientRect(); -}; +import { useRect } from '@vant/use'; +import { Ref, ref, onMounted, nextTick } from 'vue'; export const useHeight = (element: Element | Ref) => { const height = ref(); diff --git a/src/composition/use-placeholder.tsx b/src/composition/use-placeholder.tsx index 02e5727e6..8361023b5 100644 --- a/src/composition/use-placeholder.tsx +++ b/src/composition/use-placeholder.tsx @@ -1,4 +1,4 @@ -import { useHeight } from './use-rect'; +import { useHeight } from './use-height'; import type { Ref, VNode } from 'vue'; import type { BEM } from '../utils/create/bem'; diff --git a/src/dropdown-menu/index.js b/src/dropdown-menu/index.js index 57ffc290c..8e5862059 100644 --- a/src/dropdown-menu/index.js +++ b/src/dropdown-menu/index.js @@ -5,12 +5,12 @@ import { createNamespace, isDef } from '../utils'; // Composition import { + useRect, useChildren, useClickAway, useScrollParent, useEventListener, } from '@vant/use'; -import { useRect } from '../composition/use-rect'; const [createComponent, bem] = createNamespace('dropdown-menu'); diff --git a/src/index-anchor/index.js b/src/index-anchor/index.js index 5a16c0404..df74a9200 100644 --- a/src/index-anchor/index.js +++ b/src/index-anchor/index.js @@ -7,7 +7,7 @@ import { INDEX_BAR_KEY } from '../index-bar'; // Composition import { useParent } from '@vant/use'; -import { useHeight } from '../composition/use-rect'; +import { useHeight } from '../composition/use-height'; import { useExpose } from '../composition/use-expose'; const [createComponent, bem] = createNamespace('index-anchor'); diff --git a/src/index-bar/index.js b/src/index-bar/index.js index f4aa08996..3adff0b03 100644 --- a/src/index-bar/index.js +++ b/src/index-bar/index.js @@ -13,8 +13,12 @@ import { } from '../utils'; // Composition -import { useChildren, useScrollParent, useEventListener } from '@vant/use'; -import { useRect } from '../composition/use-rect'; +import { + useRect, + useChildren, + useScrollParent, + useEventListener, +} from '@vant/use'; import { useTouch } from '../composition/use-touch'; export const INDEX_BAR_KEY = 'vanIndexBar'; diff --git a/src/list/index.js b/src/list/index.js index 77081bc61..65555d360 100644 --- a/src/list/index.js +++ b/src/list/index.js @@ -4,8 +4,7 @@ import { ref, watch, nextTick, onUpdated, onMounted } from 'vue'; import { isHidden, createNamespace } from '../utils'; // Composition -import { useScrollParent, useEventListener } from '@vant/use'; -import { useRect } from '../composition/use-rect'; +import { useRect, useScrollParent, useEventListener } from '@vant/use'; import { useExpose } from '../composition/use-expose'; // Components @@ -44,19 +43,6 @@ export default createComponent({ const placeholder = ref(); const scrollParent = useScrollParent(root); - const getScrollParentRect = () => { - const element = scrollParent.value; - if (element.getBoundingClientRect) { - return element.getBoundingClientRect(); - } - const height = element.innerHeight; - return { - top: 0, - height, - bottom: height, - }; - }; - const check = () => { nextTick(() => { if (loading.value || props.finished || props.error) { @@ -64,7 +50,7 @@ export default createComponent({ } const { offset, direction } = props; - const scrollParentRect = getScrollParentRect(); + const scrollParentRect = useRect(scrollParent); if (!scrollParentRect.height || isHidden(root)) { return false; diff --git a/src/notice-bar/index.js b/src/notice-bar/index.js index 58874460a..0ea1f0f01 100644 --- a/src/notice-bar/index.js +++ b/src/notice-bar/index.js @@ -2,8 +2,7 @@ import { ref, watch, reactive, nextTick, onActivated } from 'vue'; import { isDef, doubleRaf, createNamespace } from '../utils'; // Composition -import { useRect } from '../composition/use-rect'; -import { useEventListener } from '@vant/use'; +import { useRect, useEventListener } from '@vant/use'; // Components import Icon from '../icon'; diff --git a/src/slider/index.js b/src/slider/index.js index bc0bcc98e..aa9b68847 100644 --- a/src/slider/index.js +++ b/src/slider/index.js @@ -10,7 +10,7 @@ import { import { deepClone } from '../utils/deep-clone'; // Composition -import { useRect } from '../composition/use-rect'; +import { useRect } from '@vant/use'; import { useTouch } from '../composition/use-touch'; import { useLinkField } from '../composition/use-link-field'; diff --git a/src/swipe-cell/index.js b/src/swipe-cell/index.js index e8cef67c2..33410c0c4 100644 --- a/src/swipe-cell/index.js +++ b/src/swipe-cell/index.js @@ -5,8 +5,7 @@ import { range, createNamespace, preventDefault } from '../utils'; import { callInterceptor } from '../utils/interceptor'; // Composition -import { useClickAway } from '@vant/use'; -import { useRect } from '../composition/use-rect'; +import { useRect, useClickAway } from '@vant/use'; import { useTouch } from '../composition/use-touch'; import { useExpose } from '../composition/use-expose'; diff --git a/src/swipe/index.js b/src/swipe/index.js index 2f2d38306..eb635d853 100644 --- a/src/swipe/index.js +++ b/src/swipe/index.js @@ -19,8 +19,12 @@ import { } from '../utils'; // Composition -import { useChildren, useWindowSize, usePageVisibility } from '@vant/use'; -import { useRect } from '../composition/use-rect'; +import { + useRect, + useChildren, + useWindowSize, + usePageVisibility, +} from '@vant/use'; import { useTouch } from '../composition/use-touch'; import { useExpose } from '../composition/use-expose'; diff --git a/yarn.lock b/yarn.lock index 10ac8c696..9c6790d48 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2175,10 +2175,10 @@ resolved "https://registry.yarnpkg.com/@vant/touch-emulator/-/touch-emulator-1.2.0.tgz#486300b23e57db9ce9231a04e0a0c621c68692d8" integrity sha512-sJ97zU85zOq51qoi7+CpBEcOyH3CitjP1KC7/GQwqaurUJni+EP7/F9n0HMnAh8GXMjgtgDBNJ5z48x+coNKYQ== -"@vant/use@^0.0.3": - version "0.0.3" - resolved "https://registry.npmjs.org/@vant/use/-/use-0.0.3.tgz#e3043830206344442a22d302d556c2e36b492eb5" - integrity sha512-7B7tmc8OqkNlMsNzZrIBy2hE8pBvQexBdE74+xr8/HtLhFbVkiZeO+YsOmgYTrsnSEvACBreoIkFX2tjRjzdMw== +"@vant/use@^0.0.4": + version "0.0.4" + resolved "https://registry.npmjs.org/@vant/use/-/use-0.0.4.tgz#c02fb1b4cec53135efc85b1a7479ab186c0e5e73" + integrity sha512-beX+RH6SCyOIZ9nyod2nIK1/bNSPAAZoOvXQ2OZ0Dv9Dd55MyVnDTBZ1WCgE8Hf/PjgSOuIMp715ekEncSHoEg== "@vue/babel-helper-vue-transform-on@^1.0.0-rc.2": version "1.0.0-rc.2"