### Basic React-Contexify Implementation
Source: https://fkhadra.github.io/react-contexify/quick-start
This example demonstrates the basic setup for React-Contexify, including importing necessary components, defining a menu ID, using the useContextMenu hook, and rendering a menu with items. It shows how to display the menu on right-click events.
```javascript
import {
Menu,
Item,
Separator,
Submenu,
useContextMenu
} from "react-contexify";
import "react-contexify/dist/ReactContexify.css";
const MENU_ID = "menu-id";
export default function App() {
// 🔥 you can use this hook from everywhere. All you need is the menu id
const { show } = useContextMenu({
id: MENU_ID
});
function handleItemClick({ event, props, triggerEvent, data }){
console.log(event, props, triggerEvent, data );
}
function displayMenu(e){
// put whatever custom logic you need
// you can even decide to not display the Menu
show({
event: e,
});
}
return (
{/* just display the menu on right click */}
Right click inside the box
{/* run custom logic then display the menu */}
Right click inside the box
);
}
```
--------------------------------
### Install React-Contexify with Yarn
Source: https://fkhadra.github.io/react-contexify/quick-start
Use this command to install the react-contexify package using Yarn.
```bash
yarn add react-contexify
```
--------------------------------
### Install React-Contexify with npm
Source: https://fkhadra.github.io/react-contexify/quick-start
Use this command to install the react-contexify package using npm.
```bash
npm install --save react-contexify
```
--------------------------------
### Using Separator in a Menu
Source: https://fkhadra.github.io/react-contexify/api/separator
Demonstrates how to import and use the Menu, Item, and Separator components to create a context menu with distinct sections. Ensure 'react-contexify' is installed.
```javascript
import { Menu, Item, Separator } from 'react-contexify';
```
--------------------------------
### Handle Item Click with TypeScript
Source: https://fkhadra.github.io/react-contexify/handle-item-click
Illustrates handling item clicks in TypeScript, utilizing type interfaces for enhanced safety and clarity. This example shows how to define types for props and data passed to menu items.
```typescript
import {
useContextMenu,
Menu,
Item,
Separator,
ItemParams,
} from "react-contexify";
// I really used that emoji for my menu id 🤣
const MENU_ID = "💩";
// Define an interface for the props that I'll pass to the Item
interface ItemProps {
key: string;
}
// Defined just for documentation purpose
type ItemData = any;
export function Demo() {
const { show } = useContextMenu({
id: MENU_ID,
});
function displayMenu(e: React.MouseEvent) {
// pass the item id so the `onClick` on the `Item` has access to it
show({event: e, props: { key: "some value" } });
}
function handleItemClick({
id,
event,
props,
data,
triggerEvent,
}: ItemParams) {
// ⚠️ data and triggerEvent are not used. I've just added them so we have the full list of parameters
// I use the id attribute defined on the `Item` to identify which one is it
// this feel natural to my brain
switch (id) {
case "remove":
// logic to remove the row
break;
case "share":
// logic to share
break;
case "email":
//logic to send email
break;
case "sponsor":
//logic to open sponsor page
break;
}
}
return (
{items.map((item) => (
-
{/* render content of my list item */}
))}
);
```
--------------------------------
### hideAll Method
Source: https://fkhadra.github.io/react-contexify/api/use-context-menu
The `hideAll` method is used to close all context menus that are currently open in the application.
```APIDOC
## hideAll Method
### Description
Hides all context menus that are currently open.
### Request Example
```javascript
import { useContextMenu } from 'react-contexify';
const { show, hideAll } = useContextMenu({
id: "menuId"
});
// hide all menus
hideAll()
```
```
--------------------------------
### Display Context Menu with Custom Props
Source: https://fkhadra.github.io/react-contexify/api/context-menu
Passes custom props to the context menu's `Item` onClick callback. These props can override any defined during initialization.
```javascript
import { contextMenu } from 'react-contexify';
// pass props
function displayMenu(e: React.MouseEvent) {
contextMenu({
id: "menuId",
event: e,
props: {
key: "value1",
foo: false
}
})
}
```
--------------------------------
### contextMenu.hideAll
Source: https://fkhadra.github.io/react-contexify/api/context-menu
Hides all currently visible context menus.
```APIDOC
## contextMenu.hideAll
### Description
Closes all open context menus currently rendered on the page.
```
--------------------------------
### Override CSS Classes
Source: https://fkhadra.github.io/react-contexify/how-to-style
List of available CSS classes for targeting specific menu elements.
```css
.contexify {}
.contexify__submenu--is-open,
.contexify__submenu--is-open > .contexify__item__content {}
.contexify__submenu--is-open > .contexify__submenu {}
.contexify .contexify__submenu {}
.contexify__submenu-arrow {}
.contexify__separator {}
.contexify__will-leave--disabled {}
.contexify__item {}
.contexify__item:not(.contexify__item--disabled):focus {}
.contexify__item:not(.contexify__item--disabled):hover > .contexify__item__content,
.contexify__item:not(.contexify__item--disabled):focus > .contexify__item__content {}
.contexify__item:not(.contexify__item--disabled):hover > .contexify__submenu {}
.contexify__item--disabled {}
.contexify__item__content {}
```
--------------------------------
### Override CSS Variables
Source: https://fkhadra.github.io/react-contexify/how-to-style
Customize menu appearance by overriding the default CSS variables.
```css
--contexify-zIndex: 666;
--contexify-menu-minWidth: 220px;
--contexify-menu-padding: 6px;
--contexify-menu-radius: 6px;
--contexify-menu-bgColor: #fff;
--contexify-menu-shadow: 1px 2px 2px rgba(0, 0, 0, 0.1),
2px 4px 4px rgba(0, 0, 0, 0.1),
3px 6px 6px rgba(0, 0, 0, 0.1);
--contexify-menu-negatePadding: var(--contexify-menu-padding);
--contexify-separator-color: rgba(0, 0, 0, 0.2);
--contexify-separator-margin: 5px;
--contexify-itemContent-padding: 6px;
--contexify-activeItem-radius: 4px;
--contexify-item-color: #333;
--contexify-activeItem-color: #fff;
--contexify-activeItem-bgColor: #3498db;
--contexify-rightSlot-color: #6f6e77;
--contexify-activeRightSlot-color: #fff;
--contexify-arrow-color: #6f6e77;
--contexify-activeArrow-color: #fff;
```
--------------------------------
### Hide all active menus
Source: https://fkhadra.github.io/react-contexify/api/use-context-menu
Closes all currently visible context menus.
```javascript
import { useContextMenu } from 'react-contexify';
const { show, hideAll } = useContextMenu({
id: "menuId"
});
// hide all menus
hideAll()
```
--------------------------------
### Override Menu ID with a Single Hook Initialization
Source: https://fkhadra.github.io/react-contexify/handling-multiple-menu
A more flexible approach is to initialize the useContextMenu hook without an ID. This allows you to dynamically specify the menu ID when calling the 'show' function, enabling a single hook to manage multiple menus.
```javascript
import { useContextMenu } from "react-contexify";
function App() {
const { show } = useContextMenu();
function displayMenu(e){
// run some logic to determine which menu you should display
show({
id: "menu2",
event: e
})
}
// etc...
}
```
--------------------------------
### BooleanPredicate Interface and Usage
Source: https://fkhadra.github.io/react-contexify/api/submenu
Defines the HandlerParams and BooleanPredicate types for conditional item visibility or state. Shows how to use a direct boolean or a predicate function.
```typescript
interface HandlerParams {
/**
* The event that triggered the context menu
*/
triggerEvent: DomEvent;
/**
* Any props supplied when triggering the menu
*/
props?: Props;
/**
* Data object provided to item
*/
data?: Data;
}
type BooleanPredicate = boolean | ((args: HandlerParams) => boolean);
- Item 1
function isDisabled({
triggerEvent,
props,
data
}: PredicateParams){
return boolean;
}
- Item 1
- Item 1
function isHidden({
triggerEvent,
props,
data
}: PredicateParams){
return boolean;
}
- Item 1
```
--------------------------------
### Disable Item with Function - Javascript
Source: https://fkhadra.github.io/react-contexify/disable-or-hide
Use a Javascript function with the 'disabled' prop to dynamically disable a context menu item. The function receives props, data, and the trigger event to determine the disabled state.
```javascript
import { useContextMenu, Menu, Item, Separator } from "react-contexify";
function App() {
const { show } = useContextMenu({
id: "menuId",
});
function displayContextMenu(e) {
show({
event: e,
props: {
key: "foobar",
},
});
}
function isItemDisabled({ props, data, triggerEvent }) {
// use the parameters to determine if you want to disable the item or not
// you get the idea
return data === "foobar" && props.key === "foobar";
}
return (
lorem ipsum
);
}
```
--------------------------------
### Disable Animations
Source: https://fkhadra.github.io/react-contexify/how-to-style
Set the animation prop to false to disable all menu transitions.
```javascript