1 | /*
|
2 | * The MIT License (MIT)
|
3 | *
|
4 | * Copyright (c) 2015 - present Instructure, Inc.
|
5 | *
|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | * of this software and associated documentation files (the "Software"), to deal
|
8 | * in the Software without restriction, including without limitation the rights
|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | * copies of the Software, and to permit persons to whom the Software is
|
11 | * furnished to do so, subject to the following conditions:
|
12 | *
|
13 | * The above copyright notice and this permission notice shall be included in all
|
14 | * copies or substantial portions of the Software.
|
15 | *
|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22 | * SOFTWARE.
|
23 | */
|
24 | import { isEmpty } from '@instructure/ui-utils';
|
25 | import { formatVariableNames } from "./formatVariableNames.js";
|
26 | import { pickOverrides } from "./pickOverrides.js";
|
27 | /**
|
28 | * ---
|
29 | * category: utilities/themes
|
30 | * ---
|
31 | * Apply theme variables to a DOM node (as CSS variables, with polyfill)
|
32 | * @module applyVariablesToNode
|
33 | * @param {Element} domNode HTML element to apply variables to using the style attribute
|
34 | * @param {Object} variables JS variables
|
35 | * @param {Object} defaults Default JS variables
|
36 | * @param {String} prefix A variable prefix/namespace
|
37 | */
|
38 |
|
39 | function applyVariablesToNode(domNode, variables, defaults, prefix) {
|
40 | if (!domNode || isEmpty(variables)) {
|
41 | return;
|
42 | }
|
43 |
|
44 | clearCustomProperties(domNode, prefix);
|
45 | var overrides = pickOverrides(defaults, variables);
|
46 |
|
47 | if (overrides && !isEmpty(overrides)) {
|
48 | setCustomProperties(domNode, formatVariableNames(overrides, prefix));
|
49 | }
|
50 | }
|
51 |
|
52 | function clearCustomProperties(domNode, prefix) {
|
53 | var styles = domNode.style;
|
54 |
|
55 | for (var i = styles.length - 1; i >= 0; i--) {
|
56 | var prop = styles[i];
|
57 |
|
58 | if (prop.indexOf("--".concat(prefix, "-")) >= 0) {
|
59 | domNode.style.removeProperty(prop);
|
60 | }
|
61 | }
|
62 | }
|
63 |
|
64 | function setCustomProperties(domNode, properties) {
|
65 | Object.keys(properties).forEach(function (propertyName) {
|
66 | var value = properties[propertyName];
|
67 |
|
68 | if (value) {
|
69 | domNode.style.setProperty(propertyName, value);
|
70 | }
|
71 | });
|
72 | }
|
73 |
|
74 | export default applyVariablesToNode;
|
75 | export { applyVariablesToNode }; |
\ | No newline at end of file |