Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://unpkg.com/vue@3.1.0/dist/vue.global.js"></script>
<script src="https://unpkg.com/vue3-menus@1.0.16/dist/vue3-menus.min.js"></script>

<body>
  <a class="github" href="https://github.com/xfy520/vue3-menus" target="_blank"><svg focusable="false" class="" data-icon="github" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896">
      <path d="M511.6 76.3C264.3 76.2 64 276.4 64 523.5 64 718.9 189.3 885 363.8 946c23.5 5.9 19.9-10.8 19.9-22.2v-77.5c-135.7 15.9-141.2-73.9-150.3-88.9C215 726 171.5 718 184.5 703c30.9-15.9 62.4 4 98.9 57.9 26.4 39.1 77.9 32.5 104 26 5.7-23.5 17.9-44.5 34.7-60.8-140.6-25.2-199.2-111-199.2-213 0-49.5 16.3-95 48.3-131.7-20.4-60.5 1.9-112.3 4.9-120 58.1-5.2 118.5 41.6 123.2 45.3 33-8.9 70.7-13.6 112.9-13.6 42.4 0 80.2 4.9 113.5 13.9 11.3-8.6 67.3-48.8 121.3-43.9 2.9 7.7 24.7 58.3 5.5 118 32.4 36.8 48.9 82.7 48.9 132.3 0 102.2-59 188.1-200 212.9a127.5 127.5 0 0138.1 91v112.5c.8 9 0 17.9 15 17.9 177.1-59.7 304.6-227 304.6-424.1 0-247.2-200.4-447.3-447.5-447.3z"></path>
    </svg></a>
  <div class="content" id="app">
    <div style="margin: 0 20px;">
      <h2>指令方式实现</h2>
      <div class="block">
        <h2>复制区</h2>
        <div class="area">
          <div class="body" @contextmenu="onCopy1" v-menus="menus1">
            Vue3.0 自定义右键菜单
            Vue3.0 原生实现完全自定义右键菜单组件, 零依赖,可根据可视区域自动调节显示位置,可支持插槽完全重写每一项菜单
          </div>
        </div>
      </div>
      <div class="block">
        <h2>粘贴区</h2>
        <div class="area">
          <div class="body" @contextmenu="onPaste1" v-menus="menus1">{{ pasteValue1 }}</div>
        </div>
      </div>
    </div>
    <div style="margin: 0 20px;">
      <h2>事件方式实现</h2>
      <div class="block">
        <h2>复制区</h2>
        <div class="area">
          <div class="body" @contextmenu="onCopy2">
            Vue3.0 自定义右键菜单
            Vue3.0 原生实现完全自定义右键菜单组件, 零依赖,可根据可视区域自动调节显示位置,可支持插槽完全重写每一项菜单
          </div>
        </div>
      </div>
      <div class="block">
        <h2>粘贴区</h2>
        <div class="area">
          <div class="body" @contextmenu="onPaste2">{{ pasteValue2 }}</div>
        </div>
      </div>
    </div>
    <div style="margin: 0 20px;">
      <h2>组件方式实现</h2>
      <div class="block">
        <h2>复制区</h2>
        <div class="area">
          <div class="body" @contextmenu="onCopy3">
            Vue3.0 自定义右键菜单
            Vue3.0 原生实现完全自定义右键菜单组件, 零依赖,可根据可视区域自动调节显示位置,可支持插槽完全重写每一项菜单
          </div>
        </div>
      </div>
      <div class="block">
        <h2>粘贴区</h2>
        <div class="area">
          <div class="body" @contextmenu="onPaste3">{{ pasteValue3 }}</div>
        </div>
      </div>
    </div>
    <vue3-menus v-model:open="isOpen" :event="eventVal" :menus="menus3"></vue3-menus>
  </div>

</body>
              
            
!

CSS

              
                body {
  text-align: center;
}

.github {
  margin-top: 60px;
  font-size: 36px;
}

