Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit dd18727

Browse files
mmcotejasonLaster
authored andcommitted
Added VisibilityHandler to handle the mounting and unmounting of child components of App when changing panel tabs. (#5688)
1 parent 499f14b commit dd18727

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

src/components/App.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { bindActionCreators } from "redux";
1010
import { features } from "../utils/prefs";
1111
import actions from "../actions";
1212
import { ShortcutsModal } from "./ShortcutsModal";
13+
import VisibilityHandler from "./shared/VisibilityHandler";
1314

1415
import {
1516
getSelectedSource,
@@ -299,16 +300,18 @@ class App extends Component<Props, State> {
299300
render() {
300301
const { quickOpenEnabled } = this.props;
301302
return (
302-
<div className="debugger">
303-
{this.renderLayout()}
304-
{quickOpenEnabled === true && (
305-
<QuickOpenModal
306-
shortcutsModalEnabled={this.state.shortcutsModalEnabled}
307-
toggleShortcutsModal={() => this.toggleShortcutsModal()}
308-
/>
309-
)}
310-
{this.renderShortcutsModal()}
311-
</div>
303+
<VisibilityHandler>
304+
<div className="debugger">
305+
{this.renderLayout()}
306+
{quickOpenEnabled === true && (
307+
<QuickOpenModal
308+
shortcutsModalEnabled={this.state.shortcutsModalEnabled}
309+
toggleShortcutsModal={() => this.toggleShortcutsModal()}
310+
/>
311+
)}
312+
{this.renderShortcutsModal()}
313+
</div>
314+
</VisibilityHandler>
312315
);
313316
}
314317
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
/**
6+
* Helper class to disable panel rendering when it is in background.
7+
*
8+
* Toolbox code hides the iframes when switching to another panel
9+
* and triggers `visibilitychange` events.
10+
*
11+
* See devtools/client/framework/toolbox.js:setIframeVisible().
12+
*/
13+
14+
import PropTypes from "prop-types";
15+
import { Component } from "react";
16+
17+
class VisibilityHandler extends Component {
18+
static get propTypes() {
19+
return {
20+
children: PropTypes.element.isRequired
21+
};
22+
}
23+
24+
constructor(props) {
25+
super(props);
26+
this.isVisible = true;
27+
this.onVisibilityChange = this.onVisibilityChange.bind(this);
28+
}
29+
30+
componentDidMount() {
31+
window.addEventListener("visibilitychange", this.onVisibilityChange);
32+
}
33+
34+
shouldComponentUpdate() {
35+
return document.visibilityState == "visible";
36+
}
37+
38+
componentWillUnmount() {
39+
window.removeEventListener("visibilitychange", this.onVisibilityChange);
40+
}
41+
42+
onVisibilityChange() {
43+
this.isVisible = false;
44+
if (document.visibilityState == "visible") {
45+
this.isVisible = true;
46+
}
47+
this.forceUpdate();
48+
}
49+
50+
render() {
51+
return this.isVisible ? this.props.children : null;
52+
}
53+
}
54+
55+
module.exports = VisibilityHandler;

0 commit comments

Comments
 (0)