-
Notifications
You must be signed in to change notification settings - Fork 734
Video 4454 message list scrolling #467
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
Merged
olipyskoty
merged 9 commits into
feature/conversations
from
VIDEO-4454-message-list-scrolling
Mar 25, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
822e42c
add class component for MessageList auto-scrolling to the bottom
olipyskoty aca68bb
move css to scroll container component
olipyskoty e75ee50
create scroll container component for chat window
olipyskoty e94eadc
update class name
olipyskoty f894e88
add test file for scroll container component
olipyskoty 2eea93a
Merge branch 'feature/conversations' into VIDEO-4454-message-list-scr…
olipyskoty d096cc4
remove unit testing file and add comments to MessageListScrollContainer
olipyskoty 9ed8940
update snapshot test to include hidden button
olipyskoty 769e178
update comment to be more specific
olipyskoty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
149 changes: 149 additions & 0 deletions
149
...mponents/ChatWindow/MessageList/MessageListScrollContainer/MessageListScrollContainer.tsx
This file contains hidden or 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,149 @@ | ||
/* istanbul ignore file */ | ||
import React from 'react'; | ||
import ArrowDownwardIcon from '@material-ui/icons/ArrowDownward'; | ||
import Button from '@material-ui/core/Button'; | ||
import clsx from 'clsx'; | ||
import { Message } from '@twilio/conversations/lib/message'; | ||
import throttle from 'lodash.throttle'; | ||
import { withStyles, WithStyles, createStyles } from '@material-ui/core/styles'; | ||
|
||
const styles = createStyles({ | ||
messageListContainer: { | ||
overflowY: 'auto', | ||
flex: '1', | ||
}, | ||
outerContainer: { | ||
minHeight: 0, | ||
flex: 1, | ||
position: 'relative', | ||
}, | ||
innerScrollContainer: { | ||
height: '100%', | ||
overflowY: 'auto', | ||
padding: '0 1.2em 1em', | ||
}, | ||
button: { | ||
position: 'absolute', | ||
bottom: '14px', | ||
right: '2em', | ||
zIndex: 100, | ||
padding: '0.5em 0.9em', | ||
visibility: 'hidden', | ||
opacity: 0, | ||
boxShadow: '0px 4px 16px rgba(18, 28, 45, 0.2)', | ||
transition: 'all 0.5s ease', | ||
}, | ||
showButton: { | ||
visibility: 'visible', | ||
opacity: 1, | ||
bottom: '24px', | ||
}, | ||
}); | ||
interface MessageListScrollContainerProps extends WithStyles<typeof styles> { | ||
messages: Message[]; | ||
} | ||
interface MessageListScrollContainerState { | ||
isScrolledToBottom: boolean; | ||
showButton: boolean; | ||
messageNotificationCount: number; | ||
} | ||
|
||
/* | ||
* This component is a scrollable container that wraps around the 'MessageList' component. | ||
* The MessageList will ultimately grow taller than its container as it continues to receive | ||
* new messages, and users will need to have the ability to scroll up and down the chat window. | ||
* A "new message" button will be displayed when the user receives a new message, and is not scrolled | ||
* to the bottom. This button will be hidden if the user clicks on it, or manually scrolls | ||
* to the bottom. Otherwise, this component will auto-scroll to the bottom when a new message is | ||
* received, and the user is already scrolled to the bottom. | ||
* | ||
* Note that this component is tested with Cypress only. | ||
*/ | ||
export class MessageListScrollContainer extends React.Component< | ||
MessageListScrollContainerProps, | ||
MessageListScrollContainerState | ||
> { | ||
chatThreadRef = React.createRef<HTMLDivElement>(); | ||
state = { isScrolledToBottom: true, showButton: false, messageNotificationCount: 0 }; | ||
|
||
scrollToBottom() { | ||
const innerScrollContainerEl = this.chatThreadRef.current!; | ||
innerScrollContainerEl.scrollTop = innerScrollContainerEl!.scrollHeight; | ||
} | ||
|
||
componentDidMount() { | ||
this.scrollToBottom(); | ||
this.chatThreadRef.current!.addEventListener('scroll', this.handleScroll); | ||
} | ||
|
||
// this component updates as users send new messages: | ||
componentDidUpdate(prevProps: MessageListScrollContainerProps, prevState: MessageListScrollContainerState) { | ||
if (prevState.isScrolledToBottom) { | ||
this.scrollToBottom(); | ||
} else if (this.props.messages.length !== prevProps.messages.length) { | ||
const numberOfNewMessages = this.props.messages.length - prevProps.messages.length; | ||
|
||
this.setState(previousState => ({ | ||
// if there's at least one new message, show the 'new message' button: | ||
showButton: !previousState.isScrolledToBottom, | ||
// if 'new message' button is visible, | ||
// messageNotificationCount will be the number of previously unread messages + the number of new messages | ||
// otherwise, messageNotificationCount is set to 1: | ||
messageNotificationCount: previousState.showButton | ||
? previousState.messageNotificationCount + numberOfNewMessages | ||
: 1, | ||
})); | ||
} | ||
} | ||
|
||
handleScroll = throttle(() => { | ||
const innerScrollContainerEl = this.chatThreadRef.current!; | ||
const isScrolledToBottom = | ||
innerScrollContainerEl.clientHeight + innerScrollContainerEl.scrollTop === innerScrollContainerEl!.scrollHeight; | ||
|
||
this.setState(prevState => ({ | ||
isScrolledToBottom, | ||
showButton: isScrolledToBottom ? false : prevState.showButton, | ||
})); | ||
}, 300); | ||
|
||
handleClick = () => { | ||
const innerScrollContainerEl = this.chatThreadRef.current!; | ||
|
||
innerScrollContainerEl.scrollTo({ top: innerScrollContainerEl.scrollHeight, behavior: 'smooth' }); | ||
|
||
this.setState({ showButton: false }); | ||
}; | ||
|
||
componentWillUnmount() { | ||
const innerScrollContainerEl = this.chatThreadRef.current!; | ||
|
||
innerScrollContainerEl.removeEventListener('scroll', this.handleScroll); | ||
} | ||
|
||
render() { | ||
const { classes } = this.props; | ||
|
||
return ( | ||
<div className={classes.outerContainer}> | ||
<div className={classes.innerScrollContainer} ref={this.chatThreadRef}> | ||
<div className={classes.messageListContainer}> | ||
{this.props.children} | ||
<Button | ||
className={clsx(classes.button, { [classes.showButton]: this.state.showButton })} | ||
onClick={this.handleClick} | ||
startIcon={<ArrowDownwardIcon />} | ||
color="primary" | ||
variant="contained" | ||
> | ||
{this.state.messageNotificationCount} new message | ||
{this.state.messageNotificationCount > 1 && 's'} | ||
</Button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default withStyles(styles)(MessageListScrollContainer); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timmydoza do you mind proofreading this? want to make sure it's not too big
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Not too big. Can't have too many comments 🙂