.content {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}

.area {
  width: 300px;
  height: 200px;
  border: 1px solid #999;
  border-radius: 5px;
  overflow: hidden;
}

.body {
  width: 100%;
  height: 100%;
  overflow-y: auto;
  padding: 8px 16px;
  box-sizing: border-box;
}

              
            
!

JS

              
                const vue3Composition = {
  name: "copy",
  setup() {
    const { proxy } = Vue.getCurrentInstance();
    const clipboardValue1 = Vue.ref("");
    const pasteValue1 = Vue.ref("");
    const clipboardValue2 = Vue.ref("");
    const pasteValue2 = Vue.ref("");
    const clipboardValue3 = Vue.ref("");
    const pasteValue3 = Vue.ref("");
    const isOpen = Vue.ref(false);
    const eventVal = Vue.ref({});
    function getSelection() {
      const selection = window.getSelection();
      if (selection.anchorNode == null) {
        return null;
      }
      const text = selection.anchorNode.wholeText;
      return (
        text && text.substring(selection.anchorOffset, selection.focusOffset)
      );
    }
    const menus1 = Vue.shallowRef({
      menus: [
        {
          label: "复制",
          disabled: !getSelection(),
          click: () => {
            clipboardValue1.value = getSelection();
          }
        },
        {
          label: "粘贴",
          disabled: true,
          click: () => {
            pasteValue1.value += clipboardValue1.value;
          }
        }
      ]
    });
    const menus2 = Vue.shallowRef({
      menus: [
        {
          label: "复制",
          disabled: !getSelection(),
          click: () => {
            clipboardValue2.value = getSelection();
          }
        },
        {
          label: "粘贴",
          disabled: true,
          click: () => {
            pasteValue2.value += clipboardValue2.value;
          }
        }
      ]
    });
    const menus3 = Vue.shallowRef([
      {
        label: "复制",
        disabled: !getSelection(),
        click: () => {
          clipboardValue3.value = getSelection();
        }
      },
      {
        label: "粘贴",
        disabled: true,
        click: () => {
          pasteValue3.value += clipboardValue3.value;
        }
      }
    ]);
    function onCopy1(event) {
      const content = getSelection();
      menus1.value.menus[0].disabled = !content;
      menus1.value.menus[1].disabled = true;
      event.preventDefault();
    }
    function onCopy2(event) {
      const content = getSelection();
      menus2.value.menus[0].disabled = !content;
      menus2.value.menus[1].disabled = true;
      event.preventDefault();
      proxy.$menusEvent(event, menus2.value);
    }
    function onCopy3(event) {
      const content = getSelection();
      menus3.value[0].disabled = !content;
      menus3.value[1].disabled = true;
      eventVal.value = event;
      isOpen.value = true;
      event.preventDefault();
    }
    function onPaste1(event) {
      const content = getSelection();
      menus1.value.menus[0].disabled = !content;
      menus1.value.menus[1].disabled = !clipboardValue1.value;
      event.preventDefault();
    }
    function onPaste2(event) {
      const content = getSelection();
      menus2.value.menus[0].disabled = !content;
      menus2.value.menus[1].disabled = !clipboardValue1.value;
      event.preventDefault();
      proxy.$menusEvent(event, menus2.value);
    }
    function onPaste3(event) {
      const content = getSelection();
      menus3.value[0].disabled = !content;
      menus3.value[1].disabled = !clipboardValue1.value;
      eventVal.value = event;
      isOpen.value = true;
      event.preventDefault();
    }
    return {
      menus1,
      menus3,
      onCopy1,
      onCopy2,
      onCopy3,
      onPaste1,
      onPaste2,
      onPaste3,
      pasteValue1,
      pasteValue2,
      pasteValue3,
      isOpen,
      eventVal
    };
  }
};
Vue.createApp(vue3Composition).use(Vue3Menus).mount("#app");

              
            
!
999px

Console