@@ -7,6 +7,9 @@ import { Icon } from "@cocalc/frontend/components/icon";
7
7
import { Alert , Button } from "antd" ;
8
8
import type { JupyterActions } from "../browser-actions" ;
9
9
import { Map } from "immutable" ;
10
+ import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context" ;
11
+
12
+ // TODO: it would be better if somehow this were in @cocalc /jupyter, to 100% ensure css stays aligned.
10
13
require ( "@jupyter-widgets/controls/css/widgets.css" ) ;
11
14
12
15
let loadFontAwesomeDone = false ;
@@ -34,9 +37,13 @@ export function IpyWidget({ id: cell_id, value, actions }: WidgetProps) {
34
37
// console.log("IpyWidget", { value: value.toJS(), actions });
35
38
const [ unknown , setUnknown ] = useState < boolean > ( false ) ;
36
39
const divRef = useRef < any > ( null ) ;
40
+ // We *ONLY* render widgets when they are visible. Why?
41
+ // (1) some widgets -- k3d labels!! -- assume they are visible, and just totally crash if not, due to bad code
42
+ // (2) efficiency.
43
+ const { isVisible } = useFrameContext ( ) ;
37
44
38
45
useEffect ( ( ) => {
39
- if ( actions == null ) {
46
+ if ( actions == null || ! isVisible ) {
40
47
// console.log("IpyWidget: not rendering due to actions=null");
41
48
return ;
42
49
}
@@ -64,7 +71,11 @@ export function IpyWidget({ id: cell_id, value, actions }: WidgetProps) {
64
71
return ;
65
72
}
66
73
render ( { manager, id, div } ) ;
67
- } , [ ] ) ;
74
+
75
+ return ( ) => {
76
+ $ ( div ) . empty ( ) ;
77
+ } ;
78
+ } , [ isVisible ] ) ;
68
79
69
80
if ( unknown ) {
70
81
const msg = "Run cell to load widget." ;
0 commit comments