Skip to content

Commit 9fe658a

Browse files
Docs: add Usage section with JavaScript guide for Accordion component
1 parent 048f56f commit 9fe658a

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

site/content/docs/5.3/components/accordion.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,88 @@ As part of Bootstrap's evolving CSS variables approach, accordions now use local
167167
### Sass variables
168168

169169
{{< scss-docs name="accordion-variables" file="scss/_variables.scss" >}}
170+
171+
## Usage
172+
173+
The accordion plugin utilizes a few classes to handle the heavy lifting:
174+
175+
- `.collapse` hides the content
176+
- `.collapse.show` shows the content
177+
- `.collapsing` is added when the transition starts, and removed when it finishes
178+
179+
These classes can be found in `_transitions.scss`.
180+
181+
### Via data attributes
182+
183+
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`.
184+
185+
To add accordion group management to a collapsible area, add the data attribute `data-bs-parent="#selector"`.
186+
187+
### Via JavaScript
188+
189+
Enable manually with:
190+
191+
```js
192+
const accordionCollapseElementList = document.querySelectorAll('#myAccordion .collapse')
193+
const collapseList = [...accordionCollapseElementList].map(collapseEl => new bootstrap.Collapse(collapseEl))
194+
```
195+
196+
### Options
197+
198+
{{< markdown >}}
199+
{{< partial "js-data-attributes.md" >}}
200+
{{< /markdown >}}
201+
202+
{{< bs-table "table" >}}
203+
| Name | Type | Default | Description |
204+
| --- | --- | --- | --- |
205+
`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. |
206+
`toggle` | boolean | `true` | Toggles the collapsible element on invocation. |
207+
{{< /bs-table >}}
208+
209+
### Methods
210+
211+
{{< callout danger >}}
212+
{{< partial "callouts/danger-async-methods.md" >}}
213+
{{< /callout >}}
214+
215+
Activates your content as a collapsible element. Accepts an optional options `object`.
216+
217+
You can create a collapse instance with the constructor, for example:
218+
219+
```js
220+
const bsCollapse = new bootstrap.Collapse('#myCollapse', {
221+
toggle: false
222+
})
223+
```
224+
225+
{{< bs-table >}}
226+
| Method | Description |
227+
| --- | --- |
228+
| `dispose` | Destroys an element's collapse. (Removes stored data on the DOM element) |
229+
| `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)`. |
230+
| `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)`. |
231+
| `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). |
232+
| `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). |
233+
| `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). |
234+
{{< /bs-table >}}
235+
236+
### Events
237+
238+
Bootstrap's collapse class exposes a few events for hooking into collapse functionality.
239+
240+
{{< bs-table >}}
241+
| Event type | Description |
242+
| --- | --- |
243+
| `hide.bs.collapse` | This event is fired immediately when the `hide` method has been called. |
244+
| `hidden.bs.collapse` | This event is fired when a collapse element has been hidden from the user (will wait for CSS transitions to complete). |
245+
| `show.bs.collapse` | This event fires immediately when the `show` instance method is called. |
246+
| `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). |
247+
{{< /bs-table >}}
248+
249+
```js
250+
const myCollapsible = document.getElementById('myCollapsible')
251+
myCollapsible.addEventListener('hidden.bs.collapse', event => {
252+
// do something...
253+
})
254+
```

0 commit comments

Comments
 (0)