All Projects → nkappler → ctxmenu

nkappler / ctxmenu

Licence: MIT license
Tiny and customizable context menu generator

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to ctxmenu

ng2-right-click-menu
Right click context menu for Angular 2+
Stars: ✭ 51 (+155%)
Mutual labels:  contextmenu, context-menu, right-click
rctx-contextmenu
✨ Context menu for React
Stars: ✭ 23 (+15%)
Mutual labels:  contextmenu, context-menu, right-click
Printf
Tiny, fast, non-dependent and fully loaded printf implementation for embedded systems. Extensive test suite passing.
Stars: ✭ 1,157 (+5685%)
Mutual labels:  tiny, no-dependencies
Perfectwindows
PerfectWindows 软件家族 - Windows 从未如此完美!
Stars: ✭ 1,326 (+6530%)
Mutual labels:  tiny, easy-to-use
Fat
Web's fastest and most lightweight animation tool.
Stars: ✭ 157 (+685%)
Mutual labels:  standalone, javascript-library
Ngcontextmenu
Handcraft your very own context menus for a richer UX!
Stars: ✭ 81 (+305%)
Mutual labels:  contextmenu, context-menu
Context Menu.ios
You can easily add awesome animated context menu to your app.
Stars: ✭ 1,854 (+9170%)
Mutual labels:  contextmenu, context-menu
Fine Uploader
Multiple file upload plugin with image previews, drag and drop, progress bars. S3 and Azure support, image scaling, form support, chunking, resume, pause, and tons of other features.
Stars: ✭ 8,158 (+40690%)
Mutual labels:  standalone, javascript-library
VirusTotalScanner
Scan suspicious applications with over 60 different anti-viruses with a mere two clicks and five seconds!
Stars: ✭ 18 (-10%)
Mutual labels:  contextmenu, context-menu
PermissionManager
This Library automatically search for permission in androidmanifests file and request for the same
Stars: ✭ 45 (+125%)
Mutual labels:  customizable, easy-to-use
JonContextMenu
A beautiful and minimalist arc menu like the Pinterest one, written in Swift
Stars: ✭ 60 (+200%)
Mutual labels:  contextmenu, context-menu
elm-contextmenu
Flexible context menu for Elm
Stars: ✭ 16 (-20%)
Mutual labels:  contextmenu, context-menu
ember-right-click-menu
An easy and flexible addon to add context menus anywhere in your application
Stars: ✭ 14 (-30%)
Mutual labels:  context-menu, right-click
soloalert
A customizable lightweight Alert Library with Material UI and awesome features.
Stars: ✭ 18 (-10%)
Mutual labels:  customizable, easy-to-use
Open in Windows Terminal
No description or website provided.
Stars: ✭ 24 (+20%)
Mutual labels:  contextmenu, context-menu
Flexsearch
Next-Generation full text search library for Browser and Node.js
Stars: ✭ 8,108 (+40440%)
Mutual labels:  standalone, javascript-library
vue3-context-menu
A very simple context menu component for Vue3 一个简洁美观简单的Vue3右键菜单组件
Stars: ✭ 74 (+270%)
Mutual labels:  contextmenu, context-menu
ContextMenuSwift
A better version of iOS 13 Context Menu
Stars: ✭ 162 (+710%)
Mutual labels:  contextmenu, context-menu
ShellAnything
ShellAnything is a C++ open-source software which allow one to easily customize and add new options to *Windows Explorer* context menu. Define specific actions when a user right-click on a file or a directory.
Stars: ✭ 103 (+415%)
Mutual labels:  contextmenu, context-menu
bulksearch
Lightweight and read-write optimized full text search library.
Stars: ✭ 108 (+440%)
Mutual labels:  standalone, javascript-library

ctxmenu.js Iconnpm npm

Tiny* and customizable context menu generator.

* <3kB minified and gzipped

DEMO

Table of contents

Features
Installation
Menu Definition
Item Types
API
Customize
Contributing

Screenshot

Screenshot

Features

  • Create custom context menus for every browser.
  • Style the context menu with css.
  • No dependencies.
  • Callback to customize based on event properties (Cursor position, etc.)
  • Different menu items: headings, anchors, action items, dividers and submenus
  • Interactive menu items can be disabled

Installation

with npm:

Install ctxmenu

your_project> npm install -s ctxmenu

Import ctxmenu:

import { ctxmenu } from "ctxmenu";

without npm

ctxmenu.js is also available as a standalone version. to use it, just download and link ctxmenu.js or ctxmenu.min.js in your websites header.

<head>
    <!-- ... -->

    <script src="../ctxmenu.js"></script>

</head>

Menu Definition

Menu definitions are used to describe the content of a context menu. A menu definition is an array of objects, where each object defines a single item in the menu.

Example:

Screenshot

var menuDefinition = [
    { text: "Heading" },
    {
        text: "Action Item",
        action: () => alert("Hello World!")
    },
    { isDivider: true },
    {
        text: "Anchor Item",
        href: "",
        disabled: true
    }
]

Item Types

Heading
Anchor
Action Item
Submenu
Divider

Heading Item

