twbs / twbs/bootstrap

Collapse: make it possible to add a root or scope for where to look for triggers

Open
#39,506 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
MDX
Stars
175k
Forks
78.6k
Avg merge
7h 19m
Merged PRs (30d)
35

Description

Prerequisites
Proposal

It would be nice if it were possible to add a root or scope that can limit where Collapse looks for elements that trigger the same target.

Something like an attribute -- perhaps data-bs-root -- with a selector, just like for data-bs-target. The Collapse constructor could then use this selector to find the root element to pass to SelectorEngine. SelectorEngine could then limit the DOM tree it searches to only the sub tree from the root element -- potentially by-passing a large part of the DOM.

HTML example

<div class="collapse-root-1">
  <button data-bs-toggle="collapse" data-bs-target=".collapse-root-1 collapse">Click me</button>
  <span class="collapse" data-bs-root=".collapse-root-1">Show me</span>
</div>
[...]
<div class="collapse-root-500">
  <button data-bs-toggle="collapse" data-bs-target=".collapse-root-500 collapse">Click me</button>
  <span class="collapse" data-bs-root=".collapse-root-500">Show me</span>
</div>

Collapse constructor proposal

class Collapse extends BaseComponent {
  constructor(element, config) {
    ...
    const root = SelectorEngine.find(element.dataset.bsRoot)
    const toggleList = SelectorEngine.find(SELECTOR_DATA_TOGGLE, root)
    ...
  }
}
Motivation and context

When you have a large DOM with many Collapse triggers, it can take multiple seconds for the Collapse constructor to iterate through all of them to find all triggers for the given Collapse target. If it were possible to limit this DOM query to a specific sub tree, it would be possible to reduce the time spent considerably.

If you have this issue today, the only way to solve it -- at least as far as I can see -- is to completely take over from Bootstrap and implement the Collapse functionality yourself. This negates the reason to use Bootstrap, so I think it would make a great addition to the library.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the Collapse constructor and SelectorEngine behavior described in the proposal, focusing on how triggers for a target are currently discovered. Define how a data-bs-root selector scopes those searches and how invalid or missing roots behave; done means the feature limits trigger lookup to the intended subtree without changing existing Collapse behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.