Dropdowns: Modified Dropdown's _parent to use a new method called 'cl… #41037
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Dropdowns: Modified Dropdown's _parent to use a new method called 'closest', to allow dropdown menus to not necessarily be the sibling of the trigger button element. So instead of trying to use dropdown wrapper as the direct parent, it can be an ancestor.
Description
This is a non-breaking improvement change to the Dropdown and Selector Engine JS files. More in the Motivation or context. But the long story short is that inside the dropdown's
constructor
, instead of settingthis._parent
to the direct parent element, this change will use a new method for the selector engine calledclosest
which will find the closest ancestor that contains thedropdown menu element
. This will then allow a not-so restrictive DOM structure. All existing behaviour as per Bootstrap documentation remains the same.Motivation & Context
Currently, I'm trying to create web components that use Bootstrap's dropdowns as the main mechanism for our dropdowns in our design system. Currently based on the
Dropdown
'sconstructor
,this._parent
is set to only use thethis._element
's direct parent. So as per documentation, it allows usages like this:Which is fine for most use cases. However, in our use case, since it is a design system, we also create web components for things such as buttons like this:
On render, the HTML would look something typical to this:
As you can see in the above, the
button
with thedata-bs-toggle
which is theelement
in the dropdown constructor is not a sibling of the.dropdown-menu
element, so when the dropdown open is called, the code is stuck because of the restrictive HTML structure.The proposed changes will address the issue whilst maintaining the original intent of the HTML structure of these elements, and works for the current HTML structure. I have tested this with all of the combinations within the Bootstrap documentation including dropdowns inside navbars, split dropdown and standard default use cases.
In terms of documentation, I don't feel a change is required on that front since all of the use cases in the documentation have not changed.
Type of changes
Checklist
npm run lint
)