Last active
November 25, 2024 20:31
-
-
Save kyrylo/861eb55f7ad166432789ed491d558cde to your computer and use it in GitHub Desktop.
StimulusJS dropdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Author
kyrylo
commented
Nov 25, 2024

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