Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ember.js component button may have one of a fixed listed of values, how to make it use minimum width for longest string?

Single button, it may display the text "Show", or "Hide".

Toggling between show and hide causes the width of the button to change.

I want the button to take up as much width as it needs to show the widest string irrespective of which string is displayed, so that it takes up as little width as needed but doesn’t jiggle the display.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve dug through various sites, my google-foo is not up to finding this answer.

Demonstrates width bounce

Ember code…

<div ...attributes>
{{#let (unique-id) as |passwordId|}}
<label class="block uppercase" for="{{passwordId}}">password</label>
<div class="flex flex-row">
    <div class="border-2 border-besler-blue p-2">
        <input class="outline-none" id="{{passwordId}}" type="{{if this.isHidden "password" "text"}}" value="{{@startValue}}" placeholder="{{@placeholder}}">
    </div>
    <div class="border-r-2 border-y-2 border-besler-blue p-2 bg-gray-200">
        <button type="button" {{on "click" this.toggleShow}}>{{if this.isHidden "Show" "Hide"}}</button>
    </div>
</div>
{{/let}}
</div>

>Solution :

You can do this with CSS by overlaying both texts on top of eachother and turning the inactive text invisible.

I’ve made a runnable demo here, reproducing the original problem:

    <button {{on "click" this.increment}}>
      Click<br>
      {{#if (isEven this.count)}}
        This is Even
      {{else}}
        Odd
      {{/if}}
    </button>

if we change these plain text values to be contained within elements that we can style, and change the position of:

    <button {{on "click" this.increment}}>
      Click<br>
      <div style="position: relative">
        <span style="position: absolute">Odd</span>  
        <span>This is Even</span>
      </div>
    </button>

ends up looking like this
enter image description here

and then if we add back our show/hide logic:

    <button {{on "click" this.increment}}>
      Click<br>
      <div style="position: relative">
        <span style="position: absolute; {{if (isEven this.count) "opacity:0;"}}">Odd</span>  
        <span style={{if (isEven this.count) "" "opacity:0;"}}>This is Even</span>
      </div>
    </button>

Play with it here

looks like this:
https://imgur.com/a/wLU83Dm

enter image description here
enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading