diff --git a/package.json b/package.json index c5885cf48..9c57a507b 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.6", + "@vant/use": "^0.0.7", "vue-lazyload": "1.2.3" }, "peerDependencies": { diff --git a/src/calendar/index.js b/src/calendar/index.js index b46d41c59..43d7452e2 100644 --- a/src/calendar/index.js +++ b/src/calendar/index.js @@ -1,7 +1,7 @@ import { ref, watch, reactive, computed, onMounted, onActivated } from 'vue'; // Utils -import { raf, pick, getScrollTop } from '../utils'; +import { pick, getScrollTop } from '../utils'; import { isDate } from '../utils/validate/date'; import { t, @@ -17,7 +17,7 @@ import { } from './utils'; // Composition -import { useRect } from '@vant/use'; +import { raf, useRect } from '@vant/use'; import { useRefs } from '../composition/use-refs'; import { useExpose } from '../composition/use-expose'; diff --git a/src/circle/index.js b/src/circle/index.js index c64f1c738..0f218eb58 100644 --- a/src/circle/index.js +++ b/src/circle/index.js @@ -1,11 +1,6 @@ import { watch, computed } from 'vue'; -import { - raf, - isObject, - cancelRaf, - getSizeStyle, - createNamespace, -} from '../utils'; +import { raf, cancelRaf } from '@vant/use'; +import { isObject, getSizeStyle, createNamespace } from '../utils'; import { BLUE, WHITE } from '../utils/constant'; const [createComponent, bem] = createNamespace('circle'); diff --git a/src/collapse-item/index.js b/src/collapse-item/index.js index d5118fe19..00ef6c242 100644 --- a/src/collapse-item/index.js +++ b/src/collapse-item/index.js @@ -1,10 +1,10 @@ import { ref, watch, computed, nextTick } from 'vue'; // Utils -import { raf, doubleRaf, createNamespace } from '../utils'; +import { createNamespace } from '../utils'; // Composition -import { useParent } from '@vant/use'; +import { raf, doubleRaf, useParent } from '@vant/use'; import { useExpose } from '../composition/use-expose'; import { useLazyRender } from '../composition/use-lazy-render'; diff --git a/src/count-down/index.js b/src/count-down/index.js index f571e5724..f005a5333 100644 --- a/src/count-down/index.js +++ b/src/count-down/index.js @@ -5,8 +5,8 @@ import { createNamespace } from '../utils'; import { parseFormat } from './utils'; // Composition +import { useCountDown } from '@vant/use'; import { useExpose } from '../composition/use-expose'; -import { useCountDown } from './use-count-down'; const [createComponent, bem] = createNamespace('count-down'); diff --git a/src/count-down/use-count-down.ts b/src/count-down/use-count-down.ts deleted file mode 100644 index a53a51e52..000000000 --- a/src/count-down/use-count-down.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { - ref, - computed, - onActivated, - onDeactivated, - onBeforeUnmount, -} from 'vue'; -import { raf, cancelRaf } from '../utils'; - -export type CurrentTime = { - days: number; - hours: number; - total: number; - minutes: number; - seconds: number; - milliseconds: number; -}; - -export type UseCountDownOptions = { - time: number; - millisecond?: boolean; - onChange?: (current: CurrentTime) => void; - onFinish?: () => void; -}; - -const SECOND = 1000; -const MINUTE = 60 * SECOND; -const HOUR = 60 * MINUTE; -const DAY = 24 * HOUR; - -export function parseTime(time: number): CurrentTime { - const days = Math.floor(time / DAY); - const hours = Math.floor((time % DAY) / HOUR); - const minutes = Math.floor((time % HOUR) / MINUTE); - const seconds = Math.floor((time % MINUTE) / SECOND); - const milliseconds = Math.floor(time % SECOND); - - return { - total: time, - days, - hours, - minutes, - seconds, - milliseconds, - }; -} - -function isSameSecond(time1: number, time2: number): boolean { - return Math.floor(time1 / 1000) === Math.floor(time2 / 1000); -} - -export function useCountDown(options: UseCountDownOptions) { - let rafId: number; - let endTime: number; - let counting: boolean; - let deactivated: boolean; - - const remain = ref(options.time); - const current = computed(() => parseTime(remain.value)); - - const pause = () => { - counting = false; - cancelRaf(rafId); - }; - - const getCurrentRemain = () => Math.max(endTime - Date.now(), 0); - - const setRemain = (value: number) => { - remain.value = value; - options.onChange?.(current.value); - - if (value === 0) { - pause(); - options.onFinish?.(); - } - }; - - const microTick = () => { - rafId = raf(() => { - // in case of call reset immediately after finish - if (counting) { - setRemain(getCurrentRemain()); - - if (remain.value > 0) { - microTick(); - } - } - }); - }; - - const macroTick = () => { - rafId = raf(() => { - // in case of call reset immediately after finish - if (counting) { - const remainRemain = getCurrentRemain(); - - if (!isSameSecond(remainRemain, remain.value) || remainRemain === 0) { - setRemain(remainRemain); - } - - if (remain.value > 0) { - macroTick(); - } - } - }); - }; - - const tick = () => { - if (options.millisecond) { - microTick(); - } else { - macroTick(); - } - }; - - const start = () => { - if (!counting) { - endTime = Date.now() + remain.value; - counting = true; - tick(); - } - }; - - const reset = (totalTime: number) => { - pause(); - remain.value = totalTime; - }; - - onBeforeUnmount(pause); - - onActivated(() => { - if (deactivated) { - counting = true; - deactivated = false; - tick(); - } - }); - - onDeactivated(() => { - if (counting) { - pause(); - deactivated = true; - } - }); - - return { - start, - pause, - reset, - current, - }; -} diff --git a/src/count-down/utils.ts b/src/count-down/utils.ts index 18623f69b..ce66f1fbb 100644 --- a/src/count-down/utils.ts +++ b/src/count-down/utils.ts @@ -1,5 +1,5 @@ import { padZero } from '../utils'; -import { CurrentTime } from './use-count-down'; +import { CurrentTime } from '@vant/use'; export function parseFormat(format: string, currentTime: CurrentTime): string { const { days } = currentTime; diff --git a/src/notice-bar/index.js b/src/notice-bar/index.js index 0ea1f0f01..8e82966ad 100644 --- a/src/notice-bar/index.js +++ b/src/notice-bar/index.js @@ -1,8 +1,8 @@ import { ref, watch, reactive, nextTick, onActivated } from 'vue'; -import { isDef, doubleRaf, createNamespace } from '../utils'; +import { isDef, createNamespace } from '../utils'; // Composition -import { useRect, useEventListener } from '@vant/use'; +import { useRect, doubleRaf, useEventListener } from '@vant/use'; // Components import Icon from '../icon'; diff --git a/src/swipe/index.js b/src/swipe/index.js index eb635d853..92a8c4259 100644 --- a/src/swipe/index.js +++ b/src/swipe/index.js @@ -10,17 +10,12 @@ import { } from 'vue'; // Utils -import { - range, - isHidden, - doubleRaf, - preventDefault, - createNamespace, -} from '../utils'; +import { range, isHidden, preventDefault, createNamespace } from '../utils'; // Composition import { useRect, + doubleRaf, useChildren, useWindowSize, usePageVisibility, diff --git a/src/tabs/utils.ts b/src/tabs/utils.ts index f42afd070..2477dd899 100644 --- a/src/tabs/utils.ts +++ b/src/tabs/utils.ts @@ -1,4 +1,5 @@ -import { raf, cancelRaf, getScrollTop, setScrollTop } from '../utils'; +import { raf, cancelRaf } from '@vant/use'; +import { getScrollTop, setScrollTop } from '../utils'; let rafId: number; diff --git a/src/utils/dom/raf.ts b/src/utils/dom/raf.ts deleted file mode 100644 index 0af210e98..000000000 --- a/src/utils/dom/raf.ts +++ /dev/null @@ -1,36 +0,0 @@ -/** - * requestAnimationFrame polyfill - */ - -import { inBrowser } from '../base'; - -let prev = Date.now(); - -function fallback(fn: FrameRequestCallback): number { - const curr = Date.now(); - const ms = Math.max(0, 16 - (curr - prev)); - const id = setTimeout(fn, ms); - prev = curr + ms; - return id; -} - -const root = (inBrowser ? window : global) as Window; - -const iRaf = root.requestAnimationFrame || fallback; - -const iCancel = root.cancelAnimationFrame || root.clearTimeout; - -export function raf(fn: FrameRequestCallback): number { - return iRaf.call(root, fn); -} - -// double raf for animation -export function doubleRaf(fn: FrameRequestCallback): void { - raf(() => { - raf(fn); - }); -} - -export function cancelRaf(id: number) { - iCancel.call(root, id); -} diff --git a/src/utils/index.ts b/src/utils/index.ts index 01ff55df8..b940e5293 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,7 +3,6 @@ export * from './create'; export * from './format/unit'; export * from './format/number'; export * from './format/string'; -export * from './dom/raf'; export * from './dom/style'; export * from './dom/event'; export * from './dom/scroll'; diff --git a/yarn.lock b/yarn.lock index 6e8ffef8b..305d38df8 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.6": - version "0.0.6" - resolved "https://registry.npmjs.org/@vant/use/-/use-0.0.6.tgz#96939bacbd21964f1cc2a84dc4bfa702c9670175" - integrity sha512-ML5i3U/cbnIYYHocNqZewjbsbgamCJ2bR3Vo9R8znjVzvjBm2OnGBE68UZKUWwBmcF0prBI+QdaLYbkQLow7jA== +"@vant/use@^0.0.7": + version "0.0.7" + resolved "https://registry.npmjs.org/@vant/use/-/use-0.0.7.tgz#9ab1f54dd38c02b2707d2bcab7b725e81402db71" + integrity sha512-7QztMvrYWEJdlOd5xmQNjT+46AdoMZs9h9f72baAtHsl/a4Q+abTzVQo4Rg8E592djsDIVfnlWr9IZ1VomdJag== "@vue/babel-helper-vue-transform-on@^1.0.0-rc.2": version "1.0.0-rc.2"