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

Commit 51dc792

Browse files
authored
Make VisualDebugger no-op for non-DOM environments (#53)
1 parent 7859190 commit 51dc792

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

src/visualDebugger.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
const WIDTH = window.innerWidth;
2-
const HEIGHT = window.innerHeight;
1+
// We'll make VisualDebugger no-op for any environments lacking a DOM (e.g. SSR and React Native non-web platforms).
2+
const hasDOM = window && window.document;
3+
4+
const WIDTH = hasDOM ? window.innerWidth : 0;
5+
const HEIGHT = hasDOM ? window.innerHeight : 0;
36

47
class VisualDebugger {
58
constructor() {
6-
this.debugCtx = VisualDebugger.createCanvas('sn-debug', 1010);
7-
this.layoutsCtx = VisualDebugger.createCanvas('sn-layouts', 1000);
9+
if (hasDOM) {
10+
this.debugCtx = VisualDebugger.createCanvas('sn-debug', 1010);
11+
this.layoutsCtx = VisualDebugger.createCanvas('sn-layouts', 1000);
12+
}
813
}
914

1015
static createCanvas(id, zIndex) {
@@ -25,14 +30,23 @@ class VisualDebugger {
2530
}
2631

2732
clear() {
33+
if (!hasDOM) {
34+
return;
35+
}
2836
this.debugCtx.clearRect(0, 0, WIDTH, HEIGHT);
2937
}
3038

3139
clearLayouts() {
40+
if (!hasDOM) {
41+
return;
42+
}
3243
this.layoutsCtx.clearRect(0, 0, WIDTH, HEIGHT);
3344
}
3445

3546
drawLayout(layout, focusKey, parentFocusKey) {
47+
if (!hasDOM) {
48+
return;
49+
}
3650
this.layoutsCtx.strokeStyle = 'green';
3751
this.layoutsCtx.strokeRect(layout.left, layout.top, layout.width, layout.height);
3852
this.layoutsCtx.font = '8px monospace';
@@ -44,6 +58,9 @@ class VisualDebugger {
4458
}
4559

4660
drawPoint(x, y, color = 'blue', size = 10) {
61+
if (!hasDOM) {
62+
return;
63+
}
4764
this.debugCtx.strokeStyle = color;
4865
this.debugCtx.lineWidth = 3;
4966
this.debugCtx.strokeRect(x - (size / 2), y - (size / 2), size, size);

0 commit comments

Comments
 (0)