open-nomad/ui/app/utils/properties/style-string.js
2020-10-30 16:14:31 -07:00

27 lines
788 B
JavaScript

import { computed, get } from '@ember/object';
import { htmlSafe } from '@ember/template';
// An Ember.Computed property for transforming an object into an
// html compatible style attribute
//
// ex. styleProps: { color: '#FF0', border-width: '1px' }
// styleStr: styleStringProperty('styleProps') // color:#FF0;border-width:1px
export default function styleStringProperty(prop) {
return computed(prop, function() {
const styles = get(this, prop);
let str = '';
if (styles) {
str = Object.keys(styles)
.reduce(function(arr, key) {
const val = styles[key];
arr.push(key + ':' + (typeof val === 'number' ? val.toFixed(2) + 'px' : val));
return arr;
}, [])
.join(';');
}
return htmlSafe(str);
});
}