rc-scroll-into-view
Version:
Solving the issue of scrollIntoView failing during asynchronous scrolling in JavaScript.
2 lines (1 loc) • 1.11 kB
JavaScript
import{__rest as e,__assign as r}from"/Users/yuchen/project/rc-scroll-into-view/node_modules/tslib/tslib.es6.js";import{jsx as o}from"react/jsx-runtime";import{useSize as t,useDebounceEffect as l}from"ahooks";import{useRef as n,useState as i,useEffect as c}from"react";import{getTarget as s}from"./getTarget.js";var u=function(u){var d=u.children,a=u.selector,f=u.scrollOptions,v=void 0===f?{}:f,m=u.isScrollable,p=void 0===m||m,h=u.scrollRef,b=u.target,g=e(u,["children","selector","scrollOptions","isScrollable","scrollRef","target"]),j=s(b,document),w=n(null),E=t(w),L=i(p),k=L[0],S=L[1],x=function(){S(!1)};return c((function(){void 0!==p&&S(p)}),[p]),l((function(){k&&function(){var e;if(a){var r=document.querySelector(a);null==r||r.scrollIntoView(v)}else h&&(null==h?void 0:h.current)&&(null===(e=h.current)||void 0===e||e.scrollIntoView(v))}()}),[E,k],{wait:300}),c((function(){return k&&j&&(j.addEventListener("scroll",x),j.addEventListener("click",x)),function(){j&&(j.removeEventListener("scroll",x),j.removeEventListener("click",x))}}),[]),o("div",r({ref:w},g,{children:d}))};export{u as default};