Skip to content

Commit bb61e34

Browse files
authored
feat: Added callback prop for scroll position (#155)
* Added callback prop for scroll * Review * Replace null prop value with undefined Co-authored-by: Tasso Evangelista <tassoevan7@gmail.com>
1 parent cd0122b commit bb61e34

File tree

1 file changed

+19
-2
lines changed
  • packages/fuselage/src/components/Box/Scrollable

1 file changed

+19
-2
lines changed

packages/fuselage/src/components/Box/Scrollable/index.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import PropTypes from 'prop-types';
2-
import React from 'react';
2+
import React, { useCallback, useRef } from 'react';
33

44
import { useProps } from '../../../hooks';
55

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+
722
const [, PropsProvider] = useProps(({ className, ...props }) => ({
823
className: [
924
className,
@@ -12,6 +27,7 @@ export function Scrollable({ children, horizontal, vertical, smooth }) {
1227
vertical && 'rcx-box--scrollable-vertical',
1328
smooth && 'rcx-box--scrollable-smooth',
1429
].filter(Boolean).join(' '),
30+
onScroll: typeof onScrollContent !== 'undefined' ? handleScroll : undefined,
1531
...props,
1632
}), [horizontal, vertical, smooth]);
1733

@@ -22,4 +38,5 @@ Scrollable.propTypes = {
2238
horizontal: PropTypes.bool,
2339
vertical: PropTypes.bool,
2440
smooth: PropTypes.bool,
41+
onScrollContent: PropTypes.func,
2542
};

0 commit comments

Comments
 (0)