import React, { FC, useEffect, useReducer, useRef, useState } from 'react'; /** * @description */ export type PanAndFadeProps = { vertical? : boolean, horizontal? : boolean, style? : React.CSSProperties } const getClientScrollPos = (el : HTMLElement)=>{ const x = el.scrollLeft; const y = el.scrollTop; return { x, y } } const HotScroller : FC<{ el : HTMLElement, timing? : number horizontalSpeed? : number, verticalSpeed? : number, forceUpdate? : ()=>void }> = ({children, el, timing=50, horizontalSpeed=10, verticalSpeed=0, forceUpdate})=>{ const [updater, setUpdater] = useState|undefined>() const {x , y} = el ? getClientScrollPos(el) : {x : 0, y : 0}; const makeRepeatingUpdate = ()=>{ return setInterval(()=>{ if(el){ el.scroll({ top : el.scrollTop + verticalSpeed, left : el.scrollLeft + horizontalSpeed, behavior : "smooth" }); } }, timing)} return (
{ if(updater){ clearInterval(updater); } setUpdater(makeRepeatingUpdate()); }} onMouseLeave={()=>{ if(updater){ clearInterval(updater); if(forceUpdate){ forceUpdate(); } } }} style={{ height: "100%", width : "100%", right : 0, zIndex: 1000 }} > {children}
) } const Fader : FC<{ rotation? : React.CSSProperties["rotate"] }> = ({rotation = "0deg"})=>{ return (
) } const HotScrollingCorners : FC<{el: HTMLElement, horizontalSpeed? : number, verticalSpeed? : number}> = ({ el, horizontalSpeed=100, verticalSpeed=0, })=>{ const [tick, forceUpdate] = useReducer(x=>x+1, 0) const {x , y} = el ? getClientScrollPos(el) : {x : 0, y : 0}; return (
{horizontalSpeed > 0 && (el ? x > 1 : true) &&
} {horizontalSpeed > 0 && (el ? ((x + el.offsetWidth) < el.scrollWidth) : true) &&
} {verticalSpeed > 0 && (el ? y < el.clientHeight : true) &&
} {verticalSpeed > 0 && (el ? y > 1: true) &&
}
) } /** * Component fades the around the edges an pans on hover. * @param param0 * @returns */ export const PanAndFade : FC = ({vertical=false, horizontal=true, children, style}) =>{ const ref = useRef(null); const [el, setEl] = useState(ref.current); useEffect(()=>{ if(el !== ref.current){ setEl(ref.current); } }) return (
{children}
) }