Search code examples
javascriptcssreactjsmaterial-uistyled-components

How to get MuiList to auto scroll


I am creating a chat window component and I am using Material UI. I would have thought putting a height or max-height either on the MuiList or MuiBox that surrounds the list would make it auto scroll each time a new message is sent, but it doesn't seem to do the trick. I am somewhat baffled by this and any direction would be great. Below is my code. I am using Styled Components for styling.

export const ChatWindow = styled(MuiBox)`
  && {
    position: absolute;
    right: 0px;
    bottom: 0px;
    box-shadow: 0px 7px 40px 2px rgba(148, 149, 150, 0.3);
    background: ${Color.white};
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    transition: 0.3s ease-in-out;
    border-radius: 5px;
    width: ${props => (props.fullscreen ? '100%' : `${450}px`)};
    height: ${props => (props.fullscreen ? '100%' : `${584}px`)};
    /* max-height: 550px; */
  }
`;

export const ChatWindowHeader = styled(MuiBox)`
  && {
    background: ${Color.primary};
    min-height: 40px;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    color: white;
    padding: 10px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
`;

export const MessageList = styled(MuiList)`
  && {
    height: 80%;
    overflow-y: auto;
  }
`;

export const MessageListItem = styled(MuiListItem)`
  && {
    margin: auto;
    padding-bottom: 10px;
    display: flex;
  }
`;

export const MessageReceivedBubble = styled(MuiBox)`
  && {
    padding: 8px 16px;
    margin-bottom: 10px;
    border-radius: 10px 10px 10px 2px;
    font-weight: 300;
    font-size: 14px;
    word-wrap: break-word;
  }
`;

export const MessageSentBubble = styled(MuiBox)`
  && {
    padding: 8px 16px;
    margin-bottom: 10px;
    border-radius: 10px 10px 2px 10px;
    font-weight: 300;
    font-size: 14px;
    word-wrap: break-word;
  }
`;


Solution

  • I was able to accomplish what I wanted by using the answer from Diego Lara on this stack overflow question: How to scroll to bottom in react?