github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/utils/properties/style-string.js (about)

     1  import { computed, get } from '@ember/object';
     2  import { htmlSafe } from '@ember/template';
     3  
     4  // An Ember.Computed property for transforming an object into an
     5  // html compatible style attribute
     6  //
     7  // ex. styleProps: { color: '#FF0', border-width: '1px' }
     8  //     styleStr: styleStringProperty('styleProps') // color:#FF0;border-width:1px
     9  export default function styleStringProperty(prop) {
    10    return computed(prop, function () {
    11      const styles = get(this, prop);
    12      let str = '';
    13  
    14      if (styles) {
    15        str = Object.keys(styles)
    16          .reduce(function (arr, key) {
    17            const val = styles[key];
    18            arr.push(
    19              key + ':' + (typeof val === 'number' ? val.toFixed(2) + 'px' : val)
    20            );
    21            return arr;
    22          }, [])
    23          .join(';');
    24      }
    25  
    26      return htmlSafe(str);
    27    });
    28  }