Skip to content

Commit

Permalink
split layout components to their own part, makes sample more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
maapteh committed Oct 12, 2020
1 parent 7904ef6 commit c6a93aa
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 85 deletions.
88 changes: 3 additions & 85 deletions example/chat/src/Room.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,7 @@
import React, { useState, useEffect, useRef } from 'react';
import styled from 'styled-components';
import gql from 'graphql-tag';
import { useQuery, useMutation } from '@apollo/react-hooks';

export const Chat = styled.div`
padding: 4px;
margin: 0 0 12px;
max-width: 400px;
max-height: 400px;
border: 1px solid #ccc;
background-color: #babdc6;
overflow-x: hidden;
overflow-y: scroll;
position: relative;
font: 14px verdana;
`;

export const ChatContainer = styled.div`
&:after {
content: "";
display: table;
clear: both;
}
`;

export const Message = styled.div`
color: #000;
clear: both;
line-height: 120%;
padding: 8px;
position: relative;
margin: 8px 0;
max-width: 85%;
word-wrap: break-word;
z-index: 1;
&:after {
position: absolute;
content: "";
width: 0;
height: 0;
border-style: solid;
}
span {
display: inline-block;
float: right;
padding: 0 0 0 7px;
position: relative;
bottom: -4px;
}
}`;

const MessageReceived = styled(Message)`
background: #fff;
border-radius: 0px 5px 5px 5px;
float: left;
&:after {
border-width: 0px 10px 10px 0;
border-color: transparent #fff transparent transparent;
top: 0;
left: -4px;
}
span {
display: block;
color: #bbb;
font-size: 10px;
}
`;

const MessageMine = styled(Message)`
background: #e1ffc7;
border-radius: 5px 0px 5px 5px;
float: right;
&:after {
border-width: 0px 0 10px 10px;
border-color: transparent transparent transparent #e1ffc7;
top: 0;
right: -4px;
}
`;
import { Chat, ChatContainer, Message, MessageReceived } from './components/room';

export const Room = ({ channel, name }) => {
const messagesEndRef = useRef(null)
Expand Down Expand Up @@ -149,9 +67,9 @@ export const Room = ({ channel, name }) => {
<Chat>
<ChatContainer>
{data.room.messages.map((msg) =>
msg.createdBy === name ? <MessageMine key={msg.id}>
msg.createdBy === name ? <Message key={msg.id}>
{msg.text}
</MessageMine> : <MessageReceived key={msg.id}>
</Message> : <MessageReceived key={msg.id}>
<span>{msg.createdBy}</span>
{msg.text}
</MessageReceived>
Expand Down
83 changes: 83 additions & 0 deletions example/chat/src/components/room.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import styled from 'styled-components';

export const Chat = styled.div`
padding: 4px;
margin: 0 0 12px;
max-width: 400px;
max-height: 400px;
border: 1px solid #ccc;
background-color: #babdc6;
overflow-x: hidden;
overflow-y: scroll;
position: relative;
font: 14px verdana;
`;

export const ChatContainer = styled.div`
&:after {
content: "";
display: table;
clear: both;
}
`;

const ChatLine = styled.div`
color: #000;
clear: both;
line-height: 120%;
padding: 8px;
position: relative;
margin: 8px 0;
max-width: 85%;
word-wrap: break-word;
z-index: 1;
&:after {
position: absolute;
content: "";
width: 0;
height: 0;
border-style: solid;
}
span {
display: inline-block;
float: right;
padding: 0 0 0 7px;
position: relative;
bottom: -4px;
}
}`;

export const MessageReceived = styled(ChatLine)`
background: #fff;
border-radius: 0px 5px 5px 5px;
float: left;
&:after {
border-width: 0px 10px 10px 0;
border-color: transparent #fff transparent transparent;
top: 0;
left: -4px;
}
span {
display: block;
color: #bbb;
font-size: 10px;
}
`;

export const Message = styled(ChatLine)`
background: #e1ffc7;
border-radius: 5px 0px 5px 5px;
float: right;
&:after {
border-width: 0px 0 10px 10px;
border-color: transparent transparent transparent #e1ffc7;
top: 0;
right: -4px;
}
`;

0 comments on commit c6a93aa

Please sign in to comment.