Skip to content

Commit

Permalink
Forcing use of setProperty for CSS variables
Browse files Browse the repository at this point in the history
  • Loading branch information
zero298 committed Jul 16, 2018
1 parent e0b07f6 commit 66be580
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/binding/defaultBindings/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,30 @@ ko.bindingHandlers['style'] = {
if (jQueryInstance) {
jQueryInstance(element)['css'](styleName, styleValue);
} else {
styleName = styleName.replace(/-(\w)/g, function (all, letter) {
return letter.toUpperCase();
});
var previousStyle;
var postStyle;
var requiresGetPropertyValue = (styleName.substring(0, 2) === "--");

var previousStyle = element.style[styleName];
element.style[styleName] = styleValue;
if(requiresGetPropertyValue){
previousStyle = element.style.getPropertyValue(styleName);
element.style.setProperty(styleName, styleValue);
postStyle = element.style.getPropertyValue(styleName);

if (styleValue !== previousStyle && element.style[styleName] == previousStyle && !isNaN(styleValue)) {
element.style[styleName] = styleValue + "px";
if (styleValue !== previousStyle && postStyle == previousStyle && !isNaN(styleValue)) {
element.style.setProperty(styleName, styleValue + "px");
}
} else {
styleName = styleName.replace(/-(\w)/g, function (all, letter) {
return letter.toUpperCase();
});

previousStyle = element.style[styleName];
element.style[styleName] = styleValue;
postStyle = element.style[styleName];

if (styleValue !== previousStyle && postStyle == previousStyle && !isNaN(styleValue)) {
element.style[styleName] = styleValue + "px";
}
}
}
});
Expand Down

0 comments on commit 66be580

Please sign in to comment.