Gathering detailed insights and metrics for headless-context-menu
Gathering detailed insights and metrics for headless-context-menu
Gathering detailed insights and metrics for headless-context-menu
Gathering detailed insights and metrics for headless-context-menu
npm install headless-context-menu
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A versatile and customizable headless context menu React component built with TypeScript, Tailwind CSS, and Zustand. NPM package here.
Install the headless-context-menu
package using Yarn:
1yarn add headless-context-menu
To see an example of usage check the ./example
folder. You can customize the appearance and behavior of the context menu by providing your own markup and styles.
The headless-context-menu
UI kit is built using the following technologies:
Transition
and Dialog
components for handling animations and focus managementThe small UI kit is comprised of a few parts:
ContextMenu: The main component that renders the context menu when the show
state is true
. It uses the Transition
and Dialog
components from Headless UI to handle animations and focus management. The component takes optional className
and backgroundClassName
properties to customize the appearance.
useContextMenu: A Zustand store that manages the state of the context menu, including its visibility, location, and options. The store provides several methods to update the state and clear the context menu.
handleContextMenu: A function that handles the context menu logic when a context menu is requested. It calculates the position of the context menu based on the mouse event and ensures that the menu stays within the viewport. It then updates the useContextMenu
state with the new location and options.
renderOptions: A helper function that takes an array of ContextMenuOption
objects and renders them as either buttons or anchor tags, depending on the provided properties. Each option can have a custom className
for styling.
ContextMenuOption: A type representing an individual option in the context menu. It can be either an object with a name
property and an onClick
callback function, or an object with a name
property and an href
string for navigation. Optionally, you can provide a className
property for custom styling.
ContextMenuState: An interface that defines the shape of the Zustand state for the context menu, which includes properties like show
, location
, and options
, along with methods for updating the state, such as setNewShow
, setNewLocation
, setNewOptions
, and clearContextMenu
.
To use the component in your application, simply import and render the ContextMenu
component, and use the handleContextMenu
function to show the context menu when desired.
1import React from "react"; 2import { ContextMenu, handleContextMenu } from "headless-context-menu"; 3 4const App = () => { 5 // Define your context menu options here 6 const options = [ 7 { 8 name: "Option 1", 9 onClick: () => console.log("Option 1 clicked"), 10 }, 11 { 12 name: "Option 2", 13 href: "https://dextrac.com", 14 }, 15 ]; 16 17 return ( 18 <div> 19 <ContextMenu /> 20 <div 21 onContextMenu={(e) => { 22 e.preventDefault(); 23 handleContextMenu(e, options); 24 }} 25 > 26 Right click me! 27 </div> 28 </div> 29 ); 30}; 31 32export default App;
You can customize the appearance and behavior of the context menu by providing your own styles. See ./example
.
No vulnerabilities found.
No security vulnerabilities found.