Skip to content

Commit 2394495

Browse files
committed
Add size data to callback
1 parent e19168f commit 2394495

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

src/ResizeSensor.js

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -152,32 +152,50 @@
152152
var expand = element.resizeSensor.childNodes[0];
153153
var expandChild = expand.childNodes[0];
154154
var shrink = element.resizeSensor.childNodes[1];
155-
var dirty, rafId, newWidth, newHeight;
155+
156+
var dirty, rafId;
156157
var size = getElementSize(element);
157158
var lastWidth = size.width;
158159
var lastHeight = size.height;
159-
160+
var initialHiddenCheck = true, resetRAF_id;
161+
162+
163+
var resetExpandShrink_ = function () {
164+
expandChild.style.width = '100000px';
165+
expandChild.style.height = '100000px';
166+
167+
expand.scrollLeft = 100000;
168+
expand.scrollTop = 100000;
169+
170+
shrink.scrollLeft = 100000;
171+
shrink.scrollTop = 100000;
172+
};
160173
var reset = function() {
161-
//set display to block, necessary otherwise hidden elements won't ever work
162-
var invisible = element.offsetWidth === 0 && element.offsetHeight === 0;
163-
164-
if (invisible) {
165-
var saveDisplay = element.style.display;
166-
element.style.display = 'block';
167-
}
168-
169-
expandChild.style.width = '100000px';
170-
expandChild.style.height = '100000px';
171-
172-
expand.scrollLeft = 100000;
173-
expand.scrollTop = 100000;
174-
175-
shrink.scrollLeft = 100000;
176-
shrink.scrollTop = 100000;
177-
178-
if (invisible) {
179-
element.style.display = saveDisplay;
180-
}
174+
// Check if element is hidden
175+
if (initialHiddenCheck){
176+
if (!expand.scrollTop && !expand.scrollLeft) {
177+
178+
// reset
179+
resetExpandShrink_();
180+
181+
// Check in next frame
182+
if (!resetRAF_id){
183+
resetRAF_id = requestAnimationFrame(function(){
184+
resetRAF_id = 0;
185+
186+
reset();
187+
});
188+
}
189+
190+
return;
191+
}
192+
// Stop checking
193+
else{
194+
initialHiddenCheck = false;
195+
}
196+
}
197+
198+
resetExpandShrink_();
181199
};
182200
element.resizeSensor.resetSensor = reset;
183201

@@ -186,8 +204,8 @@
186204

187205
if (!dirty) return;
188206

189-
lastWidth = newWidth;
190-
lastHeight = newHeight;
207+
lastWidth = size.width;
208+
lastHeight = size.height;
191209

192210
if (element.resizedAttached) {
193211
element.resizedAttached.call(
@@ -199,10 +217,8 @@
199217
};
200218

201219
var onScroll = function() {
202-
var size = getElementSize(element);
203-
var newWidth = size.width;
204-
var newHeight = size.height;
205-
dirty = newWidth != lastWidth || newHeight != lastHeight;
220+
size = getElementSize(element);
221+
dirty = size.width !== lastWidth || size.height !== lastHeight;
206222

207223
if (dirty && !rafId) {
208224
rafId = requestAnimationFrame(onResized);
@@ -222,8 +238,8 @@
222238
addEvent(expand, 'scroll', onScroll);
223239
addEvent(shrink, 'scroll', onScroll);
224240

225-
// Fix for custom Elements
226-
requestAnimationFrame(reset);
241+
// Fix for custom Elements
242+
requestAnimationFrame(reset);
227243
}
228244

229245
forEachElement(element, function(elem){

0 commit comments

Comments
 (0)