The short-form tutorial below represents how to pair Rails UI, Stimulus.js, and Ruby on Rails together to achieve a dropdown pattern using a set of elements working together.
Stimulus.js, stimulus-use, and railsui-stimulus are already installed as dependencies of the Hound theme.
1. Initializing a dropdown
Use the 'data-controller="dropdown" attribute to initialize a new dropdown component using Stimulus.js.
Be sure the controller is registered inside app/javascript/controllers/index.js. (This should already be the case when you installed the Hound theme.)
2. Triggering a dropdown
To trigger a dropdown to appear you will need some form of actionable target like a button or a element. Append the following attributes to your target. data-action="railsui-dropdown#toggle click@window->railsui-dropdown#hide" (see the base example below for a complete concept).
3. Defining the menu
Each dropdown has a corresponding menu that gets hidden by default. The menu requires the following attribute to function data-railsui-dropdown-target="menu". The element the target is appended to also requires the class name class='hidden' to be present.
4. Adding transitions and effects
Stimulus.js doesn't provide help in the context of animation and transitions so we reached for stimulus-use to help.
Using the library you can leverage data attributes to add specific effects provided by Tailwind CSS classes at different states of a dropdown transition.