Skip to content

Instantly share code, notes, and snippets.

@kyrylo
Last active November 25, 2024 20:31
Show Gist options
  • Save kyrylo/861eb55f7ad166432789ed491d558cde to your computer and use it in GitHub Desktop.
Save kyrylo/861eb55f7ad166432789ed491d558cde to your computer and use it in GitHub Desktop.
StimulusJS dropdown
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dropdown with Stimulus</title>
<script type="module">
import { Application, Controller } from "https://cdn.jsdelivr.net/npm/@hotwired/[email protected]/+esm";
const application = Application.start()
class DropdownController extends Controller {
static targets = ["menu"];
toggle() {
this.menuTarget.classList.toggle("menu--hidden");
}
}
application.register("dropdown", DropdownController)
</script>
<style>
body {
font-family: "Lucida Grande";
margin: 0;
}
.mt-40 {
margin: 10rem;
}
.mx-auto {
margin-left: auto;
margin-right: auto;
}
.menu {
position: absolute;
}
.menu--hidden {
display: none;
}
</style>
</head>
<body>
<div class="mt-40 mx-auto" style="width: 400px;">
<h1>Stimulus dropdown</h1>
<div data-controller="dropdown">
<button data-action="dropdown#toggle">
Open menu
</button>
<ul data-dropdown-target="menu" class="menu menu--hidden">
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</div>
</div>
</body>
</html>
@kyrylo
Copy link
Author

kyrylo commented Nov 25, 2024

Screenshot 2024-11-25 at 4 30 53 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment