UNPKG

2.03 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = linearGradient;
5var _constructGradientValue = _interopRequireDefault(require("../internalHelpers/_constructGradientValue"));
6var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
7var _templateObject;
8function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9function _taggedTemplateLiteralLoose(strings, raw) { if (!raw) { raw = strings.slice(0); } strings.raw = raw; return strings; }
10/**
11 * CSS for declaring a linear gradient, including a fallback background-color. The fallback is either the first color-stop or an explicitly passed fallback color.
12 *
13 * @example
14 * // Styles as object usage
15 * const styles = {
16 * ...linearGradient({
17 colorStops: ['#00FFFF 0%', 'rgba(0, 0, 255, 0) 50%', '#0000FF 95%'],
18 toDirection: 'to top right',
19 fallback: '#FFF',
20 })
21 * }
22 *
23 * // styled-components usage
24 * const div = styled.div`
25 * ${linearGradient({
26 colorStops: ['#00FFFF 0%', 'rgba(0, 0, 255, 0) 50%', '#0000FF 95%'],
27 toDirection: 'to top right',
28 fallback: '#FFF',
29 })}
30 *`
31 *
32 * // CSS as JS Output
33 *
34 * div: {
35 * 'backgroundColor': '#FFF',
36 * 'backgroundImage': 'linear-gradient(to top right, #00FFFF 0%, rgba(0, 0, 255, 0) 50%, #0000FF 95%)',
37 * }
38 */
39function linearGradient(_ref) {
40 var colorStops = _ref.colorStops,
41 fallback = _ref.fallback,
42 _ref$toDirection = _ref.toDirection,
43 toDirection = _ref$toDirection === void 0 ? '' : _ref$toDirection;
44 if (!colorStops || colorStops.length < 2) {
45 throw new _errors["default"](56);
46 }
47 return {
48 backgroundColor: fallback || colorStops[0].replace(/,\s+/g, ',').split(' ')[0].replace(/,(?=\S)/g, ', '),
49 backgroundImage: (0, _constructGradientValue["default"])(_templateObject || (_templateObject = _taggedTemplateLiteralLoose(["linear-gradient(", "", ")"])), toDirection, colorStops.join(', ').replace(/,(?=\S)/g, ', '))
50 };
51}
52module.exports = exports.default;
\No newline at end of file