easycontext
v1.0.2
Published
Simple context menu for the web.
Downloads
51
Maintainers
Readme
Table of Contents
Install
NPM:
npm install easycontext
CDN:
<script src="https://unpkg.com/easycontext"></script>
Usage
Import as module:
import { contextmenu } from 'easycontext';
Or when using the CDN:
const { contextmenu } = easycontext;
Use
Use contextmenu
for selecting one or more HTMLElements to append a context menu to.
You can it one ore more HTMLElements, a selector string or a NodeList. It uses Universal Element Accept under the hood.
Example:
contextmenu('#my-button', [
{
text: 'This is a button',
onClick() {
console.log('Do something');
},
},
]);
// you can also use a funtion to dynamically generate menu items:
contextmenu('button', (target) => {
return target.classList.contains('removable')
? [
{
text: 'Remove',
onClick() {
target.parentElement.removeChild(target);
},
},
]
: [
{
text: 'This is not clickable',
},
];
});
// you can also just add a context menu to everything
contextmenu(document, [
{
text: 'Reload page',
onClick() {
window.location.reload();
},
},
]);
Doc:
/**
* Creates a context menu for given element(s) / selector string
* @param element Single HTMLElement, Array or query selector string (using @compactjs/uea)
* @param items Menu items or function that returns menu items
* @param options Additional Options
*/
function contextmenu(
element: string | HTMLElement | HTMLElement[] | HTMLCollection | NodeList,
items: MenuItem[] | ((target: HTMLElement) => MenuItem[]),
options?: MenuOptions
): void;
interface MenuItem {
/**
* Menu item text, can also be HTML
*/
text?: string;
/**
* A custom classname
*/
className?: string;
/**
* Function that is called when item is clicked.
* Not clickable, when omitted.
*/
onClick?: (event: Event) => void;
}
interface MenuOptions {
/**
* Parent element for the menu element to append to
* @default document.body
*/
parentElement: HTMLElement;
/**
* Document Root, internaly used for adding a click listener to hide context menu.
* @default document
*/
root: Document | HTMLElement;
/**
* Custom class name for context menu
* @default 'context-menu'
*/
className: string;
}
Run tests
npm run test
Contact
👤 Timo Bechtel
- Website: https://timobechtel.com
- Twitter: @TimoBechtel
- GitHub: @TimoBechtel
🤝 Contributing
Contributions, issues and feature requests are welcome!
- Check issues
- Fork the Project
- Create your Feature Branch (
git checkout -b feat/AmazingFeature
) - Test your changes
npm run test
- Commit your Changes (
git commit -m 'feat: add amazingFeature'
) - Push to the Branch (
git push origin feat/AmazingFeature
) - Open a Pull Request
Commit messages
This project uses semantic-release for automated release versions. So commits in this project follow the Conventional Commits guidelines. I recommend using commitizen for automated commit messages.
Show your support
Give a ⭐️ if this project helped you!
📝 License
Distributed under the MIT License.
This README was generated with ❤️ by readme-md-generator