diff --git a/core/misc/dropbutton/dropbutton.base.css b/core/misc/dropbutton/dropbutton.base.css index 35e9692..4b87ff6 100644 --- a/core/misc/dropbutton/dropbutton.base.css +++ b/core/misc/dropbutton/dropbutton.base.css @@ -17,6 +17,10 @@ display: block; min-height: 2em; position: relative; + float: left; +} +.js .dropbutton-wrapper.width-processed { + float: none; } .js .dropbutton-wrapper, .js .dropbutton-widget { @@ -37,7 +41,7 @@ } -.js .dropbutton-widget { +.js .dropbutton-widget.width-processed { position: absolute; } /* UL styles are over-scoped in core, so this selector needs weight parity. */ diff --git a/core/misc/dropbutton/dropbutton.js b/core/misc/dropbutton/dropbutton.js index daebca2..d952f00 100644 --- a/core/misc/dropbutton/dropbutton.js +++ b/core/misc/dropbutton/dropbutton.js @@ -14,10 +14,19 @@ Drupal.behaviors.dropButton = { if ($body.length) { $body.on('click', '.dropbutton-toggle', dropbuttonClickHandler); } - // Initialize all buttons. + // Initialize all buttons, and while doing so find the widest button. + var $widestButton = 0; for (var i = 0, il = $dropbuttons.length; i < il; i++) { DropButton.dropbuttons.push(new DropButton($dropbuttons[i], settings.dropbutton)); + $widestButton = dropbuttonGetMaxWidth($dropbuttons[i], $widestButton); } + // Set the width of the first dropbutton to that of the widest dropbutton + // to prevent button text overflow on small screens. + $($dropbuttons[0]).width($widestButton); + // Add a class to the dropbutton-wrapper and dropbutton-widget HTML + // elements. This enables styles to be applied that ensure the + // dropbutton text displays correctly. + $('.js .dropbutton-wrapper, .js .dropbutton-widget').addClass('width-processed'); } } }; @@ -161,4 +170,24 @@ $.extend(Drupal.theme, { // Expose constructor in the public space. Drupal.DropButton = DropButton; +/** +* Compares the widths of $button and $widestButton and returns the larger value. +* +* @param {Jquery} $button +* A jQuery $button element. +* +* @param {String} $widestButton +* The width to compare against $button. +* +* @return {String} +* The larger value after comparing the widths of $button and $widestButton. +*/ +function dropbuttonGetMaxWidth($button, $widestButton) { + var buttonWidth = $('.dropbutton-widget', $button).outerWidth(); + if (buttonWidth > $widestButton) { + return buttonWidth; + } + return $widestButton; +} + })(jQuery, Drupal);