UNPKG

vuetify

Version:

Vue Material Component Framework

1 lines 6.6 kB
{"version":3,"file":"scroll.mjs","names":["computed","getCurrentInstance","onBeforeUnmount","onMounted","ref","watch","consoleWarn","propsFactory","makeScrollProps","scrollTarget","type","String","scrollThreshold","Number","useScroll","props","args","thresholdMetCallback","canScroll","previousScroll","target","currentScroll","savedScroll","currentThreshold","isScrollActive","isScrollingUp","computedScrollThreshold","onScroll","targetEl","value","pageYOffset","scrollTop","Math","abs","newTarget","document","querySelector","window","removeEventListener","addEventListener","passive","immediate","thresholdMet"],"sources":["../../src/composables/scroll.ts"],"sourcesContent":["// Utilities\nimport {\n computed,\n getCurrentInstance,\n onBeforeUnmount,\n onMounted,\n ref,\n watch,\n} from 'vue'\nimport { consoleWarn, propsFactory } from '@/util'\n\n// Types\nimport type { Ref } from 'vue'\n\nexport interface ScrollProps {\n scrollTarget?: string\n scrollThreshold?: string | number\n}\n\nexport interface ThresholdMetCallbackData {\n isScrollingUp: boolean\n currentThreshold: number\n savedScroll: Ref<number>\n}\n\n// Composables\nexport const makeScrollProps = propsFactory({\n scrollTarget: {\n type: String,\n },\n scrollThreshold: {\n type: [String, Number],\n },\n}, 'scroll')\n\nexport interface ScrollArguments {\n thresholdMetCallback?: (data: ThresholdMetCallbackData) => void\n scrollThreshold?: Readonly<Ref<number>>\n canScroll?: Readonly<Ref<boolean>>\n}\n\nexport function useScroll (\n props: ScrollProps,\n args: ScrollArguments = {},\n) {\n const { thresholdMetCallback, scrollThreshold, canScroll } = args\n let previousScroll = 0\n const target = ref<Element | Window | null>(null)\n const currentScroll = ref(0)\n const savedScroll = ref(0)\n const currentThreshold = ref(0)\n const isScrollActive = ref(false)\n const isScrollingUp = ref(false)\n\n const computedScrollThreshold = computed(() => {\n return Number(props.scrollThreshold ?? scrollThreshold ?? 300)\n })\n\n const onScroll = () => {\n const targetEl = target.value\n\n if (!targetEl || (canScroll && !canScroll.value)) return\n\n previousScroll = currentScroll.value\n currentScroll.value = ('window' in targetEl) ? targetEl.pageYOffset : targetEl.scrollTop\n\n isScrollingUp.value = currentScroll.value < previousScroll\n currentThreshold.value = Math.abs(currentScroll.value - computedScrollThreshold.value)\n }\n\n watch(isScrollingUp, () => {\n savedScroll.value = savedScroll.value || currentScroll.value\n })\n\n watch(isScrollActive, () => {\n savedScroll.value = 0\n })\n\n onMounted(() => {\n watch(() => props.scrollTarget, scrollTarget => {\n const newTarget = scrollTarget ? document.querySelector(scrollTarget) : window\n\n if (!newTarget) {\n consoleWarn(`Unable to locate element with identifier ${scrollTarget}`, getCurrentInstance())\n return\n }\n\n if (newTarget === target.value) return\n\n target.value?.removeEventListener('scroll', onScroll)\n target.value = newTarget\n target.value.addEventListener('scroll', onScroll, { passive: true })\n }, { immediate: true })\n })\n\n onBeforeUnmount(() => {\n target.value?.removeEventListener('scroll', onScroll)\n })\n\n thresholdMetCallback && watch(() => (\n Math.abs(currentScroll.value - savedScroll.value) > computedScrollThreshold.value\n ), thresholdMet => {\n thresholdMet && thresholdMetCallback({\n currentThreshold: currentThreshold.value,\n isScrollingUp: isScrollingUp.value,\n savedScroll,\n })\n }, { immediate: true })\n\n // Do we need this? If yes - seems that\n // there's no need to expose onScroll\n canScroll && watch(canScroll, onScroll, { immediate: true })\n\n return {\n isScrollActive,\n\n // required only for testing\n // probably can be removed\n // later (2 chars chlng)\n isScrollingUp,\n savedScroll,\n }\n}\n"],"mappings":"AAAA;AACA,SACEA,QAAQ,EACRC,kBAAkB,EAClBC,eAAe,EACfC,SAAS,EACTC,GAAG,EACHC,KAAK,QACA,KAAK;AAAA,SACHC,WAAW,EAAEC,YAAY,6BAElC;AAcA;AACA,OAAO,MAAMC,eAAe,GAAGD,YAAY,CAAC;EAC1CE,YAAY,EAAE;IACZC,IAAI,EAAEC;EACR,CAAC;EACDC,eAAe,EAAE;IACfF,IAAI,EAAE,CAACC,MAAM,EAAEE,MAAM;EACvB;AACF,CAAC,EAAE,QAAQ,CAAC;AAQZ,OAAO,SAASC,SAAS,CACvBC,KAA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