1.2.7 • Published 6 months ago

pure-context-menu v1.2.7

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Pure Context Menu

NPM Downloads

How to use

Easily manage the right click context menu.

No additional CSS needed if you are using Bootstrap!

import PureContextMenu from "./pure-context-menu.js";

const items = [
  {
    label: "Click here",
    callback: () => {
      alert("You clicked here");
    },
  },
  "-",
  {
    label: "Target click",
    callback: (e) => {
      if (e.target.id) {
        alert("You clicked on target " + e.target.id);
      } else {
        alert("You didn't click on a target");
      }
    },
  },
  {
    label: "This is a long menu item that spans on two line",
    callback: () => {
      alert("You clicked on a long line");
    },
  },
  {
    label: "This will not close",
    preventCloseOnClick: true,
    callback: () => {
      alert("It didn't close");
    },
  },
];
// bind to html if body does not have 100% height
let bodyMenu = new PureContextMenu(document.querySelector("html"), items);

Features

Built-in styles for Bootstrap

Easy context menu use default Bootstrap styles so you don't need extra css. Otherwise, look at styles.scss to see some default styles you can use. It can either use dropdown or list groups styles.

Prevent close on click

By default, clicking on an item will close the menu. You can control this with preventCloseOnClick.

Determining target

The callback receive the event that originally opened the context menu. This allow determing the target under the context menu. If you need to adjust items based on the current target, use setItems during the show callback.

Dividers

Simply pass "-" in the list of elements to mark dividers. This doesn't work well with list group styles since items are already separated.

Mobile support

Surprisingly, modern mobile browsers translate long press to a contextmenu event that it works out of the box :-)

If it's not working, long-press is supported, simply add it to your pages

<script src="https://cdn.jsdelivr.net/npm/long-press-event@2.4.6/dist/long-press-event.min.js" type="module"></script>

Options

Options can be either passed to the constructor (eg: optionName) or globally updated using PureContextMenu.updateGlobalOptions

NameTypeDescription
contextMenuClassStringClass applied for holder element
dropdownClassStringClass applied for dropdown. Accepts space separated classes
dividerClassStringClass applied to the divider item
menuItemClassStringClass applied to the li in all cases.
itemClassStringClass applied to the menu item. Accepts space separated classes
disabledClassStringClass applied to the disabled items
zIndexNumberz-index assigned to the menu
preventCloseOnClickBooleanGlobal behaviour for items when clicking
useListsBooleanEnable list groups
listClassStringClass applied to the list
listItemClassStringClass applied to the list item. Accepts space separated classes
fastClickBooleanTriggers click on touchstart for mobile devices
closeIfOpenBooleanClose menu with right close if already opened
showfunctionWhether to show menu based on event

Item

NameType
labelString
htmlBoolean
classesArray
preventCloseOnClickBoolean
disabledBoolean
callbackfunction

Demo

https://codepen.io/lekoalabe/pen/LYJbGYg

Browser supports

Modern browsers (edge, chrome, firefox, safari... not IE11). Add a warning if necessary.

1.2.7

6 months ago

1.2.6

6 months ago

1.2.5

8 months ago

1.2.0

10 months ago

1.2.4

8 months ago

1.2.3

10 months ago

1.2.2

10 months ago

1.2.1

10 months ago

1.1.1

1 year ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago