Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/mjlescano/toggle-parent

Toggle class on parent element.
https://github.com/mjlescano/toggle-parent

Last synced: 22 days ago
JSON representation

Toggle class on parent element.

Awesome Lists containing this project

README

        

# Toggle Parent

Simple component to toggle a class on parentNode of an element. When activated
a backdrop element (`.toggle-parent-backdrop`) is added that will remove the
class when clicked.

## Installation

With [npm](https://github.com/component/component):

```
$ npm install toggle-parent
```

With [component](https://github.com/component/component):

```
$ component install mjlescano/toggle-parent
```

## Example

```html

#dropdown-container.active .dropdown-menu {
display: block;
}

.dropdown-menu {
display: none;
}


Toggle Menu

  • option 1

  • option 2

  • option 3



```

```js
var ToggleParent = require('toggle-parent');

var button = document.querySelector('#dropdown-container button');

// Every time 'button' is pressed, the class 'active' will
// be toggled on '#dropdown-container'
var dropdown = new ToggleParent(button);

// Toggle programatically
dropdown.toggle()
```

## Options
Options that can be passed as a second argument to `new ToggleParent(button, opts)`

* **parent**: Element to toggle the class (Default: button.parentNode)
* **toggleClass**: Class name to be toggled (Default: `active`)

## API
* **.show()**: Add `toggleClass` to `parent`
* **.hide()**: Remove `toggleClass` from `parent`
* **.toggle()**: Toggle `toggleClass` on `parent`
* **.destroy()**: Remove ToggleParent instance, with all the events attached.

## License
MIT