UNPKG

2.26 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = retinaImage;
5var _hiDPI = _interopRequireDefault(require("./hiDPI"));
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); }
9/**
10 * A helper to generate a retina background image and non-retina
11 * background image. The retina background image will output to a HiDPI media query. The mixin uses
12 * a _2x.png filename suffix by default.
13 *
14 * @example
15 * // Styles as object usage
16 * const styles = {
17 * ...retinaImage('my-img')
18 * }
19 *
20 * // styled-components usage
21 * const div = styled.div`
22 * ${retinaImage('my-img')}
23 * `
24 *
25 * // CSS as JS Output
26 * div {
27 * backgroundImage: 'url(my-img.png)',
28 * '@media only screen and (-webkit-min-device-pixel-ratio: 1.3),
29 * only screen and (min--moz-device-pixel-ratio: 1.3),
30 * only screen and (-o-min-device-pixel-ratio: 1.3/1),
31 * only screen and (min-resolution: 144dpi),
32 * only screen and (min-resolution: 1.5dppx)': {
33 * backgroundImage: 'url(my-img_2x.png)',
34 * }
35 * }
36 */
37function retinaImage(filename, backgroundSize, extension, retinaFilename, retinaSuffix) {
38 var _ref;
39 if (extension === void 0) {
40 extension = 'png';
41 }
42 if (retinaSuffix === void 0) {
43 retinaSuffix = '_2x';
44 }
45 if (!filename) {
46 throw new _errors["default"](58);
47 }
48 // Replace the dot at the beginning of the passed extension if one exists
49 var ext = extension.replace(/^\./, '');
50 var rFilename = retinaFilename ? retinaFilename + "." + ext : "" + filename + retinaSuffix + "." + ext;
51 return _ref = {
52 backgroundImage: "url(" + filename + "." + ext + ")"
53 }, _ref[(0, _hiDPI["default"])()] = _extends({
54 backgroundImage: "url(" + rFilename + ")"
55 }, backgroundSize ? {
56 backgroundSize: backgroundSize
57 } : {}), _ref;
58}
59module.exports = exports.default;
\No newline at end of file