This is a heading item which displays a text and optionally shows a tooltip when hovering over it. If you need finer control over the content of the menu item, you can supply your own HTML string by using the html property instead of text. Alternatively you can also supply an HTMLElement JavaScript Object. For all properties you can supply the value directly or a factory function which will be called just before the menu is opened (i.e. on right click). You can also supply a URL or Data URL to an image used as icon for the menu item. Recommended resolution is 18×18px.

{
    text?: string | () => string,
    tooltip?: string | () => string,
    html?: string | () => string,
    element?: HTMLElement | () => HTMLElement,
    icon?: string | () => string,
    style?: string | () => string,
}

NOTE: All other menu items (except the divider item) derive from this and can have at least these properties

Anchor Item

This is an interactive item which implements an anchor tag (<a>) and will redirect to a given URL (href).

{
    /*...Standard Props */

    /** URL */
    href: string | () => string,

    /** https://www.w3schools.com/tags/att_a_target.asp */
    target?: string | () => string,

    /** defaults to false */
    disabled?: boolean | () => boolean
}

Action Item

This is an interactive item which will execute a given callback function when clicked. The callback receives the event as parameter, so you can access the Action Item List Element via e.currentTarget.

{
    /*...Standard Props */

    /** callback fired when the item is clicked */
    action: (event: MouseEvent) => void,

    /** defaults to false */
    disabled?: boolean | () => boolean
}

Submenu Item

This is an interactive item which holds another menu definition. You can create infinitely deep nested submenus.

{
    /*...Standard Props */

    /** Submenu Definition, */
    subMenu: Array | () => Array,       // A menu definition

    /** defaults to false */
    disabled?: boolean | () => boolean  // default false
}

Divider Item

This is a divider item which draws a horizontal line.

{ isDivider: true }

API

This library exports a singleton object ctxmenu. In the standalone version the singleton is a global variable (window.ctxmenu). It has the following five APIs:

attach
update
delete
show
hide

ctxmenu.attach

ctxmenu.attach(target: string, ctxmenu: Array, beforeRender?: (menu: Array, event: MouseEvent) => Array)

The attach method is used to bind a context menu to any DOM Node and takes the following arguments:

  • target: A selector string to define the target node (eg 'body', or '#someID')
  • ctxmenu: An Array of objects defining the menu layout. See Menu Definition.
  • beforeRender?: An optional callback function that is called before the context menu is opened. It is passed two arguments: menu - the menu definition, event - the MouseEvent and needs to return a new menu definition which will be used.

ctxmenu.update

ctxmenu.update(target: string, ctxmenu?: Array, beforeRender?: (menu: Array, event: MouseEvent) => Array)

The update method is used to update an existing context menu. You can update each the menu definition or beforeRender function only by passing undefined for the other argument. If you try to update a menu which does not exist, it will silently be attached instead.

update takes two or three arguments:

  • target - the selector string to define the target element
  • ctxmenu - the updated menu definition. (might be undefined when only updating beforeRender)
  • beforeRender - the updated callback function that is called before the context menu is opened.

ctxmenu.delete

ctxmenu.delete(target: string)

The delete method is used to delete a context menu and only takes the target selector string.

ctxmenu.show

ctxmenu.show(ctxmenu: Array, e: MouseEvent | HTMLElement)

The show method can be used to show a context menu without using the attach method to set up a contextmenu for specific elements first. You need to pass the original event or a target element, which will be used to calculate the menu's position.

This may be useful when integrating with other libraries or frameworks that already provide a contextmenu handler or when trying to show a context menu on a different user interaction (for example showing a context menu when left-clicking a button).

⚠️Positioning of the menu: If the second parameter you pass is of type MouseEvent, the menu will appear at the cursors position, if it is of type HTMLElement it will appear next to the element. See #36

⚠️ Event propagation: When passing a target element, you will need to stop the propagation of the event to prevent the context menu from being immediately closed again:

clickHandler(e: MouseEvent) {
  e.stopPropagation();
  ctxmenu.show([ ... /* menu definition */ ], e.target);
}

ctxmenu.hide

ctxmenu.hide()

Hide any open context menu.

Customize

ctxmenu.js uses the following css classes which you might want to overwrite:

.ctxmenu                /* the main menu div */
.ctxmenu li             /* any menu item */
.ctxmenu li.disabled    /* any disabled menu item */
.ctxmenu li.divider     /* any horizontal divider */
.ctxmenu li.interactive /* any interactive item (anchor, action item, submenu)*/
.ctxmenu li.submenu     /* any menu item that has a submenu */

Contributing

You can find the source code in the src/ directory and the files for manual testing in the test/ directory.

Don't change any JavaScript files manually, as those changes will be overwritten by the build!

Test your changes

Please test your changes before opening a PR. To test your changes locally, run npm run build. This will compile the typescript source files and update the files in the standalone/ and docs/ directories. You can now open the docs/index.html file in your browser to test your changes.

Adding Commits

Currently, the build pipeline updates all javascript files simultaneously, including those used for the demo website and downloads. Those files should be kept in sync with the releases however. Please only commit changes to the typescript source files, if possible. If you need to touch other files as well, please add a note in the PR explaining why. I am working on an update for the build pipeline to address this manual step... 🙂

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].