A reusable dropdown menu component
npm install js-simple-dropdown-menuA lightweight, reusable, and customizable dropdown menu component for modern web applications. Zero dependencies, works with any framework, and easy to integrate.
- No dependencies - Pure vanilla JavaScript and CSS
- Simple API - Auto-initializes or use manual control
- Customizable - Hover or click triggers, multiple configuration options
- Accessible - Proper ARIA attributes and keyboard navigation ready
- Mobile-friendly - Touch-optimized with overlay support
- Multiple instances - Manage multiple dropdowns on the same page
npm install js-simple-dropdown-menu- Import the CSS (required):
import "js-simple-dropdown-menu/dist/dropdown.min.css";- Add HTML structure:
<div class="dropdown" data-dropdown>
<button class="dropdown-toggle" data-dropdown-toggle>
Select Option
<span class="dropdown-arrow">▼</span>
</button>
<ul class="dropdown-menu" data-dropdown-menu>
<li><a href="#" class="dropdown-item">Option 1</a></li>
<li><a href="#" class="dropdown-item">Option 2</a></li>
<li><a href="#" class="dropdown-item">Option 3</a></li>
</ul>
</div>- Import JavaScript:
import Dropdown from "js-simple-dropdown-menu";
// That's it! Auto-initializes on page loadimport Dropdown from "js-simple-dropdown-menu";
import "js-simple-dropdown-menu/dist/dropdown.min.css";<link
rel="stylesheet"
href="https://unpkg.com/js-simple-dropdown-menu@latest/dist/dropdown.min.css"
/>
<script type="module">
import Dropdown from "https://unpkg.com/js-simple-dropdown-menu@latest/dist/dropdown.min.js";
</script>const Dropdown = require("js-simple-dropdown-menu");
require("js-simple-dropdown-menu/dist/dropdown.min.css");| Method | Parameters | Description |
|---|---|---|
Dropdown.create() |
selector (string, default: "[data-dropdown]")options (object) |
Initializes all dropdowns matching selector |
Dropdown.closeAll() |
exclude (Dropdown instance) |
Closes all open dropdowns except optional exclude |
Dropdown.destroyAll() |
None | Destroys all dropdown instances and cleans up event listeners |
| Method | Description |
|---|---|
open() |
Opens the dropdown |
close() |
Closes the dropdown |
toggle() |
Toggles the dropdown open/closed |
destroy() |
Removes event listeners and cleans up |
Pass options to Dropdown.create() or when creating instances manually:
// Default options
const options = {
trigger: "click", // 'click' or 'hover'
closeOnClick: true, // Close when clicking menu items
closeOnOutsideClick: true, // Close when clicking outside
};
Dropdown.create("[data-dropdown]", options);import Dropdown from "js-simple-dropdown-menu";
import "js-simple-dropdown-menu/dist/dropdown.min.css";
// Automatically initializes all [data-dropdown] elements on DOMContentLoadedimport Dropdown from "js-simple-dropdown-menu";
// Initialize specific elements only
Dropdown.create(".custom-dropdown", {
trigger: "hover",
closeOnClick: false,
});<!-- Dropdown 1 - Click trigger -->
<div class="dropdown" data-dropdown data-config='{"trigger":"click"}'>
<!-- ... -->
</div>
<!-- Dropdown 2 - Hover trigger -->
<div class="dropdown" data-dropdown data-config='{"trigger":"hover"}'>
<!-- ... -->
</div>// Initialize with different options based on data-config
document.querySelectorAll("[data-dropdown]").forEach((element) => {
const config = element.dataset.config
? JSON.parse(element.dataset.config)
: {};
new Dropdown(element, config);
});import Dropdown from "js-simple-dropdown-menu";
// Get reference to a dropdown instance
const dropdownElement = document.querySelector("[data-dropdown]");
const dropdownInstance = new Dropdown(dropdownElement);
// Open/close programmatically
dropdownInstance.open();
dropdownInstance.close();
// Close all dropdowns
Dropdown.closeAll();The component uses these CSS classes by default:
.dropdown- Container element.dropdown-toggle- Toggle button.dropdown-arrow- Arrow indicator.dropdown-menu- Menu container.dropdown-item- Menu items.show- Added to menu when open (handled by JavaScript).open- Added to arrow when open (handled by JavaScript)
Override the default styles by adding your own CSS:
/* Example custom theme */
.dropdown-toggle {
background-color: #8b5cf6;
border-radius: 8px;
font-weight: bold;
}
.dropdown-menu {
border-radius: 8px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}
.dropdown-item {
padding: 12px 20px;
transition: all 0.2s;
}
.dropdown-item:hover {
background-color: #f5f3ff;
padding-left: 24px;
}<div class="dropdown" data-dropdown>
<button class="dropdown-toggle" data-dropdown-toggle>
Main Menu
<span class="dropdown-arrow">▼</span>
</button>
<ul class="dropdown-menu" data-dropdown-menu>
<li><a href="#" class="dropdown-item">Home</a></li>
<li>
<div class="dropdown" data-dropdown>
<button class="dropdown-toggle dropdown-item" data-dropdown-toggle>
Products
<span class="dropdown-arrow">▶</span>
</button>
<ul class="dropdown-menu" data-dropdown-menu>
<li><a href="#" class="dropdown-item">Product 1</a></li>
<li><a href="#" class="dropdown-item">Product 2</a></li>
</ul>
</div>
</li>
<li><a href="#" class="dropdown-item">Contact</a></li>
</ul>
</div><div class="dropdown" data-dropdown>
<button class="dropdown-toggle" data-dropdown-toggle>
<span>Settings</span>
<span class="dropdown-arrow">▼</span>
</button>
<ul class="dropdown-menu" data-dropdown-menu>
<li>
<a href="#" class="dropdown-item">
<span>🔐</span>
<span>Account</span>
</a>
</li>
<li>
<a href="#" class="dropdown-item">
<span>🔔</span>
<span>Notifications</span>
</a>
</li>
</ul>
</div>-
Dropdown menu not hiding/showing
- Make sure you imported the CSS file
- Check that
.dropdown-menuhasopacity: 0andvisibility: hiddeninitially
-
Clicking doesn't open menu
- Verify you have
data-dropdown-toggleon the button - Check browser console for JavaScript errors
- Ensure CSS is loaded before JavaScript executes
- Verify you have
-
Multiple dropdowns interfere with each other
- Use
Dropdown.closeAll()in your custom logic - Ensure proper event propagation with
e.stopPropagation()
- Use
-
Menu appears in wrong position
- Parent container needs
position: relative - Check for CSS conflicts with other frameworks
- Parent container needs
- Chrome 60+
- Firefox 55+
- Safari 11+
- Edge 79+
# Clone repository
git clone https://github.com/yourusername/js-simple-dropdown-menu.git
# Install dependencies
npm install
# Build for production
npm run build
# Test locally
npm packjs-simple-dropdown-menu/
├── src/
│ ├── dropdown.js # Main JavaScript component
│ └── dropdown.css # Stylesheet
├── dist/ # Built files
├── examples/ # Usage examples
├── package.json
└── README.md
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
MIT © Your Name
- Open an issue on GitHub
- Check the examples directory for more usage patterns
Ready to use? Install now:
npm install js-simple-dropdown-menuNeed help? Check the examples directory or open an issue on GitHub.