Skip to content

Commit ebf1b48

Browse files
authored
Merge pull request #42827 from twbs/docs/accordion-expand-collapse-all
Document accordion expand/collapse all toggle
2 parents ea61a30 + 85cf704 commit ebf1b48

6 files changed

Lines changed: 131 additions & 3 deletions

File tree

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"llms",
113113
"llmstxt",
114114
"longdesc",
115+
"longhands",
115116
"Lowercased",
116117
"markdownify",
117118
"maxlength",

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Thumbs.db
3737
# Folders to ignore
3838
/dist-sass/
3939
/js/coverage/
40-
/node_modules/
40+
/node_modules
4141

4242
# Site
4343
/site/dist

node_modules

Lines changed: 0 additions & 1 deletion
This file was deleted.

site/src/assets/partials/snippets.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,36 @@ export default () => {
128128
}
129129
// js-docs-end live-alert
130130

131+
// -------------------------------
132+
// Accordion expand / collapse all
133+
// -------------------------------
134+
// js-docs-start accordion-expand-collapse
135+
const accordion = document.getElementById('accordionExpandCollapse')
136+
const toggleBtn = document.getElementById('btnAccordionToggleAll')
137+
138+
if (accordion && toggleBtn) {
139+
const items = accordion.querySelectorAll('.accordion-item')
140+
const groupName = 'accordionExpandCollapse'
141+
142+
toggleBtn.addEventListener('click', () => {
143+
const expand = toggleBtn.getAttribute('aria-expanded') !== 'true'
144+
145+
for (const item of items) {
146+
if (expand) {
147+
item.removeAttribute('name')
148+
item.open = true
149+
} else {
150+
item.open = false
151+
item.setAttribute('name', groupName)
152+
}
153+
}
154+
155+
toggleBtn.setAttribute('aria-expanded', String(expand))
156+
toggleBtn.textContent = expand ? 'Collapse all' : 'Expand all'
157+
})
158+
}
159+
// js-docs-end accordion-expand-collapse
160+
131161
// --------
132162
// Carousels
133163
// --------

site/src/content/docs/components/accordion.mdx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,46 @@ Omit the `name` attribute on each `<details>` element to allow multiple accordio
190190
<!-- More accordion items here -->
191191
</div>`} />
192192

193+
## Expand & Collapse all
194+
195+
To enable expanding and collapsing of all accordion items at once, remove the `name` attribute from each `<details>` element using JavaScript. This allows multiple accordion items to be open simultaneously. Restore the `name` attribute when you collapse the items again if you want exclusive behavior back.
196+
197+
<Example code={`<button type="button" class="btn-subtle btn-sm theme-secondary mb-3" id="btnAccordionToggleAll" aria-expanded="false">Expand all</button>
198+
199+
<div class="accordion accordion-sm" id="accordionExpandCollapse">
200+
<details class="accordion-item" name="accordionExpandCollapse" open>
201+
<summary class="accordion-header">
202+
Accordion Item #1
203+
<svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg>
204+
</summary>
205+
<div class="accordion-body">
206+
<strong>This is the first item’s accordion body.</strong> Use Expand all to open every item, even when they share a <code>name</code>.
207+
</div>
208+
</details>
209+
<details class="accordion-item" name="accordionExpandCollapse">
210+
<summary class="accordion-header">
211+
Accordion Item #2
212+
<svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg>
213+
</summary>
214+
<div class="accordion-body">
215+
<strong>This is the second item’s accordion body.</strong> Collapse all closes every item and restores exclusive <code>name</code> grouping.
216+
</div>
217+
</details>
218+
<details class="accordion-item" name="accordionExpandCollapse">
219+
<summary class="accordion-header">
220+
Accordion Item #3
221+
<svg class="accordion-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><path d="m2 5 6 6 6-6"/></svg>
222+
</summary>
223+
<div class="accordion-body">
224+
<strong>This is the third item’s accordion body.</strong> If your accordion has no <code>name</code>, you only need to set <code>open</code> on each item.
225+
</div>
226+
</details>
227+
</div>`} />
228+
229+
We use the following JavaScript for this demo:
230+
231+
<JsDocs name="accordion-expand-collapse" file="site/src/assets/partials/snippets.js" />
232+
193233
## Nested
194234

195235
Accordions can be nested inside each other by placing a full `.accordion` within an `.accordion-body`. Use unique `name` attribute values for each level so the parent and child accordions operate independently.

site/src/content/docs/utilities/border.mdx

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Or remove borders:
4040

4141
## Width
4242

43-
Border width utilities are available in increments of 1px from 0 to 5px.
43+
Border width utilities are available in increments of 1px from 0 to 5px. These set `border-width` on all sides.
4444

4545
<Example class="bd-example-border-utils" code={`<span class="border border-keyline"></span>
4646
<span class="border border-1"></span>
@@ -49,6 +49,64 @@ Border width utilities are available in increments of 1px from 0 to 5px.
4949
<span class="border border-4"></span>
5050
<span class="border border-5"></span>`} />
5151

52+
### Per-side widths
53+
54+
We do not ship per-side border width utilities. Combine a side utility with a width utility when one side needs a thicker border:
55+
56+
<Example class="bd-example-border-utils" code={`<span class="border-top border-2"></span>
57+
<span class="border-end border-3"></span>
58+
<span class="border-bottom border-4"></span>
59+
<span class="border-start border-5"></span>`} />
60+
61+
### Extend utilities
62+
63+
Extend the border classes with our [utilities API]([[docsref:/utilities/api#add-utilities]]) when you need different widths on different sides, without combining classes as shown above. Here‘s an example using the width longhands instead of the border shorthand so values from `$border-widths` apply cleanly.
64+
65+
```scss
66+
// _my-utilities.scss
67+
@use "sass:map";
68+
@use "bootstrap/scss/config" as *;
69+
@use "bootstrap/scss/utilities";
70+
71+
$border-width-values: map.merge($border-widths, (keyline: var(--border-width-keyline)));
72+
73+
$utilities: map.merge(
74+
$utilities,
75+
(
76+
"border-top-width": (
77+
property: border-block-start-width,
78+
class: border-t,
79+
values: $border-width-values
80+
),
81+
"border-end-width": (
82+
property: border-inline-end-width,
83+
class: border-e,
84+
values: $border-width-values
85+
),
86+
"border-bottom-width": (
87+
property: border-block-end-width,
88+
class: border-b,
89+
values: $border-width-values
90+
),
91+
"border-start-width": (
92+
property: border-inline-start-width,
93+
class: border-s,
94+
values: $border-width-values
95+
)
96+
)
97+
);
98+
```
99+
100+
Load that partial before Bootstrap:
101+
102+
```scss
103+
// my-bootstrap.scss
104+
@use "my-utilities";
105+
@use "bootstrap/scss/bootstrap";
106+
```
107+
108+
This adds classes like `.border-t-5`, `.border-e-5`, `.border-b-5`, and `.border-s-5`. Pair them with `.border` or a side utility such as `.border-bottom` so the border still has a style and color.
109+
52110
## CSS
53111

54112
### Variables

0 commit comments

Comments
 (0)