Skip to content

Commit c50fccf

Browse files
committed
chore: merge getColor
1 parent e1b5e41 commit c50fccf

File tree

6 files changed

+74
-36
lines changed

6 files changed

+74
-36
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## v2.0.19
2-
- feat: sidebar set active using query string
2+
- refactor: extract getCssCustomProperties function
3+
- feat: add getColor function
4+
- feat: sidebar set active using query string PR #21
35
- chore: update `node-sass` to `4.9.4`
46
- chore: update `eslint` to `5.7.0`
57
- chore: update `babel-plugin-istanbul` to `5.1.0`

js/src/utilities/get-color.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* --------------------------------------------------------------------------
3+
* CoreUI Utilities (v2.0.18): get-color.js
4+
* Licensed under MIT (https://coreui.io/license)
5+
* --------------------------------------------------------------------------
6+
*/
7+
import getCssCustomProperties from './get-css-custom-properties'
8+
9+
const minIEVersion = 10
10+
const isIE1x = () => Boolean(document.documentMode) && document.documentMode >= minIEVersion
11+
const isCustomProperty = (property) => property.match(/^--.*/i)
12+
13+
const getColor = (rawProperty, element = document.body) => {
14+
const property = `--${rawProperty}`
15+
let style
16+
if (isCustomProperty(property) && isIE1x()) {
17+
const cssCustomProperties = getCssCustomProperties()
18+
style = cssCustomProperties[property]
19+
} else {
20+
style = window.getComputedStyle(element, null).getPropertyValue(property).replace(/^\s/, '')
21+
}
22+
return style ? style : rawProperty
23+
}
24+
25+
export default getColor
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* --------------------------------------------------------------------------
3+
* CoreUI Utilities (v2.0.18): get-css-custom-properties.js
4+
* Licensed under MIT (https://coreui.io/license)
5+
* @returns {string} css custom property name
6+
* --------------------------------------------------------------------------
7+
*/
8+
const getCssCustomProperties = () => {
9+
const cssCustomProperties = {}
10+
const sheets = document.styleSheets
11+
let cssText = ''
12+
for (let i = sheets.length - 1; i > -1; i--) {
13+
const rules = sheets[i].cssRules
14+
for (let j = rules.length - 1; j > -1; j--) {
15+
if (rules[j].selectorText === '.ie-custom-properties') {
16+
cssText = rules[j].cssText
17+
break
18+
}
19+
}
20+
if (cssText) {
21+
break
22+
}
23+
}
24+
25+
cssText = cssText.substring(
26+
cssText.lastIndexOf('{') + 1,
27+
cssText.lastIndexOf('}')
28+
)
29+
30+
cssText.split(';').forEach((property) => {
31+
if (property) {
32+
const name = property.split(': ')[0]
33+
const value = property.split(': ')[1]
34+
if (name && value) {
35+
cssCustomProperties[`--${name.trim()}`] = value.trim()
36+
}
37+
}
38+
})
39+
return cssCustomProperties
40+
}
41+
42+
export default getCssCustomProperties

js/src/utilities/get-style.js

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,7 @@
44
* Licensed under MIT (https://coreui.io/license)
55
* --------------------------------------------------------------------------
66
*/
7-
8-
const getCssCustomProperties = () => {
9-
const cssCustomProperties = {}
10-
const sheets = document.styleSheets
11-
let cssText = ''
12-
for (let i = sheets.length - 1; i > -1; i--) {
13-
const rules = sheets[i].cssRules
14-
for (let j = rules.length - 1; j > -1; j--) {
15-
if (rules[j].selectorText === '.ie-custom-properties') {
16-
cssText = rules[j].cssText
17-
break
18-
}
19-
}
20-
if (cssText) {
21-
break
22-
}
23-
}
24-
25-
cssText = cssText.substring(
26-
cssText.lastIndexOf('{') + 1,
27-
cssText.lastIndexOf('}')
28-
)
29-
30-
cssText.split(';').forEach((property) => {
31-
if (property) {
32-
const name = property.split(': ')[0]
33-
const value = property.split(': ')[1]
34-
if (name && value) {
35-
cssCustomProperties[`--${name.trim()}`] = value.trim()
36-
}
37-
}
38-
})
39-
return cssCustomProperties
40-
}
7+
import getCssCustomProperties from './get-css-custom-properties'
418

429
const minIEVersion = 10
4310
const isIE1x = () => Boolean(document.documentMode) && document.documentMode >= minIEVersion

js/src/utilities/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import deepObjectsMerge from './deep-objects-merge'
2+
import getColor from './get-color'
23
import getStyle from './get-style'
34
import hexToRgb from './hex-to-rgb'
45
import hexToRgba from './hex-to-rgba'
56
import rgbToHex from './rgb-to-hex'
67

78
export {
89
deepObjectsMerge,
10+
getColor,
911
getStyle,
1012
hexToRgb,
1113
hexToRgba,

scss/coreui-standalone.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@
6262
@import "rtl";
6363

6464
// Custom Properties support for Internet Explorer
65-
@import "ie-custom-properties"
65+
@import "ie-custom-properties";

0 commit comments

Comments
 (0)