ocrcc-chatbox/src/components/widget.js

77 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-05-29 02:49:38 +00:00
import React, { Component } from 'react';
2018-05-28 01:09:44 +00:00
import { Transition } from 'react-transition-group';
2020-02-01 05:30:58 +00:00
import Chatbox from './chatbox';
2020-02-14 16:38:20 +00:00
import './styles.scss';
2018-05-26 18:53:54 +00:00
2018-05-29 02:49:38 +00:00
class Widget extends Component {
2020-02-01 05:30:58 +00:00
constructor(props) {
super(props);
this.state = {
opened: false,
showDock: true,
};
2018-05-26 18:53:54 +00:00
}
2018-05-28 01:09:44 +00:00
handleToggleOpen = () => {
this.setState((prev) => {
2018-05-29 02:49:38 +00:00
let { showDock } = prev;
2018-05-28 01:09:44 +00:00
if (!prev.opened) {
showDock = false;
}
return {
showDock,
opened: !prev.opened,
};
});
2018-05-26 18:53:54 +00:00
}
2018-05-28 01:09:44 +00:00
handleWidgetExit = () => {
this.setState({
showDock: true,
});
}
render() {
2020-02-01 05:30:58 +00:00
const { opened, showDock } = this.state;
2018-05-28 01:09:44 +00:00
2018-05-26 18:53:54 +00:00
return (
2020-02-14 16:38:20 +00:00
<div className="docked-widget" role="complementary">
<Transition in={opened} timeout={250} onExited={this.handleWidgetExit}>
2020-02-01 05:30:58 +00:00
{(status) => (
2020-02-14 16:38:20 +00:00
<div className={`widget widget-${status}`} aria-hidden={!opened}>
<Chatbox
handleToggleOpen={this.handleToggleOpen}
opened={opened}
status={status}
{...this.props} // eslint-disable-line
/>
2018-05-28 01:09:44 +00:00
</div>
)}
</Transition>
2020-02-01 05:30:58 +00:00
{showDock && (
<button
type="button"
className="dock"
onClick={this.handleToggleOpen}
onKeyPress={this.handleToggleOpen}
2020-02-14 16:38:20 +00:00
aria-labelledby="open-chatbox-label"
2020-02-01 05:30:58 +00:00
>
2020-02-14 16:38:20 +00:00
<span id="open-chatbox-label">Open support chat</span>
<span className={`arrow ${opened ? 'opened' : 'closed'}`} aria-label={`${opened ? 'Close' : 'Open'} support chat window`}></span>
2020-02-01 05:30:58 +00:00
</button>
)}
2018-05-26 18:53:54 +00:00
</div>
);
}
}
Widget.propTypes = {
};
Widget.defaultProps = {
};
2018-05-26 18:53:54 +00:00
export default Widget;