UNPKG

3.29 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getCssText = getCssText;
7exports.default = void 0;
8
9var _replaceValuesWithVariableNames = require("./replaceValuesWithVariableNames.js");
10
11var _formatVariableNames = require("./formatVariableNames.js");
12
13var _applyCustomMediaToCss = require("./applyCustomMediaToCss.js");
14
15/*
16 * The MIT License (MIT)
17 *
18 * Copyright (c) 2015 - present Instructure, Inc.
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining a copy
21 * of this software and associated documentation files (the "Software"), to deal
22 * in the Software without restriction, including without limitation the rights
23 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24 * copies of the Software, and to permit persons to whom the Software is
25 * furnished to do so, subject to the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be included in all
28 * copies or substantial portions of the Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * SOFTWARE.
37 */
38
39/**
40 * ---
41 * category: utilities/themes
42 * ---
43 * Returns the CSS as a string with variables applied
44 * @module getCssText
45 * @param {Function} template A template function that returns the CSS as a string with variables injected
46 * @param {Object} variables JS variables
47 * @param {string} prefix CSS variable prefix/namespace
48 * @returns {String} css text
49 */
50function getCssText(template, variables, prefix) {
51 var variableNames = variables ? (0, _replaceValuesWithVariableNames.replaceValuesWithVariableNames)(variables, prefix) : {}; // inject the CSS variable names into the style template
52
53 var cssText = template(variableNames); // inject values for @custom-media rules (https://www.w3.org/TR/2016/WD-mediaqueries-4-20160126/#custom-mq)
54
55 var customMedia = variables ? function () {
56 return (0, _formatVariableNames.formatVariableNames)(variables);
57 } : {};
58 cssText = (0, _applyCustomMediaToCss.applyCustomMediaToCss)(cssText, customMedia);
59 var cssVariablesString = variables ? (0, _formatVariableNames.formatVariableNames)(variables, prefix) : ''; // append the CSS variables (defaults) to the result
60
61 cssText = [cssText, variablesToCSSText(cssVariablesString)].join('\n');
62 return cssText;
63}
64
65function variablesToCSSText() {
66 var variables = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
67 var rules = [];
68
69 for (var key in variables) {
70 if (Object.prototype.hasOwnProperty.call(variables, key) && typeof variables[key] !== 'undefined') {
71 rules.push("".concat(key, ": ").concat(variables[key]));
72 }
73 }
74
75 if (rules.length > 0) {
76 return "\n :root {\n ".concat(rules.join(';\n'), ";\n }\n ");
77 } else {
78 return '';
79 }
80}
81
82var _default = getCssText;
83exports.default = _default;
\No newline at end of file