diff --git a/src/App.js b/src/App.js index 2109c25..e047301 100644 --- a/src/App.js +++ b/src/App.js @@ -105,6 +105,31 @@ class App extends Component { this.copyToClipboard = this.copyToClipboard.bind(this); this.markupRef = React.createRef(); this.textareaRef = React.createRef(); + this.sidebarRef = React.createRef(); + this.openerRef = React.createRef(); + } + + componentDidMount() { + window.focus(); + const iframe = document.querySelector('iframe'); + const sidebar = this.sidebarRef.current; + const opener = this.openerRef.current; + + window.addEventListener('blur', () => { + if (document.activeElement === iframe) { + this.setState({ sidebar: false }); + } + }); + + document.addEventListener('click', (e) => { + if (e.target === opener) { + return; + } + + if ((!e.target === sidebar || !sidebar.contains(e.target))) { + this.setState({ sidebar: false }); + } + }); } changeMode() { @@ -200,7 +225,7 @@ class App extends Component { return viewList.map((v, k) => ); } - toggleSidebar() { + toggleSidebar(e) { this.setState({ sidebar: !this.state.sidebar }); } @@ -225,11 +250,11 @@ class App extends Component { return (