npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vanilla-context-menu

v1.6.0

Published

Easily create context menus using vanilla JavaScript and integrate them in any web application

Downloads

1,596

Readme

Vanilla Context Menu

vanilla-context-menu - easily create context-menus using Vanilla JavaScript and integrate them in any web application

Installation

Browser CDN

<script src="https://unpkg.com/[email protected]/dist/vanilla-context-menu.js"></script>

Where @1.4.1 is the version that you want to use.

Then anywhere in your JavaScript code you can access the library with window.VanillaContextMenu or simply VanillaContextMenu.

Via NPM

npm i vanilla-context-menu

Then anywhere in your code.

import VanillaContextMenu from 'vanilla-context-menu';

How to use it

new VanillaContextMenu({
  scope: document.querySelector('main'),
  menuItems: [
    {
      label: 'Copy',
      callback: () => {
        // your code here
      },
    },
    'hr',
    {
      label: 'Paste',
      callback: pasteFunction,
    },
    {
      label: 'Cut',
      callback: pasteFunction,
      iconClass: 'fa fa-scissors', // this only works if you have FontAwesome icons
    },
    { label: 'Face', iconHTML: `<span class="material-icons">face</span>` // this only works if you have Google Material Icons icons },
  ],
});

Configuration options

VanillaContextMenu(configurableOptions: ConfigurableOptions):VanillaContextMenu

ConfigurableOptions

| Option | Required | Type | Default | Description | | :-----------------: | :------: | :----------------: | :-------: | :-----------------------------------------------------------------------------------------------------------------------------------------------: | | scope | yes | HTMLElement | undefined | The HTML element on which you want to bind the contextmenu event listener. | | menuItems | yes | MenuItem[] | undefined | Menu items to be built. | | customClass | no | string | undefined | A custom CSS class that can be added to the context menu | | customThemeClass | no | string | undefined | A custom CSS class that can be added to the context menu theme. A value for this property will exclude the theme option. | | preventCloseOnClick | no | boolean | false | If this variable is true, then the context menu will not close when its elements are clicked. | | transitionDuration | no | number | 200 | Duration of the context menu transition. Set this value to 0 if you want to disable the animation. | | theme | no | 'black' | 'white' | black | By default, the library provides two themes for the context menu: black and white. You can use this option to choose the one you want to use. | | normalizePosition | no | boolean | true | If true, the position of the contextmenu is bound to the scope. Otherwise the left top corner of the contextmenu is bound to the mouse position. |

MenuItem

type MenuItem = MenuOption | 'hr';

MenuOption | Option | Required | Type | Default | Description | |:-------------------:|:--------:|:---------:|:---------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------:| | label | yes | string | undefined | Menu option label. | | iconClass | no | string | undefined | This property can be used to display an optional icon. It presents the CSS classes that will be added for the <i></i> tag. | | iconHTML | no | string | undefined | This property can be used to display an optional icon. It presents an HTML string that will be sanitized internally using DOMPurify. | | callback | no | (ev:MouseEvent) => any | undefined | Callback to be executed. The parameter ev is the MouseEvent that occurred when the contextmenu event was triggered | | preventCloseOnClick | no | boolean | false | If this variable is true, then the context menu will not close when this menu option is clicked. A value set for this option, either true or false will override the global one. | | nestedMenu | no | NestedMenuItem[] | undefined | Nested menu to be built.

NestedMenuItem

export type NestedMenuItem = BaseMenuOption | 'hr';
export interface BaseMenuOption {
  label: string;
  callback?: (ev: MouseEvent) => unknown;
  iconClass?: string;
  iconHTML?: string;
  preventCloseOnClick?: boolean;
}

API (3)

The following methods and properties are available through the class instance.

const myContextMenu = new VanillaContextMenu(...)

(1)

off(): void

This method will remove all event listeners that have been registered for the context-menu.

! It should be called when you want to deactivate the context menu or when the container item has been removed from the DOM.

(2)

updateOptions(configurableOptions: Partial<ConfigurableOptions>): void

(3)

close(): void

This method closes the context-menu.

Examples

Define your own theme

.context-menu-orange-theme {
  background: #d35400;

  hr {
    background-color: #eee;
  }

  // text color for each item
  & > *:not(hr) {
    color: #eee;

    &:hover {
      background: #e67e22;
    }
  }
}

Define your own CSS class for styling

.custom-context-menu-cls {
  width: 100px !important;
  font-family: 'Roboto', sans-serif; /* DEFAULT -- font-family: 'Open Sans', sans-serif; */
}
const myContextMenu = new window.VanillaContextMenu({
  scope: ...,
  menuItems: [...],
  customThemeClass: 'context-menu-orange-theme',
  customClass: 'custom-context-menu-cls',
});

Add icons for your menu itmes

Firstly you need to add an icon library inside your application and then you can use the iconClass property to specify the CSS classes that will be added for the <i></i> tag.

The following example will add a FontAwesome scissors icon near the menu option Cut.

new VanillaContextMenu({
  scope: document.querySelector('main'),
  menuItems: [
    {
      label: 'Cut',
      callback: pasteFunction,
      iconClass: 'fa fa-scissors', // this only works if you have FontAwesome icons
    },
  ],
});

You can check the demo file for more examples from demo/index.html.

Contributing

Pull requests and stars are always welcome. Please check the guidelines.

Changelog

For project updates you can also reference the changelog.

Stay in touch

Discussions page

License

This project is licensed under the MIT License