Search code examples
javascriptcssgoogle-chromepolymerpolymer-2.x

CSS not applied to Polymer 2 web component in Chrome


I have a Polymer v2 web component defined below. In the main page I include a CSS file which defines style named n-action-button. When I open the main page in FireFox or IE the style is applied to the web component, but when I do the same in Chrome the content of the web component is not styled.

Everything was working fine when the application was using Polymer v1 library. It has changed when I upgraded to Polymer v2. I've read in docs on the net that externally-defined styles should be applied to web components. I have no idea why it isn't working under Google Chrome browser.

<link rel="import" href="../polymer/polymer-element.html">
<dom-module id="login-form">
  <template>
        <h1>
            Use your username &amp; password to sign in.
        </h1>
        <form id="form" method="post" action="j_security_check">
            <input id="username" name="j_username" type="text" placeholder="username"/>
            <input type="submit" id="submit" value="Log In" class="n-action-button">
        </form>
  </template>
  <script>
    class LoginForm extends Polymer.Element {
      static get is() { return 'login-form'; }
    }
    window.customElements.define(LoginForm.is, LoginForm);
  </script>
</dom-module>

EDIT: The style looks like this:

.n-action-button,
.n-action-button:hover,
.n-action-button:focus,
.n-action-button:active,
.n-action-button:visited,
.n-action-button[disabled],
.z-button.n-action-button,
.z-button.n-action-button:hover,
.z-button.n-action-button:focus,
.z-button.n-action-button:active,
.z-button.n-action-button:visited,
.z-button.n-action-button[disabled] {
    display: inline-block;
    color: #fff;
    text-shadow: none;
    text-decoration: none;
    padding: 15px 30px;
    line-height: 22px;
    -webkit-border-radius: 6px;
    -moz-border-radius: 6px;
    border-radius: 6px;
    border: 0;
    -webkit-transition: color .25s, background .25s;
    -moz-transition: color .25s, background .25s;
    -o-transition: color .25s, background .25s;
    transition: color .25s, background .25s;
}

.n-action-button,
.n-action-button:visited,
.z-button.n-action-button,
.z-button.n-action-button:visited {
    background: #49b87b;
}

.n-action-button:hover,
.n-action-button:focus,
.n-action-button:active,
.z-button.n-action-button:hover,
.z-button.n-action-button:focus,
.z-button.n-action-button:active {
    color: #fff;
    background: #4bbe7f;
}

.n-action-button[disabled],
.z-button.n-action-button[disabled],
.z-button.n-action-button[disabled]:hover,
.z-button.n-action-button[disabled]:focus,
.z-button.n-action-button[disabled]:active {
    color: #fff;
    background: #b1b1b1;
}

Solution

  • Global styles should NOT influence element inside your shadow dom. I'm afraid but your approach only worked before because of the limitations of the polyfills. Now with polymer 2 you get true shadow dom and encapsulation.

    So for encapsulation to work you have to expose css mixins and globally set those.

    Example:

    <link rel="import" href="../polymer/polymer-element.html">
    <dom-module id="login-form">
        <template>
            <style>
                #submit {
                    background: #49b87b;
                    display: inline-block;
                    color: #fff;
                    text-shadow: none;
                    text-decoration: none;
                    padding: 15px 30px;
                    line-height: 22px;
                    -webkit-border-radius: 6px;
                    -moz-border-radius: 6px;
                    border-radius: 6px;
                    border: 0;
                    -webkit-transition: color .25s, background .25s;
                    -moz-transition: color .25s, background .25s;
                    -o-transition: color .25s, background .25s;
                    transition: color .25s, background .25s;
                    @apply --submit;
                }
                #submit:hover, #submit:focus, #submit:active {
                    color: #fff;
                    background: #4bbe7f;
                    @apply --submit-hover;
                }
                #submit[disabled], #submit[disabled]:hover, #submit[disabled]:focus, #submit[disabled]:active {
                    color: #fff;
                    background: #b1b1b1;
                    @apply --submit-disabled;
                }
            </style>
    
            <h1>
                    Use your username &amp; password to sign in.
            </h1>
            <form id="form" method="post" action="j_security_check">
                    <input id="username" name="j_username" type="text" placeholder="username"/>
                    <input type="submit" id="submit" value="Log In" class="n-action-button">
            </form>
        </template>
        <script>
            class LoginForm extends Polymer.Element {
                static get is() { return 'login-form'; }
            }
            window.customElements.define(LoginForm.is, LoginForm);
        </script>
    </dom-module>
    

    so then in your html you could just override certain values if you need them. styles.html

    <custom-style>
      <style is="custom-style">
        html {
          --submit: {
                    color: orange;
                };
          --submit-hover: {
                    color: orange;
                    background: grey;
                };
          --submit-disabled: {
                    color: green;
                    background: grey;
                };
        }
      </style>
    </custom-style>