1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.Breakpoints = void 0;
|
7 |
|
8 | var _Utils = require("./Utils");
|
9 |
|
10 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
|
11 |
|
12 | function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
|
13 |
|
14 | function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
|
15 |
|
16 | function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
|
17 |
|
18 | function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
|
19 |
|
20 | function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
|
21 |
|
22 | function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
|
23 |
|
24 | function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
25 |
|
26 | function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
27 |
|
28 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
29 |
|
30 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
31 |
|
32 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
33 |
|
34 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
35 |
|
36 | function breakpointKey(breakpoint) {
|
37 | return Array.isArray(breakpoint) ? breakpoint.join("-") : breakpoint;
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | var Breakpoints =
|
46 |
|
47 | function () {
|
48 | _createClass(Breakpoints, null, [{
|
49 | key: "validKeys",
|
50 | value: function validKeys() {
|
51 | return ["at", "lessThan", "greaterThan", "greaterThanOrEqual", "between"];
|
52 | }
|
53 | }]);
|
54 |
|
55 | function Breakpoints(_breakpoints) {
|
56 | var _this = this;
|
57 |
|
58 | _classCallCheck(this, Breakpoints);
|
59 |
|
60 | _defineProperty(this, "_sortedBreakpoints", void 0);
|
61 |
|
62 | _defineProperty(this, "_breakpoints", void 0);
|
63 |
|
64 | _defineProperty(this, "_mediaQueries", void 0);
|
65 |
|
66 | _defineProperty(this, "findBreakpointsForWidths", function (fromWidth, throughWidth) {
|
67 | var fromBreakpoint = _this.findBreakpointAtWidth(fromWidth);
|
68 |
|
69 | if (!fromBreakpoint) {
|
70 | return undefined;
|
71 | }
|
72 |
|
73 | var throughBreakpoint = _this.findBreakpointAtWidth(throughWidth);
|
74 |
|
75 | if (!throughBreakpoint || fromBreakpoint === throughBreakpoint) {
|
76 | return [fromBreakpoint];
|
77 | } else {
|
78 | return _this._sortedBreakpoints.slice(_this._sortedBreakpoints.indexOf(fromBreakpoint), _this._sortedBreakpoints.indexOf(throughBreakpoint) + 1);
|
79 | }
|
80 | });
|
81 |
|
82 | _defineProperty(this, "findBreakpointAtWidth", function (width) {
|
83 | return _this._sortedBreakpoints.find(function (breakpoint, i) {
|
84 | var nextBreakpoint = _this._sortedBreakpoints[i + 1];
|
85 |
|
86 | if (nextBreakpoint) {
|
87 | return width >= _this._breakpoints[breakpoint] && width < _this._breakpoints[nextBreakpoint];
|
88 | } else {
|
89 | return width >= _this._breakpoints[breakpoint];
|
90 | }
|
91 | });
|
92 | });
|
93 |
|
94 | _defineProperty(this, "valuesWithBreakpointProps", function (values) {
|
95 | var max = values.length;
|
96 | var valueBreakpoints = [];
|
97 | var lastTuple;
|
98 |
|
99 | _this._sortedBreakpoints.forEach(function (breakpoint, i) {
|
100 | var value = values[i];
|
101 |
|
102 | if (i < max && (!lastTuple || lastTuple[0] !== value)) {
|
103 | lastTuple = [value, [breakpoint]];
|
104 | valueBreakpoints.push(lastTuple);
|
105 | } else {
|
106 | lastTuple[1].push(breakpoint);
|
107 | }
|
108 | });
|
109 |
|
110 | return valueBreakpoints.map(function (_ref, i) {
|
111 | var _ref2 = _slicedToArray(_ref, 2),
|
112 | value = _ref2[0],
|
113 | breakpoints = _ref2[1];
|
114 |
|
115 | var props = {};
|
116 |
|
117 | if (i === valueBreakpoints.length - 1) {
|
118 | props.greaterThanOrEqual = breakpoints[0];
|
119 | } else if (breakpoints.length === 1) {
|
120 | props.at = breakpoints[0];
|
121 | } else {
|
122 |
|
123 |
|
124 | props.between = [breakpoints[0], valueBreakpoints[i + 1][1][0]];
|
125 | }
|
126 |
|
127 | return [value, props];
|
128 | });
|
129 | });
|
130 |
|
131 | this._breakpoints = _breakpoints;
|
132 | this._sortedBreakpoints = Object.keys(_breakpoints).map(function (breakpoint) {
|
133 | return [breakpoint, _breakpoints[breakpoint]];
|
134 | }).sort(function (a, b) {
|
135 | return a[1] < b[1] ? -1 : 1;
|
136 | }).map(function (breakpointAndValue) {
|
137 | return breakpointAndValue[0];
|
138 | });
|
139 |
|
140 | var betweenCombinations = this._sortedBreakpoints.slice(0, -1).reduce(function (acc, b1, i) {
|
141 | return _toConsumableArray(acc).concat(_toConsumableArray(_this._sortedBreakpoints.slice(i + 1).map(function (b2) {
|
142 | return [b1, b2];
|
143 | })));
|
144 | }, []);
|
145 |
|
146 | this._mediaQueries = {
|
147 | at: this._createBreakpointQueries("at", this._sortedBreakpoints),
|
148 | lessThan: this._createBreakpointQueries("lessThan", this._sortedBreakpoints.slice(1)),
|
149 | greaterThan: this._createBreakpointQueries("greaterThan", this._sortedBreakpoints.slice(0, -1)),
|
150 | greaterThanOrEqual: this._createBreakpointQueries("greaterThanOrEqual", this._sortedBreakpoints),
|
151 | between: this._createBreakpointQueries("between", betweenCombinations)
|
152 | };
|
153 | }
|
154 |
|
155 | _createClass(Breakpoints, [{
|
156 | key: "toRuleSets",
|
157 | value: function toRuleSets() {
|
158 | return Object.entries(this._mediaQueries).reduce(function (acc, _ref3) {
|
159 | var _ref4 = _slicedToArray(_ref3, 2),
|
160 | type = _ref4[0],
|
161 | queries = _ref4[1];
|
162 |
|
163 | queries.forEach(function (query, breakpoint) {
|
164 |
|
165 |
|
166 | acc.push((0, _Utils.createRuleSet)((0, _Utils.createClassName)(type, breakpoint), "not all and ".concat(query)));
|
167 | });
|
168 | return acc;
|
169 | }, []);
|
170 | }
|
171 | }, {
|
172 | key: "shouldRenderMediaQuery",
|
173 | value: function shouldRenderMediaQuery(breakpointProps, onlyRenderAt) {
|
174 | var _this2 = this;
|
175 |
|
176 | breakpointProps = this._normalizeProps(breakpointProps);
|
177 |
|
178 | if (breakpointProps.lessThan) {
|
179 | var width = this._breakpoints[breakpointProps.lessThan];
|
180 | var lowestAllowedWidth = Math.min.apply(Math, _toConsumableArray(onlyRenderAt.map(function (breakpoint) {
|
181 | return _this2._breakpoints[breakpoint];
|
182 | })));
|
183 | return lowestAllowedWidth < width;
|
184 | } else if (breakpointProps.greaterThan) {
|
185 | var _width = this._breakpoints[this._findNextBreakpoint(breakpointProps.greaterThan)];
|
186 |
|
187 | var highestAllowedWidth = Math.max.apply(Math, _toConsumableArray(onlyRenderAt.map(function (breakpoint) {
|
188 | return _this2._breakpoints[breakpoint];
|
189 | })));
|
190 | return highestAllowedWidth >= _width;
|
191 | } else if (breakpointProps.greaterThanOrEqual) {
|
192 | var _width2 = this._breakpoints[breakpointProps.greaterThanOrEqual];
|
193 |
|
194 | var _highestAllowedWidth = Math.max.apply(Math, _toConsumableArray(onlyRenderAt.map(function (breakpoint) {
|
195 | return _this2._breakpoints[breakpoint];
|
196 | })));
|
197 |
|
198 | return _highestAllowedWidth >= _width2;
|
199 | } else if (breakpointProps.between) {
|
200 |
|
201 |
|
202 |
|
203 | var fromWidth = this._breakpoints[breakpointProps.between[0]];
|
204 | var toWidth = this._breakpoints[breakpointProps.between[1]];
|
205 | var allowedWidths = onlyRenderAt.map(function (breakpoint) {
|
206 | return _this2._breakpoints[breakpoint];
|
207 | });
|
208 | return !(Math.max.apply(Math, _toConsumableArray(allowedWidths)) < fromWidth || Math.min.apply(Math, _toConsumableArray(allowedWidths)) >= toWidth);
|
209 | }
|
210 |
|
211 | return false;
|
212 | }
|
213 | }, {
|
214 | key: "_normalizeProps",
|
215 | value: function _normalizeProps(breakpointProps) {
|
216 | if (breakpointProps.at) {
|
217 | var fromIndex = this._sortedBreakpoints.indexOf(breakpointProps.at);
|
218 |
|
219 | var to = this._sortedBreakpoints[fromIndex + 1];
|
220 | return to ? {
|
221 | between: [breakpointProps.at, to]
|
222 | } : {
|
223 | greaterThanOrEqual: breakpointProps.at
|
224 | };
|
225 | }
|
226 |
|
227 | return breakpointProps;
|
228 | }
|
229 | }, {
|
230 | key: "_createBreakpointQuery",
|
231 | value: function _createBreakpointQuery(breakpointProps) {
|
232 | breakpointProps = this._normalizeProps(breakpointProps);
|
233 |
|
234 | if (breakpointProps.lessThan) {
|
235 | var width = this._breakpoints[breakpointProps.lessThan];
|
236 | return "(max-width:".concat(width - 1, "px)");
|
237 | } else if (breakpointProps.greaterThan) {
|
238 | var _width3 = this._breakpoints[this._findNextBreakpoint(breakpointProps.greaterThan)];
|
239 |
|
240 | return "(min-width:".concat(_width3, "px)");
|
241 | } else if (breakpointProps.greaterThanOrEqual) {
|
242 | var _width4 = this._breakpoints[breakpointProps.greaterThanOrEqual];
|
243 | return "(min-width:".concat(_width4, "px)");
|
244 | } else if (breakpointProps.between) {
|
245 |
|
246 |
|
247 |
|
248 | var fromWidth = this._breakpoints[breakpointProps.between[0]];
|
249 | var toWidth = this._breakpoints[breakpointProps.between[1]];
|
250 | return "(min-width:".concat(fromWidth, "px) and (max-width:").concat(toWidth - 1, "px)");
|
251 | }
|
252 |
|
253 | throw new Error("Unexpected breakpoint props: ".concat(JSON.stringify(breakpointProps)));
|
254 | }
|
255 | }, {
|
256 | key: "_createBreakpointQueries",
|
257 | value: function _createBreakpointQueries(key, forBreakpoints) {
|
258 | var _this3 = this;
|
259 |
|
260 | return forBreakpoints.reduce(function (map, breakpoint) {
|
261 | map.set(breakpointKey(breakpoint), _this3._createBreakpointQuery(_defineProperty({}, key, breakpoint)));
|
262 | return map;
|
263 | }, new Map());
|
264 | }
|
265 | }, {
|
266 | key: "_findNextBreakpoint",
|
267 | value: function _findNextBreakpoint(breakpoint) {
|
268 | var nextBreakpoint = this._sortedBreakpoints[this._sortedBreakpoints.indexOf(breakpoint) + 1];
|
269 |
|
270 | if (!nextBreakpoint) {
|
271 | throw new Error("There is no breakpoint larger than ".concat(breakpoint));
|
272 | }
|
273 |
|
274 | return nextBreakpoint;
|
275 | }
|
276 | }, {
|
277 | key: "sortedBreakpoints",
|
278 | get: function get() {
|
279 | return this._sortedBreakpoints;
|
280 | }
|
281 | }, {
|
282 | key: "dynamicResponsiveMediaQueries",
|
283 | get: function get() {
|
284 | return Array.from(this._mediaQueries.at.entries()).reduce(function (acc, _ref5) {
|
285 | var _ref6 = _slicedToArray(_ref5, 2),
|
286 | k = _ref6[0],
|
287 | v = _ref6[1];
|
288 |
|
289 | return _objectSpread({}, acc, _defineProperty({}, k, v));
|
290 | }, {});
|
291 | }
|
292 | }, {
|
293 | key: "largestBreakpoint",
|
294 | get: function get() {
|
295 | return this._sortedBreakpoints[this._sortedBreakpoints.length - 1];
|
296 | }
|
297 | }]);
|
298 |
|
299 | return Breakpoints;
|
300 | }();
|
301 |
|
302 | exports.Breakpoints = Breakpoints;
|
303 |
|
\ | No newline at end of file |