Skip to content

Commit

Permalink
Trigger initially the resize sensor, fixes #265
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed May 9, 2019
1 parent 389ca99 commit eab5e1b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/ElementQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,12 @@
if (!element.elementQueriesSetupInformation) {
element.elementQueriesSetupInformation = new SetupInformation(element, id);
}

if (!element.elementQueriesSensor) {
element.elementQueriesSensor = new ResizeSensor(element, function () {
element.elementQueriesSetupInformation.call();
});
}

element.elementQueriesSetupInformation.call();
}

/**
Expand Down Expand Up @@ -441,7 +440,7 @@
for (var i = 0, j = document.styleSheets.length; i < j; i++) {
try {
if (document.styleSheets[i].href && 0 === document.styleSheets[i].href.indexOf('file://')) {
console.log("CssElementQueries: unable to parse local css files, " + document.styleSheets[i].href);
console.warn("CssElementQueries: unable to parse local css files, " + document.styleSheets[i].href);
}

readRules(document.styleSheets[i].cssRules || document.styleSheets[i].rules || document.styleSheets[i].cssText);
Expand Down
21 changes: 21 additions & 0 deletions src/ResizeSensor.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
export declare type ResizeSensorCallback = (size: { width: number; height: number; }) => void;

export declare class ResizeSensor {
/**
* Creates a new resize sensor on given elements. The provided callback is called max 1 times per requestAnimationFrame and
* is called initially.
*/
constructor(element: Element | Element[], callback: ResizeSensorCallback);

/**
* Removes the resize sensor, and stops listening to resize events.
*/
detach(callback?: ResizeSensorCallback): void;

/**
* Resets the resize sensors, so for the next element resize is correctly detected. This is rare cases necessary
* when the resize sensor isn't initialised correctly or is in a broken state due to DOM modifications.
*/
reset(): void;

/**
* Removes the resize sensor, and stops listening to resize events.
*/
static detach(element: Element | Element[], callback?: ResizeSensorCallback): void;

/**
* Resets the resize sensors, so for the next element resize is correctly detected. This is rare cases necessary
* when the resize sensor isn't initialised correctly or is in a broken state due to DOM modifications.
*/
static reset(element: Element | Element[]): void;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ResizeSensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@

var dirty, rafId;
var size = getElementSize(element);
var lastWidth = size.width;
var lastHeight = size.height;
var lastWidth = 0;
var lastHeight = 0;
var initialHiddenCheck = true;
var lastAnimationFrame = 0;

Expand Down

0 comments on commit eab5e1b

Please sign in to comment.