Skip to content

Commit a7ff198

Browse files
authored
Merge pull request #115 from nicolaserny/improve-defer-init
Improve the div intial size detection
2 parents c1f7cfe + 4b56f2e commit a7ff198

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-gojs",
3-
"version": "4.6.1",
3+
"version": "4.6.2",
44
"description": "GoJS React integration",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/GojsDiagram.test.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,19 @@ describe('<GojsDiagram />', () => {
9696
let keyIndex = 0;
9797

9898
beforeEach(() => {
99-
Object.defineProperty(Element.prototype, 'clientWidth', { value: 100 });
100-
Object.defineProperty(Element.prototype, 'clientHeight', { value: 100 });
99+
Element.prototype.getBoundingClientRect = function() {
100+
return {
101+
width: 100,
102+
height: 100,
103+
top: 0,
104+
left: 0,
105+
x: 0,
106+
y: 0,
107+
toJSON: null,
108+
bottom: 0,
109+
right: 0
110+
};
111+
};
101112
keyIndex = 0;
102113
const dom = document.body;
103114
modelChangeCallback = jest.fn();

src/GojsDiagram.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,22 @@ class GojsDiagram<N extends BaseNodeModel, L extends LinkModel> extends React.Pu
6565
}
6666

6767
componentDidMount() {
68-
let intervalCount = 0;
69-
this.mountInterval = setInterval(() => {
70-
if (this.divRef.current && this.divRef.current.clientWidth && this.divRef.current.clientHeight) {
71-
this.init();
72-
clearInterval(this.mountInterval);
73-
} else {
74-
if (intervalCount > 10) {
68+
if (this.divRef.current) {
69+
let prevValue = JSON.stringify(this.divRef.current.getBoundingClientRect());
70+
this.mountInterval = setInterval(() => {
71+
if (this.divRef.current) {
72+
let nextValue = JSON.stringify(this.divRef.current.getBoundingClientRect());
73+
if (nextValue === prevValue) {
74+
clearInterval(this.mountInterval);
75+
this.init();
76+
} else {
77+
prevValue = nextValue;
78+
}
79+
} else {
7580
clearInterval(this.mountInterval);
7681
}
77-
intervalCount++;
78-
}
79-
}, 10);
82+
}, 50);
83+
}
8084
}
8185

8286
componentWillUnmount() {

0 commit comments

Comments
 (0)