Skip to content

Commit

Permalink
fix: picker position
Browse files Browse the repository at this point in the history
  • Loading branch information
CrisVega08 committed Jun 4, 2021
1 parent ccb62b6 commit 50db64b
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Props = {
}

function Message({ message, showTimeStamp }: Props) {
const sanitizedHTML = markdownIt()
const sanitizedHTML = markdownIt({ break: true })
.use(markdownItClass, {
img: ['rcw-message-img']
})
Expand All @@ -26,7 +26,7 @@ function Message({ message, showTimeStamp }: Props) {

return (
<div className={`rcw-${message.sender}`}>
<div className="rcw-message-text" dangerouslySetInnerHTML={{ __html: sanitizedHTML }} />
<div className="rcw-message-text" dangerouslySetInnerHTML={{ __html: sanitizedHTML.replace(/\n$/,'') }} />
{showTimeStamp && <span className="rcw-timestamp">{format(message.timestamp, 'hh:mm')}</span>}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.rcw-message {
margin: 10px;
display: flex;
white-space: pre-wrap;
word-wrap: break-word;
}

Expand All @@ -19,6 +20,9 @@

.rcw-message-text {
@include message-bubble($turqois-2);

white-space: pre-wrap;
word-wrap: break-word;
}

.rcw-timestamp {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useRef, useEffect, useState } from 'react';
import { useRef, useEffect, useState, forwardRef, useImperativeHandle } from 'react';
import { useSelector } from 'react-redux';
import EmojiPicker from './components/EmojiPicker'
import cn from 'classnames';

import { GlobalState } from 'src/store/types';

import { getCaretIndex, isFirefox, updateCaret, insertNodeAtCaret, getSelection } from '../../../../../../utils/contentEditable'
const send = require('../../../../../../../assets/send_button.svg') as string;
const emoji = require('../../../../../../../assets/icon-smiley.svg') as string;
const brRegex = /<br>/g;

import './style.scss';
Expand All @@ -17,18 +17,28 @@ type Props = {
autofocus: boolean;
sendMessage: (event: any) => void;
buttonAlt: string;
onPressEmoji: () => void;
onChangeSize: (event: any) => void;
onTextInputChange?: (event: any) => void;
}

function Sender({ sendMessage, placeholder, disabledInput, autofocus, onTextInputChange, buttonAlt }: Props) {
function Sender({ sendMessage, placeholder, disabledInput, autofocus, onTextInputChange, buttonAlt, onPressEmoji, onChangeSize }: Props, ref) {
const showChat = useSelector((state: GlobalState) => state.behavior.showChat);
const inputRef = useRef<HTMLDivElement>(null!);
const refContainer = useRef<HTMLDivElement>(null);
const [enter, setEnter]= useState(false)
const [firefox, setFirefox] = useState(false);
const [height, setHeight] = useState(0)
// @ts-ignore
useEffect(() => { if (showChat && autofocus) inputRef.current?.focus(); }, [showChat]);
useEffect(() => { setFirefox(isFirefox())}, [])

useImperativeHandle(ref, () => {
return {
onSelectEmoji: handlerOnSelectEmoji,
};
});

const handlerOnChange = (event) => {
onTextInputChange && onTextInputChange(event)
}
Expand Down Expand Up @@ -68,6 +78,16 @@ function Sender({ sendMessage, placeholder, disabledInput, autofocus, onTextInpu
}
}

// TODO use a context for checkSize and toggle picker
const checkSize = () => {
const senderEl = refContainer.current
if(senderEl && height !== senderEl.clientHeight) {
const {clientHeight} = senderEl;
setHeight(clientHeight)
onChangeSize(clientHeight ? clientHeight -1 : 0)
}
}

const handlerOnKeyUp = (event) => {
const el = inputRef.current;
if(!el) return true;
Expand All @@ -81,6 +101,7 @@ function Sender({ sendMessage, placeholder, disabledInput, autofocus, onTextInpu
el.innerHTML = el.innerHTML.replace(brRegex, '')
}
}
checkSize()
}

const handlerOnKeyDown= (event) => {
Expand All @@ -98,9 +119,16 @@ function Sender({ sendMessage, placeholder, disabledInput, autofocus, onTextInpu
}
}

const handlerPressEmoji = () => {
onPressEmoji();
checkSize();
}

return (
<div className="rcw-sender">
<EmojiPicker onSelectEmoji={handlerOnSelectEmoji}/>
<div ref={refContainer} className="rcw-sender">
<button className='rcw-picker-btn' type="submit" onClick={handlerPressEmoji}>
<img src={emoji} className="rcw-picker-icon" alt="" />
</button>
<div className={cn('rcw-new-message', {
'rcw-message-disable': disabledInput,
})
Expand All @@ -126,4 +154,4 @@ function Sender({ sendMessage, placeholder, disabledInput, autofocus, onTextInpu
);
}

export default Sender;
export default forwardRef(Sender);
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
min-height: 45px;
overflow: hidden;
padding: 10px;
position: relative;

&.expand {
height: 55px;
Expand Down Expand Up @@ -37,7 +38,7 @@
display: block;
height: 100%;
line-height: 20px;
max-height: 75px;
max-height: 78px;
overflow-y: auto;
user-select: text;
white-space: pre-wrap;
Expand All @@ -53,9 +54,10 @@
}
}

.rcw-send {
.rcw-send, .rcw-picker-btn {
background: $grey-2;
border: 0;
cursor: pointer;

.rcw-send-icon {
height: 25px;
Expand Down
32 changes: 31 additions & 1 deletion src/components/Widget/components/Conversation/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useRef, useState, useEffect } from 'react';
import { Picker } from 'emoji-mart';
import cn from 'classnames';

import Header from './components/Header';
Expand All @@ -9,6 +11,10 @@ import { AnyFunction } from '../../../../utils/types';

import './style.scss';

interface ISenderRef {
onSelectEmoji: (event: any) => void;
}

type Props = {
title: string;
subtitle: string;
Expand Down Expand Up @@ -44,6 +50,23 @@ function Conversation({
sendButtonAlt,
showTimeStamp
}: Props) {
const [pickerOffset, setOffset] = useState(0)
const senderRef = useRef<ISenderRef>(null!);
const [pickerStatus, setPicket] = useState(false)

const onSelectEmoji = (emoji) => {
senderRef.current?.onSelectEmoji(emoji)
}

const togglePicker = () => {
setPicket(prevPickerStatus => !prevPickerStatus)
}

const handlerSendMsn = (event) => {
sendMessage(event)
if(pickerStatus) setPicket(false)
}

return (
<div className={cn('rcw-conversation-container', className)} aria-live="polite">
<Header
Expand All @@ -55,13 +78,20 @@ function Conversation({
/>
<Messages profileAvatar={profileAvatar} showTimeStamp={showTimeStamp} />
<QuickButtons onQuickButtonClicked={onQuickButtonClicked} />
{pickerStatus && (<Picker
style={{ position: 'absolute', bottom: pickerOffset, left: '0', width: '100%' }}
onSelect={onSelectEmoji}
/>)}
<Sender
sendMessage={sendMessage}
ref={senderRef}
sendMessage={handlerSendMsn}
placeholder={senderPlaceHolder}
disabledInput={disabledInput}
autofocus={autofocus}
onTextInputChange={onTextInputChange}
buttonAlt={sendButtonAlt}
onPressEmoji={togglePicker}
onChangeSize={setOffset}
/>
</div>
);
Expand Down
5 changes: 5 additions & 0 deletions src/components/Widget/components/Conversation/style.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import 'common';
@import 'variables/colors';
@import 'animation';
@import "~emoji-mart/css/emoji-mart.css";

.rcw-conversation-container {
border-radius: 10px;
Expand All @@ -21,6 +22,10 @@
}
}

.emoji-mart-preview {
display: none;
}

.rcw-full-screen {
.rcw-conversation-container {
@include conversation-container-fs;
Expand Down
4 changes: 0 additions & 4 deletions src/scss/_common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
max-width: 215px;
padding: 15px;
text-align: left;

> p {
white-space: pre-wrap;
}
}

// Full screen mixins
Expand Down

0 comments on commit 50db64b

Please sign in to comment.