From ade90621ad4095e37d7df8479ac1f305a08593f5 Mon Sep 17 00:00:00 2001 From: Johann-S Date: Wed, 17 May 2017 16:11:17 +0200 Subject: [PATCH] Use popper to align dropdown menu instead of using css with important --- js/src/dropdown.js | 31 +++++++++++++++++++--- js/tests/visual/dropdown.html | 48 +++++++++++++++++++++++++++-------- scss/_dropdown.scss | 14 ---------- 3 files changed, 65 insertions(+), 28 deletions(-) diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 39a4a86ca4ce..846746384b07 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -52,8 +52,11 @@ const Dropdown = (($) => { } const ClassName = { - DISABLED : 'disabled', - SHOW : 'show' + DISABLED : 'disabled', + SHOW : 'show', + DROPUP : 'dropup', + MENURIGHT : 'dropdown-menu-right', + MENULEFT : 'dropdown-menu-left' } const Selector = { @@ -142,7 +145,7 @@ const Dropdown = (($) => { } // Handle dropup - const dropdownPlacement = $(this._element).parent().hasClass('dropup') ? AttachmentMap.TOP : this._config.placement + const dropdownPlacement = $(this._element).parent().hasClass(ClassName.DROPUP) ? AttachmentMap.TOP : this._config.placement this._popper = new Popper(this._element, this._menu, { placement : dropdownPlacement, modifiers : { @@ -151,6 +154,11 @@ const Dropdown = (($) => { }, flip : { enabled : this._config.flip + }, + beforeApplyStyle: { + order: 899, // 900 is the order of applyStyle + enabled: true, + fn: this._beforePopperApplyStyle } } }) @@ -230,6 +238,23 @@ const Dropdown = (($) => { return this._menu } + _beforePopperApplyStyle(data) { + if ($(data.instance.popper).hasClass(ClassName.MENURIGHT)) { + data.styles = { + right: 0, + left: 'auto' + } + } + + if ($(data.instance.popper).hasClass(ClassName.MENULEFT)) { + data.styles = { + right: 'auto', + left: 0 + } + } + return data + } + // static static _jQueryInterface(config) { diff --git a/js/tests/visual/dropdown.html b/js/tests/visual/dropdown.html index 45c5d51722b5..166cbf7259b7 100644 --- a/js/tests/visual/dropdown.html +++ b/js/tests/visual/dropdown.html @@ -59,19 +59,45 @@

Dropdown Bootstrap Visual Test

- -
- - - -
diff --git a/scss/_dropdown.scss b/scss/_dropdown.scss index 981a17cb8634..d9e75b0aa745 100644 --- a/scss/_dropdown.scss +++ b/scss/_dropdown.scss @@ -108,20 +108,6 @@ } } -// Menu positioning -// -// Add extra class to `.dropdown-menu` to flip the alignment of the dropdown -// menu with the parent. -.dropdown-menu-right { - right: 0; - left: auto !important; // Reset the default from `.dropdown-menu` -} - -.dropdown-menu-left { - right: auto !important; - left: 0; -} - .dropdown-menu.show { display: block; }