The idea behind web components is to make the web as declarative as possible. In this spirit, the Polymer way of implementing dynamic classes should be
Declarative approach: ( https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#native-binding )
... <dom-module id="poly-test"> ... <template> <div class$="{{computeClass(isBlue)}}"> <content></content> </div> </template> </dom-module> <script> Polymer({ is: 'poly-test', properties: { 'isBlue': { type: Boolean, value: false } }, listeners: { 'click': 'clickHandler' }, clickHandler: function () { this.isBlue = !this.isBlue; }, computeClass: function (f) { return f ? "blue" : "red"; } }); </script>
style-scope used by the framework when updating elements and stamping nodes in the DOM (in the shadow behavior that I think), and I donβt think we should touch it.
If you really want to handle it strongly , I recommend using the Polymer API toggleClass() method.
Imperative approach: ( http://polymer.imtqy.com/polymer/ )
... <dom-module id="poly-test"> ... <template> <div id="wrapper" class="red"><content></content></div> </template> </dom-module> <script> Polymer({ is: 'poly-test', properties: { 'isBlue': { type: Boolean, value: false } }, listeners: { 'click': 'clickHandler' }, clickHandler: function () { this.isBlue = !this.isBlue; this.toggleClass("blue", this.isBlue, this.$.wrapper); this.toggleClass("red", !this.isBlue, this.$.wrapper); } }); </script>
source share