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

     1  import { htmlSafe } from '@ember/template';
     2  
     3  // A decorator for transforming an object into an html
     4  // compatible style attribute string.
     5  //
     6  // ex. @styleString
     7  //     get styleStr() {
     8  //       return { color: '#FF0', 'border-width': '1px' }
     9  //     }
    10  export default function styleString(target, name, descriptor) {
    11    if (!descriptor.get) throw new Error('styleString only works on getters');
    12    const orig = descriptor.get;
    13    descriptor.get = function () {
    14      const styles = orig.apply(this);
    15  
    16      let str = '';
    17  
    18      if (styles) {
    19        str = Object.keys(styles)
    20          .reduce(function (arr, key) {
    21            const val = styles[key];
    22            arr.push(
    23              key + ':' + (typeof val === 'number' ? val.toFixed(2) + 'px' : val)
    24            );
    25            return arr;
    26          }, [])
    27          .join(';');
    28      }
    29  
    30      return htmlSafe(str);
    31    };
    32    return descriptor;
    33  }