UNPKG

@bothub-chat/ui

Version:

Bothub UI Components

2 lines (1 loc) 1.37 kB
import{useState as e,useRef as t,useEffect as n}from"react";const l=()=>{const[l,o]=e(!0),[r,i]=e(!0),c=t(null);return n((()=>{const e=c.current,t=()=>{var e;const t=null===(e=c.current)||void 0===e?void 0:e.element;if(!t)return;if(!(t.scrollWidth>t.clientWidth))return o(!0),void i(!0);t.scrollWidth-t.scrollLeft<=t.clientWidth+1?i(!0):i(!1),t.scrollLeft<=0?o(!0):o(!1)};if(e&&e.element)return e.element.addEventListener("scroll",t),t(),()=>{e&&e.element&&e.element.removeEventListener("scroll",t)}}),[]),{isLeftDisabled:l,isRightDisabled:r,onScrollLeft:()=>{var e;const t=null===(e=c.current)||void 0===e?void 0:e.element,n=null==t?void 0:t.children;if(!n||!t)return;const l=t.getBoundingClientRect();let o=0,r=-9999;for(let e=0;e<n.length;e+=1){const t=n[e].getBoundingClientRect().left-l.left;t+1<0&&t>r&&(r=Math.floor(t),o=e)}-9999===r&&0===o||n[o].scrollIntoView({behavior:"smooth",block:"nearest",inline:"center"})},onScrollRight:()=>{var e;const t=null===(e=c.current)||void 0===e?void 0:e.element,n=null==t?void 0:t.children;if(!n||!t)return;const l=t.getBoundingClientRect();let o=0,r=9999;const i=t.clientWidth;if(i){for(let e=0;e<n.length;e+=1){const t=n[e].getBoundingClientRect().right-l.left;t-1>i&&t<r&&(r=Math.floor(t),o=e)}9999===r&&0===o||n[o].scrollIntoView({behavior:"smooth",block:"nearest",inline:"center"})}},scrollbarRef:c}};export{l as useSlider};