Created
October 19, 2021 17:55
-
-
Save kirpalmakanga/5232373c75fcec29c454e24c337c7845 to your computer and use it in GitHub Desktop.
Flex scroll container for Solid JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from 'solid-js'; | |
import { HTMLElementEvent } from '../../@types/alltypes'; | |
import { throttle } from '../lib/helpers'; | |
interface Props { | |
className?: string; | |
children: any; | |
ref: (el: HTMLDivElement) => HTMLDivElement; | |
onScroll?: (e: HTMLElementEvent<HTMLDivElement>) => void; | |
onScrollEnd?: () => void; | |
} | |
const ScrollContainer: Component<Props> = (props) => { | |
const handleScroll = throttle((e) => { | |
const { onScroll, onScrollEnd } = props; | |
if (onScroll) onScroll(e); | |
if (onScrollEnd) { | |
const { | |
currentTarget: { scrollTop, scrollHeight, offsetHeight } | |
} = e; | |
if (scrollTop >= scrollHeight - offsetHeight) { | |
onScrollEnd(); | |
} | |
} | |
}, 10); | |
return ( | |
<div | |
ref={props.ref} | |
class="ScrollContainer" | |
classList={{ ...(props.className && { [props.className]: true }) }} | |
onScroll={handleScroll} | |
> | |
<div class="ScrollContainer__Inner">{props.children}</div> | |
</div> | |
); | |
}; | |
export default ScrollContainer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment