Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kanety/stimulus-accordion


https://github.com/kanety/stimulus-accordion

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# stimulus-accordion

A stimulus controller for simple accordion menu.

## Dependencies

* @hotwired/stimulus 3.0+

## Installation

Install from npm:

$ npm install @kanety/stimulus-accordion --save

## Usage

Register controller:

```javascript
import { Application } from '@hotwired/stimulus';
import AccordionController from '@kanety/stimulus-accordion';

const application = Application.start();
application.register('accordion', AccordionController);
```

Import css:

```css
@import '@kanety/stimulus-accordion';
```

Build html as follows:

```html


Content 1


content 1



Content 2


content 2




```

### Options

#### store-key

Save opened state to `sessionStorage`:

```html



```

#### opened-text, closed-text

You can change header text of accordion when opened or closed:

```html


Content 1


```

### Callbacks

Run callbacks when a content is opened or closed:

```javascript
let element = document.querySelector('[data-controller="accordion"]');
element.addEventListener('accordion:opened', (e) => {
// e.detail.toggler: clicked element
// e.detail.content: opened content
console.log('opened: ' + e.detail.content.getAttribute('data-accordion-id'));
});
element.addEventListener('accordion:closed', (e) => {
// e.detail.toggler: clicked element
// e.detail.content: closed content
console.log('closed: ' + e.detail.content.getAttribute('data-accordion-id'));
});
```

## License

The library is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).