Skip to content

Commit

Permalink
feat: add scrollBehavior option to Channel (#676)
Browse files Browse the repository at this point in the history
Addresses one of request in https://sendbird.atlassian.net/browse/AC-12

Added
[`scrollBehavior`](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior)
prop to allow user to pass `"smooth"` option on scroll event via Channel
component(provider).
But I set the default option to `auto` which is an initial value of this
CSS property not to change the existing behavior.
  • Loading branch information
AhyoungRyu committed Jul 6, 2023
1 parent cf3c084 commit a4337a4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.d.ts
Expand Up @@ -743,6 +743,7 @@ type ChannelContextProps = {
renderUserProfile?: (props: RenderUserProfileProps) => React.ReactElement;
disableUserProfile?: boolean;
disableMarkAsRead?: boolean;
scrollBehavior?: 'smooth' | 'auto';
};

interface ChannelUIProps {
Expand Down
@@ -0,0 +1,35 @@
import { renderHook } from '@testing-library/react';
import { useScrollBehavior } from '../useScrollBehavior';
import { useChannelContext } from '../../../../context/ChannelProvider';

jest.mock('../../../../context/ChannelProvider', () => ({
useChannelContext: jest.fn(),
}));

describe('useScrollBehavior', () => {
it('should set scroll behavior on scrollRef', () => {
const scrollRefMock = { current: { style: { scrollBehavior: 'auto' } } };
const scrollBehaviorMock = 'smooth';

useChannelContext.mockReturnValue({
scrollRef: scrollRefMock,
scrollBehavior: scrollBehaviorMock,
});

renderHook(() => useScrollBehavior());

expect(scrollRefMock.current.style.scrollBehavior).toBe(scrollBehaviorMock);
});

it('should set the scrollBehavior to `auto` by default if scrollBehavior prop is not set', () => {
const scrollRefMock = { current: { style: { } } };

useChannelContext.mockReturnValue({
scrollRef: scrollRefMock,
});

renderHook(() => useScrollBehavior());

expect(scrollRefMock.current.style.scrollBehavior).toBe('auto');
});
});
@@ -0,0 +1,17 @@
import { useEffect } from 'react';
import { useChannelContext } from '../../../context/ChannelProvider';

export function useScrollBehavior() {
const {
scrollRef,
scrollBehavior = 'auto',
} = useChannelContext();

useEffect(() => {
if (scrollRef.current) {
scrollRef.current.style.scrollBehavior = scrollBehavior;
}
}, [scrollRef.current]);

return null;
}
3 changes: 3 additions & 0 deletions src/modules/Channel/components/MessageList/index.tsx
Expand Up @@ -17,6 +17,7 @@ import { UserMessage } from '@sendbird/chat/message';
import { MessageProvider } from '../../../Message/context/MessageProvider';
import { useHandleOnScrollCallback } from '../../../../hooks/useHandleOnScrollCallback';
import { useSetScrollToBottom } from './hooks/useSetScrollToBottom';
import { useScrollBehavior } from './hooks/useScrollBehavior';

const SCROLL_BOTTOM_PADDING = 50;

Expand Down Expand Up @@ -61,6 +62,8 @@ const MessageList: React.FC<MessageListProps> = ({
: allMessages;
const markAsReadScheduler = store.config.markAsReadScheduler;

useScrollBehavior();

const onScroll = () => {
const element = scrollRef?.current;
if (element == null) {
Expand Down
3 changes: 3 additions & 0 deletions src/modules/Channel/context/ChannelProvider.tsx
Expand Up @@ -95,6 +95,7 @@ export type ChannelContextProps = {
onQuoteMessageClick?: (props: { message: UserMessage | FileMessage }) => void;
onMessageAnimated?: () => void;
onMessageHighlighted?: () => void;
scrollBehavior?: 'smooth' | 'auto';
};

interface MessageStoreInterface {
Expand Down Expand Up @@ -186,6 +187,7 @@ const ChannelProvider: React.FC<ChannelContextProps> = (props: ChannelContextPro
onQuoteMessageClick,
onMessageAnimated,
onMessageHighlighted,
scrollBehavior = 'auto',
} = props;

const globalStore = useSendbirdStateContext();
Expand Down Expand Up @@ -483,6 +485,7 @@ const ChannelProvider: React.FC<ChannelContextProps> = (props: ChannelContextPro
onScrollCallback,
onScrollDownCallback,
scrollRef,
scrollBehavior,
toggleReaction,
}}>
<UserProfileProvider
Expand Down
1 change: 1 addition & 0 deletions src/modules/Channel/index.tsx
Expand Up @@ -36,6 +36,7 @@ const Channel: React.FC<ChannelProps> = (props: ChannelProps) => {
onQuoteMessageClick={props?.onQuoteMessageClick}
onMessageAnimated={props?.onMessageAnimated}
onMessageHighlighted={props?.onMessageHighlighted}
scrollBehavior={props.scrollBehavior}
>
<ChannelUI
isLoading={props?.isLoading}
Expand Down

0 comments on commit a4337a4

Please sign in to comment.