Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(FEC-14059): fix the height of scrollable component #909

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/components/scrollable/scrollable.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@
display: flex;
width: 100%;

.items-container {
display: flex;
width: 100%;
height: fit-content;
SivanA-Kaltura marked this conversation as resolved.
Show resolved Hide resolved
}

&.horizontal {
flex-direction: row;
overflow: auto hidden;
.items-container {
flex-direction: row;
}
}
&.vertical {
flex-direction: column;
height: 100%;
overflow: hidden auto;
.items-container {
flex-direction: column;
}
}
&::-webkit-scrollbar {
height: 4px;
Expand Down
35 changes: 23 additions & 12 deletions src/components/scrollable/scrollable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ const SCROLL_BAR_TIMEOUT = 250;
* @returns {any} Scrollable component
*/
const Scrollable = ({children, isVertical}: {children: ComponentChildren; isVertical: boolean}) => {
const ref: MutableRef<HTMLDivElement | null> = useRef<HTMLDivElement | null>(null);
const scrollableRef: MutableRef<HTMLDivElement | null> = useRef<HTMLDivElement | null>(null);
const itemsContainerRef: MutableRef<HTMLDivElement | null> = useRef<HTMLDivElement | null>(null);
const [scrolling, setScrolling] = useState<boolean>(false);
const [scrollTimeoutId, setScrollTimeoutId] = useState<number>(-1);
const [scrollableHeight, setScrollableHeight] = useState<string>('');
const [scrollableHeight, setScrollableHeight] = useState<number>(-1);

const handleScroll = (): void => {
clearTimeout(scrollTimeoutId);
Expand All @@ -31,34 +32,44 @@ const Scrollable = ({children, isVertical}: {children: ComponentChildren; isVert

const handleWheel = (e: WheelEvent): void => {
e.preventDefault();
if (ref?.current) {
ref.current.scrollLeft += e.deltaY;
if (scrollableRef?.current) {
scrollableRef.current.scrollLeft += e.deltaY;
handleScroll();
}
};

useLayoutEffect(() => {
if (isVertical && ref?.current) {
const scrollableEl = ref.current;
if (isVertical && scrollableRef?.current && itemsContainerRef?.current) {
const scrollableEl = scrollableRef.current;
const parentHeight = scrollableEl.parentElement?.clientHeight;
if (parentHeight) {
const cs = getComputedStyle(scrollableEl.parentElement);
const verticalPadding = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom);
setScrollableHeight(`${parentHeight - verticalPadding}px`);
const availableHeight = parentHeight - verticalPadding;
const itemsHeight = itemsContainerRef.current.clientHeight;

if (itemsHeight > availableHeight) {
setScrollableHeight(availableHeight);
} else if (scrollableHeight !== -1) {
// there is enough space for the items - remove the height style
setScrollableHeight(-1);
}
}
}
}, []);
});

const scrollableParams = useMemo(() => (isVertical ? {onScroll: handleScroll} : {onWheel: handleWheel, ref}), [isVertical]);
const scrollableParams = useMemo(() => (isVertical ? {onScroll: handleScroll} : {onWheel: handleWheel, ref: scrollableRef}), [isVertical]);

return (
<div
className={`${styles.scrollable} ${scrolling ? styles.scrolling : ''} ${isVertical ? styles.vertical : styles.horizontal}`}
style={`${isVertical && scrollableHeight ? `height: ${scrollableHeight}` : ''}`}
ref={ref}
style={`${isVertical && scrollableHeight > -1 ? `height: ${scrollableHeight}px` : ''}`}
ref={scrollableRef}
{...scrollableParams}
>
{children}
<div className={styles.itemsContainer} ref={itemsContainerRef}>
{children}
</div>
</div>
);
};
Expand Down
Loading