Skip to content

Commit bde8286

Browse files
committed
Add centered dropdown and dropup options
1 parent 28d9aaf commit bde8286

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

js/src/dropdown.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const CLASS_NAME_SHOW = 'show'
4848
const CLASS_NAME_DROPUP = 'dropup'
4949
const CLASS_NAME_DROPEND = 'dropend'
5050
const CLASS_NAME_DROPSTART = 'dropstart'
51+
const CLASS_NAME_DROPUP_CENTER = 'dropup-center'
52+
const CLASS_NAME_DROPDOWN_CENTER = 'dropdown-center'
5153

5254
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]:not(.disabled):not(:disabled)'
5355
const SELECTOR_DATA_TOGGLE_SHOWN = `${SELECTOR_DATA_TOGGLE}.${CLASS_NAME_SHOW}`
@@ -62,6 +64,8 @@ const PLACEMENT_BOTTOM = isRTL() ? 'bottom-end' : 'bottom-start'
6264
const PLACEMENT_BOTTOMEND = isRTL() ? 'bottom-start' : 'bottom-end'
6365
const PLACEMENT_RIGHT = isRTL() ? 'left-start' : 'right-start'
6466
const PLACEMENT_LEFT = isRTL() ? 'right-start' : 'left-start'
67+
const PLACEMENT_TOPCENTER = 'top'
68+
const PLACEMENT_BOTTOMCENTER = 'bottom'
6569

6670
const Default = {
6771
offset: [0, 2],
@@ -248,6 +252,14 @@ class Dropdown extends BaseComponent {
248252
return PLACEMENT_LEFT
249253
}
250254

255+
if (parentDropdown.classList.contains(CLASS_NAME_DROPUP_CENTER)) {
256+
return PLACEMENT_TOPCENTER
257+
}
258+
259+
if (parentDropdown.classList.contains(CLASS_NAME_DROPDOWN_CENTER)) {
260+
return PLACEMENT_BOTTOMCENTER
261+
}
262+
251263
// We need to trim the value because custom properties can also include spaces
252264
const isEnd = getComputedStyle(this._menu).getPropertyValue('--bs-position').trim() === 'end'
253265

scss/_dropdown.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
.dropup,
33
.dropend,
44
.dropdown,
5-
.dropstart {
5+
.dropstart,
6+
.dropup-center,
7+
.dropdown-center {
68
position: relative;
79
}
810

site/content/docs/5.1/components/dropdowns.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,23 @@ And putting it to use in a navbar:
400400
Directions are mirrored when using Bootstrap in RTL, meaning `.dropstart` will appear on the right side.
401401
{{< /callout >}}
402402

403+
### Centered
404+
405+
Make the dropdown menu centered below the toggle with `.dropdown-center` on the parent element.
406+
407+
{{< example >}}
408+
<div class="dropdown-center">
409+
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownCenterBtn" data-bs-toggle="dropdown" aria-expanded="false">
410+
Centered dropdown
411+
</button>
412+
<ul class="dropdown-menu" aria-labelledby="dropdownCenterBtn">
413+
<li><a class="dropdown-item" href="#">Action</a></li>
414+
<li><a class="dropdown-item" href="#">Action two</a></li>
415+
<li><a class="dropdown-item" href="#">Action three</a></li>
416+
</ul>
417+
</div>
418+
{{< /example >}}
419+
403420
### Dropup
404421

405422
Trigger dropdown menus above elements by adding `.dropup` to the parent element.
@@ -459,6 +476,23 @@ Trigger dropdown menus above elements by adding `.dropup` to the parent element.
459476
</div>
460477
```
461478

479+
### Dropup centered
480+
481+
Make the dropup menu centered above the toggle with `.dropup-center` on the parent element.
482+
483+
{{< example >}}
484+
<div class="dropup-center">
485+
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropupCenterBtn" data-bs-toggle="dropdown" aria-expanded="false">
486+
Centered dropup
487+
</button>
488+
<ul class="dropdown-menu" aria-labelledby="dropupCenterBtn">
489+
<li><a class="dropdown-item" href="#">Action</a></li>
490+
<li><a class="dropdown-item" href="#">Action two</a></li>
491+
<li><a class="dropdown-item" href="#">Action three</a></li>
492+
</ul>
493+
</div>
494+
{{< /example >}}
495+
462496
### Dropend
463497

464498
Trigger dropdown menus at the right of the elements by adding `.dropend` to the parent element.

0 commit comments

Comments
 (0)