Pocketninja
Pocketninja

Reputation: 415

Applying Polymer Property within elements Shadow Root styles

So setting styles dynamically is easy enough, the question I have is based on dynamic styles within a Media Query, so between max-width: 1000px I want the styling to be something based on a property or some calculated JS like the total width of a carousel by the amount of components.

Anyway here is a code snippet of something that doesn't work but shows how my thinking about how I HOPED the properties could be applied :-P

    <link rel="import" href="../../bower_components/polymer/polymer-element.html">

    <dom-module id="hello-eggs">
      <template>
        <style>
          :host {
            display: block;
            background: [[prop2]];
          }

          @media screen and (max-width: 1000px) {
            background: [[prop2]]
          }
        </style>
        <span>[[prop1]] are here</span>
      </template>

      <script>
        /**
        * @customElement
        * @polymer
        */
        class HelloEggs extends Polymer.Element {
          static get is() { return 'hello-eggs'; }
          static get properties() {
            return {
              prop1: {
                type: String,
                value: 'Hello Eggs'
              },
              prop2: {
                type: String,
                value: '#fc0'
              }
            };
          }
        }

        window.customElements.define(HelloEggs.is, HelloEggs);
      </script>
    </dom-module>

Thank you in advance

Upvotes: 0

Views: 204

Answers (1)

Pocketninja
Pocketninja

Reputation: 415

It's okay I figured out a way that makes me happy and hopefully helps other people like myself :-D

Basically I get the stylesheet and insert a rule for a new Media Query which lets me set what I want. I also changed the width to 500px

    <link rel="import" href="../../bower_components/polymer/polymer-element.html">

    <dom-module id="hello-eggs">
      <template>
        <style>
          :host {
            display: block;
            background: #eee;
            margin-bottom: 10px;
          }
        </style>
        <span>[[prop1]] are here</span>
      </template>

      <script>
        /**
        * @customElement
        * @polymer
        */
        class HelloEggs extends Polymer.Element {
          static get is() { return 'hello-eggs'; }
          static get properties() {
            return {
              prop1: {
                type: String,
                value: 'Hello Eggs'
              },
              prop2: {
                type: String,
                value: '#fc0'
              }
            };
          }

          connectedCallback() {
            super.connectedCallback();

            let sheet = this.shadowRoot.styleSheets[0];
            sheet.insertRule(`@media screen and (max-width: 500px) { span { background: ${this.prop2}; } }`, 1);
          }
        }

        window.customElements.define(HelloEggs.is, HelloEggs);
      </script>
    </dom-module>

I still think it would be great to be able to put properties in styles area for extreme cases, but this still gives me this if I have all the styles applied with insertRule which is great for widths and heights etc.

Upvotes: 1

Related Questions