UNPKG

2.92 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.alignProperty = alignProperty;
9exports.fontGrid = fontGrid;
10exports.responsiveProperty = responsiveProperty;
11
12var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
14function alignProperty(_ref) {
15 var size = _ref.size,
16 grid = _ref.grid;
17 var sizeBelow = size - size % grid;
18 var sizeAbove = sizeBelow + grid;
19 return size - sizeBelow < sizeAbove - size ? sizeBelow : sizeAbove;
20} // fontGrid finds a minimal grid (in rem) for the fontSize values so that the
21// lineHeight falls under a x pixels grid, 4px in the case of Material Design,
22// without changing the relative line height
23
24
25function fontGrid(_ref2) {
26 var lineHeight = _ref2.lineHeight,
27 pixels = _ref2.pixels,
28 htmlFontSize = _ref2.htmlFontSize;
29 return pixels / (lineHeight * htmlFontSize);
30}
31/**
32 * generate a responsive version of a given CSS property
33 * @example
34 * responsiveProperty({
35 * cssProperty: 'fontSize',
36 * min: 15,
37 * max: 20,
38 * unit: 'px',
39 * breakpoints: [300, 600],
40 * })
41 *
42 * // this returns
43 *
44 * {
45 * fontSize: '15px',
46 * '@media (min-width:300px)': {
47 * fontSize: '17.5px',
48 * },
49 * '@media (min-width:600px)': {
50 * fontSize: '20px',
51 * },
52 * }
53 *
54 * @param {Object} params
55 * @param {string} params.cssProperty - The CSS property to be made responsive
56 * @param {number} params.min - The smallest value of the CSS property
57 * @param {number} params.max - The largest value of the CSS property
58 * @param {string} [params.unit] - The unit to be used for the CSS property
59 * @param {Array.number} [params.breakpoints] - An array of breakpoints
60 * @param {number} [params.alignStep] - Round scaled value to fall under this grid
61 * @returns {Object} responsive styles for {params.cssProperty}
62 */
63
64
65function responsiveProperty(_ref3) {
66 var cssProperty = _ref3.cssProperty,
67 min = _ref3.min,
68 max = _ref3.max,
69 _ref3$unit = _ref3.unit,
70 unit = _ref3$unit === void 0 ? 'rem' : _ref3$unit,
71 _ref3$breakpoints = _ref3.breakpoints,
72 breakpoints = _ref3$breakpoints === void 0 ? [600, 960, 1280] : _ref3$breakpoints,
73 _ref3$transform = _ref3.transform,
74 transform = _ref3$transform === void 0 ? null : _ref3$transform;
75 var output = (0, _defineProperty2.default)({}, cssProperty, "".concat(min).concat(unit));
76 var factor = (max - min) / breakpoints[breakpoints.length - 1];
77 breakpoints.forEach(function (breakpoint) {
78 var value = min + factor * breakpoint;
79
80 if (transform !== null) {
81 value = transform(value);
82 }
83
84 output["@media (min-width:".concat(breakpoint, "px)")] = (0, _defineProperty2.default)({}, cssProperty, "".concat(Math.round(value * 10000) / 10000).concat(unit));
85 });
86 return output;
87}
\No newline at end of file