UNPKG

4.57 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = fluidRange;
5var _between = _interopRequireDefault(require("./between"));
6var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
8function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
9function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
10function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
11function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
12/**
13 * Returns a set of media queries that resizes a property (or set of properties) between a provided fromSize and toSize. Accepts optional minScreen (defaults to '320px') and maxScreen (defaults to '1200px') to constrain the interpolation.
14 *
15 * @example
16 * // Styles as object usage
17 * const styles = {
18 * ...fluidRange(
19 * {
20 * prop: 'padding',
21 * fromSize: '20px',
22 * toSize: '100px',
23 * },
24 * '400px',
25 * '1000px',
26 * )
27 * }
28 *
29 * // styled-components usage
30 * const div = styled.div`
31 * ${fluidRange(
32 * {
33 * prop: 'padding',
34 * fromSize: '20px',
35 * toSize: '100px',
36 * },
37 * '400px',
38 * '1000px',
39 * )}
40 * `
41 *
42 * // CSS as JS Output
43 *
44 * div: {
45 * "@media (min-width: 1000px)": Object {
46 * "padding": "100px",
47 * },
48 * "@media (min-width: 400px)": Object {
49 * "padding": "calc(-33.33333333333334px + 13.333333333333334vw)",
50 * },
51 * "padding": "20px",
52 * }
53 */
54function fluidRange(cssProp, minScreen, maxScreen) {
55 if (minScreen === void 0) {
56 minScreen = '320px';
57 }
58 if (maxScreen === void 0) {
59 maxScreen = '1200px';
60 }
61 if (!Array.isArray(cssProp) && typeof cssProp !== 'object' || cssProp === null) {
62 throw new _errors["default"](49);
63 }
64 if (Array.isArray(cssProp)) {
65 var mediaQueries = {};
66 var fallbacks = {};
67 for (var _iterator = _createForOfIteratorHelperLoose(cssProp), _step; !(_step = _iterator()).done;) {
68 var _extends2, _extends3;
69 var obj = _step.value;
70 if (!obj.prop || !obj.fromSize || !obj.toSize) {
71 throw new _errors["default"](50);
72 }
73 fallbacks[obj.prop] = obj.fromSize;
74 mediaQueries["@media (min-width: " + minScreen + ")"] = _extends({}, mediaQueries["@media (min-width: " + minScreen + ")"], (_extends2 = {}, _extends2[obj.prop] = (0, _between["default"])(obj.fromSize, obj.toSize, minScreen, maxScreen), _extends2));
75 mediaQueries["@media (min-width: " + maxScreen + ")"] = _extends({}, mediaQueries["@media (min-width: " + maxScreen + ")"], (_extends3 = {}, _extends3[obj.prop] = obj.toSize, _extends3));
76 }
77 return _extends({}, fallbacks, mediaQueries);
78 } else {
79 var _ref, _ref2, _ref3;
80 if (!cssProp.prop || !cssProp.fromSize || !cssProp.toSize) {
81 throw new _errors["default"](51);
82 }
83 return _ref3 = {}, _ref3[cssProp.prop] = cssProp.fromSize, _ref3["@media (min-width: " + minScreen + ")"] = (_ref = {}, _ref[cssProp.prop] = (0, _between["default"])(cssProp.fromSize, cssProp.toSize, minScreen, maxScreen), _ref), _ref3["@media (min-width: " + maxScreen + ")"] = (_ref2 = {}, _ref2[cssProp.prop] = cssProp.toSize, _ref2), _ref3;
84 }
85}
86module.exports = exports.default;
\No newline at end of file