Skip to content

Commit 7a74d82

Browse files
committed
Allow container option to be used only when dropdowns aren't in navbars
1 parent cd8a5be commit 7a74d82

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

docs/4.0/components/dropdowns.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
602602
<td>container</td>
603603
<td>boolean | string | element</td>
604604
<td>false</td>
605-
<td>Appends the dropdown to a specific element. Example: <code>container: 'body'</code>.</td>
605+
<td>Appends the dropdown to a specific element. Example: <code>container: 'body'</code>. Allowed only for dropdowns not in a navbar.</td>
606606
</tr>
607607
</tbody>
608608
</table>

js/src/dropdown.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ const Dropdown = (() => {
9898
constructor(element, config) {
9999
this._element = element
100100
this._popper = null
101+
this._inNavbar = this._detectNavbar()
101102
this._config = this._getConfig(config)
102103
this._menu = this._getMenuElement()
103-
this._inNavbar = this._detectNavbar()
104104

105105
this._addEventListeners()
106106
}
@@ -154,7 +154,7 @@ const Dropdown = (() => {
154154
element = parent
155155
}
156156
}
157-
if (Util.isElement(this._config.container)) {
157+
if (Util.isElement(this._config.container) && !this._inNavbar) {
158158
$(this._config.container).append(this._menu)
159159
}
160160
this._popper = new Popper(element, this._menu, this._getPopperConfig())
@@ -219,13 +219,16 @@ const Dropdown = (() => {
219219
this.constructor.DefaultType
220220
)
221221

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
222+
// Only for dropdowns not in a navbar because we need Popper.js
223+
if (!this._inNavbar) {
224+
if (Util.isElement(config.container) || typeof config.container === 'string') {
225+
// if it's a jQuery object or a collection of elements
226+
if (typeof config.container.jquery === 'string' && typeof config.container[0] !== 'undefined') {
227+
config.container = config.container[0]
228+
} else if (typeof config.container === 'string') {
229+
const tmpContainer = $(document).find(config.container)
230+
config.container = typeof tmpContainer[0] !== 'undefined' ? tmpContainer[0] : false
231+
}
229232
}
230233
}
231234

@@ -352,7 +355,7 @@ const Dropdown = (() => {
352355
}
353356

354357
toggles[i].setAttribute('aria-expanded', 'false')
355-
if (Util.isElement(context._config.container)) {
358+
if (Util.isElement(context._config.container) && !context._inNavbar) {
356359
$(parent).append(context._menu)
357360
}
358361

0 commit comments

Comments
 (0)