The chosen behaviour doesn't take resize events into account.

Consequently, when a resize occurs, the chosen-generated markup retains a fixed, un-overrideable size in a style attribute that may not be correct for the new viewport width. The chosen library handles the same situation gracefully when used independently.

To reproduce, change the orientation of a mobile device, or use Firefox' or Chrome's Responsive Design Mode: the Chosen elements will retain their prior size after the viewport size.

A superficial reading of the code suggests to me that it should be possible to add an updateChosen() function alongside the existing createChosen() function using jQuery's .resize() method, but I haven't studied the code in detail.

CommentFileSizeAuthor
#2 separate_pixel_and_percent_calc.patch1.27 KBbedlam

Comments

bedlam created an issue. See original summary.

bedlam’s picture

StatusFileSize
new1.27 KB

I dug in a bit to see what was going on, and I realize that I misconstrued the cause.

In fact what is going on with the current state of the module is that the javascript mixes percentage and pixel dimensions at line 158 and line 162 when use_relative_width is set:

        var dimension = this.settings.use_relative_width ? '%' : 'px';

        if ($element.width() < this.settings.minimum_width) {
          options.width = this.settings.minimum_width + dimension;
        }
        else {
          options.width = $element.width() + dimension;
        }

So what this means is that, when use_relative_width is set, the initial if compares the pixel width ($element.width()) with a percentage.

Given that 100% is usually the maximum percentage value that'll be set, and that 100px is a very small value for the size of a select, the if statement will almost always evaluate false (i.e. because the 100 entered as a percentage value is smaller than the pixel value of 223.9882 returned by jQuery).

This in turn means that we can wind up setting unhelpful values like width: 223.9882%.

The attached patch keeps percentage and pixel values in their own lanes :)

bedlam’s picture

By the way, this may address some of the other issues here such as https://www.drupal.org/project/chosen/issues/2828214 (there are multiple similar-sounding issues filed against the 7.x branch as well).

nagy.balint’s picture

Status: Active » Needs review
mr.york’s picture

Status: Needs review » Reviewed & tested by the community

Good patch, solved this problem.

  • bedlam authored d13721a on 8.x-2.x
    Issue #3094249 by bedlam, mr.york: Chosen elements not responsive when...
nagy.balint’s picture

Status: Reviewed & tested by the community » Fixed

Thanks!

bedlam’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.