UNPKG

2.6 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = between;
5var _getValueAndUnit5 = _interopRequireDefault(require("../helpers/getValueAndUnit"));
6var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
8/**
9 * Returns a CSS calc formula for linear interpolation of a property between two values. Accepts optional minScreen (defaults to '320px') and maxScreen (defaults to '1200px').
10 *
11 * @example
12 * // Styles as object usage
13 * const styles = {
14 * fontSize: between('20px', '100px', '400px', '1000px'),
15 * fontSize: between('20px', '100px')
16 * }
17 *
18 * // styled-components usage
19 * const div = styled.div`
20 * fontSize: ${between('20px', '100px', '400px', '1000px')};
21 * fontSize: ${between('20px', '100px')}
22 * `
23 *
24 * // CSS as JS Output
25 *
26 * h1: {
27 * 'fontSize': 'calc(-33.33333333333334px + 13.333333333333334vw)',
28 * 'fontSize': 'calc(-9.090909090909093px + 9.090909090909092vw)'
29 * }
30 */
31function between(fromSize, toSize, minScreen, maxScreen) {
32 if (minScreen === void 0) {
33 minScreen = '320px';
34 }
35 if (maxScreen === void 0) {
36 maxScreen = '1200px';
37 }
38 var _getValueAndUnit = (0, _getValueAndUnit5["default"])(fromSize),
39 unitlessFromSize = _getValueAndUnit[0],
40 fromSizeUnit = _getValueAndUnit[1];
41 var _getValueAndUnit2 = (0, _getValueAndUnit5["default"])(toSize),
42 unitlessToSize = _getValueAndUnit2[0],
43 toSizeUnit = _getValueAndUnit2[1];
44 var _getValueAndUnit3 = (0, _getValueAndUnit5["default"])(minScreen),
45 unitlessMinScreen = _getValueAndUnit3[0],
46 minScreenUnit = _getValueAndUnit3[1];
47 var _getValueAndUnit4 = (0, _getValueAndUnit5["default"])(maxScreen),
48 unitlessMaxScreen = _getValueAndUnit4[0],
49 maxScreenUnit = _getValueAndUnit4[1];
50 if (typeof unitlessMinScreen !== 'number' || typeof unitlessMaxScreen !== 'number' || !minScreenUnit || !maxScreenUnit || minScreenUnit !== maxScreenUnit) {
51 throw new _errors["default"](47);
52 }
53 if (typeof unitlessFromSize !== 'number' || typeof unitlessToSize !== 'number' || fromSizeUnit !== toSizeUnit) {
54 throw new _errors["default"](48);
55 }
56 if (fromSizeUnit !== minScreenUnit || toSizeUnit !== maxScreenUnit) {
57 throw new _errors["default"](76);
58 }
59 var slope = (unitlessFromSize - unitlessToSize) / (unitlessMinScreen - unitlessMaxScreen);
60 var base = unitlessToSize - slope * unitlessMaxScreen;
61 return "calc(" + base.toFixed(2) + (fromSizeUnit || '') + " + " + (100 * slope).toFixed(2) + "vw)";
62}
63module.exports = exports.default;
\No newline at end of file