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:
// Probably in app/javascript/controllers/index.js
import { Application } from '@hotwired/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.
βοΈ Sponsors
Stimulus Component is an MIT licensed open source project and completely free to use. However, the amount of effort needed to maintain and develop new features for the project is not sustainable without proper financial backing. You can support Stimulus Components development 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.
π License
This project is released under the MIT license.