Skip to content

Change the Way Width is Determined #1439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Change the Way Width is Determined #1439

wants to merge 4 commits into from

Conversation

pfiller
Copy link
Contributor

@pfiller pfiller commented Aug 1, 2013

@harvesthq/chosen-developers

This fixes some issues that come when applying Chosen to a select that is not visible (when you haven't set width: n for whatever reason). Basically, it uses jQuery & Prototypes magic methods for getting width if they're set and falls back to offsetWidth if they're not. As always, a width specified in the option object will take precedence.

I went a little crazy testing this. Each of these fiddles demonstrates 4 ways you would apply width to Chosen in both a container that is hidden on page load and a container that is visible on page load. As you can see, with the exception of a hidden select that has no style applied to it, the new version does a wonderful job of matching size:

Please Note: This doesn't fix the % width issue (people will still need to specify those via width: %. I only mention this because one of these examples uses a percentage width set via a class. If you adjust the width of the output area, Chosen might not match it's original field. The best thing to do is adjust the width of the area and then click "Run" again. (Can you tell I learned this the hard way?)

pfiller added 4 commits August 1, 2013 17:42
For jQuery, we first test the css('width') of a select. This always
returns a value for visible selects, but it will return false
for hidden selects with no style set (inline or class based).

For Prototype, we first check the getStyle('width') of a select. This
behaves much the same way as jQuery's css('width'), fortunately.
fields width gets set to "-10px". So let's make sure there's a value!
This solves an issue with jQuery's `outerWidth` method which breaks
on hidden objects with a % width in their style. offsetWidth simply
returns a 0 on those objects which suits our purpose just fine.
return if @options.width? then @options.width else "#{@form_field.offsetWidth}px"
return @options.width if @options.width?

if jQuery? and $ is jQuery
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should have jquery-specific code in AbstractChosen. The method should be moved to the different Chosen implementations

@alexoanea
Copy link

Is anyone currently looking into these width issue (the one that occurs when select is hidden)? I did try these fixes that @pfiller pushed here but these haven't solved the problem for me.

@pfiller
Copy link
Contributor Author

pfiller commented Jun 2, 2016

Going to close this olllllllld PR. @koenpunt's #2495 is probably a better solution ultimately.

@pfiller pfiller closed this Jun 2, 2016
@koenpunt koenpunt deleted the fix-hidden-width branch September 20, 2016 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants