Skip to content

Commit

Permalink
Added horizontal scroll to flow container.
Browse files Browse the repository at this point in the history
  • Loading branch information
hubuk committed Aug 9, 2022
1 parent c645be5 commit 477c3fa
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/components/common/FlowContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// @jsxImportSource @emotion/react

import { CSSObject } from '@emotion/react';
import React, { useMemo } from 'react';
import React, { useCallback, useEffect, useMemo } from 'react';
import { mergeStyles } from '../../styles/mergeStyles';

const baseStyle: CSSObject = {
Expand Down Expand Up @@ -37,6 +37,7 @@ const calculateDirection = () => {

export default ({ children }: React.PropsWithChildren) => {
const [direction, setDirection] = React.useState<CSSObject>(calculateDirection());
const divRef = React.useRef<HTMLDivElement>(null);

const style = useMemo(() => {
return mergeStyles(baseStyle, direction);
Expand All @@ -51,8 +52,23 @@ export default ({ children }: React.PropsWithChildren) => {
return () => window.removeEventListener('resize', handleResize);
}, []);

const handleWheelEvent = useCallback((e: WheelEvent) => {
if (divRef.current) {
divRef.current.scrollLeft += e.deltaY;
}
}, [divRef]);

useEffect(() => {
if (direction === columnStyle && divRef.current) {
divRef.current.addEventListener('wheel', handleWheelEvent);
return () => divRef.current?.removeEventListener('wheel', handleWheelEvent);
}

return undefined;
}, [direction, handleWheelEvent]);

return (
<div css={style}>
<div ref={divRef} css={style}>
{children}
</div>
);
Expand Down

0 comments on commit 477c3fa

Please sign in to comment.