
Research
/Security News
npm Author Qix Compromised via Phishing Email in Major Supply Chain Attack
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.
@pd200x/custom-dropdown-menu
Advanced tools
A beginner-friendly, lightweight dropdown menu component that's easy to use and customize. Features smart positioning, smooth animations, and style isolation.
Live Demo - See the component in action!
First, install the package in your project:
npm install @pd200x/custom-dropdown-menu
If you're not using npm, you can manually download and include these two essential files:
drop_down_menu.js
- The main component JavaScript filecomponent.css
- The required CSS stylesYou can find these files:
src/js/components/drop_down_menu.js
src/css/component.css
Then include them in your HTML:
<link rel="stylesheet" href="path/to/component.css" />
<script src="path/to/drop_down_menu.js"></script>
<button id="menu-trigger">Click Me</button>
// Import the component
import { CustomDropDownMenu, ActionItem } from "@pd200x/custom-dropdown-menu";
// Create menu items (what happens when each item is clicked)
const menuItems = [
new ActionItem("Save", () => alert("Save clicked")),
new ActionItem("Delete", () => alert("Delete clicked")),
new ActionItem("Share", () => alert("Share clicked")),
];
// Create the dropdown menu
const menu = new CustomDropDownMenu({
menuTitle: "My Menu", // Title at the top of the dropdown
triggerElementID: "menu-trigger", // ID of your button from Step 1
actionItemArray: menuItems, // Menu items from above
height: "200px", // Height of the dropdown
width: "180px", // Width of the dropdown
logEvent: false, // Set to true to see debug logs
});
// IMPORTANT: Always call this to activate the menu
menu.setEventListeners();
That's it! Your dropdown menu is ready to use.
You can easily change how the dropdown menu looks by targeting these CSS classes:
.mycdm-menu-card
- The entire dropdown box.mycdm-menu-title
- The title bar at the top.mycdm-action-group
- The container that holds all menu options (scrollable area).mycdm-action-item
- Each clickable menu item/* Make the dropdown more stylish */
.mycdm-menu-card {
border-radius: 8px !important; /* Rounded corners */
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2) !important; /* Better shadow */
}
/* Give the title a colored background */
.mycdm-menu-title {
background-color: #4a5568 !important;
color: white !important;
font-weight: bold !important;
}
/* Style the container for all menu options */
.mycdm-action-group {
padding: 0 4px !important; /* Add some padding */
background-color: #fafafa !important; /* Light background */
}
/* Make the menu items more interactive */
.mycdm-action-item {
padding: 12px 16px !important;
transition: background-color 0.2s !important;
}
.mycdm-action-item:hover {
background-color: #f7fafc !important;
cursor: pointer !important;
}
You need to use !important
in your CSS rules when customizing the component styles. This is because the component uses its own styles internally, and !important
ensures your custom styles take priority.
setEventListeners()
After creating your menu, you must call the setEventListeners()
method to make it work:
// Create your menu
const menu = new CustomDropDownMenu({
// ...your options here
});
// ALWAYS do this or your menu won't work!
menu.setEventListeners();
This step is essential because it:
Without this step, your menu will be created but won't respond to any user interaction.
import { CustomDropDownMenu, ActionItem } from "@pd200x/custom-dropdown-menu";
// Create and activate a settings menu
const settingsMenu = new CustomDropDownMenu({
menuTitle: "Settings",
triggerElementID: "settings-button",
actionItemArray: [
new ActionItem("Profile", () => openProfilePage()),
new ActionItem("Preferences", () => showPreferences()),
new ActionItem("Logout", () => logoutUser()),
],
height: "180px",
width: "160px",
});
// Don't forget this step!
settingsMenu.setEventListeners();
// First menu - Document options
const documentMenu = new CustomDropDownMenu({
menuTitle: "Document",
triggerElementID: "doc-menu-button",
actionItemArray: [
new ActionItem("New", () => createNewDoc()),
new ActionItem("Open", () => openDocDialog()),
new ActionItem("Save", () => saveCurrentDoc()),
new ActionItem("Print", () => printDoc()),
],
height: "200px",
width: "150px",
});
documentMenu.setEventListeners();
// Second menu - User options
const userMenu = new CustomDropDownMenu({
menuTitle: "User",
triggerElementID: "user-menu-button",
actionItemArray: [
new ActionItem("View Profile", () => viewProfile()),
new ActionItem("Settings", () => openSettings()),
new ActionItem("Sign Out", () => signOut()),
],
height: "160px",
width: "160px",
});
userMenu.setEventListeners();
Option | Type | Required | Description |
---|---|---|---|
menuTitle | string | Yes | Title shown at the top of the menu |
triggerElementID | string | Yes | ID of the HTML element that opens the menu |
actionItemArray | ActionItem[] | Yes | Array of menu items |
height | string | Yes | Height of the menu (e.g., "200px") |
width | string | Yes | Width of the menu (e.g., "180px") |
logEvent | boolean | No | Set to true to see events in console |
Method | What it does |
---|---|
setEventListeners() | Required: Activates the menu (always call this!) |
close() | Manually close the menu (rarely needed) |
This component uses Smooth Scrollbar for better scrolling in long menus.
Works in all modern browsers including:
ISC © Pritam Debnath
FAQs
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.