Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make 'dropdown-content' same width as paper-dropdown-menu?

I have the following piece of code:

      <paper-dropdown-menu id="mydropdown" label="City?">
        <paper-listbox class="dropdown-content">
          <paper-item>Inbox</paper-item>
          <paper-item>Starred</paper-item>
          <paper-item>Sent mail</paper-item>
          <paper-item>Drafts</paper-item>
        </paper-listbox>
      </paper-dropdown-menu>

But as seen in the image below, the dropdown-content will have very small width. How can I in a clean way set the width to the same size as the actual paper-dropdown-menu?

enter image description here

like image 260
Whyser Avatar asked Nov 04 '15 22:11

Whyser


3 Answers

I believe I have created what you are looking for in this jsfiddle.

or if you prefer here is the code snippet

.custom {
  width: 190px;
}
.custom2 {
  width: 400px;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <base href="//polygit.org/components/">

  <link rel="import" href="polymer/polymer.html">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>

  <link rel="import" href="paper-dropdown-menu/paper-dropdown-menu.html">
  <link rel="import" href="paper-item/paper-item.html">
  <link rel="import" href="paper-listbox/paper-listbox.html">

</head>

<body unresolved>
  <dom-module id='base-page'>
    <template>
      <paper-dropdown-menu id="mydropdown" class="custom" label="City?">
        <paper-listbox class="dropdown-content custom" horizontalAlign='left'>
          <paper-item>Inbox</paper-item>
          <paper-item>Starred</paper-item>
          <paper-item>Sent mail</paper-item>
          <paper-item>Drafts</paper-item>
        </paper-listbox>
      </paper-dropdown-menu>
      <br>
      <paper-dropdown-menu id="mydropdown" class="custom2" label="City?">
        <paper-listbox class="dropdown-content custom2" horizontalAlign='left'>
          <paper-item>Inbox</paper-item>
          <paper-item>Starred</paper-item>
          <paper-item>Sent mail</paper-item>
          <paper-item>Drafts</paper-item>
        </paper-listbox>
      </paper-dropdown-menu>
    </template>
  </dom-module>
  <script>
    HTMLImports.whenReady(function() {
      Polymer({
        is: 'base-page',
        properties: {}
      });
    });
  </script>
  <base-page></base-page>
</body>

</html>

All I did was created a custom class that was applied both to the dropdown-menu and the listbox that gave them each the same width. I believe 190px is the default, but once you have the class you can size it how you want it.

like image 115
Jarod Moser Avatar answered Oct 26 '22 03:10

Jarod Moser


In this u can adjust the width according to you iron-dropdown give the left 0 and width:100%

    .custom {
      width: 100%;
    }

    .custom2 {
      width: 100%;
    }
    iron-dropdown{
      left:0 !important;
      width:100% !important;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<!DOCTYPE html>
    <html>

    <head>
      <meta charset="utf-8">
      <title>JS Bin</title>
      <base href="//polygit.org/components/">

      <link rel="import" href="polymer/polymer.html">
      <script src="webcomponentsjs/webcomponents-lite.js"></script>

      <link rel="import" href="paper-dropdown-menu/paper-dropdown-menu.html">
      <link rel="import" href="paper-item/paper-item.html">
      <link rel="import" href="paper-listbox/paper-listbox.html">

    </head>

    <body unresolved>
      <dom-module id='base-page'>
        <template>
          <paper-dropdown-menu id="mydropdown" class="custom" label="City?">
            <paper-listbox class="dropdown-content custom" horizontalAlign='left'>
              <paper-item>Inbox</paper-item>
              <paper-item>Starred</paper-item>
              <paper-item>Sent mail</paper-item>
              <paper-item>Drafts</paper-item>
            </paper-listbox>
          </paper-dropdown-menu>
          <br>
          <paper-dropdown-menu id="mydropdown" class="custom2" label="City?">
            <paper-listbox class="dropdown-content custom2" horizontalAlign='left'>
              <paper-item>Inbox</paper-item>
              <paper-item>Starred</paper-item>
              <paper-item>Sent mail</paper-item>
              <paper-item>Drafts</paper-item>
            </paper-listbox>
          </paper-dropdown-menu>
        </template>
      </dom-module>
      <script>
        HTMLImports.whenReady(function() {
          Polymer({
            is: 'base-page',
            properties: {}
          });
        });
      </script>
      <base-page></base-page>
    </body>

    </html>
like image 3
aditya shrivastava Avatar answered Oct 26 '22 03:10

aditya shrivastava


Actually i believe the two answers by @aditya shrivastava and @jarod moser are not working if you want it the "pure" polymer style.

Here my reasoning:

  • you use CSS that's not getting shimmed by Polymer
  • Styles are bleeding through the DOM Tree, which is not intended by Polymer

So my solution here:

  • set the width of input as proposed in the other two answers
  • set the with of dropdown with the help of the CSS Mixin:
.custom2 {
  --paper-dropdown-menu-button: {
     left: 0;
     width: 100%;
  };
}

The Mixin gets applied to the dropdown box - an iron-dropdown actually - when the CSS is getting rewritten. See the documentation at: https://www.polymer-project.org/1.0/docs/devguide/styling#custom-css-mixins

Then your code should follow this basic setup:

<dom-module id="some-kind-of-element">
  <style>
    #mydropdown {
      --paper-dropdown-menu-button: {
        left: 0;
        width: 100%;
      };
    }
  </style>

  <template>
    <paper-dropdown-menu id="mydropdown" label="City?">
        <paper-listbox class="dropdown-content">
          <paper-item>Inbox</paper-item>
          <paper-item>Starred</paper-item>
          <paper-item>Sent mail</paper-item>
          <paper-item>Drafts</paper-item>
        </paper-listbox>
      </paper-dropdown-menu>
  </template>
</dom-module>

I'm not 100% percent sure about the selector now, but you can see in the web inspector how the styles are added to the elements inside the paper-dropdown-menu element. See the element documentation at polymer: https://elements.polymer-project.org/elements/paper-dropdown-menu to find guidance which CSS Mixins are avaliable to you and play around with them, until you nail it. Maybe you also have to overrule the default settings with the use of !important CSS attribute modifier.

Custom CSS is still quite annoying in Polymer...

like image 2
Kjell Avatar answered Oct 26 '22 03:10

Kjell