Skip to content

Commit 65ae622

Browse files
Johann-Smdo
authored andcommitted
Dropdown - Allow to disable Popper.js style (#24092)
* Dropdown - Allow to disable Popper.js style * Update dropdown.js * Update dropdown.html * copy changes
1 parent ba878eb commit 65ae622

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

docs/4.0/components/dropdowns.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,12 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
807807
<td>'toggle'</td>
808808
<td>Reference element of the dropdown menu. Accepts the values of <code>'toggle'</code>, <code>'parent'</code>, or an HTMLElement reference. For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#referenceObject">referenceObject docs</a>.</td>
809809
</tr>
810+
<tr>
811+
<td>display</td>
812+
<td>string</td>
813+
<td>dynamic | static</td>
814+
<td>By default, we use Popper.js for dynamic positioning. Disable this with `static`.</td>
815+
</tr>
810816
</tbody>
811817
</table>
812818

js/src/dropdown.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,16 @@ const Dropdown = (($) => {
7575
offset : 0,
7676
flip : true,
7777
boundary : 'scrollParent',
78-
reference : 'toggle'
78+
reference : 'toggle',
79+
display : 'dynamic'
7980
}
8081

8182
const DefaultType = {
8283
offset : '(number|string|function)',
8384
flip : 'boolean',
8485
boundary : '(string|element)',
85-
reference : '(string|element)'
86+
reference : '(string|element)',
87+
display : 'string'
8688
}
8789

8890
/**
@@ -295,6 +297,12 @@ const Dropdown = (($) => {
295297
}
296298
}
297299

300+
// Disable Popper.js if we have a static display
301+
if (this._config.display === 'static') {
302+
popperConfig.modifiers.applyStyle = {
303+
enabled: false
304+
}
305+
}
298306
return popperConfig
299307
}
300308

js/tests/unit/dropdown.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,4 +908,34 @@ $(function () {
908908
})
909909
$textarea.trigger('click')
910910
})
911+
912+
QUnit.test('should not use Popper.js if display set to static', function (assert) {
913+
assert.expect(1)
914+
var dropdownHTML =
915+
'<div class="dropdown">' +
916+
'<a href="#" class="dropdown-toggle" data-toggle="dropdown" data-display="static">Dropdown</a>' +
917+
'<div class="dropdown-menu">' +
918+
'<a class="dropdown-item" href="#">Secondary link</a>' +
919+
'<a class="dropdown-item" href="#">Something else here</a>' +
920+
'<div class="divider"/>' +
921+
'<a class="dropdown-item" href="#">Another link</a>' +
922+
'</div>' +
923+
'</div>'
924+
925+
var $dropdown = $(dropdownHTML)
926+
.appendTo('#qunit-fixture')
927+
.find('[data-toggle="dropdown"]')
928+
.bootstrapDropdown()
929+
var done = assert.async()
930+
var dropdownMenu = $dropdown.next()[0]
931+
932+
$dropdown.parent('.dropdown')
933+
.on('shown.bs.dropdown', function () {
934+
// Popper.js add this attribute when we use it
935+
assert.strictEqual(dropdownMenu.getAttribute('x-placement'), null)
936+
done()
937+
})
938+
939+
$dropdown.trigger('click')
940+
})
911941
})

js/tests/visual/dropdown.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,17 @@ <h1>Dropdown <small>Bootstrap Visual Test</small></h1>
164164
</div>
165165

166166
<div class="row">
167-
<div class="col-sm-12 mt-4">
167+
<div class="col-sm-3 mt-4">
168168
<div class="btn-group dropdown">
169-
<button type="button" class="btn btn-secondary" data-offset="10,20">Dropdown offset</button>
169+
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" data-offset="10,20">Dropdown offset</button>
170170
<div class="dropdown-menu">
171171
<a class="dropdown-item" href="#">Action</a>
172172
<a class="dropdown-item" href="#">Another action</a>
173173
<a class="dropdown-item" href="#">Something else here</a>
174174
</div>
175175
</div>
176176
</div>
177-
<div class="col-sm-12 mt-4">
177+
<div class="col-sm-3 mt-4">
178178
<div class="btn-group dropdown">
179179
<button type="button" class="btn btn-secondary">Dropdown reference</button>
180180
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" data-reference="parent">
@@ -187,6 +187,18 @@ <h1>Dropdown <small>Bootstrap Visual Test</small></h1>
187187
</div>
188188
</div>
189189
</div>
190+
<div class="col-sm-3 mt-4">
191+
<div class="dropdown">
192+
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" data-display="static" aria-haspopup="true" aria-expanded="false">
193+
Dropdown menu without Popper.js
194+
</button>
195+
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
196+
<a class="dropdown-item" href="#">Action</a>
197+
<a class="dropdown-item" href="#">Another action</a>
198+
<a class="dropdown-item" href="#">Something else here</a>
199+
</div>
200+
</div>
201+
</div>
190202
</div>
191203

192204
</div>

0 commit comments

Comments
 (0)