Skip to content

Commit ff12271

Browse files
committed
refactor(getStyle): polyfill IE custom properties
1 parent 881435d commit ff12271

File tree

3 files changed

+22
-34
lines changed

3 files changed

+22
-34
lines changed

js/src/utilities/get-style.js

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,17 @@
55
* --------------------------------------------------------------------------
66
*/
77

8-
/* eslint-disable */
98
const getCssCustomProperties = () => {
109
const cssCustomProperties = {}
11-
const stylesheets = new Map(Object.entries(document.styleSheets))
12-
stylesheets.forEach((stylesheet) => {
13-
console.log(property)
14-
const rules = new Map(Object.entries(stylesheet.cssRules))
15-
rules.forEach((rule) => {
16-
if (rule.selectorText === '.ie-custom-properties') {
17-
rule.style.cssText.split('; ').forEach((property) => {
18-
console.log(property)
19-
const name = property.split(': ')[0]
20-
const value = property.split(': ')[1]
21-
cssCustomProperties[`--${name}`] = value
22-
})
23-
}
24-
})
10+
const root = Object.entries(document.styleSheets).filter((value) => value[1].cssText.substring(0, ':root'.length) === ':root')
11+
const rule = Object.entries(root[0][1].cssRules).filter((value) => value[1].selectorText === '.ie-custom-properties')
12+
const cssText = rule[0][1].style.cssText
13+
cssText.split(';').forEach((property) => {
14+
if (property) {
15+
const name = property.split(': ')[0]
16+
const value = property.split(': ')[1]
17+
cssCustomProperties[`--${name.trim()}`] = value.trim()
18+
}
2519
})
2620
return cssCustomProperties
2721
}
@@ -34,28 +28,22 @@ const getStyle = (property, element = document.body) => {
3428
if (isCustomProperty(property) && isIE11()) {
3529
const cssCustomProperties = getCssCustomProperties()
3630
style = cssCustomProperties[property]
37-
// console.log(cssCustomProperties)
3831
} else {
3932
style = window.getComputedStyle(element, null).getPropertyValue(property).replace(/^\s/, '')
4033
}
41-
// eslint-disable-next-line no-console
42-
// console.log(isIE11())
43-
// eslint-disable-next-line no-console
44-
// console.log(property)
45-
// eslint-disable-next-line no-console
46-
// console.log(isCustomProperty(property))
4734
return style
4835
}
4936

50-
if (!Object.entries)
51-
Object.entries = function(obj){
52-
var ownProps = Object.keys(obj),
53-
i = ownProps.length,
54-
resArray = new Array(i); // preallocate the Array
55-
while (i--)
56-
resArray[i] = [ownProps[i], obj[ownProps[i]]]
57-
58-
return resArray
59-
};
37+
if (!Object.entries) {
38+
Object.entries = function (obj) {
39+
const ownProps = Object.keys(obj)
40+
let i = ownProps.length
41+
const resArray = new Array(i)
42+
while (i--) {
43+
resArray[i] = [ownProps[i], obj[ownProps[i]]]
44+
}
45+
return resArray
46+
}
47+
}
6048

6149
export default getStyle

js/src/utilities/hex-to-rgb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const hexToRgb = (color) => {
2727
b = parseInt(color.substring(3, 5), 16)
2828
}
2929

30-
return `rgba(${r}, ${g}, ${b}`
30+
return `rgba(${r}, ${g}, ${b})`
3131
}
3232

3333
export default hexToRgb

js/src/utilities/hex-to-rgba.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const hexToRgba = (color, opacity = 100) => {
2727
b = parseInt(color.substring(3, 5), 16)
2828
}
2929

30-
return `rgba(${r}, ${g}, ${b}, ${opacity / 100}`
30+
return `rgba(${r}, ${g}, ${b}, ${opacity / 100})`
3131
}
3232

3333
export default hexToRgba

0 commit comments

Comments
 (0)