@@ -7,8 +7,6 @@ exports["default"] = void 0;
7
7
8
8
var _utils = require ( "./utils" ) ;
9
9
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
-
12
10
function _slicedToArray ( arr , i ) { return _arrayWithHoles ( arr ) || _iterableToArrayLimit ( arr , i ) || _unsupportedIterableToArray ( arr , i ) || _nonIterableRest ( ) ; }
13
11
14
12
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" || !(
17
15
18
16
function _arrayWithHoles ( arr ) { if ( Array . isArray ( arr ) ) return arr ; }
19
17
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
+
20
20
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 ; } } } ; }
21
21
22
22
function _toConsumableArray ( arr ) { return _arrayWithoutHoles ( arr ) || _iterableToArray ( arr ) || _unsupportedIterableToArray ( arr ) || _nonIterableSpread ( ) ; }
@@ -54,7 +54,7 @@ var ElementQueries = /*#__PURE__*/function () {
54
54
55
55
if ( ! ( 'ResizeObserver' in window ) ) {
56
56
// 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 ) ) ;
58
58
return ;
59
59
}
60
60
@@ -81,7 +81,7 @@ var ElementQueries = /*#__PURE__*/function () {
81
81
82
82
if ( this . opts . observeDom ) {
83
83
if ( ! ( 'MutationObserver' in window ) ) {
84
- console . error ( _utils . Errors . NOT_SUPPORTED_OBSERVE ) ;
84
+ console . error ( ( 0 , _utils . prefixLog ) ( _utils . Errors . NOT_SUPPORTED_OBSERVE ) ) ;
85
85
} else {
86
86
this . domObserver = new MutationObserver ( this . onDomMutation . bind ( this ) ) ;
87
87
this . domObserver . observe ( document . body , {
@@ -147,8 +147,8 @@ var ElementQueries = /*#__PURE__*/function () {
147
147
if ( ( 0 , _utils . isValidElement ) ( element ) && hasBreakpoints ) {
148
148
try {
149
149
this . watch ( element ) ;
150
- } catch ( e ) {
151
- console . error ( e , element ) ;
150
+ } catch ( error ) {
151
+ console . error ( ( 0 , _utils . prefixLog ) ( error ) , element ) ;
152
152
}
153
153
}
154
154
}
@@ -170,7 +170,6 @@ var ElementQueries = /*#__PURE__*/function () {
170
170
var elements = document . querySelectorAll ( attrs . map ( function ( i ) {
171
171
return "[" . concat ( i , "]" ) ;
172
172
} ) . join ( ',' ) ) ;
173
- console . log ( elements ) ;
174
173
175
174
var _iterator = _createForOfIteratorHelper ( elements ) ,
176
175
_step ;
@@ -182,7 +181,7 @@ var ElementQueries = /*#__PURE__*/function () {
182
181
try {
183
182
this . watch ( element ) ;
184
183
} catch ( error ) {
185
- console . error ( error , element ) ;
184
+ console . error ( ( 0 , _utils . prefixLog ) ( error ) , element ) ;
186
185
}
187
186
}
188
187
} catch ( err ) {
@@ -194,45 +193,29 @@ var ElementQueries = /*#__PURE__*/function () {
194
193
/**
195
194
* Watch an element manually
196
195
* @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
197
197
*/
198
198
199
199
} , {
200
200
key : "watch" ,
201
201
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 ;
205
203
206
204
if ( ! element || ! ( 0 , _utils . isValidElement ) ( element ) ) {
207
205
throw new Error ( _utils . Errors . INVALID_ELEMENT ) ;
208
206
}
209
207
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
+ }
214
212
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
+ }
217
216
}
218
217
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 ) ;
236
219
237
220
if ( ! Object . values ( breakpoints ) . filter ( Boolean ) . length ) {
238
221
throw new Error ( _utils . Errors . BREAKPOINTS_MISSING ) ;
@@ -281,10 +264,10 @@ var ElementQueries = /*#__PURE__*/function () {
281
264
if ( ! entry || ! ElementQueries . hasBreakpoints ( entry ) ) return {
282
265
v : void 0
283
266
} ;
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 ] ;
288
271
289
272
acc [ k ] = ElementQueries . getActiveBreakpoint ( entry [ k ] , bps ) ;
290
273
return acc ;
@@ -322,6 +305,39 @@ var ElementQueries = /*#__PURE__*/function () {
322
305
this . elements = new WeakMap ( ) ;
323
306
} // internal
324
307
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
+ }
325
341
} ] , [ {
326
342
key : "hasBreakpoints" ,
327
343
value : function hasBreakpoints ( entry ) {
0 commit comments