Link Search Menu Expand Document

Netlify Status

This component is available on GitHub.

Stimulus Dropdown

A Stimulus controller to create a dropdown.

Installation

$ yarn add stimulus-dropdown

And use it in your JS file:

import { Application } from "stimulus"
import Dropdown from "stimulus-dropdown"

const application = Application.start()
application.register("dropdown", Dropdown)

This controller uses stimulus-use/use-transition under the hood. You can change the animation behavior as you want.

πŸ‘€ Demo

See Demo

Usage

In your view:

<div data-controller="dropdown" class="relative">
  <button type="button" data-action="dropdown#toggle click@window->dropdown#hide">
    Options
  </button>

  <div
    data-dropdown-target="menu"
    class="hidden transition transform origin-top-right absolute right-0"
    data-transition-enter-from="opacity-0 scale-95"
    data-transition-enter-to="opacity-100 scale-100"
    data-transition-leave-from="opacity-100 scale-100"
    data-transition-leave-to="opacity-0 scale-95"
  >
    <a href="#" data-action="dropdown#toggle">Account settings</a>
    <a href="#" data-action="dropdown#toggle">Support</a>
    <a href="#" data-action="dropdown#toggle">License</a>
  </div>
</div>

TailwindCSS is used in this example but it’s up to you to style the dropdown as you want.

πŸŽ› Extending Controller

You can use inheritance to extend the functionality of any Stimulus component:

import Dropdown from "stimulus-dropdown"

export default class extends Dropdown {
  connect() {
    super.connect()
    console.log("Do what you want here.")
  }

  toggle (event) {
    super.toggle()
  }

  hide (event) {
    super.hide(event)
  }
}

This controller will automatically have access to targets defined in the parent class.

If you override the connect, disconnect or any other methods from the parent, you’ll want to call super.method() to make sure the parent functionality is executed.

πŸ“¦ Development

Project setup

$ yarn install
$ yarn dev

Linter

Prettier and ESLint are responsible to lint and format this component:

$ yarn lint && yarn format

🍻 Support

You can support my work on GitHub Sponsors. πŸ™

πŸ‘·β€β™‚οΈ Contributing

Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.

Don’t forget to drop a 🌟 on GitHub to support the project.

πŸš€ Releasing

$ yarn np

πŸ“ License

This project is released under the MIT license.