1 Star 2 Fork 1

JA+/jaContextMenu

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ja-contextmenu

Demo: npm run dev 本项目即可 img

简介

  • 原生js右键菜单封装。也可用于onclick事件打开菜单。
  • 默认样式通过js插入style标签完成,注意class命名空间。
  • 仅提供最基础的样式。
  • 支持 TypeScript。
  • 默认 z-index = 5000;
  • Gitee

用法

npm i ja-contextmenu

import ContextMenu from 'ja-contextmenu';
const contextMenu = new ContextMenu();
const menuOption = {
  item:[
    { label:'go', onclick(e, payload){...} }
  ]
};
const menu = contextMenu.create(menuOption);
window.addEventListener('contextmenu', e => { menu.show(e, payload) });

// async create menu
const menu2 = contextMenu.createAsync(menuOption);
window.addEventListener('click', e => { menu2().show(e, payload) });

注意

安装后请把package.json 中ja-contextmenu 的版本号前的"^"删除,防止npm的预料之外的自动更新。(例: "ja-contextmenu":"^1.3.0" => "ja-contextmenu":"1.3.0")
精力有限,不保证小版本更新时,不改动使用方式。

功能更新记录

  • ContextMenu.createAsync 异步创建Menu (v1.7.2)
  • MenuItemOption.onclick 返回true 则点击不关闭menu (v1.6.0)
  • MenuItemOption.icon 支持 HTMLElement (v1.6.0)
  • MenuItemOption.show: boolean。控制MenuItem展示。 (v1.5.0)
  • click 外部关闭事件,capture:true。
  • 有子菜单的项不能点击
  • 支持配置class
  • 支持配置图表icon
  • 滚动时隐藏
  • 使用position:fixed
  • title/tip 格式化
  • 支持传入dom,自定义菜单项

Example

import ContextMenu, { h } from 'ja-contextmenu'; // types.d.ts supported
// import ContextMenu from 'ja-contextmenu/src/index.ts'  
const contextMenu = new ContextMenu({
  width: 200, // default: 200
  fixMenuWhenScroll: false, // 滚动时菜单是否固定(position:fixed) default:false
  hideMenuWhenScroll: true // 滚动时是否关闭菜单,default:true
});
const menuOption = {
  items: [
    { 
      label: 'menu1', // 选项名称
      icon: './assets/images/ico.png', // icon url | HTMLElement
      class: 'customClass', // 选项自定义class, default: ''
      tip: 'tip1', // 选项右侧提示文字, default: ''
      show: true, // 是否展示, default: true
      disabled: false, // 是否禁用选项, default: false
      onclick(e, payload) {
        // payload 为调用menu.show方法传入的参数
        console.log('menu1 click', payload);
        // return true; // 点击不关闭菜单
      },
    },
    { type: '---' }, // 分割线
    { 
      // 支持选项内容根据payload变动
      label: payload => 'menu2', 
      icon: payload => 'icon href2',
      class: payload => 'class2',
      tip: payload => 'tip2',
      show: payload => true,
      disabled: payload => true
      children: {
        width: 120,// 不传则继承父菜单宽度
        items: [
          {
            label: 'sub menu1',
            onclick: (e, payload) => {
              console.log('sub menu1 click', payload)
            }
          },{
            class: 'li-class-name',
            // 自定义选项内容
            customItem: document.createElement('div')
          },{
            // 我封装了createElement的函数h
            customItem: h('div',[
              h('span', {
                // {[element.key]:value}
                textContent: 'hello', // element.textContent = 'hello'
                style:{
                  fontWeight:'bolder'.// element.style.fontWeight = 'holder'
                  cssText: 'font-size:14px;' // element.style.cssText = 'font-size:14px;'
                }, 
                className:'class-name', 
              }),
              h('span.class-name#id',' world')
            ])
          }
        ]
      }
    },
  ],
}
let menu = contextMenu.create(menuOption);

document.body.oncontextmenu = (e) => {
  let payload = 'payload data: callback when click items';
  menu.show(e, payload);
};
// or
someButton.onclick = (e) => {
  menu.show(e);
}

// menu.hide(); // 隐藏
// menu.destroy(); // 销毁
// menu = null;

ContextMenu constructor 构造函数

new ContextMenu(option: ContextMenuOption);

ContextMenuOption

key: type default desc
width: number 200 Menu width
fixMenuWhenScroll: boolean false 滚动时菜单是否固定(hideMenuWhenScroll=false)
hideMenuWhenScroll: boolean true 滚动时是否关闭菜单

ContextMenu instance method 实例方法

create<PayloadType>(option: MenuOption): MenuWrapper

创建一个菜单,返回一个MenuWrapper对象

MenuOption

param: type default desc
width?: number 200 菜单宽度,子菜单不配置,则继承父菜单宽度
class?: string|(payload)=>string Menu ul class
items: MenuItemOption 列表配置项

MenuItemOption

param: type default desc
icon?: string|HTMLElement|(payload)=>string|HTMLElement 图标icon url
class?: string|(payload)=>string 菜单项li class
label?: string|(payload)=>string 选项文字
tip?: string|(payload)=>string 选项右侧提示文字
show?: boolean|(payload)=>boolean true 是否展示
disabled?: boolean|(payload)=>boolean false 是否禁用
type?: MenuItemType 取值 '---' | 'hr' => <hr> 分割线
customItem?: HTMLElement 自定义菜单项
onclick?: function(event, payload):boolean 点击事件回调,参数payload为调用showMenu时传入的参数.return true 则点击后不关闭菜单.
children?: MenuOption 子菜单配置

MenuWrapper

const menu:MenuWrapper = contextMenu.create<Payload>(...)

1.show(pos: { x: number, y: number }, payload?: any)

展示菜单。

  • pos: PointerEvent, MouseEvent, T extends { x: number, y: number }
  • payload: 在点击菜单的onclick回调中返回。

2.hide()

3.destroy()

Typescript Demo

import ContextMenu from 'ja-contextmenu';
const contextMenu = new ContextMenu();
// 泛型 - PayloadType
const menu = contextMenu.create<number>({
  width: 100,
  items: [
    {
      label: 'label',
      onclick(e, payload:number) { // type
        console.log(payload);
      },
    },
  ],
});

menu.show({x: 100,y:100}, 1) // payload type :number
//menu.show({x: 100,y:100}, '2') // payload type error not number

关于项目

  • 例子: npm run dev。代码在/test目录下。
  • 打包: npm run bd
  • src/utils/h.ts => document.createElement()
MIT License Copyright (c) 2022 JA+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

js contextMenu 原生右键菜单封装 展开 收起
JavaScript 等 5 种语言
MIT
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/japlus/ja-context-menu.git
git@gitee.com:japlus/ja-context-menu.git
japlus
ja-context-menu
jaContextMenu
master

搜索帮助

Cb406eda 1850385 E526c682 1850385