UNPKG

4.67 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.selectedStyle = exports.activeStyle = exports.backgroundStyle = exports.backgroundIsDark = exports.normalizeBackground = void 0;
5
6var _styledComponents = require("styled-components");
7
8var _colors = require("./colors");
9
10var _styles = require("./styles");
11
12var normalizeBackground = function normalizeBackground(background, theme) {
13 // If the background has a light or dark object, use that
14 var result = background;
15
16 if (background) {
17 if (theme.dark && background.dark && typeof background.dark !== 'boolean') {
18 result = background.dark;
19 } else if (!theme.dark && background.light && typeof background.light !== 'boolean') {
20 result = background.light;
21 }
22
23 result = (0, _styles.evalStyle)(result, theme);
24 }
25
26 return result;
27};
28
29exports.normalizeBackground = normalizeBackground;
30
31var backgroundIsDark = function backgroundIsDark(backgroundArg, theme) {
32 var background = normalizeBackground(backgroundArg, theme);
33 var result;
34
35 if (background) {
36 if (typeof background === 'object') {
37 var color = background.color,
38 dark = background.dark,
39 opacity = background.opacity;
40
41 if (typeof dark === 'boolean') {
42 result = dark;
43 } else if (color && ( // weak opacity means we keep the existing darkness
44 !opacity || opacity !== 'weak')) {
45 var backgroundColor = (0, _colors.normalizeColor)(background.color, theme);
46
47 if (backgroundColor) {
48 result = (0, _colors.colorIsDark)(backgroundColor);
49 }
50 }
51 } else {
52 var _color = (0, _colors.normalizeColor)(background, theme);
53
54 if (_color) {
55 result = (0, _colors.colorIsDark)(_color);
56 }
57 }
58 }
59
60 return result;
61};
62
63exports.backgroundIsDark = backgroundIsDark;
64
65var backgroundStyle = function backgroundStyle(backgroundArg, theme, textColorArg) {
66 // If the background has a light or dark object, use that
67 var background = normalizeBackground(backgroundArg, theme);
68 var textColor = textColorArg || theme.global.colors.text;
69
70 if (typeof background === 'object') {
71 var styles = [];
72
73 if (background.image) {
74 var color;
75
76 if (background.dark === false) {
77 color = textColor.light;
78 } else if (background.dark) {
79 color = textColor.dark;
80 } else if (!textColorArg) {
81 color = 'inherit';
82 }
83
84 styles.push((0, _styledComponents.css)(["background-image:", ";background-repeat:no-repeat;background-position:", ";background-size:cover;color:", ";"], background.image, background.position || 'center center', color));
85 }
86
87 if (background.color) {
88 var _color2 = (0, _colors.normalizeColor)(background.color, theme);
89
90 var backgroundColor = (0, _colors.getRGBA)(_color2, background.opacity === true ? theme.global.opacity.medium : theme.global.opacity[background.opacity] || background.opacity) || _color2;
91
92 styles.push((0, _styledComponents.css)(["background-color:", ";", ""], backgroundColor, (!background.opacity || background.opacity !== 'weak') && "color: " + textColor[background.dark || (0, _colors.colorIsDark)(backgroundColor) ? 'dark' : 'light'] + ";"));
93 }
94
95 if (background.dark === false) {
96 styles.push((0, _styledComponents.css)(["color:", ";"], textColor.light));
97 } else if (background.dark) {
98 styles.push((0, _styledComponents.css)(["color:", ";"], textColor.dark));
99 }
100
101 return styles;
102 }
103
104 if (background) {
105 if (background.lastIndexOf('url', 0) === 0) {
106 return (0, _styledComponents.css)(["background:", " no-repeat center center;background-size:cover;"], background);
107 }
108
109 var _color3 = (0, _colors.normalizeColor)(background, theme);
110
111 if (_color3) {
112 return (0, _styledComponents.css)(["background:", ";color:", ";"], _color3, textColor[(0, _colors.colorIsDark)(_color3) ? 'dark' : 'light']);
113 }
114 }
115
116 return undefined;
117};
118
119exports.backgroundStyle = backgroundStyle;
120var activeStyle = (0, _styledComponents.css)(["", " color:", ";"], function (props) {
121 return backgroundStyle((0, _colors.normalizeColor)(props.theme.global.hover.background, props.theme), props.theme);
122}, function (props) {
123 return (0, _colors.normalizeColor)(props.theme.global.hover.color, props.theme);
124});
125exports.activeStyle = activeStyle;
126var selectedStyle = (0, _styledComponents.css)(["", " color:", ";"], function (props) {
127 return backgroundStyle((0, _colors.normalizeColor)(props.theme.global.selected.background, props.theme), props.theme);
128}, function (props) {
129 return (0, _colors.normalizeColor)(props.theme.global.selected.color, props.theme);
130});
131exports.selectedStyle = selectedStyle;
\No newline at end of file