Paper-badge appears in wrong position when used with app-layout

249 views Asked by At

I'm using app-layout with app-drawer / app-drawer-layout and app-header / app-header-layout (see MWE below). The paper-badge is attached to an element inside the app-header. The first time the page is loaded, it briefly appears in the correct position (issue #12), than disappears off the right of the screen, exactly 256px too far to the right, which is the exact size of the app-drawer.

When the screen is resized, it recalculates the position to the correct place. I suspect that the position is calculated before the drawer is fully loaded.

This does not happen when the app-drawer is retracted and not visible.

I tried running updatePosition() at ready but that did not help.

Here is my MWE:

<template>
<style is="custom-style">
    app-drawer {
      --app-drawer-content-container: {
          background-color: green;
      };
    }
    app-header {
      background-color: yellow;
    }
</style>

  <app-drawer-layout fullbleed>

    <app-drawer slot="drawer">
        <div id="drawerbox">Navigation Drawer</div>
    </app-drawer>

    <div class="container">
      <app-header-layout>
        <app-header id="header" condenses reveals effects="waterfall" slot="header">
           <app-toolbar id="toolbarheader">
              <div main-title id="toolbartitlebox">Title Toolbar</div>
              <paper-icon-button id="discoursebutton" icon="communication:forum"></paper-icon-button>
              <paper-badge for="discoursebutton" label="20" title="20 results"></paper-badge>
           </app-toolbar>
        </app-header>
      </app-header-layout>
    </div>
  </app-drawer-layout>
</template>

One hack that works is to set a setTimeouton the updatePosition():

ready: function() {
  var self = this;
  setTimeout(function(){
    self.$$('paper-badge').updatePosition();
  }, 100);
}

But is there a more elegant way of solving this? Thanks for your help!

2

There are 2 answers

1
Ofisora On

One solution is to put paper-badge outside/after app-drawer-layout :

<template>
    <style>
      app-drawer {
        --app-drawer-content-container: {
          background-color: green;
        }
        ;
      }

      app-header {
        background-color: yellow;
      }

      paper-badge {
        margin-top: 10px;
      }
    </style>

    <app-drawer-layout fullbleed>

      <app-drawer id="drawer" slot="drawer">
        <div id="drawerbox">Navigation Drawer</div>
      </app-drawer>


      <app-header-layout>
        <app-header id="header" condenses reveals effects="waterfall" slot="header">
          <app-toolbar id="toolbarheader">
            <paper-icon-button id="toggle" on-tap="_onToggle" icon="menu"></paper-icon-button>

            <div main-title id="toolbartitlebox">Title Toolbar</div>
            <paper-icon-button id="discoursebutton" icon="inbox"></paper-icon-button>
            <!--  <paper-badge for="discoursebutton" label="20" title="20 results"></paper-badge>-->
          </app-toolbar>
        </app-header>
      </app-header-layout>

      <div class="container">
        container
      </div>
    </app-drawer-layout>

    <paper-badge for="discoursebutton" id="pd" label="20" title="20 results"></paper-badge>
</template>

Plnkr: http://plnkr.co/edit/V7zGBpqqzRonbAYyudea?p=preview

0
lukebjerring On

I avoided this same issue by putting the <paper-icon-button> and <paper-badge> in a container with position: relative, and stopped using the for attribute.

  <div style="position: relative">
    <paper-icon-button id="notification-icon" icon="social:notifications" onclick="[[showNotifications]]">
    </paper-icon-button>
    <template is="dom-if" if="[[messages.length]]">
      <paper-badge label="[[messages.length]]" class="red"></paper-badge>
    </template>
  </div>