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

Commit fa5cf5c

Browse files
committed
1.1.0
1 parent 513fc3f commit fa5cf5c

File tree

6 files changed

+72
-48
lines changed

6 files changed

+72
-48
lines changed

dist/element-queries.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/element-queries.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ElementQueries.js

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ exports["default"] = void 0;
77

88
var _utils = require("./utils");
99

10-
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
11-
1210
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
1311

1412
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
@@ -17,6 +15,8 @@ function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(
1715

1816
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
1917

18+
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
19+
2020
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
2121

2222
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
@@ -54,7 +54,7 @@ var ElementQueries = /*#__PURE__*/function () {
5454

5555
if (!('ResizeObserver' in window)) {
5656
// not throwing to avoid breaking client's apps
57-
console.error(_utils.Errors.NOT_SUPPORTED);
57+
console.error((0, _utils.prefixLog)(_utils.Errors.NOT_SUPPORTED));
5858
return;
5959
}
6060

@@ -81,7 +81,7 @@ var ElementQueries = /*#__PURE__*/function () {
8181

8282
if (this.opts.observeDom) {
8383
if (!('MutationObserver' in window)) {
84-
console.error(_utils.Errors.NOT_SUPPORTED_OBSERVE);
84+
console.error((0, _utils.prefixLog)(_utils.Errors.NOT_SUPPORTED_OBSERVE));
8585
} else {
8686
this.domObserver = new MutationObserver(this.onDomMutation.bind(this));
8787
this.domObserver.observe(document.body, {
@@ -147,8 +147,8 @@ var ElementQueries = /*#__PURE__*/function () {
147147
if ((0, _utils.isValidElement)(element) && hasBreakpoints) {
148148
try {
149149
this.watch(element);
150-
} catch (e) {
151-
console.error(e, element);
150+
} catch (error) {
151+
console.error((0, _utils.prefixLog)(error), element);
152152
}
153153
}
154154
}
@@ -170,7 +170,6 @@ var ElementQueries = /*#__PURE__*/function () {
170170
var elements = document.querySelectorAll(attrs.map(function (i) {
171171
return "[".concat(i, "]");
172172
}).join(','));
173-
console.log(elements);
174173

175174
var _iterator = _createForOfIteratorHelper(elements),
176175
_step;
@@ -182,7 +181,7 @@ var ElementQueries = /*#__PURE__*/function () {
182181
try {
183182
this.watch(element);
184183
} catch (error) {
185-
console.error(error, element);
184+
console.error((0, _utils.prefixLog)(error), element);
186185
}
187186
}
188187
} catch (err) {
@@ -194,45 +193,29 @@ var ElementQueries = /*#__PURE__*/function () {
194193
/**
195194
* Watch an element manually
196195
* @param {HTMLElement, SVGElement} element The DOM element you would like to watch
196+
* @param {Object} [bps] breakpoints to use instead of looking in html attributes
197197
*/
198198

199199
}, {
200200
key: "watch",
201201
value: function watch(element) {
202-
var _this$opts3 = this.opts,
203-
htmlAttrBreakpoints = _this$opts3.htmlAttrBreakpoints,
204-
htmlAttrHeightBreakpoints = _this$opts3.htmlAttrHeightBreakpoints;
202+
var bps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
205203

206204
if (!element || !(0, _utils.isValidElement)(element)) {
207205
throw new Error(_utils.Errors.INVALID_ELEMENT);
208206
}
209207

210-
var breakpointAttrs = {
211-
width: element.getAttribute(htmlAttrBreakpoints),
212-
height: element.getAttribute(htmlAttrHeightBreakpoints)
213-
};
208+
if (bps) {
209+
if (this.opts.observeDom) {
210+
console.warn((0, _utils.prefixLog)(_utils.Errors.DOM_OBSERVE_CONFLICT));
211+
}
214212

215-
if (!Object.values(breakpointAttrs).filter(Boolean).length) {
216-
throw new Error(_utils.Errors.BREAKPOINTS_MISSING);
213+
if (_typeof(bps) !== 'object' || !bps.width && !bps.height) {
214+
throw new Error(_utils.Errors.BREAKPOINTS_INVALID);
215+
}
217216
}
218217

219-
var breakpoints = Object.entries(breakpointAttrs).reduce(function (acc, _ref) {
220-
var _ref2 = _slicedToArray(_ref, 2),
221-
k = _ref2[0],
222-
v = _ref2[1];
223-
224-
if (!v) return acc;
225-
226-
var matches = _toConsumableArray((0, _utils.removeWhitespace)(v).matchAll(_utils.BREAKPOINT_REGEX));
227-
228-
if (!matches) return acc;
229-
acc[k] = matches.reduce(function (accumulator, match) {
230-
if (!match[1] || !match[2]) return accumulator;
231-
accumulator[match[1]] = +match[2];
232-
return accumulator;
233-
}, {});
234-
return acc;
235-
}, {});
218+
var breakpoints = bps || this.getBreakpointsFromAttrs(element);
236219

237220
if (!Object.values(breakpoints).filter(Boolean).length) {
238221
throw new Error(_utils.Errors.BREAKPOINTS_MISSING);
@@ -281,10 +264,10 @@ var ElementQueries = /*#__PURE__*/function () {
281264
if (!entry || !ElementQueries.hasBreakpoints(entry)) return {
282265
v: void 0
283266
};
284-
var activeBreakpoints = Object.entries(entry.breakpoints).reduce(function (acc, _ref3) {
285-
var _ref4 = _slicedToArray(_ref3, 2),
286-
k = _ref4[0],
287-
bps = _ref4[1];
267+
var activeBreakpoints = Object.entries(entry.breakpoints).reduce(function (acc, _ref) {
268+
var _ref2 = _slicedToArray(_ref, 2),
269+
k = _ref2[0],
270+
bps = _ref2[1];
288271

289272
acc[k] = ElementQueries.getActiveBreakpoint(entry[k], bps);
290273
return acc;
@@ -322,6 +305,39 @@ var ElementQueries = /*#__PURE__*/function () {
322305
this.elements = new WeakMap();
323306
} // internal
324307

308+
}, {
309+
key: "getBreakpointsFromAttrs",
310+
value: function getBreakpointsFromAttrs(element) {
311+
var _this$opts3 = this.opts,
312+
htmlAttrBreakpoints = _this$opts3.htmlAttrBreakpoints,
313+
htmlAttrHeightBreakpoints = _this$opts3.htmlAttrHeightBreakpoints;
314+
var breakpointAttrs = {
315+
width: element.getAttribute(htmlAttrBreakpoints),
316+
height: element.getAttribute(htmlAttrHeightBreakpoints)
317+
};
318+
319+
if (!Object.values(breakpointAttrs).filter(Boolean).length) {
320+
throw new Error(_utils.Errors.BREAKPOINTS_MISSING);
321+
}
322+
323+
return Object.entries(breakpointAttrs).reduce(function (acc, _ref3) {
324+
var _ref4 = _slicedToArray(_ref3, 2),
325+
k = _ref4[0],
326+
v = _ref4[1];
327+
328+
if (!v) return acc;
329+
330+
var matches = _toConsumableArray((0, _utils.removeWhitespace)(v).matchAll(_utils.BREAKPOINT_REGEX));
331+
332+
if (!matches) return acc;
333+
acc[k] = matches.reduce(function (accumulator, match) {
334+
if (!match[1] || !match[2]) return accumulator;
335+
accumulator[match[1]] = +match[2];
336+
return accumulator;
337+
}, {});
338+
return acc;
339+
}, {});
340+
}
325341
}], [{
326342
key: "hasBreakpoints",
327343
value: function hasBreakpoints(entry) {

0 commit comments

Comments
 (0)