Skip to content

Commit c7829b5

Browse files
Docs: add Usage section with JavaScript guide for Accordion component
Co-authored-by: Tommaso Allegretti <tommasoallegretti@users.noreply.github.com>
1 parent ed36faa commit c7829b5

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,84 @@ As part of Bootstrap’s evolving CSS variables approach, accordions now use loc
157157
### Sass variables
158158

159159
<ScssDocs name="accordion-variables" file="scss/_variables.scss" />
160+
161+
## Usage
162+
163+
The collapse plugin utilizes a few classes to handle the heavy lifting:
164+
165+
- `.collapse` hides the content
166+
- `.collapse.show` shows the content
167+
- `.collapsing` is added when the transition starts, and removed when it finishes
168+
169+
These classes can be found in `_transitions.scss`.
170+
171+
### Via data attributes
172+
173+
Just add `data-bs-toggle="collapse"` and a `data-bs-target` to the element to automatically assign control of one or more collapsible elements. The `data-bs-target` attribute accepts a CSS selector to apply the collapse to. Be sure to add the class `collapse` to the collapsible element. If you’d like it to default open, add the additional class `show`.
174+
175+
To add accordion group management to a collapsible area, add the data attribute `data-bs-parent="#selector"`.
176+
177+
### Via JavaScript
178+
179+
Enable manually with:
180+
181+
```js
182+
const accordionCollapseElementList = document.querySelectorAll('#myAccordion.collapse')
183+
const accordionCollapseList = [...accordionCollapseElementList].map(accordionCollapseEl => new bootstrap.Collapse(accordionCollapseEl))
184+
```
185+
186+
### Options
187+
188+
<JsDataAttributes />
189+
190+
<BsTable>
191+
| Name | Type | Default | Description |
192+
| --- | --- | --- | --- |
193+
`parent` | selector, DOM element | `null` | If parent is provided, then all collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior - this is dependent on the `card` class). The attribute has to be set on the target collapsible area. |
194+
`toggle` | boolean | `true` | Toggles the collapsible element on invocation. |
195+
</BsTable>
196+
197+
### Methods
198+
199+
<Callout name="danger-async-methods" type="danger" />
200+
201+
Activates your content as a collapsible element. Accepts an optional options `object`.
202+
203+
You can create a collapse instance with the constructor, for example:
204+
205+
```js
206+
const bsCollapse = new bootstrap.Collapse('#myCollapse', {
207+
toggle: false
208+
})
209+
```
210+
211+
<BsTable>
212+
| Method | Description |
213+
| --- | --- |
214+
| `dispose` | Destroys an element’s collapse. (Removes stored data on the DOM element) |
215+
| `getInstance` | Static method which allows you to get the collapse instance associated to a DOM element, you can use it like this: `bootstrap.Collapse.getInstance(element)`. |
216+
| `getOrCreateInstance` | Static method which returns a collapse instance associated to a DOM element or create a new one in case it wasn’t initialized. You can use it like this: `bootstrap.Collapse.getOrCreateInstance(element)`. |
217+
| `hide` | Hides a collapsible element. **Returns to the caller before the collapsible element has actually been hidden** (e.g., before the `hidden.bs.collapse` event occurs). |
218+
| `show` | Shows a collapsible element. **Returns to the caller before the collapsible element has actually been shown** (e.g., before the `shown.bs.collapse` event occurs). |
219+
| `toggle` | Toggles a collapsible element to shown or hidden. **Returns to the caller before the collapsible element has actually been shown or hidden** (i.e. before the `shown.bs.collapse` or `hidden.bs.collapse` event occurs). |
220+
</BsTable>
221+
222+
### Events
223+
224+
Bootstrap’s collapse class exposes a few events for hooking into collapse functionality.
225+
226+
<BsTable>
227+
| Event type | Description |
228+
| --- | --- |
229+
| `hide.bs.collapse` | This event is fired immediately when the `hide` method has been called. |
230+
| `hidden.bs.collapse` | This event is fired when a collapse element has been hidden from the user (will wait for CSS transitions to complete). |
231+
| `show.bs.collapse` | This event fires immediately when the `show` instance method is called. |
232+
| `shown.bs.collapse` | This event is fired when a collapse element has been made visible to the user (will wait for CSS transitions to complete). |
233+
</BsTable>
234+
235+
```js
236+
const myCollapsible = document.getElementById('myCollapsible')
237+
myCollapsible.addEventListener('hidden.bs.collapse', event => {
238+
// do something...
239+
})
240+
```

0 commit comments

Comments
 (0)