Polymer 1.0 binding properties to inline styles in template
Asked Answered
D

4

9

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?

Dysplasia answered 17/8, 2015 at 0:48 Comment(0)
U
13

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.

Uproar answered 17/12, 2015 at 16:43 Comment(1)
I've been using 1.2 for a while but didn't know this... very helpful thanksDysplasia
S
2

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.

Squadron answered 17/8, 2015 at 1:9 Comment(0)
C
1

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 $

Concealment answered 17/8, 2015 at 1:9 Comment(0)
P
1

Yes, but you need to create computed binding for that:

<dom-module id="logo-standard">
    <style>
        :host {
            display : block;
        }
    </style>
    <template>
        <div class="logo-wrap">
            <div style$="[[logoStyle(logoWidth)]]">
                Some awesome logo
            </div>
    </template>
    <script>
        Polymer({
            is : 'logo-standard',

            properties : {

                logoWidth : {
                    type  : String,
                    value : '400'
                }
            },
            logoStyle  : function (logoWidth) {
                return 'width: ' + logoWidth + 'px';
            }
        });
    </script>
</dom-module>

It will be possible to do without computed bindings when https://github.com/Polymer/polymer/issues/2182 is resolved, which seems to be in progress now.

Parasynapsis answered 17/8, 2015 at 1:10 Comment(1)
Note that style$= will bind value to attribute, not just element propertyParasynapsis

© 2022 - 2024 — McMap. All rights reserved.