Mailspring/app/internal_packages/mode-switch/lib/mode-toggle.jsx

63 lines
1.7 KiB
React
Raw Normal View History

import { WorkspaceStore, Actions } from 'mailspring-exports';
2017-09-27 02:46:00 +08:00
import { RetinaImg } from 'mailspring-component-kit';
2017-09-27 02:33:08 +08:00
import React from 'react';
export default class ModeToggle extends React.Component {
static displayName = 'ModeToggle';
constructor(props) {
super(props);
2017-09-27 02:33:08 +08:00
this.column = WorkspaceStore.Location.MessageListSidebar;
this.state = this._getStateFromStores();
}
componentDidMount() {
2017-09-27 02:33:08 +08:00
this._unsubscriber = WorkspaceStore.listen(this._onStateChanged);
this._mounted = true;
}
componentWillUnmount() {
2017-09-27 02:33:08 +08:00
this._mounted = false;
if (this._unsubscriber) {
this._unsubscriber();
}
}
_getStateFromStores() {
return {
hidden: WorkspaceStore.isLocationHidden(this.column),
};
}
_onStateChanged = () => {
// We need to keep track of this because our parent unmounts us in the same
// event listener cycle that we receive the event in. ie:
//
// for listener in listeners
// # 1. workspaceView remove left column
// # ---- Mode toggle unmounts, listeners array mutated in place
// # 2. ModeToggle update
if (!this._mounted) {
return;
}
this.setState(this._getStateFromStores());
2017-09-27 02:33:08 +08:00
};
_onToggleMode = () => {
2017-09-27 02:33:08 +08:00
Actions.toggleWorkspaceLocationHidden(this.column);
};
render() {
return (
<button
className={`btn btn-toolbar mode-toggle mode-${this.state.hidden}`}
2017-09-27 02:33:08 +08:00
style={{ order: 500 }}
title={this.state.hidden ? 'Show sidebar' : 'Hide sidebar'}
onClick={this._onToggleMode}
>
2017-09-27 02:33:08 +08:00
<RetinaImg name="toolbar-person-sidebar.png" mode={RetinaImg.Mode.ContentIsMask} />
</button>
);
}
}