Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymer 1.0 binding properties to inline styles in template

I would like to do something like this in polymer...

<dom-module id="logo-standard">
 <style>
:host {
  display: block;
}
</style>

<template>

<div class="logo-wrap">

  <div style="width: {{logoWidth}}px;">
    Some awesome logo
  </div>

</template>

<script>

(function() {

Polymer({
  is: 'logo-standard',

  properties: {

    logoWidth: {
      type: String,
      value: '400'
    }
  }

});
})();
</script>
</dom-module>

i.e. dynamically style an element using a property.

Is this possible? If so... how?

like image 314
markstewie Avatar asked Aug 17 '15 00:08

markstewie


3 Answers

This question has also been answered by me here

As of Polymer 1.2.0, you can now use Compound Bindings to

combine string literals and bindings in a single property binding or text content binding

like so:

<img src$="https://www.example.com/profiles/{{userId}}.jpg">

<span>Name: {{lastname}}, {{firstname}}</span>

and your example

<div style$="width: {{logoWidth}}px;">

so this is no longer an issue.

like image 142
Ryan White Avatar answered Oct 22 '22 22:10

Ryan White


In Polymer 1.0, you will need Computed Bindings to do this -

  <div style$="[[_computeWidth(logoWidth)]]">
    Some awesome logo
  </div>

  _computeWidth: function (width) {
      return 'width: ' + width + 'px' + ';color:red;';
  },

See this plunker for reference.

like image 2
Justin XL Avatar answered Oct 22 '22 21:10

Justin XL


In Polymer 1.0 they changed something that doesn't allow you to inline styles like that. You have to use a computed styles function and have it return the values you want.

 <div style$="{{computeWidth}}">



 computeWidth: function () {
   return 'width:' + this.logoWidth + 'px;'
 }

Here is another post about this subject

Polymer 1.0 - Binding css classes

edit: i forgot the $

like image 1
jimi dough10 Avatar answered Oct 22 '22 21:10

jimi dough10