UNPKG

@tarojs/components

Version:
1 lines 4.5 kB
import{__awaiter,__generator}from"tslib";import{r as registerInstance,c as createEvent,h,H as Host,g as getElement}from"./index-ab3c86da.js";import{c as classnames}from"./index-c3e4004b.js";import{d as debounce}from"./index-a00a7418.js";import{h as handleStencilNodes}from"./helper-8a85bb65.js";import"@tarojs/taro";var indexCss="taro-scroll-view-core{-webkit-overflow-scrolling:auto;width:100%;display:block}taro-scroll-view-core::-webkit-scrollbar{display:none}.taro-scroll-view__scroll-x{overflow:scroll hidden}.taro-scroll-view__scroll-y{overflow:hidden scroll}";function easeOutScroll(o,t,e,r){if(e===void 0){e=500}if(o===t||typeof o!=="number"){return}var l=t-o;var i=Date.now();var s=t>=o;function n(o,t,e,r){return e*o/r+t}function c(){o=n(Date.now()-i,o,l,e);if(s&&o>=t||!s&&t>=o){r(t);return}r(o);requestAnimationFrame(c)}c()}var ScrollView=function(){function o(o){var t=this;registerInstance(this,o);this.onScroll=createEvent(this,"scroll",3);this.onScrollToUpper=createEvent(this,"scrolltoupper",3);this.onScrollToLower=createEvent(this,"scrolltolower",3);this._scrollLeft=0;this._scrollTop=0;this.upperAndLower=debounce((function(){var o=t.el,e=o.offsetWidth,r=o.offsetHeight,l=o.scrollLeft,i=o.scrollTop,s=o.scrollHeight,n=o.scrollWidth;var c=Number(t.lowerThreshold);var a=Number(t.upperThreshold);if(!isNaN(c)&&(t.scrollY&&r+i+c>=s||t.scrollX&&e+l+c>=n)){t.onScrollToLower.emit({direction:t.scrollX?"right":t.scrollY?"bottom":""})}if(!isNaN(a)&&(t.scrollY&&i<=a||t.scrollX&&l<=a)){t.onScrollToUpper.emit({direction:t.scrollX?"left":t.scrollY?"top":""})}}),200);this.scrollX=false;this.scrollY=false;this.upperThreshold=50;this.lowerThreshold=50;this.mpScrollTop=undefined;this.mpScrollLeft=undefined;this.mpScrollIntoView=undefined;this.mpScrollIntoViewAlignment=undefined;this.animated=false}o.prototype.watchScrollLeft=function(o){var t=Number(o);var e=this.animated;this.mpScrollToMethod({left:t,animated:e})};o.prototype.watchScrollTop=function(o){var t=Number(o);var e=this.animated;this.mpScrollToMethod({top:t,animated:e})};o.prototype.watchScrollIntoView=function(o){this.mpScrollIntoViewMethod(o)};o.prototype.handleScroll=function(o){var t;if(o instanceof CustomEvent)return;o.stopPropagation();(t=o.stopImmediatePropagation)===null||t===void 0?void 0:t.call(o);var e=this.el,r=e.scrollLeft,l=e.scrollTop,i=e.scrollHeight,s=e.scrollWidth;this._scrollLeft=r;this._scrollTop=l;this.upperAndLower();this.onScroll.emit({scrollLeft:r,scrollTop:l,scrollHeight:i,scrollWidth:s})};o.prototype.handleTouchMove=function(o){if(o instanceof CustomEvent)return;o.stopPropagation()};o.prototype.mpScrollToMethod=function(o){return __awaiter(this,void 0,void 0,(function(){var t,e,r,l,i;var s=this;return __generator(this,(function(n){t=o.top,e=o.left,r=o.duration,l=o.animated,i=l===void 0?false:l;if(this.scrollY&&typeof t==="number"&&!isNaN(t)&&t!==this._scrollTop){if(i){easeOutScroll(this._scrollTop,t,r,(function(o){return s.el.scrollTop=o}))}else{this.el.scrollTop=t}this._scrollTop=t}if(this.scrollX&&typeof e==="number"&&!isNaN(e)&&e!==this._scrollLeft){if(i){easeOutScroll(this._scrollLeft,e,r,(function(o){return s.el.scrollLeft=o}))}else{this.el.scrollLeft=e}this._scrollLeft=e}return[2]}))}))};o.prototype.mpScrollIntoViewMethod=function(o){return __awaiter(this,void 0,void 0,(function(){var t;return __generator(this,(function(e){if(typeof o==="string"&&o){(t=document.querySelector("#".concat(o)))===null||t===void 0?void 0:t.scrollIntoView({behavior:this.animated?"smooth":"auto",block:this.scrollY?this.mpScrollIntoViewAlignment||"center":"center",inline:this.scrollX?this.mpScrollIntoViewAlignment||"start":"start"})}return[2]}))}))};o.prototype.componentDidLoad=function(){var o=Number(this.mpScrollTop);var t=Number(this.mpScrollLeft);var e=this.animated;this.mpScrollToMethod({top:o,left:t,animated:e})};o.prototype.componentDidRender=function(){handleStencilNodes(this.el)};o.prototype.render=function(){var o=this,t=o.scrollX,e=o.scrollY;var r=classnames({"taro-scroll-view__scroll-x":t,"taro-scroll-view__scroll-y":e});return h(Host,{class:r},h("slot",null))};Object.defineProperty(o.prototype,"el",{get:function(){return getElement(this)},enumerable:false,configurable:true});Object.defineProperty(o,"watchers",{get:function(){return{mpScrollLeft:["watchScrollLeft"],mpScrollTop:["watchScrollTop"],mpScrollIntoView:["watchScrollIntoView"]}},enumerable:false,configurable:true});return o}();ScrollView.style=indexCss;export{ScrollView as taro_scroll_view_core};