dialogpolymeropacitypaper-dialog

Polymer paper-dialog backdrop opacity?


I have a paper-dialog in my Polymer element. I want to make the backdrop opaque, right now it is semi-transparent. I would also like to change the color.

Does anyone know how to do it. I have already tried this css in my custom element:

<style is="custom-style">
--iron-overlay-backdrop-opacity:1;
</style>

<paper-dialog modal></paper-dialog>

But it had no effect.

I also tried

<style is="custom-style">
:host {
  --iron-overlay-backdrop-opacity:1;
}
</style>

<paper-dialog modal></paper-dialog>

Solution

  • The iron-overlay-backdrop is appended directly to the document body, outside of your custom element, and due to Polymer's CSS encapsulation, you can't style the backdrop with a <style> from within the element.

    However, your element could imperatively style the backdrop with Polymer.updateStyles(...), which updates the styles for all custom elements, including the iron-overlay-backdrop outside your element:

    Polymer.updateStyles({
      '--iron-overlay-backdrop-background-color': 'red',
      '--iron-overlay-backdrop-opacity': '1'
    });
    

    <head>
      <base href="https://polygit.org/polymer+1.11.1/components/">
      <script src="webcomponentsjs/webcomponents-lite.js"></script>
      <link rel="import" href="polymer/polymer.html">
      <link rel="import" href="paper-button/paper-button.html">
      <link rel="import" href="paper-dialog/paper-dialog.html">
    </head>
    <body>
    
      <div>Click button for dialog</div>
      <x-demo></x-demo>
    
      <dom-module id="x-demo">
        <template>
          <x-dialog id="blue" backdrop-color="blue" backdrop-opacity="1"></x-dialog>
          <x-dialog id="red" backdrop-color="red" backdrop-opacity="0.2"></x-dialog>
          <x-dialog id="green" backdrop-color="green" backdrop-opacity="0.5"></x-dialog>
          <paper-button on-tap="_openDialog" data-dialog="blue">Blue (opacity 100%)</paper-button>
          <paper-button on-tap="_openDialog" data-dialog="red">Red (opacity 20%)</paper-button>
          <paper-button on-tap="_openDialog" data-dialog="green">Green (opacity 50%)</paper-button>
        </template>
        <script>
          HTMLImports.whenReady(function() {
            Polymer({
              is: 'x-demo',
              _openDialog: function(e) {
                var dialog = e.target.dataset.dialog;
                this.$[dialog].opened = true;
              }
            })
          });
        </script>
      </dom-module>
    
      <dom-module id="x-dialog">
        <template>
          <paper-dialog modal with-backdrop opened="{{opened}}">
            <div class="buttons">
              <paper-button dialog-dismiss>Close</paper-button>
            </div>
          </paper-dialog>
        </template>
        <script>
          HTMLImports.whenReady(function() {
            Polymer({
              is: 'x-dialog',
              properties: {
                backdropColor: {
                  type: String,
                  value: 'green'
                },
                backdropOpacity: {
                  type: String,
                  value: '0.6'
                },
                opened: {
                  type: Boolean,
                  value: false
                }
              },
    
              observers: ['_updateBackdrop(opened, backdropColor, backdropOpacity)'],
    
              _updateBackdrop: function(opened, color, opacity) {
                if (opened && color && opacity) {
                  Polymer.RenderStatus.afterNextRender(this, function() {
                  	this._setBackdropStyles(color, opacity);
                  });
                }
              },
              _setBackdropStyles: function(color, opacity) {
                Polymer.updateStyles({
                  '--iron-overlay-backdrop-background-color': color,
                  '--iron-overlay-backdrop-opacity': opacity
                });
              }
            });
          });
        </script>
      </dom-module>
    </body>

    codepen

    Or to set a fixed static style for all backdrops, use <style is="custom-style"> from the body:

    <body>
      <style is="custom-style">
        iron-overlay-backdrop {
          --iron-overlay-backdrop-opacity: 1;
          --iron-overlay-backdrop-background-color: green;
        }
      </style>
      <x-dialog></x-dialog>
    </body>
    

    <head>
      <base href="https://polygit.org/polymer+1.11.1/components/">
      <script src="webcomponentsjs/webcomponents-lite.js"></script>
      <link rel="import" href="polymer/polymer.html">
      <link rel="import" href="paper-button/paper-button.html">
      <link rel="import" href="paper-dialog/paper-dialog.html">
    </head>
    <body>
      <style is="custom-style">
        iron-overlay-backdrop {
          --iron-overlay-backdrop-opacity: 1;
          --iron-overlay-backdrop-background-color: green;
        }
      </style>
      <div>Hello world</div>
      <x-dialog></x-dialog>
    
      <dom-module id="x-dialog">
        <template>
          <paper-dialog modal with-backdrop opened>
            <div class="buttons">
              <paper-button dialog-dismiss>Close</paper-button>
            </div>
          </paper-dialog>
        </template>
        <script>
          HTMLImports.whenReady(function() {
            Polymer({ is: 'x-dialog' });
          });
        </script>
      </dom-module>
    </body>

    codepen