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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ol-contextmenu

v6.0.0

Published

Custom Context Menu for Openlayers

Readme

🗺️ OpenLayers Context Menu

A customizable, feature-rich context menu extension for OpenLayers maps

Quick Links: DemoInstallationQuick StartAPITypeScriptExamples


✨ Features

  • 🎯 Easy Integration - Works seamlessly with OpenLayers 7.x - 10.x
  • 🎨 Fully Customizable - Control width, icons, styles, and appearance
  • 🔗 Nested Submenus - Unlimited nesting levels for complex menu structures
  • 📦 Zero CSS Dependencies - Styles are bundled inline (since v6.0)
  • 🎪 Event-Driven - React to beforeopen, open, and close events
  • 🌐 Multiple Trigger Types - Context menu, click, or double-click
  • 📱 Viewport-Aware - Automatically repositions to stay visible at screen edges
  • 💪 TypeScript Support - Full type definitions included out of the box
  • Lightweight - Minimal footprint with only one dependency (tiny-emitter)
  • Well Tested - 191 tests covering all functionality

🎪 Demo

Try it live:

📦 Installation

npm (Recommended):

npm install ol ol-contextmenu

CDN:

<script src="https://cdn.jsdelivr.net/npm/ol-contextmenu"></script>

Requires: OpenLayers 7.0.0 or higher

📖 See Getting Started Guide for detailed installation instructions and setup.

🚀 Quick Start

import ContextMenu from 'ol-contextmenu';

const contextmenu = new ContextMenu({
    width: 170,
    defaultItems: true, // Includes Zoom In/Zoom Out
    items: [
        {
            text: 'Center map here',
            callback: (obj, map) => {
                map.getView().setCenter(obj.coordinate);
            },
        },
        {
            text: 'Add a Marker',
            icon: 'img/marker.png',
            callback: addMarker,
        },
        '-', // Separator
    ],
});

map.addControl(contextmenu);

📖 See Getting Started Guide for complete setup instructions and Examples for advanced patterns.

📖 Documentation

📘 TypeScript

Full TypeScript support with comprehensive type definitions included:

import ContextMenu, { type Item } from 'ol-contextmenu';

const items: Item[] = [
    {
        text: 'Center map here',
        callback: (obj, map) => {
            map.getView().setCenter(obj.coordinate);
        },
    },
];

const contextmenu = new ContextMenu({ width: 170, items });

📖 See the TypeScript Guide for complete type definitions and usage patterns.

🎯 API

Constructor

new ContextMenu(options)

Options:

  • width (number, default: 150) - Menu width in pixels
  • defaultItems (boolean, default: true) - Include default Zoom In/Out items
  • items (Item[], default: []) - Array of menu items
  • eventType (string, default: 'contextmenu') - Event type to trigger menu

Methods

  • clear() - Remove all items
  • close() - Close menu programmatically
  • extend(items) - Add multiple items
  • push(item) - Add single item
  • pop() / shift() - Remove items
  • getDefaultItems() - Get default items array
  • isOpen() - Check if menu is open
  • enable() / disable() - Control menu state

Events

  • beforeopen - Fired before menu opens
  • open - Fired when menu opens
  • close - Fired when menu closes

📖 See API Reference for complete documentation with examples.

🎨 Examples

🔧 Troubleshooting

Common issues:

  • Menu doesn't appear: Ensure map.addControl(contextmenu) is called
  • Menu cut off: Update to v5.5.0+ for automatic viewport positioning
  • TypeScript errors: Use import ContextMenu, { type Item } from 'ol-contextmenu'

📖 See the Troubleshooting Guide for detailed solutions and common issues.

🌍 Browser Support

Modern browsers supporting ES6+:

  • Chrome/Edge (latest)
  • Firefox (latest)
  • Safari (latest)

🤝 Contributing

Contributions are welcome! We appreciate:

  • 🐛 Bug reports
  • 💡 Feature requests
  • 📝 Documentation improvements
  • ✨ Code contributions

Please read our Contributing Guidelines before submitting a PR.

Development Setup

# Clone the repository
git clone https://github.com/jonataswalker/ol-contextmenu.git
cd ol-contextmenu

# Install dependencies
npm install

# Run tests
npm test

# Start development server
npm run dev

# Build for production
npm run build

📄 License

MIT © Jonatas Walker

🙏 Acknowledgments

Built with ❤️ for the OpenLayers community.

Special thanks to all contributors who have helped improve this project.