1
1
import PropTypes from 'prop-types' ;
2
- import React from 'react' ;
2
+ import React , { useCallback , useRef } from 'react' ;
3
3
4
4
import { useProps } from '../../../hooks' ;
5
5
6
- export function Scrollable ( { children, horizontal, vertical, smooth } ) {
6
+ export function Scrollable ( { children, horizontal, vertical, smooth, onScrollContent } ) {
7
+ const scrollTimeoutRef = useRef ( ) ;
8
+
9
+ const handleScroll = useCallback ( function ( event ) {
10
+ const { target } = event ;
11
+ const returnTouchingEdges = ( ) => ( { top : ! target . scrollTop , bottom : ! ( target . scrollTop + target . clientHeight - target . scrollHeight ) , left : ! target . scrollLeft , right : ! ( target . scrollLeft + target . clientWidth - target . scrollWidth ) } ) ;
12
+ if ( ! scrollTimeoutRef . current ) {
13
+ onScrollContent ( returnTouchingEdges ( ) ) ;
14
+ }
15
+ clearTimeout ( scrollTimeoutRef . current ) ;
16
+ scrollTimeoutRef . current = setTimeout ( ( ) => {
17
+ scrollTimeoutRef . current = false ;
18
+ onScrollContent ( returnTouchingEdges ( ) ) ;
19
+ } , 200 ) ;
20
+ } , [ onScrollContent ] ) ;
21
+
7
22
const [ , PropsProvider ] = useProps ( ( { className, ...props } ) => ( {
8
23
className : [
9
24
className ,
@@ -12,6 +27,7 @@ export function Scrollable({ children, horizontal, vertical, smooth }) {
12
27
vertical && 'rcx-box--scrollable-vertical' ,
13
28
smooth && 'rcx-box--scrollable-smooth' ,
14
29
] . filter ( Boolean ) . join ( ' ' ) ,
30
+ onScroll : typeof onScrollContent !== 'undefined' ? handleScroll : undefined ,
15
31
...props ,
16
32
} ) , [ horizontal , vertical , smooth ] ) ;
17
33
@@ -22,4 +38,5 @@ Scrollable.propTypes = {
22
38
horizontal : PropTypes . bool ,
23
39
vertical : PropTypes . bool ,
24
40
smooth : PropTypes . bool ,
41
+ onScrollContent : PropTypes . func ,
25
42
} ;
0 commit comments