Skip to content
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

Resizable widget #261

Merged
merged 4 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export default App;
|**sendButtonAlt**|string|NO|'Send'|Send button alt for a11y purposes|
|**handleTextInputChange**|(event) => any|NO| |Prop that triggers on input change|
|**handleSubmit**|(event) => any|NO| |Prop that triggers when a message is submitted, used for custom validation|
|**resizable**|boolean|NO|false|Prop that allows to resize the widget by dragging it's left border|
|**emojis**|boolean|NO|false|enable emoji picker|

#### Styles
Expand Down
1 change: 1 addition & 0 deletions dev/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default class App extends Component {
imagePreview
handleSubmit={this.handleSubmit}
emojis
resizable
/>
);
}
Expand Down
38 changes: 36 additions & 2 deletions src/components/Widget/components/Conversation/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState } from 'react';
import { useRef, useState, useEffect } from 'react';
import { Picker } from 'emoji-mart';
import cn from 'classnames';

Expand Down Expand Up @@ -31,6 +31,7 @@ type Props = {
onTextInputChange?: (event: any) => void;
sendButtonAlt: string;
showTimeStamp: boolean;
resizable?: boolean;
emojis?: boolean;
};

Expand All @@ -50,8 +51,39 @@ function Conversation({
onTextInputChange,
sendButtonAlt,
showTimeStamp,
resizable,
emojis
}: Props) {
const [containerDiv, setContainerDiv] = useState<HTMLElement | null>();
let startX, startWidth;

useEffect(() => {
const containerDiv = document.getElementById('rcw-conversation-container');
setContainerDiv(containerDiv);
}, []);

const initResize = (e) => {
if (resizable) {
startX = e.clientX;
if (document.defaultView && containerDiv){
startWidth = parseInt(document.defaultView.getComputedStyle(containerDiv).width);
window.addEventListener('mousemove', resize, false);
window.addEventListener('mouseup', stopResize, false);
}
}
}

const resize = (e) => {
if (containerDiv) {
containerDiv.style.width = (startWidth - e.clientX + startX) + 'px';
}
}

const stopResize = (e) => {
window.removeEventListener('mousemove', resize, false);
window.removeEventListener('mouseup', stopResize, false);
}

const [pickerOffset, setOffset] = useState(0)
const senderRef = useRef<ISenderRef>(null!);
const [pickerStatus, setPicket] = useState(false)
Expand All @@ -70,7 +102,9 @@ function Conversation({
}

return (
<div className={cn('rcw-conversation-container', className)} aria-live="polite">
<div id="rcw-conversation-container" onMouseDown={initResize}
className={cn('rcw-conversation-container', className)} aria-live="polite">
{resizable && <div className="rcw-conversation-resizer" />}
<Header
title={title}
subtitle={subtitle}
Expand Down
12 changes: 12 additions & 0 deletions src/components/Widget/components/Conversation/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
.rcw-conversation-container {
border-radius: 10px;
box-shadow: 0px 2px 10px 1px $grey-3;
min-width: 370px;
max-width: 90vw;
position: relative;

&.active {
opacity: 1;
Expand All @@ -22,6 +25,15 @@
}
}

.rcw-conversation-resizer {
cursor: col-resize;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 5px;
}

.emoji-mart-preview {
display: none;
}
Expand Down
3 changes: 3 additions & 0 deletions src/components/Widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Props = {
imagePreview?: boolean;
zoomStep?: number;
handleSubmit?: AnyFunction;
resizable?: boolean;
emojis?: boolean;
}

Expand Down Expand Up @@ -57,6 +58,7 @@ function Widget({
imagePreview,
zoomStep,
handleSubmit,
resizable,
emojis
}: Props) {
const dispatch = useDispatch();
Expand Down Expand Up @@ -105,6 +107,7 @@ function Widget({
showTimeStamp={showTimeStamp}
imagePreview={imagePreview}
zoomStep={zoomStep}
resizable={resizable}
emojis={emojis}
/>
);
Expand Down
3 changes: 3 additions & 0 deletions src/components/Widget/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Props = {
showTimeStamp: boolean;
imagePreview?: boolean;
zoomStep?: number;
resizable?: boolean;
emojis?: boolean
}

Expand All @@ -61,6 +62,7 @@ function WidgetLayout({
showTimeStamp,
imagePreview,
zoomStep,
resizable,
emojis
}: Props) {
const dispatch = useDispatch();
Expand Down Expand Up @@ -138,6 +140,7 @@ function WidgetLayout({
onTextInputChange={onTextInputChange}
sendButtonAlt={sendButtonAlt}
showTimeStamp={showTimeStamp}
resizable={resizable}
emojis={emojis}
/>
}
Expand Down
2 changes: 0 additions & 2 deletions src/components/Widget/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
display: flex;
flex-direction: column;
margin: 0 20px 20px 0;
max-width: 370px;
position: fixed;
right: 0;
width: 90vw;
z-index: 9999;
}

Expand Down
3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Props = {
zoomStep?: number;
emojis?: boolean;
handleSubmit?: AnyFunction;
resizable?: boolean;
} & typeof defaultProps;

function ConnectedWidget({
Expand All @@ -57,6 +58,7 @@ function ConnectedWidget({
imagePreview,
zoomStep,
handleSubmit,
resizable,
emojis
}: Props) {
return (
Expand Down Expand Up @@ -85,6 +87,7 @@ function ConnectedWidget({
imagePreview={imagePreview}
zoomStep={zoomStep}
handleSubmit={handleSubmit}
resizable={resizable}
emojis={emojis}
/>
</Provider>
Expand Down