Make 'dropdown-content' same width as paper-dropdown-menu?
Asked Answered
T

4

11

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

Tiffanytiffi answered 4/11, 2015 at 22:6 Comment(5)
Possible duplicate of Autosize the paper-dropdown-menu widthDavina
Actually, it is a different question. The other question asks how to automatically resize paper-dropdown-menu to the longest element, while this questions asks how to size dropdown-content to the paper-dropdown-menu.Davina
Not too familiar with polymer but is this a CSS question?Lory
Could you show this in codepenKibosh
Show us a jsfiddle please.Hawkes
R
5

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.

Riemann answered 4/10, 2016 at 22:36 Comment(3)
Looks like this only works if the width is fixed in pixels, but does not work for say 80%. This is because individual items have their position set to relative and therefore are not affected by the parent element... maybe.Davina
@SergiyByelozyorov Yes that is right, Is there a reason you need to use 80 %? If so you could go with what has suggested. However I would suggest against it for code maintainability. If you change iron-dropdown it will change the way all of the dropdowns throughout your app will behaveRiemann
I need percentage because width of the dropdown scales with window size. All I need it to have the size of the list to match the size of the dropdown.Davina
M
3

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>
Mandler answered 10/10, 2016 at 10:20 Comment(8)
This actually works, but for some reason it stops working as soon as I copy the snippet into my app. Looks like there is some CSS conflict...Davina
Can you please explain why do you change CSS properties of iron-dropdown even though it's not directly present in the DOM?Davina
If your css is conflict then just use iron-dropdown{left:0 !important; width:100% !important; } it will work and now another question because this iron-dropdown css was not allowing us to adjust the width of that dropdown so i Inspect it and change the width as u askedMandler
hey budy if this answer is useful then accept it and close this question thanksMandler
I think the author of the question does not care about it anymore. I started the bounty, but I can't accept the answer since I am not the author.Davina
Hello, I do care and read your solutions. But wasn't quite satisfied with the solutions since they didnt feel quite right. I do believe that Kjell's answer is more on track, but haven't been able to verify that it works yet.Tiffanytiffi
If u think my answer is not right than answer my question that how is this example possible which is running on this site? Kk thanksMandler
Just because an answer works, doesn't mean it answers the question. As Kjell noted in his post, I wanted a clean way (AKA "pure" polymer way) of doing things, and thus I didn't accept neither of your answers because they didn't feel quite right. If I wanted any possible solution to this problem I probably wouldn't have written on SO in the first place. But I appreciate your effort! :)Tiffanytiffi
L
2

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...

Levant answered 18/10, 2016 at 16:14 Comment(5)
I haven't tried this yet. But just to be clear, would you mind posting the HTML also to show which element uses the custom2-class etc? Thanks!Tiffanytiffi
I have updated my answer - hope this guides you the "polymerry" way! ;))Levant
This didn't quite work for me, using Polymer v3. --paper-menu-button-dropdown was the mixin that worked, and rather than left: 0; width: 100%, I had to set width to an absolute value (width: 289px in my case, without setting a value for left), as otherwise the dropdown would be on the left side of the page. Fortunately, my case was one where the paper-dropdown-menu is being set to a specific value anyway.Yaekoyael
Also: I have successfully re-positioned the iron-dropdown with --paper-menu-button-content, setting margin: a b c d !important. The important is required, as otherwise styling within paper-menu-button will force margin-top: -10px and margin-bottom: 20px.Yaekoyael
sorry Brian, my answer is quite dated already and refers to Polymer 1.x...Levant
U
0

to make 'dropdown-content' dependent to width of the paper-dropdown-menu do this:

  1. create your own menu:
    < resizable-dropdown-menu >
    copy all component from
    < paper-dropdown-menu-light >
    (i like it more), or
    < paper-dropdown-menu >
  2. import IronResizableBehavior enter

    <link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html">
    
       
  3. add Polymer.IronResizableBehavior

    behaviors: [
                //...
                Polymer.IronValidatableBehavior,
                Polymer.IronResizableBehavior    // add this
              ],
  4. Add this to attached function:

/*
 *
 * to make menu the same width as container;
 *
 */

var ironDropdown = Polymer.dom(this.$.menuButton.root).querySelector('iron-dropdown');
this.addEventListener('iron-resize', (e) => {
  ironDropdown.style.width = Polymer.dom(this.root).node.host.offsetWidth+'px';
});
Unexceptionable answered 2/2, 2018 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.