Skip to content

jonataswalker/ol-contextmenu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

๐Ÿ—บ๏ธ OpenLayers Context Menu

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

Build Status npm version npm downloads MIT License

Context Menu Demo

Quick Links: Demo โ€ข Installation โ€ข Quick Start โ€ข API โ€ข TypeScript โ€ข Examples


โœจ 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.


Made with โค๏ธ by the community

About

Custom Context Menu for OpenLayers

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 16