aboutsummaryrefslogtreecommitdiff
path: root/site/content/docs/5.0/components/dropdowns.md
diff options
context:
space:
mode:
authorPatrick H. Lauke <[email protected]>2021-05-04 12:46:06 +0100
committerGitHub <[email protected]>2021-05-04 12:46:06 +0100
commit8865a8ab1c7157ab81bf49afa62b75f36daee46d (patch)
tree97ef78f2ea8e07aab50014176d061fe3c1d49134 /site/content/docs/5.0/components/dropdowns.md
parent018ee6a3b50b958ddb49657086cd9168abf5a485 (diff)
parent7ea6578773cb1b7f5cfb8fb41321b3fa10349daf (diff)
downloadbootstrap-jo-docs-thanks-page.tar.xz
bootstrap-jo-docs-thanks-page.zip
Merge branch 'main' into jo-docs-thanks-pagejo-docs-thanks-page
Diffstat (limited to 'site/content/docs/5.0/components/dropdowns.md')
-rw-r--r--site/content/docs/5.0/components/dropdowns.md239
1 files changed, 208 insertions, 31 deletions
diff --git a/site/content/docs/5.0/components/dropdowns.md b/site/content/docs/5.0/components/dropdowns.md
index 69a7783dc..3ab91f807 100644
--- a/site/content/docs/5.0/components/dropdowns.md
+++ b/site/content/docs/5.0/components/dropdowns.md
@@ -30,10 +30,10 @@ Any single `.btn` can be turned into a dropdown toggle with some markup changes.
{{< example >}}
<div class="dropdown">
- <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
+ <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown button
</button>
- <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
+ <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
@@ -635,16 +635,18 @@ Add `.disabled` to items in the dropdown to **style them as disabled**.
## Menu alignment
-By default, a dropdown menu is automatically positioned 100% from the top and along the left side of its parent. Add `.dropdown-menu-end` to a `.dropdown-menu` to right align the dropdown menu. Directions are mirrored when using Bootstrap in RTL, meaning `.dropdown-menu-end` will appear on the left side.
+By default, a dropdown menu is automatically positioned 100% from the top and along the left side of its parent. You can change this with the directional `.drop*` classes, but you can also control them with additional modifier classes.
+
+Add `.dropdown-menu-end` to a `.dropdown-menu` to right align the dropdown menu. Directions are mirrored when using Bootstrap in RTL, meaning `.dropdown-menu-end` will appear on the left side.
{{< callout info >}}
-**Heads up!** Dropdowns are positioned thanks to Popper (except when they are contained in a navbar).
+**Heads up!** Dropdowns are positioned thanks to Popper except when they are contained in a navbar.
{{< /callout >}}
{{< example >}}
<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
- Right-aligned menu
+ Right-aligned menu example
</button>
<ul class="dropdown-menu dropdown-menu-end">
<li><button class="dropdown-item" type="button">Action</button></li>
@@ -690,6 +692,89 @@ To align **left** the dropdown menu with the given breakpoint or larger, add `.d
Note that you don't need to add a `data-bs-display="static"` attribute to dropdown buttons in navbars, since Popper isn't used in navbars.
+### Alignment options
+
+Taking most of the options shown above, here's a small kitchen sink demo of various dropdown alignment options in one place.
+
+{{< example >}}
+<div class="btn-group">
+ <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
+ Dropdown
+ </button>
+ <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ </ul>
+</div>
+
+<div class="btn-group">
+ <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
+ Right-aligned menu
+ </button>
+ <ul class="dropdown-menu dropdown-menu-end">
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ </ul>
+</div>
+
+<div class="btn-group">
+ <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
+ Left-aligned, right-aligned lg
+ </button>
+ <ul class="dropdown-menu dropdown-menu-lg-end">
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ </ul>
+</div>
+
+<div class="btn-group">
+ <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
+ Right-aligned, left-aligned lg
+ </button>
+ <ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-start">
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ </ul>
+</div>
+
+<div class="btn-group dropstart">
+ <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
+ Dropstart
+ </button>
+ <ul class="dropdown-menu">
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ </ul>
+</div>
+
+<div class="btn-group dropend">
+ <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
+ Dropend
+ </button>
+ <ul class="dropdown-menu">
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ </ul>
+</div>
+
+<div class="btn-group dropup">
+ <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
+ Dropup
+ </button>
+ <ul class="dropdown-menu">
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ </ul>
+</div>
+{{< /example >}}
+
## Menu content
### Headers
@@ -818,6 +903,78 @@ Use `data-bs-offset` or `data-bs-reference` to change the location of the dropdo
</div>
{{< /example >}}
+### Auto close behavior
+
+By default, the dropdown menu is closed when clicking inside or outside the dropdown menu. You can use the `autoClose` option to change this behavior of the dropdown.
+
+{{< example >}}
+<div class="btn-group">
+ <button class="btn btn-secondary dropdown-toggle" type="button" id="defaultDropdown" data-bs-toggle="dropdown" data-bs-auto-close="true" aria-expanded="false">
+ Default dropdown
+ </button>
+ <ul class="dropdown-menu" aria-labelledby="defaultDropdown">
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ </ul>
+</div>
+
+<div class="btn-group">
+ <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuClickableOutside" data-bs-toggle="dropdown" data-bs-auto-close="inside" aria-expanded="false">
+ Clickable outside
+ </button>
+ <ul class="dropdown-menu" aria-labelledby="dropdownMenuClickableOutside">
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ </ul>
+</div>
+
+<div class="btn-group">
+ <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuClickableInside" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">
+ Clickable inside
+ </button>
+ <ul class="dropdown-menu" aria-labelledby="dropdownMenuClickableInside">
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ </ul>
+</div>
+
+<div class="btn-group">
+ <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuClickable" data-bs-toggle="dropdown" data-bs-auto-close="false" aria-expanded="false">
+ Manual close
+ </button>
+ <ul class="dropdown-menu" aria-labelledby="dropdownMenuClickable">
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ <li><a class="dropdown-item" href="#">Menu item</a></li>
+ </ul>
+</div>
+{{< /example >}}
+
+## Sass
+
+### Variables
+
+Variables for all dropdowns:
+
+{{< scss-docs name="dropdown-variables" file="scss/_variables.scss" >}}
+
+Variables for the [dark dropdown](#dark-dropdowns):
+
+{{< scss-docs name="dropdown-dark-variables" file="scss/_variables.scss" >}}
+
+Variables for the CSS-based carets that indicate a dropdown's interactivity:
+
+{{< scss-docs name="caret-variables" file="scss/_variables.scss" >}}
+
+### Mixins
+
+Mixins are used to generate the CSS-based carets and can be found in `scss/mixins/_caret.scss`.
+
+{{< scss-docs name="caret-mixins" file="scss/mixins/_caret.scss" >}}
+
## Usage
Via data attributes or JavaScript, the dropdown plugin toggles hidden content (dropdown menus) by toggling the `.show` class on the parent `.dropdown-menu`. The `data-bs-toggle="dropdown"` attribute is relied on for closing dropdown menus at an application level, so it's a good idea to always use it.
@@ -860,7 +1017,7 @@ Regardless of whether you call your dropdown via JavaScript or instead use the d
### Options
-Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-bs-`, as in `data-bs-offset=""`.
+Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-bs-`, as in `data-bs-offset=""`. Make sure to change the case type of the option name from camelCase to kebab-case when passing the options via data attributes. For example, instead of using `data-bs-autoClose="false"`, use `data-bs-auto-close="false"`.
<table class="table">
<thead>
@@ -873,32 +1030,16 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
</thead>
<tbody>
<tr>
- <td><code>offset</code></td>
- <td>number | string | function</td>
- <td><code>0</code></td>
- <td>
- <p>Offset of the dropdown relative to its target.</p>
- <p>When a function is used to determine the offset, it is called with an object containing the offset data as its first argument. The function must return an object with the same structure. The triggering element DOM node is passed as the second argument.</p>
- <p>For more information refer to Popper's <a href="https://popper.js.org/docs/v1/#modifiers..offset.offset">offset docs</a>.</p>
- </td>
- </tr>
- <tr>
- <td><code>flip</code></td>
- <td>boolean</td>
- <td><code>true</code></td>
- <td>Allow Dropdown to flip in case of an overlapping on the reference element. For more information refer to Popper's <a href="https://popper.js.org/docs/v1/#modifiers..flip.enabled">flip docs</a>.</td>
- </tr>
- <tr>
<td><code>boundary</code></td>
<td>string | element</td>
- <td><code>'scrollParent'</code></td>
- <td>Overflow constraint boundary of the dropdown menu. Accepts the values of <code>'viewport'</code>, <code>'window'</code>, <code>'scrollParent'</code>, or an HTMLElement reference (JavaScript only). For more information refer to Popper's <a href="https://popper.js.org/docs/v1/#modifiers..preventOverflow.boundariesElement">preventOverflow docs</a>.</td>
+ <td><code>'clippingParents'</code></td>
+ <td>Overflow constraint boundary of the dropdown menu (applies only to Popper's preventOverflow modifier). By default it's <code>'clippingParents'</code> and can accept an HTMLElement reference (via JavaScript only). For more information refer to Popper's <a href="https://popper.js.org/docs/v2/utils/detect-overflow/#boundary">detectOverflow docs</a>.</td>
</tr>
<tr>
<td><code>reference</code></td>
- <td>string | element</td>
+ <td>string | element | object</td>
<td><code>'toggle'</code></td>
- <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's <a href="https://popper.js.org/docs/v1/#referenceObject">referenceObject docs</a>.</td>
+ <td>Reference element of the dropdown menu. Accepts the values of <code>'toggle'</code>, <code>'parent'</code>, an HTMLElement reference or an object providing <code>getBoundingClientRect</code>. For more information refer to Popper's <a href="https://popper.js.org/docs/v2/constructors/#createpopper">constructor docs</a> and <a href="https://popper.js.org/docs/v2/virtual-elements/">virtual element docs</a>.</td>
</tr>
<tr>
<td><code>display</code></td>
@@ -907,15 +1048,52 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
<td>By default, we use Popper for dynamic positioning. Disable this with <code>static</code>.</td>
</tr>
<tr>
+ <td><code>offset</code></td>
+ <td>array | string | function</td>
+ <td><code>[0, 2]</code></td>
+ <td>
+ <p>Offset of the dropdown relative to its target. You can pass a string in data attributes with comma separated values like: <code>data-bs-offset="10,20"</code></p>
+ <p>When a function is used to determine the offset, it is called with an object containing the popper placement, the reference, and popper rects as its first argument. The triggering element DOM node is passed as the second argument. The function must return an array with two numbers: <code>[<a href="https://popper.js.org/docs/v2/modifiers/offset/#skidding-1">skidding</a>, <a href="https://popper.js.org/docs/v2/modifiers/offset/#distance-1">distance</a>]</code>.</p>
+ <p>For more information refer to Popper's <a href="https://popper.js.org/docs/v2/modifiers/offset/#options">offset docs</a>.</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code>autoClose</code></td>
+ <td>boolean | string</td>
+ <td><code>true</code></td>
+ <td>
+ <p>Configure the auto close behavior of the dropdown:</p>
+ <ul>
+ <li><code>true</code> - the dropdown will be closed by clicking outside or inside the dropdown menu.</li>
+ <li><code>false</code> - the dropdown will be closed by clicking the toggle button and manually calling <code>hide</code> or <code>toggle</code> method. (Also will not be closed by pressing <kbd>esc</kbd> key)</li>
+ <li><code>'inside'</code> - the dropdown will be closed (only) by clicking inside the dropdown menu.</li>
+ <li><code>'outside'</code> - the dropdown will be closed (only) by clicking outside the dropdown menu.</li>
+ </ul>
+ </td>
+ </tr>
+ <tr>
<td><code>popperConfig</code></td>
- <td>null | object</td>
+ <td>null | object | function</td>
<td><code>null</code></td>
- <td>To change Bootstrap's default Popper config, see <a href="https://popper.js.org/docs/v1/#Popper.Defaults">Popper's configuration</a></td>
+ <td>
+ <p>To change Bootstrap's default Popper config, see <a href="https://popper.js.org/docs/v2/constructors/#options">Popper's configuration</a>.</p>
+ <p>When a function is used to create the Popper configuration, it's called with an object that contains the Bootstrap's default Popper configuration. It helps you use and merge the default with your own configuration. The function must return a configuration object for Popper.</p>
+ </td>
</tr>
</tbody>
</table>
-Note when `boundary` is set to any value other than `'scrollParent'`, the style `position: static` is applied to the `.dropdown` container.
+#### Using function with `popperConfig`
+
+```js
+var dropdown = new bootstrap.Dropdown(element, {
+ popperConfig: function (defaultBsPopperConfig) {
+ // var newPopperConfig = {...}
+ // use defaultBsPopperConfig if needed...
+ // return newPopperConfig
+ }
+})
+```
### Methods
@@ -968,8 +1146,7 @@ Note when `boundary` is set to any value other than `'scrollParent'`, the style
### Events
-All dropdown events are fired at the `.dropdown-menu`'s parent element and have a `relatedTarget` property, whose value is the toggling anchor element.
-`hide.bs.dropdown` and `hidden.bs.dropdown` events have a `clickEvent` property (only when the original Event type is `click`) that contains an Event Object for the click event.
+All dropdown events are fired at the toggling element and then bubbled up. So you can also add event listeners on the `.dropdown-menu`'s parent element. `hide.bs.dropdown` and `hidden.bs.dropdown` events have a `clickEvent` property (only when the original Event type is `click`) that contains an Event Object for the click event.
<table class="table">
<thead>