Skip to content

Commit 391e107

Browse files
committed
Add container option to our Dropdowns
1 parent 8fccaa2 commit 391e107

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

docs/4.0/components/dropdowns.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,12 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
598598
<td>true</td>
599599
<td>Allow Dropdown to flip in case of an overlapping on the reference element. For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..flip.enabled">flip docs</a>.</td>
600600
</tr>
601+
<tr>
602+
<td>container</td>
603+
<td>boolean | string | element</td>
604+
<td>false</td>
605+
<td>Appends the dropdown to a specific element. Example: <code>container: 'body'</code>.</td>
606+
</tr>
601607
</tbody>
602608
</table>
603609

js/src/dropdown.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ const Dropdown = (() => {
7676

7777
const Default = {
7878
offset : 0,
79-
flip : true
79+
flip : true,
80+
container : false
8081
}
8182

8283
const DefaultType = {
8384
offset : '(number|string|function)',
84-
flip : 'boolean'
85+
flip : 'boolean',
86+
container : '(boolean|string|element)'
8587
}
8688

8789

@@ -152,6 +154,9 @@ const Dropdown = (() => {
152154
element = parent
153155
}
154156
}
157+
if (Util.isElement(this._config.container)) {
158+
$(this._config.container).append(this._menu)
159+
}
155160
this._popper = new Popper(element, this._menu, this._getPopperConfig())
156161

157162
// if this is a touch-enabled device we add extra
@@ -214,6 +219,16 @@ const Dropdown = (() => {
214219
this.constructor.DefaultType
215220
)
216221

222+
if (Util.isElement(config.container) || typeof config.container === 'string') {
223+
// if it's a jQuery object or a collection of elements
224+
if (typeof config.container.jquery === 'string' && typeof config.container[0] !== 'undefined') {
225+
config.container = config.container[0]
226+
} else if (typeof config.container === 'string') {
227+
const tmpContainer = $(document).find(config.container)
228+
config.container = typeof tmpContainer[0] !== 'undefined' ? tmpContainer[0] : false
229+
}
230+
}
231+
217232
return config
218233
}
219234

@@ -337,6 +352,9 @@ const Dropdown = (() => {
337352
}
338353

339354
toggles[i].setAttribute('aria-expanded', 'false')
355+
if (Util.isElement(context._config.container)) {
356+
$(parent).append(context._menu)
357+
}
340358

341359
$(dropdownMenu).removeClass(ClassName.SHOW)
342360
$(parent)

js/tests/visual/dropdown.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ <h1>Dropdown <small>Bootstrap Visual Test</small></h1>
9292
<button class="dropdown-item" type="button">Something else here</button>
9393
</div>
9494
</div>
95+
<div class="btn-group">
96+
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" data-container="body" aria-haspopup="true" aria-expanded="false">
97+
This dropdown's use container option
98+
</button>
99+
<div class="dropdown-menu">
100+
<button class="dropdown-item" type="button">Action</button>
101+
<button class="dropdown-item" type="button">Another action</button>
102+
<button class="dropdown-item" type="button">Something else here</button>
103+
</div>
104+
</div>
95105
</div>
96106
<div class="col-sm-12 mt-4">
97107
<div class="btn-group dropup" role="group">

0 commit comments

Comments
 (0)