From 4f0b878a21a0898f50d02045ebc241bdf38d9b4e Mon Sep 17 00:00:00 2001
From: Kuzcoo
Date: Mon, 19 Dec 2016 14:39:08 +0100
Subject: [PATCH 01/60] Update ResizeSensor.js (#142)
---
src/ResizeSensor.js | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index af38c6e..1c922ce 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -35,6 +35,7 @@
var isCollectionTyped = ('[object Array]' === elementsType
|| ('[object NodeList]' === elementsType)
|| ('[object HTMLCollection]' === elementsType)
+ || ('[object Object]' === elementsType)
|| ('undefined' !== typeof jQuery && elements instanceof jQuery) //jquery
|| ('undefined' !== typeof Elements && elements instanceof Elements) //mootools
);
From 520c969a324fb634fa0a7bbbca6a604dd57c4fbe Mon Sep 17 00:00:00 2001
From: landabaso
Date: Mon, 19 Dec 2016 14:41:55 +0100
Subject: [PATCH 02/60] Make sure it does not throw in SSR (#137)
Make sure it does not throw in a SSR (Server Side Rendering) situation
---
src/ResizeSensor.js | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index 1c922ce..dde2e7a 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -14,6 +14,10 @@
}
}(this, function () {
+ //Make sure it does not throw in a SSR (Server Side Rendering) situation
+ if (typeof window === "undefined") {
+ return null;
+ }
// Only used for the dirty checking, so the event callback count is limted to max 1 call per fps per sensor.
// In combination with the event based resize sensor this saves cpu time, because the sensor is too fast and
// would generate too many unnecessary events.
From d9acc1893df2304b51740806839f9c3bbdc2220d Mon Sep 17 00:00:00 2001
From: Thomas Luzat
Date: Mon, 19 Dec 2016 14:45:53 +0100
Subject: [PATCH 03/60] Removed dirty checking, minor optimizations (#135)
* Removed dirty checking, minor optimizations
An update will now only be queued through requestAnimationFrame whenever
changes occurred. This means that absolutely no code is running when the
page is idling (helpful for power saving and scalability). Care was
taken to first read from the DOM and afterwards update it in the new
scroll handler to prevent any possible layout thrashing as described here:
http://wilsonpage.co.uk/preventing-layout-thrashing/
lastWidth, lastHeight and all sensor properties are only updated
once every frame.
* Run callbacks only if size changed between two frames
An element may change its size multiple times during a frame. Do not
emit events anymore if its size returned to its previous size since the
last update. This means that events occurring in between two frames -
which are never visible to the user - may be skipped.
* Reset sensors always
Sensors need to be reset even if the element's size returned to its
original dimensions.
* Fix performance regression
Resetting within the requestAnimationFrame handler lead to bad
performance. This was noticeable in the performance demo where this
version fares about as good as older versions.
* Avoid initial resize event
* Code simplification
---
src/ResizeSensor.js | 40 +++++++++++++++++++++-------------------
1 file changed, 21 insertions(+), 19 deletions(-)
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index dde2e7a..f027998 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -143,10 +143,13 @@
var expand = element.resizeSensor.childNodes[0];
var expandChild = expand.childNodes[0];
var shrink = element.resizeSensor.childNodes[1];
+ var dirty, rafId, newWidth, newHeight;
+ var lastWidth = element.offsetWidth;
+ var lastHeight = element.offsetHeight;
var reset = function() {
- expandChild.style.width = 100000 + 'px';
- expandChild.style.height = 100000 + 'px';
+ expandChild.style.width = '100000px';
+ expandChild.style.height = '100000px';
expand.scrollLeft = 100000;
expand.scrollTop = 100000;
@@ -156,31 +159,30 @@
};
reset();
- var dirty = false;
- var dirtyChecking = function() {
- if (!element.resizedAttached) return;
+ var onResized = function() {
+ rafId = 0;
- if (dirty) {
+ if (!dirty) return;
+
+ lastWidth = newWidth;
+ lastHeight = newHeight;
+
+ if (element.resizedAttached) {
element.resizedAttached.call();
- dirty = false;
}
-
- requestAnimationFrame(dirtyChecking);
};
- requestAnimationFrame(dirtyChecking);
- var lastWidth, lastHeight;
- var cachedWidth, cachedHeight; //useful to not query offsetWidth twice
-
var onScroll = function() {
- if ((cachedWidth = element.offsetWidth) != lastWidth || (cachedHeight = element.offsetHeight) != lastHeight) {
- dirty = true;
+ newWidth = element.offsetWidth;
+ newHeight = element.offsetHeight;
+ dirty = newWidth != lastWidth || newHeight != lastHeight;
+
+ if (dirty && !rafId) {
+ rafId = requestAnimationFrame(onResized);
+ }
- lastWidth = cachedWidth;
- lastHeight = cachedHeight;
- }
- reset();
+ reset();
};
var addEvent = function(el, name, cb) {
From 05c98b5f271a35555f2b288313c52e745ed7c693 Mon Sep 17 00:00:00 2001
From: Darren Scerri
Date: Wed, 8 Feb 2017 13:41:19 +0100
Subject: [PATCH 04/60] Fix small typo and comment style consistency (#149)
---
src/ResizeSensor.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index f027998..376cb5c 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -14,11 +14,11 @@
}
}(this, function () {
- //Make sure it does not throw in a SSR (Server Side Rendering) situation
+ // Make sure it does not throw in a SSR (Server Side Rendering) situation
if (typeof window === "undefined") {
return null;
}
- // Only used for the dirty checking, so the event callback count is limted to max 1 call per fps per sensor.
+ // Only used for the dirty checking, so the event callback count is limited to max 1 call per fps per sensor.
// In combination with the event based resize sensor this saves cpu time, because the sensor is too fast and
// would generate too many unnecessary events.
var requestAnimationFrame = window.requestAnimationFrame ||
From c444bc400e75256c334522d98cf70863c160218a Mon Sep 17 00:00:00 2001
From: Kyle
Date: Wed, 8 Feb 2017 13:42:09 +0100
Subject: [PATCH 05/60] Simplify else statements (#147)
---
src/ResizeSensor.js | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index 376cb5c..7dd2dce 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -100,11 +100,12 @@
function getComputedStyle(element, prop) {
if (element.currentStyle) {
return element.currentStyle[prop];
- } else if (window.getComputedStyle) {
+ }
+ if (window.getComputedStyle) {
return window.getComputedStyle(element, null).getPropertyValue(prop);
- } else {
- return element.style[prop];
}
+
+ return element.style[prop];
}
/**
@@ -113,14 +114,14 @@
* @param {Function} resized
*/
function attachResizeEvent(element, resized) {
- if (!element.resizedAttached) {
- element.resizedAttached = new EventQueue();
- element.resizedAttached.add(resized);
- } else if (element.resizedAttached) {
+ if (element.resizedAttached) {
element.resizedAttached.add(resized);
return;
}
+ element.resizedAttached = new EventQueue();
+ element.resizedAttached.add(resized);
+
element.resizeSensor = document.createElement('div');
element.resizeSensor.className = 'resize-sensor';
var style = 'position: absolute; left: 0; top: 0; right: 0; bottom: 0; overflow: hidden; z-index: -1; visibility: hidden;';
From 134443d31c10ef8c0b475b892c2ecc4f75334dc5 Mon Sep 17 00:00:00 2001
From: "Joseph J. Schmitt"
Date: Thu, 6 Apr 2017 04:18:45 -0400
Subject: [PATCH 06/60] Fix ElementQueries always attaching to window and
listening (#162)
When we originally added UMD support, we forgot to move the logic that forcibly calls .listen() out of the block at the bottom and into the UMD logic block. This, in turn, meant that EQ was always being attached to window and listen was always being called, even when using EQ or ResizeSensor in a front-end environment with module support.
---
src/ElementQueries.js | 10 +---------
1 file changed, 1 insertion(+), 9 deletions(-)
diff --git a/src/ElementQueries.js b/src/ElementQueries.js
index b71d6c9..14f1232 100755
--- a/src/ElementQueries.js
+++ b/src/ElementQueries.js
@@ -11,6 +11,7 @@
module.exports = factory(require('./ResizeSensor.js'));
} else {
root.ElementQueries = factory(root.ResizeSensor);
+ root.ElementQueries.listen();
}
}(this, function (ResizeSensor) {
@@ -501,15 +502,6 @@
domLoaded(ElementQueries.init);
};
- // make available to common module loader
- if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
- module.exports = ElementQueries;
- }
- else {
- window.ElementQueries = ElementQueries;
- ElementQueries.listen();
- }
-
return ElementQueries;
}));
From ed6a88c5681079b431e1e3bc2de6382020837bda Mon Sep 17 00:00:00 2001
From: Philippe Vachon-Rivard
Date: Thu, 4 May 2017 14:10:06 -0400
Subject: [PATCH 07/60] Fixed bug that prevented detach from being called on
instance, even when withTracking was set to true. (#169)
---
src/ElementQueries.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/ElementQueries.js b/src/ElementQueries.js
index 14f1232..0175e15 100755
--- a/src/ElementQueries.js
+++ b/src/ElementQueries.js
@@ -415,7 +415,7 @@
};
this.detach = function() {
- if (!this.withTracking) {
+ if (!trackingActive) {
throw 'withTracking is not enabled. We can not detach elements since we don not store it.' +
'Use ElementQueries.withTracking = true; before domready or call ElementQueryes.update(true).';
}
From a90832ef68b7415aa85d747e4d679c9e6bdaa668 Mon Sep 17 00:00:00 2001
From: Chakrit Likitkhajorn
Date: Fri, 5 May 2017 01:10:41 +0700
Subject: [PATCH 08/60] Fix element sometimes already unmounted using with
react (#168)
---
src/ResizeSensor.js | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index 7dd2dce..3b2e896 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -114,6 +114,7 @@
* @param {Function} resized
*/
function attachResizeEvent(element, resized) {
+ if (!element) return;
if (element.resizedAttached) {
element.resizedAttached.add(resized);
return;
@@ -209,6 +210,7 @@
ResizeSensor.detach = function(element, ev) {
forEachElement(element, function(elem){
+ if (!elem) return
if(elem.resizedAttached && typeof ev == "function"){
elem.resizedAttached.remove(ev);
if(elem.resizedAttached.length()) return;
From 85122fe77f91627e4873d45686a1bb2ecb669bfe Mon Sep 17 00:00:00 2001
From: Franck Freiburger
Date: Fri, 9 Jun 2017 18:56:37 +0200
Subject: [PATCH 09/60] ResizeSensor.js optimization (#172)
The aim of `getComputedStyle(element, 'position') == 'static'` is to detect that `element` is not "positioned".
[`HTMLElement.offsetParent`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetParent) returns the nearest positioned element in the containment hierarchy.
`element.resizeSensor.offsetParent !== element` mean that `element` is not positioned.
Right ?
---
src/ResizeSensor.js | 18 +-----------------
1 file changed, 1 insertion(+), 17 deletions(-)
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index 3b2e896..acc0138 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -92,22 +92,6 @@
}
}
- /**
- * @param {HTMLElement} element
- * @param {String} prop
- * @returns {String|Number}
- */
- function getComputedStyle(element, prop) {
- if (element.currentStyle) {
- return element.currentStyle[prop];
- }
- if (window.getComputedStyle) {
- return window.getComputedStyle(element, null).getPropertyValue(prop);
- }
-
- return element.style[prop];
- }
-
/**
*
* @param {HTMLElement} element
@@ -138,7 +122,7 @@
'';
element.appendChild(element.resizeSensor);
- if (getComputedStyle(element, 'position') == 'static') {
+ if (element.resizeSensor.offsetParent !== element) {
element.style.position = 'relative';
}
From ae2b61038bdaf6f46d81a9850f784f82c25ff416 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Bendykowski?=
Date: Fri, 21 Jul 2017 23:46:03 +0200
Subject: [PATCH 10/60] Add ResizeSensor typing for TypeScript (#179)
---
src/ResizeSensor.d.ts | 5 +++++
1 file changed, 5 insertions(+)
create mode 100644 src/ResizeSensor.d.ts
diff --git a/src/ResizeSensor.d.ts b/src/ResizeSensor.d.ts
new file mode 100644
index 0000000..8599850
--- /dev/null
+++ b/src/ResizeSensor.d.ts
@@ -0,0 +1,5 @@
+declare class ResizeSensor {
+ constructor(element: (Element | Element[]), callback: Function);
+}
+
+export = ResizeSensor;
From bc77870c8214a716e8061fede683a0e54d8fc48a Mon Sep 17 00:00:00 2001
From: Noel Rivas
Date: Fri, 21 Jul 2017 16:46:52 -0500
Subject: [PATCH 11/60] Include imported styleSheets in rule extraction. (#176)
---
src/ElementQueries.js | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/ElementQueries.js b/src/ElementQueries.js
index 0175e15..1e77ed2 100755
--- a/src/ElementQueries.js
+++ b/src/ElementQueries.js
@@ -367,6 +367,8 @@
}
} else if (4 === rules[i].type) {
readRules(rules[i].cssRules || rules[i].rules);
+ } else if (3 === rules[i].type) {
+ readRules(rules[i].styleSheet.cssRules);
}
}
}
From 4d6469bed74f1e3979dd395c050dd5c4e95cb572 Mon Sep 17 00:00:00 2001
From: newlukai
Date: Fri, 21 Jul 2017 23:56:55 +0200
Subject: [PATCH 12/60] Use 'window' if available as 'root' in exporting (#151)
---
src/ElementQueries.js | 2 +-
src/ResizeSensor.js | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/ElementQueries.js b/src/ElementQueries.js
index 1e77ed2..a3ba4bd 100755
--- a/src/ElementQueries.js
+++ b/src/ElementQueries.js
@@ -13,7 +13,7 @@
root.ElementQueries = factory(root.ResizeSensor);
root.ElementQueries.listen();
}
-}(this, function (ResizeSensor) {
+}(typeof window !== 'undefined' ? window : this, function (ResizeSensor) {
/**
*
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index acc0138..66dbf4a 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -12,7 +12,7 @@
} else {
root.ResizeSensor = factory();
}
-}(this, function () {
+}(typeof window !== 'undefined' ? window : this, function () {
// Make sure it does not throw in a SSR (Server Side Rendering) situation
if (typeof window === "undefined") {
From adefff41c47aea79e73cd485ccd7d64ff9bbfc53 Mon Sep 17 00:00:00 2001
From: Nikita
Date: Fri, 18 Aug 2017 19:35:03 +0300
Subject: [PATCH 13/60] Fix error with local variables (#167)
---
src/ElementQueries.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/ElementQueries.js b/src/ElementQueries.js
index a3ba4bd..ab121cb 100755
--- a/src/ElementQueries.js
+++ b/src/ElementQueries.js
@@ -330,8 +330,8 @@
* @param {String} css
*/
function extractQuery(css) {
- var match;
- var smatch;
+ var match, smatch, attrs, attrMatch;
+
css = css.replace(/'/g, '"');
while (null !== (match = regex.exec(css))) {
smatch = match[1] + match[3];
From 100d74f2400e1a1f5dc25d74155dc9483ebfba8e Mon Sep 17 00:00:00 2001
From: kaljak
Date: Tue, 14 Nov 2017 13:45:54 +0100
Subject: [PATCH 14/60] - fixed #95 (#198)
---
src/ElementQueries.js | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)
diff --git a/src/ElementQueries.js b/src/ElementQueries.js
index ab121cb..7fa172e 100755
--- a/src/ElementQueries.js
+++ b/src/ElementQueries.js
@@ -180,9 +180,9 @@
else allQueries[mode][property][value] += ','+selector;
}
- function getQuery() {
+ function getQuery(container) {
var query;
- if (document.querySelectorAll) query = document.querySelectorAll.bind(document);
+ if (document.querySelectorAll) query = (container) ? container.querySelectorAll.bind(container) : document.querySelectorAll.bind(document);
if (!query && 'undefined' !== typeof $$) query = $$;
if (!query && 'undefined' !== typeof jQuery) query = jQuery;
@@ -196,14 +196,14 @@
/**
* Start the magic. Go through all collected rules (readRules()) and attach the resize-listener.
*/
- function findElementQueriesElements() {
- var query = getQuery();
+ function findElementQueriesElements(container) {
+ var query = getQuery(container);
for (var mode in allQueries) if (allQueries.hasOwnProperty(mode)) {
for (var property in allQueries[mode]) if (allQueries[mode].hasOwnProperty(property)) {
for (var value in allQueries[mode][property]) if (allQueries[mode][property].hasOwnProperty(value)) {
- var elements = query(allQueries[mode][property][value]);
+ var elements = query(allQueries[mode][property][value], container);
for (var i = 0, j = elements.length; i < j; i++) {
setupElement(elements[i], {
mode: mode,
@@ -407,6 +407,15 @@
findResponsiveImages();
};
+ /**
+ * Go through all collected rules (readRules()) and attach the resize-listener.
+ *
+ * @param {HTMLElement} container only elements of the container are considered (document.body if not set)
+ */
+ this.findElementQueriesElements = function(container) {
+ findElementQueriesElements(container);
+ };
+
/**
*
* @param {Boolean} withTracking allows and requires you to use detach, since we store internally all used elements
@@ -500,6 +509,10 @@
else window.onload = callback;
};
+ ElementQueries.findElementQueriesElements = function(container) {
+ ElementQueries.instance.findElementQueriesElements(container);
+ };
+
ElementQueries.listen = function() {
domLoaded(ElementQueries.init);
};
From 004784e0d196f5d356981dba33b5fdff9e80a802 Mon Sep 17 00:00:00 2001
From: Alex Bennett
Date: Tue, 14 Nov 2017 12:46:20 +0000
Subject: [PATCH 15/60] Adding error check for InvalidAccessError (#197)
This only happens with Firefox due to their security policy.
Fix found in previous issue thread. https://github.com/marcj/css-element-queries/issues/117
Can confirm this was due to typekit being included in the project.
It was quite annoying to debug in a build environment via es6 module loader (systemJS) so hopefully, this PR solves the issue for people.
All credit to @jyrkij
---
src/ElementQueries.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/ElementQueries.js b/src/ElementQueries.js
index 7fa172e..dd53af4 100755
--- a/src/ElementQueries.js
+++ b/src/ElementQueries.js
@@ -389,7 +389,7 @@
try {
readRules(document.styleSheets[i].cssRules || document.styleSheets[i].rules || document.styleSheets[i].cssText);
} catch(e) {
- if (e.name !== 'SecurityError') {
+ if (e.name !== 'SecurityError' && e.name !== 'InvalidAccessError') {
throw e;
}
}
From d73bb3db5ab31749cadf250797537e1cd24b7fe5 Mon Sep 17 00:00:00 2001
From: James Adams
Date: Tue, 14 Nov 2017 23:46:36 +1100
Subject: [PATCH 16/60] Add detach to TS type def (#194)
* feat(typescript type defs): add detach
* refactor(semicolon)
---
src/ResizeSensor.d.ts | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/ResizeSensor.d.ts b/src/ResizeSensor.d.ts
index 8599850..fe5bb03 100644
--- a/src/ResizeSensor.d.ts
+++ b/src/ResizeSensor.d.ts
@@ -1,5 +1,6 @@
declare class ResizeSensor {
constructor(element: (Element | Element[]), callback: Function);
+ detach(callback: Function): void;
}
export = ResizeSensor;
From 9801bb0a731b8694282e9fb350194b975b8dfac3 Mon Sep 17 00:00:00 2001
From: Christian Memije
Date: Fri, 22 Dec 2017 06:09:12 -0800
Subject: [PATCH 17/60] Set direction as ltr (#201)
---
src/ResizeSensor.js | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index 66dbf4a..282ef0c 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -108,6 +108,7 @@
element.resizedAttached.add(resized);
element.resizeSensor = document.createElement('div');
+ element.resizeSensor.dir = 'ltr';
element.resizeSensor.className = 'resize-sensor';
var style = 'position: absolute; left: 0; top: 0; right: 0; bottom: 0; overflow: hidden; z-index: -1; visibility: hidden;';
var styleChild = 'position: absolute; left: 0; top: 0; transition: 0s;';
From bc8541c4fd0c89e979c04a693283864e4a6c6388 Mon Sep 17 00:00:00 2001
From: Alexis
Date: Fri, 22 Dec 2017 15:09:43 +0100
Subject: [PATCH 18/60] Fix require in documentation README.md (#200)
---
README.md | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index 833190c..7f50958 100644
--- a/README.md
+++ b/README.md
@@ -88,15 +88,15 @@ Here live http://marcj.github.io/css-element-queries/.
If you're using a module loader you need to trigger the event listening or initialization yourself:
```javascript
-var EQ = require('node_modules/css-element-queries/ElementQueries');
+var ElementQueries = require('css-element-queries/src/ElementQueries');
//attaches to DOMLoadContent
-EQ.listen();
+ElementQueries.listen();
//or if you want to trigger it yourself.
// Parse all available CSS and attach ResizeSensor to those elements which have rules attached
// (make sure this is called after 'load' event, because CSS files are not ready when domReady is fired.
-EQ.init();
+ElementQueries.init();
```
## Issues
From fdb851e910a44e9167b58987a539430302ace71d Mon Sep 17 00:00:00 2001
From: lxjwlt
Date: Fri, 22 Dec 2017 22:11:01 +0800
Subject: [PATCH 19/60] fix: sensor not working when element's width or height
is 0 (#203)
---
src/ResizeSensor.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index 282ef0c..fbdfb98 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -110,7 +110,7 @@
element.resizeSensor = document.createElement('div');
element.resizeSensor.dir = 'ltr';
element.resizeSensor.className = 'resize-sensor';
- var style = 'position: absolute; left: 0; top: 0; right: 0; bottom: 0; overflow: hidden; z-index: -1; visibility: hidden;';
+ var style = 'position: absolute; left: -10px; top: -10px; right: 0; bottom: 0; overflow: hidden; z-index: -1; visibility: hidden;';
var styleChild = 'position: absolute; left: 0; top: 0; transition: 0s;';
element.resizeSensor.style.cssText = style;
From 072a3fc71c029f9a99dca469e272602982e6786c Mon Sep 17 00:00:00 2001
From: Pulyaev Yuriy
Date: Fri, 22 Dec 2017 16:14:40 +0200
Subject: [PATCH 20/60] Fix subpixel sizes support (#204)
---
src/ElementQueries.js | 27 +++++++++++++++++++++++----
src/ResizeSensor.js | 32 +++++++++++++++++++++++++++-----
2 files changed, 50 insertions(+), 9 deletions(-)
diff --git a/src/ElementQueries.js b/src/ElementQueries.js
index dd53af4..b6e86d0 100755
--- a/src/ElementQueries.js
+++ b/src/ElementQueries.js
@@ -38,6 +38,26 @@
return parseFloat(fontSize) || 16;
}
+ /**
+ * Get element size
+ * @param {HTMLElement} element
+ * @returns {Object} {width, height}
+ */
+ function getElementSize(element) {
+ if (!element.getBoundingClientRect) {
+ return {
+ width: element.offsetWidth,
+ height: element.offsetHeight
+ }
+ }
+
+ var rect = element.getBoundingClientRect();
+ return {
+ width: Math.round(rect.width),
+ height: Math.round(rect.height)
+ }
+ }
+
/**
*
* @copyright https://github.com/Mr0grog/element-query/blob/master/LICENSE
@@ -85,7 +105,7 @@
function SetupInformation(element) {
this.element = element;
this.options = {};
- var key, option, width = 0, height = 0, value, actualValue, attrValues, attrValue, attrName;
+ var key, option, elementSize, value, actualValue, attrValues, attrValue, attrName;
/**
* @param {Object} option {mode: 'min|max', property: 'width|height', value: '123px'}
@@ -102,8 +122,7 @@
*/
this.call = function() {
// extract current dimensions
- width = this.element.offsetWidth;
- height = this.element.offsetHeight;
+ elementSize = getElementSize(this.element);
attrValues = {};
@@ -115,7 +134,7 @@
value = convertToPx(this.element, option.value);
- actualValue = option.property == 'width' ? width : height;
+ actualValue = option.property == 'width' ? elementSize.width : elementSize.height;
attrName = option.mode + '-' + option.property;
attrValue = '';
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index fbdfb98..f0a6182 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -53,6 +53,26 @@
}
}
+ /**
+ * Get element size
+ * @param {HTMLElement} element
+ * @returns {Object} {width, height}
+ */
+ function getElementSize(element) {
+ if (!element.getBoundingClientRect) {
+ return {
+ width: element.offsetWidth,
+ height: element.offsetHeight
+ }
+ }
+
+ var rect = element.getBoundingClientRect();
+ return {
+ width: Math.round(rect.width),
+ height: Math.round(rect.height)
+ }
+ }
+
/**
* Class for dimension change detection.
*
@@ -131,8 +151,9 @@
var expandChild = expand.childNodes[0];
var shrink = element.resizeSensor.childNodes[1];
var dirty, rafId, newWidth, newHeight;
- var lastWidth = element.offsetWidth;
- var lastHeight = element.offsetHeight;
+ var size = getElementSize(element);
+ var lastWidth = size.width;
+ var lastHeight = size.height;
var reset = function() {
expandChild.style.width = '100000px';
@@ -161,8 +182,9 @@
};
var onScroll = function() {
- newWidth = element.offsetWidth;
- newHeight = element.offsetHeight;
+ var size = getElementSize(element);
+ var newWidth = size.width;
+ var newHeight = size.height;
dirty = newWidth != lastWidth || newHeight != lastHeight;
if (dirty && !rafId) {
@@ -195,7 +217,7 @@
ResizeSensor.detach = function(element, ev) {
forEachElement(element, function(elem){
- if (!elem) return
+ if (!elem) return;
if(elem.resizedAttached && typeof ev == "function"){
elem.resizedAttached.remove(ev);
if(elem.resizedAttached.length()) return;
From 2f6959de9c76dcd562ef8539dd40172ea76c1c2d Mon Sep 17 00:00:00 2001
From: JeanRemiDelteil
Date: Fri, 5 Jan 2018 19:53:47 +0100
Subject: [PATCH 21/60] Fix ResizeSensor to fire when used in a CustomElement
(#206)
Calling Reset() just after initialization does not fire the 'scroll' events.
Instead, call it in the next frame. This also works with native elements and simple DOM.
---
src/ResizeSensor.js | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/src/ResizeSensor.js b/src/ResizeSensor.js
index f0a6182..45692fa 100755
--- a/src/ResizeSensor.js
+++ b/src/ResizeSensor.js
@@ -166,8 +166,6 @@
shrink.scrollTop = 100000;
};
- reset();
-
var onResized = function() {
rafId = 0;
@@ -204,6 +202,9 @@
addEvent(expand, 'scroll', onScroll);
addEvent(shrink, 'scroll', onScroll);
+
+ // Fix for custom Elements
+ requestAnimationFrame(reset);
}
forEachElement(element, function(elem){
From df0ce5c7a131ff11f8cfdc5996973a4029d162d0 Mon Sep 17 00:00:00 2001
From: "Marc J. Schmidt"
Date: Tue, 9 Jan 2018 01:16:18 +0100
Subject: [PATCH 22/60] Added dynamic tracking of newly created elements
(supporting angular&co).
Removed element tracking.
Dropped IE9 and lower support.
Fixed sensor bug with invisible elements.
Addet reset() method to ResizeSensor.
---
README.md | 8 +-
src/ElementQueries.js | 265 +++++++++++++-------------
src/ResizeSensor.js | 33 +++-
tests/demo.css | 425 ++++++++++++++++++++++++++++++++++++++++++
tests/demo.html | 396 +++++++++++++++++++++++++++++++++++++++
tests/demo.js | 135 ++++++++++++++
6 files changed, 1116 insertions(+), 146 deletions(-)
create mode 100644 tests/demo.css
create mode 100644 tests/demo.html
create mode 100644 tests/demo.js
diff --git a/README.md b/README.md
index 7f50958..61c88f5 100644
--- a/README.md
+++ b/README.md
@@ -13,9 +13,10 @@ Features:
- no performance issues since it listens only on size changes of elements that have element query rules defined through css. Other element query polifills only listen on `window.onresize` which causes performance issues and allows only to detect changes via window.resize event and not inside layout changes like css3 animation, :hover, DOM changes etc.
- no interval/timeout detection. Truly event-based through integrated ResizeSensor class.
+ - automatically discovers new DOM elements. No need to call javascript manually.
- no CSS modifications. Valid CSS Syntax
- - all CSS selectors available. Uses regular attribute selector. No need to write rules in HTML.
- - supports and tested in webkit, gecko and IE(7/8/9/10/11)
+ - all CSS selectors available. Uses regular attribute selector. No need to write rules in HTML/JS.
+ - supports and tested in webkit, gecko and IE(10+)
- `min-width`, `min-height`, `max-width` and `max-height` are supported so far
- works with any layout modifications: HTML (innerHTML etc), inline styles, DOM mutation, CSS3 transitions, fluid layout changes (also percent changes), pseudo classes (:hover etc.), window resizes and more
- no Javascript-Framework dependency (works with jQuery, Mootools, etc.)
@@ -50,7 +51,7 @@ More demos and information: http://marcj.github.io/css-element-queries/
As you can see we use the `~=` [attribute selector](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors).
Since this css-element-queries polyfill adds new element attributes on the DOM element
-(``) depending on your actual CSS,
+(``) depending on your actual CSS and element's dimension,
you should always use this attribute selector (especially if you have several element query rules on the same element).
```html
@@ -104,6 +105,7 @@ ElementQueries.init();
- So far does not work on `img` and other elements that can't contain other elements. Wrapping with a `div` works fine though (See demo).
- Adds additional hidden elements into selected target element and forces target element to be relative or absolute.
- Local stylesheets do not work (using `file://` protocol).
+ - If you have rules on an element that has a css animation, also add `element-queries`. E.g. `.widget-name { animation: 2sec my-animation, 1s element-queries;}`. We use this to detect new added DOM elements automatically.
## License
diff --git a/src/ElementQueries.js b/src/ElementQueries.js
index b6e86d0..2bd723c 100755
--- a/src/ElementQueries.js
+++ b/src/ElementQueries.js
@@ -1,9 +1,10 @@
+'use strict';
+
/**
* Copyright Marc J. Schmidt. See the LICENSE file at the top-level
* directory of this distribution and at
* https://github.com/marcj/css-element-queries/blob/master/LICENSE.
*/
-;
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define(['./ResizeSensor.js'], factory);
@@ -20,10 +21,15 @@
* @type {Function}
* @constructor
*/
- var ElementQueries = function() {
+ var ElementQueries = function () {
+ //
+
+
+
+
+
+
+
+
Examples
+
+ Drag the gray line at the right to see it in action.
+
+
+
+
+
+
Responsive Text
+
+
+
+
Element responsiveness FTW!
+
+
+
Element started display: none
+
+
+
+
+
+
Element detachable
+
+
+
+
+
+
Dynamic element {{id}}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Responsive Image
+
+
+
+
+
+
+
+
+ The image above has a default of 700px. Shrink or expend the container too see responsive image
+ working.
+ Thanks @placehold.it
+
+
+
+
+
+
+
+
+
Responsive Widget
+
+
+
Demo 1
+ This is content from the first responsive demo without media queries. It uses the element
+ queries
+ provided by this library.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Responsive Layout
+
+
+
Demo 2
+
+ Box
+
+
+ First 1/2 box
+
+
+ Second 1/2 box
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Responsive Animation
+
+
+
Demo 3 - width
+
+
+
+ This box is animated through css transitions.
+ We attached a resize-listener to this box. See below.
+
+
+ No changes.
+
+
+
+
+
+
Demo 4 - height
+
+
+
+ This box is animated through css transitions.
+ We attached a resize-listener to this box. See below.
+
+
+ No changes.
+
+
+
+
+
+
+
ResizeSensor Demo
+
+
+
+ 0 changes
+
+
+
+
+ CSS-Element-Queries comes with a Javascript ResizeSensor class you can use in Javascript directly.
+