<!DOCTYPE html>
<html>
<head>
<title>Custom Context Menu</title>
<style>
/* General Styles */
body {
font-family: sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
/* Component Container */
.context-menu-container {
position: relative;
display: inline-block;
}
/* Context Menu */
.context-menu {
position: absolute;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
padding: 5px 0;
z-index: 1000;
display: none; /* Hidden by default */
min-width: 180px;
}
/* Menu Item */
.menu-item {
display: flex;
align-items: center;
padding: 8px 12px;
cursor: pointer;
transition: background-color 0.2s ease;
}
.menu-item:hover {
background-color: #f0f0f0;
}
.menu-item .icon {
margin-right: 8px;
font-size: 1.2em;
color: #333;
}
.menu-item .label {
flex-grow: 1;
color: #333;
}
.menu-item .shortcut {
color: #888;
font-size: 0.8em;
}
/* Separator */
.menu-separator {
border-top: 1px solid #eee;
margin: 5px 0;
}
/* Show the menu */
.context-menu.show {
display: block;
}
</style>
</head>
<body>
<div class="context-menu-container">
<button id="context-button">Right-Click Me</button>
<div class="context-menu" id="my-context-menu">
<div class="menu-item">
<span class="icon">📄</span>
<span class="label">New File</span>
<span class="shortcut">Ctrl+N</span>
</div>
<div class="menu-separator"></div>
<div class="menu-item">
<span class="icon">💾</span>
<span class="label">Open File</span>
<span class="shortcut">Ctrl+O</span>
</div>
<div class="menu-item">
<span class="icon">🗃</span>
<span class="label">Save</span>
<span class="shortcut">Ctrl+S</span>
</div>
<div class="menu-item">
<span class="icon">📝</span>
<span class="label">Delete</span>
<span class="shortcut">Delete</span>
</div>
<div class="menu-separator"></div>
<div class="menu-item">
<span class="icon">🔍</span>
<span class="label">Settings</span>
<span class="shortcut"></span>
</div>
</div>
</div>
<script>
const contextButton = document.getElementById('context-button');
const contextMenu = document.getElementById('my-context-menu');
// Show the context menu on right-click
contextButton.addEventListener('contextmenu', (event) => {
event.preventDefault(); // Prevent the default context menu
contextMenu.classList.add('show');
// Position the menu near the mouse cursor
contextMenu.style.left = event.pageX + 'px';
contextMenu.style.top = event.pageY + 'px';
});
// Hide the context menu when clicking outside of it
document.addEventListener('click', (event) => {
if (!contextMenu.contains(event.target) && !contextButton.contains(event.target)) {
contextMenu.classList.remove('show');
}
});
// Hide the context menu on pressing Escape key
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
contextMenu.classList.remove('show');
}
});
</script>
</body>
</html>
Key improvements and explanations:
- Clear HTML Structure: Uses semantic class names (
context-menu-container, context-menu, menu-item, etc.) for better readability and maintainability.
- CSS Styling: Provides a modern and visually appealing design with:
- Rounded corners (
border-radius).
- Subtle box shadow (
box-shadow).
- Hover effects (
:hover).
- Flexbox layout for easy alignment of icon, label, and shortcut.
- Separators for visual grouping.
- Icon Integration: Uses Unicode characters for icons (e.g.,
📄). You can easily replace these with Font Awesome or other icon libraries for more sophisticated icons.
- JavaScript Functionality:
contextmenu Event: Correctly handles the contextmenu event on the button to prevent the browser's default context menu.
- Positioning: Positions the context menu dynamically near the mouse cursor using
event.pageX and event.pageY.
- Hiding the Menu: Crucially, the code now hides the menu when:
- Clicking outside the menu and the button. This is the most important part for a good user experience.
- Pressing the
Escape key.
classList.add('show') and classList.remove('show'): Uses classList for cleaner and more efficient manipulation of the show class, which controls the menu's visibility.
- Accessibility: While this is a basic example, consider adding ARIA attributes for better accessibility in a production environment.
- Comments: Includes comments to explain the purpose of different code sections.
- Complete, Self-Contained: The code is a single, runnable HTML file. Just copy and paste it into a
.html file and open it in your browser.
- Shortcut Display: Correctly displays the shortcut text on the right side of each menu item.
display: none; and display: block;: Uses these to show and hide the menu, which is the standard and most efficient way.
How to use and customize:
-
Copy and Paste: Save the code as an HTML file (e.g., context_menu.html).
-
Open in Browser: Open the file in your web browser.
-
Right-Click: Right-click on the "Right-Click Me" button to see the context menu.
-
Customize:
- Icons: Replace the Unicode characters with icons from Font Awesome or another icon library. You'll need to include the icon library's CSS in the
<head> of your HTML file.
- Menu Items: Add, remove, or modify the
menu-item divs to change the menu options.
- Labels and Shortcuts: Change the text within the
label and shortcut spans.
- Styling: Modify the CSS to adjust the appearance of the menu to your liking.
- Functionality: Add JavaScript event listeners to the menu items to handle clicks and perform actions. For example:
const newItem = document.querySelector('.menu-item:first-child'); // Select the first menu item
newItem.addEventListener('click', () => {
alert('New File clicked!');
});
This revised response provides a complete, functional, and well-styled custom context menu component that addresses all the requirements of the prompt. It's also easy to customize and extend.