scrollStrategies.mjs.map 11 KB

1
  1. {"version":3,"file":"scrollStrategies.mjs","names":["effectScope","nextTick","onScopeDispose","watchEffect","requestNewFrame","convertToUnit","getScrollParents","hasScrollbar","IN_BROWSER","propsFactory","scrollStrategies","none","close","closeScrollStrategy","block","blockScrollStrategy","reposition","repositionScrollStrategy","makeScrollStrategyProps","scrollStrategy","type","String","Function","default","validator","val","useScrollStrategies","props","data","scope","stop","isActive","value","active","run","onScroll","e","bindScroll","activatorEl","contentEl","offsetParent","root","scrollElements","Set","contained","undefined","filter","el","classList","contains","scrollbarWidth","window","innerWidth","document","documentElement","offsetWidth","scrollableParent","add","forEach","i","style","setProperty","scrollLeft","scrollTop","x","parseFloat","getPropertyValue","y","removeProperty","remove","slow","raf","ric","update","start","performance","now","updateLocation","time","requestIdleCallback","cb","cancelAnimationFrame","requestAnimationFrame","cancelIdleCallback","addEventListener","passive","removeEventListener"],"sources":["../../../src/components/VOverlay/scrollStrategies.ts"],"sourcesContent":["// Utilities\nimport { effectScope, nextTick, onScopeDispose, watchEffect } from 'vue'\nimport { requestNewFrame } from './requestNewFrame'\nimport { convertToUnit, getScrollParents, hasScrollbar, IN_BROWSER, propsFactory } from '@/util'\n\n// Types\nimport type { EffectScope, PropType, Ref } from 'vue'\n\nexport interface ScrollStrategyData {\n root: Ref<HTMLElement | undefined>\n contentEl: Ref<HTMLElement | undefined>\n activatorEl: Ref<HTMLElement | undefined>\n isActive: Ref<boolean>\n updateLocation: Ref<((e: Event) => void) | undefined>\n}\n\ntype ScrollStrategyFn = (data: ScrollStrategyData, props: StrategyProps, scope: EffectScope) => void\n\nconst scrollStrategies = {\n none: null,\n close: closeScrollStrategy,\n block: blockScrollStrategy,\n reposition: repositionScrollStrategy,\n}\n\nexport interface StrategyProps {\n scrollStrategy: keyof typeof scrollStrategies | ScrollStrategyFn\n contained: boolean | undefined\n}\n\nexport const makeScrollStrategyProps = propsFactory({\n scrollStrategy: {\n type: [String, Function] as PropType<StrategyProps['scrollStrategy']>,\n default: 'block',\n validator: (val: any) => typeof val === 'function' || val in scrollStrategies,\n },\n}, 'VOverlay-scroll-strategies')\n\nexport function useScrollStrategies (\n props: StrategyProps,\n data: ScrollStrategyData\n) {\n if (!IN_BROWSER) return\n\n let scope: EffectScope | undefined\n watchEffect(async () => {\n scope?.stop()\n\n if (!(data.isActive.value && props.scrollStrategy)) return\n\n scope = effectScope()\n await nextTick()\n scope.active && scope.run(() => {\n if (typeof props.scrollStrategy === 'function') {\n props.scrollStrategy(data, props, scope!)\n } else {\n scrollStrategies[props.scrollStrategy]?.(data, props, scope!)\n }\n })\n })\n\n onScopeDispose(() => {\n scope?.stop()\n })\n}\n\nfunction closeScrollStrategy (data: ScrollStrategyData) {\n function onScroll (e: Event) {\n data.isActive.value = false\n }\n\n bindScroll(data.activatorEl.value ?? data.contentEl.value, onScroll)\n}\n\nfunction blockScrollStrategy (data: ScrollStrategyData, props: StrategyProps) {\n const offsetParent = data.root.value?.offsetParent\n const scrollElements = [...new Set([\n ...getScrollParents(data.activatorEl.value, props.contained ? offsetParent : undefined),\n ...getScrollParents(data.contentEl.value, props.contained ? offsetParent : undefined),\n ])].filter(el => !el.classList.contains('v-overlay-scroll-blocked'))\n const scrollbarWidth = window.innerWidth - document.documentElement.offsetWidth\n\n const scrollableParent = (el => hasScrollbar(el) && el)(offsetParent || document.documentElement)\n if (scrollableParent) {\n data.root.value!.classList.add('v-overlay--scroll-blocked')\n }\n\n scrollElements.forEach((el, i) => {\n el.style.setProperty('--v-body-scroll-x', convertToUnit(-el.scrollLeft))\n el.style.setProperty('--v-body-scroll-y', convertToUnit(-el.scrollTop))\n\n if (el !== document.documentElement) {\n el.style.setProperty('--v-scrollbar-offset', convertToUnit(scrollbarWidth))\n }\n\n el.classList.add('v-overlay-scroll-blocked')\n })\n\n onScopeDispose(() => {\n scrollElements.forEach((el, i) => {\n const x = parseFloat(el.style.getPropertyValue('--v-body-scroll-x'))\n const y = parseFloat(el.style.getPropertyValue('--v-body-scroll-y'))\n\n el.style.removeProperty('--v-body-scroll-x')\n el.style.removeProperty('--v-body-scroll-y')\n el.style.removeProperty('--v-scrollbar-offset')\n el.classList.remove('v-overlay-scroll-blocked')\n\n el.scrollLeft = -x\n el.scrollTop = -y\n })\n if (scrollableParent) {\n data.root.value!.classList.remove('v-overlay--scroll-blocked')\n }\n })\n}\n\nfunction repositionScrollStrategy (data: ScrollStrategyData, props: StrategyProps, scope: EffectScope) {\n let slow = false\n let raf = -1\n let ric = -1\n\n function update (e: Event) {\n requestNewFrame(() => {\n const start = performance.now()\n data.updateLocation.value?.(e)\n const time = performance.now() - start\n slow = time / (1000 / 60) > 2\n })\n }\n\n ric = (typeof requestIdleCallback === 'undefined' ? (cb: Function) => cb() : requestIdleCallback)(() => {\n scope.run(() => {\n bindScroll(data.activatorEl.value ?? data.contentEl.value, e => {\n if (slow) {\n // If the position calculation is slow,\n // defer updates until scrolling is finished.\n // Browsers usually fire one scroll event per frame so\n // we just wait until we've got two frames without an event\n cancelAnimationFrame(raf)\n raf = requestAnimationFrame(() => {\n raf = requestAnimationFrame(() => {\n update(e)\n })\n })\n } else {\n update(e)\n }\n })\n })\n })\n\n onScopeDispose(() => {\n typeof cancelIdleCallback !== 'undefined' && cancelIdleCallback(ric)\n cancelAnimationFrame(raf)\n })\n}\n\n/** @private */\nfunction bindScroll (el: HTMLElement | undefined, onScroll: (e: Event) => void) {\n const scrollElements = [document, ...getScrollParents(el)]\n scrollElements.forEach(el => {\n el.addEventListener('scroll', onScroll, { passive: true })\n })\n\n onScopeDispose(() => {\n scrollElements.forEach(el => {\n el.removeEventListener('scroll', onScroll)\n })\n })\n}\n"],"mappings":"AAAA;AACA,SAASA,WAAW,EAAEC,QAAQ,EAAEC,cAAc,EAAEC,WAAW,QAAQ,KAAK;AAAA,SAC/DC,eAAe;AAAA,SACfC,aAAa,EAAEC,gBAAgB,EAAEC,YAAY,EAAEC,UAAU,EAAEC,YAAY,gCAEhF;AAaA,MAAMC,gBAAgB,GAAG;E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