Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

react typescript context menu

License

NotificationsYou must be signed in to change notification settings

hochan222/holee-contextmenu

Repository files navigation

Simple react contextmenu (Typescript support)

ReactTypescriptnpm versionnpm licensenpm bundle sizedependencies

Installing holee-contextmenu

$ yarn add holee-contextmenu# or$ npm i holee-contextmenu

Project philosophy

High customization and intuition.

Usage

  1. Create a useref.
constouterRef=useRef<HTMLDivElement>null;
  1. Designate as ref to the div tag you want to apply the context menu to.
<divref={outerRef}className="red-box"></div>
  1. Import the menu and create a menu above the div tag.
import{ContextMenu}from'holee-contextmenu';
<ContextMenuouterRef={outerRef}menuOnClick={(e)=>menuOnClickHand(e)}></ContextMenu><divref={outerRef}className="red-box"></div>
  1. A menu list of contextmenu can be created by passing the li tag as children in the menu. (Adata-option must be specified.)
<ContextMenuouterRef={outerRef}menuOnClick={(e)=>menuOnClickHand(e)}><lidata-option="profile">profile</li><lidata-option="send-message">send message</li><lidata-option="add-friend">add friend</li><lidata-option="play-game">play game</li><lidata-option="register-admin">register admin(dismissal)</li><lidata-option="block">block(unblock)</li><lidata-option="mute">mute(unmute)</li><lidata-option="forced-out">forced out</li></ContextMenu><divref={outerRef}className="red-box"></div>
  1. menuOnClickHandler defines an onclick function that is executed when the list is clicked.
constmenuOnClickHandler=(e:React.MouseEvent|React.KeyboardEvent<HTMLUListElement>)=>{const eventTarget=e.targetasHTMLUListElement;if(eventTarget){setOption(()=>eventTarget.dataset.option);}};
  1. Below is the final code, please refer to theexample code for details.
importReact,{useRef,useState}from'react';import{ContextMenu}from'holee-contextmenu';import'./App.css';functionApp(){constouterRef=useRef<HTMLDivElement>(null);const[option,setOption]=useState<string|undefined>('null');constmenuOnClickHandler=(e:React.MouseEvent|React.KeyboardEvent<HTMLUListElement>)=>{const eventTarget=e.targetasHTMLUListElement;if(eventTarget){setOption(()=>eventTarget.dataset.option);}};return(<divclassName="App"><h2>holee-context-menu</h2><p>⬇️ Click right mouse inside the red box ⬇️</p><ContextMenuouterRef={outerRef}menuOnClick={(e)=>menuOnClickHandler(e)}><lidata-option="profile">profile</li><lidata-option="send-message">send message</li><lidata-option="add-friend">add friend</li><lidata-option="play-game">play game</li><lidata-option="register-admin">register admin(dismissal)</li><lidata-option="block">block(unblock)</li><lidata-option="mute">mute(unmute)</li><lidata-option="forced-out">forced out</li></ContextMenu><divref={outerRef}className="red-box"></div><p><strong>eventTarget.dataset.option:</strong>{option}</p></div>);}exportdefaultApp;

Custom Style

Style can be specified through className:

.ul-class-nameli {padding:10px;color: blue;}
<ContextMenuclassName="ul-class-name"outerRef={outerRef}menuOnClick={(e=menuOnClickHandler(e))}><lidata-option="profile">profile</li><lidata-option="send-message">send message</li><lidata-option="add-friend">add friend</li><lidata-option="play-game">play game</li><lidata-option="register-admin">register admin(dismissal)</li><lidata-option="block">block(unblock)</li><lidata-option="mute">mute(unmute)</li><lidata-option="forced-out">forced out</li></ContextMenu>

About

react typescript context menu

Resources

License

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    [8]ページ先頭

    ©2009-2025 Movatter.jp