UNPKG

4.63 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.isUnitless = isUnitless;
9exports.getUnit = getUnit;
10exports.toUnitless = toUnitless;
11exports.convertLength = convertLength;
12exports.alignProperty = alignProperty;
13exports.fontGrid = fontGrid;
14exports.responsiveProperty = responsiveProperty;
15
16var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
17
18function isUnitless(value) {
19 return String(parseFloat(value)).length === String(value).length;
20} // Ported from Compass
21// https://github.com/Compass/compass/blob/master/core/stylesheets/compass/typography/_units.scss
22// Emulate the sass function "unit"
23
24
25function getUnit(input) {
26 return String(input).match(/[\d.\-+]*\s*(.*)/)[1] || '';
27} // Emulate the sass function "unitless"
28
29
30function toUnitless(length) {
31 return parseFloat(length);
32} // Convert any CSS <length> or <percentage> value to any another.
33// From https://github.com/KyleAMathews/convert-css-length
34
35
36function convertLength(baseFontSize) {
37 return function (length, toUnit) {
38 var fromUnit = getUnit(length); // Optimize for cases where `from` and `to` units are accidentally the same.
39
40 if (fromUnit === toUnit) {
41 return length;
42 } // Convert input length to pixels.
43
44
45 var pxLength = toUnitless(length);
46
47 if (fromUnit !== 'px') {
48 if (fromUnit === 'em') {
49 pxLength = toUnitless(length) * toUnitless(baseFontSize);
50 } else if (fromUnit === 'rem') {
51 pxLength = toUnitless(length) * toUnitless(baseFontSize);
52 return length;
53 }
54 } // Convert length in pixels to the output unit
55
56
57 var outputLength = pxLength;
58
59 if (toUnit !== 'px') {
60 if (toUnit === 'em') {
61 outputLength = pxLength / toUnitless(baseFontSize);
62 } else if (toUnit === 'rem') {
63 outputLength = pxLength / toUnitless(baseFontSize);
64 } else {
65 return length;
66 }
67 }
68
69 return parseFloat(outputLength.toFixed(5)) + toUnit;
70 };
71}
72
73function alignProperty(_ref) {
74 var size = _ref.size,
75 grid = _ref.grid;
76 var sizeBelow = size - size % grid;
77 var sizeAbove = sizeBelow + grid;
78 return size - sizeBelow < sizeAbove - size ? sizeBelow : sizeAbove;
79} // fontGrid finds a minimal grid (in rem) for the fontSize values so that the
80// lineHeight falls under a x pixels grid, 4px in the case of Material Design,
81// without changing the relative line height
82
83
84function fontGrid(_ref2) {
85 var lineHeight = _ref2.lineHeight,
86 pixels = _ref2.pixels,
87 htmlFontSize = _ref2.htmlFontSize;
88 return pixels / (lineHeight * htmlFontSize);
89}
90/**
91 * generate a responsive version of a given CSS property
92 * @example
93 * responsiveProperty({
94 * cssProperty: 'fontSize',
95 * min: 15,
96 * max: 20,
97 * unit: 'px',
98 * breakpoints: [300, 600],
99 * })
100 *
101 * // this returns
102 *
103 * {
104 * fontSize: '15px',
105 * '@media (min-width:300px)': {
106 * fontSize: '17.5px',
107 * },
108 * '@media (min-width:600px)': {
109 * fontSize: '20px',
110 * },
111 * }
112 *
113 * @param {Object} params
114 * @param {string} params.cssProperty - The CSS property to be made responsive
115 * @param {number} params.min - The smallest value of the CSS property
116 * @param {number} params.max - The largest value of the CSS property
117 * @param {string} [params.unit] - The unit to be used for the CSS property
118 * @param {Array.number} [params.breakpoints] - An array of breakpoints
119 * @param {number} [params.alignStep] - Round scaled value to fall under this grid
120 * @returns {Object} responsive styles for {params.cssProperty}
121 */
122
123
124function responsiveProperty(_ref3) {
125 var cssProperty = _ref3.cssProperty,
126 min = _ref3.min,
127 max = _ref3.max,
128 _ref3$unit = _ref3.unit,
129 unit = _ref3$unit === void 0 ? 'rem' : _ref3$unit,
130 _ref3$breakpoints = _ref3.breakpoints,
131 breakpoints = _ref3$breakpoints === void 0 ? [600, 960, 1280] : _ref3$breakpoints,
132 _ref3$transform = _ref3.transform,
133 transform = _ref3$transform === void 0 ? null : _ref3$transform;
134 var output = (0, _defineProperty2.default)({}, cssProperty, "".concat(min).concat(unit));
135 var factor = (max - min) / breakpoints[breakpoints.length - 1];
136 breakpoints.forEach(function (breakpoint) {
137 var value = min + factor * breakpoint;
138
139 if (transform !== null) {
140 value = transform(value);
141 }
142
143 output["@media (min-width:".concat(breakpoint, "px)")] = (0, _defineProperty2.default)({}, cssProperty, "".concat(Math.round(value * 10000) / 10000).concat(unit));
144 });
145 return output;
146}
\No newline at end of file