chore: prefer using useRect (#9637)

This commit is contained in:
neverland 2021-10-08 14:16:16 +08:00 committed by GitHub
parent a9fedfed24
commit 61cce4c16e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 32 deletions

View File

@ -25,7 +25,7 @@ import {
} from './utils'; } from './utils';
// Composables // Composables
import { useToggle } from '@vant/use'; import { useRect, useToggle } from '@vant/use';
import { useExpose } from '../composables/use-expose'; import { useExpose } from '../composables/use-expose';
import { useHeight } from '../composables/use-height'; import { useHeight } from '../composables/use-height';
@ -101,11 +101,7 @@ export default defineComponent({
const el = props.showSubtitle ? daysRef.value : monthRef.value; const el = props.showSubtitle ? daysRef.value : monthRef.value;
if (el) { if (el) {
const scrollTop = const scrollTop = useRect(el).top - useRect(body).top + body.scrollTop;
el.getBoundingClientRect().top -
body.getBoundingClientRect().top +
body.scrollTop;
setScrollTop(body, scrollTop); setScrollTop(body, scrollTop);
} }
}; };

View File

@ -23,7 +23,7 @@ import {
} from '../utils'; } from '../utils';
// Composables // Composables
import { useWindowSize } from '@vant/use'; import { useRect, useWindowSize } from '@vant/use';
import { useExpose } from '../composables/use-expose'; import { useExpose } from '../composables/use-expose';
// Components // Components
@ -82,7 +82,7 @@ export default defineComponent({
const resize = () => { const resize = () => {
if (swipeRef.value) { if (swipeRef.value) {
const rect = swipeRef.value.$el.getBoundingClientRect(); const rect = useRect(swipeRef.value.$el);
state.rootWidth = rect.width; state.rootWidth = rect.width;
state.rootHeight = rect.height; state.rootHeight = rect.height;
swipeRef.value.resize(); swipeRef.value.resize();

View File

@ -12,7 +12,7 @@ import {
} from '../utils'; } from '../utils';
// Composables // Composables
import { useCustomFieldValue } from '@vant/use'; import { useRect, useCustomFieldValue } from '@vant/use';
import { useRefs } from '../composables/use-refs'; import { useRefs } from '../composables/use-refs';
import { useTouch } from '../composables/use-touch'; import { useTouch } from '../composables/use-touch';
@ -101,7 +101,7 @@ export default defineComponent({
let ranges: Array<{ left: number; score: number }>; let ranges: Array<{ left: number; score: number }>;
const updateRanges = () => { const updateRanges = () => {
const rects = itemRefs.value.map((item) => item.getBoundingClientRect()); const rects = itemRefs.value.map(useRect);
ranges = []; ranges = [];
rects.forEach((rect, index) => { rects.forEach((rect, index) => {

View File

@ -24,13 +24,11 @@ import {
truthProp, truthProp,
numericProp, numericProp,
Interceptor, Interceptor,
getVisibleTop,
getElementTop, getElementTop,
makeStringProp, makeStringProp,
callInterceptor, callInterceptor,
createNamespace, createNamespace,
makeNumericProp, makeNumericProp,
getVisibleHeight,
setRootScrollTop, setRootScrollTop,
ComponentInstance, ComponentInstance,
BORDER_TOP_BOTTOM, BORDER_TOP_BOTTOM,
@ -39,6 +37,7 @@ import { scrollLeftTo, scrollTopTo } from './utils';
// Composables // Composables
import { import {
useRect,
useChildren, useChildren,
useWindowSize, useWindowSize,
useScrollParent, useScrollParent,
@ -342,7 +341,7 @@ export default defineComponent({
const getCurrentIndexOnScroll = () => { const getCurrentIndexOnScroll = () => {
for (let index = 0; index < children.length; index++) { for (let index = 0; index < children.length; index++) {
const top = getVisibleTop(children[index].$el); const { top } = useRect(children[index].$el);
if (top > scrollOffset.value) { if (top > scrollOffset.value) {
return index === 0 ? 0 : index - 1; return index === 0 ? 0 : index - 1;
@ -455,7 +454,9 @@ export default defineComponent({
setCurrentIndexByName(props.active); setCurrentIndexByName(props.active);
nextTick(() => { nextTick(() => {
state.inited = true; state.inited = true;
tabHeight = getVisibleHeight(wrapRef.value!); if (wrapRef.value) {
tabHeight = useRect(wrapRef.value).height;
}
scrollIntoView(true); scrollIntoView(true);
}); });
}; };

View File

@ -1,10 +1,9 @@
import { useRect } from '@vant/use';
import { unref, Ref } from 'vue'; import { unref, Ref } from 'vue';
import { isIOS as checkIsIOS } from './validate'; import { isIOS as checkIsIOS } from './validate';
export type ScrollElement = Element | Window; export type ScrollElement = Element | Window;
const isWindow = (val: unknown): val is Window => val === window;
export function getScrollTop(el: ScrollElement): number { export function getScrollTop(el: ScrollElement): number {
const top = 'scrollTop' in el ? el.scrollTop : el.pageYOffset; const top = 'scrollTop' in el ? el.scrollTop : el.pageYOffset;
@ -36,26 +35,12 @@ export function setRootScrollTop(value: number) {
// get distance from element top to page top or scroller top // get distance from element top to page top or scroller top
export function getElementTop(el: ScrollElement, scroller?: ScrollElement) { export function getElementTop(el: ScrollElement, scroller?: ScrollElement) {
if (isWindow(el)) { if (el === window) {
return 0; return 0;
} }
const scrollTop = scroller ? getScrollTop(scroller) : getRootScrollTop(); const scrollTop = scroller ? getScrollTop(scroller) : getRootScrollTop();
return el.getBoundingClientRect().top + scrollTop; return useRect(el).top + scrollTop;
}
export function getVisibleHeight(el: ScrollElement) {
if (isWindow(el)) {
return el.innerHeight;
}
return el.getBoundingClientRect().height;
}
export function getVisibleTop(el: ScrollElement) {
if (isWindow(el)) {
return 0;
}
return el.getBoundingClientRect().top;
} }
const isIOS = checkIsIOS(); const isIOS = checkIsIOS();