Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add scrollBehavior option to Channel (#676)
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
1 parent
cf3c084
commit a4337a4
Showing
6 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/modules/Channel/components/MessageList/hooks/__test__/useScrollBehavior.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
src/modules/Channel/components/MessageList/hooks/useScrollBehavior.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters