v-contextmenu

repository·main·Indexed 21 days ago

https://github.com/cybernika/v-contextmenu

A Vue 3.0 component library for creating customizable context menus and submenus. It features automatic placement adjustment, multiple built-in themes (Default, Bright, Dark), and support for various trigger events. The library provides a set of core components including VContextmenu, VContextmenuItem, VContextmenuSubmenu, and VContextmenuGroup, as well as a dedicated directive for linking target elements to menu instances.

Tokens
2.5K
Snippets
9
Records
13
Agent score
76%

What's inside v-contextmenu

  1. Install v-contextmenu as a Vue plugin

    main

    To use v-contextmenu in your Vue application, use the install function or the default export to register the contextmenu directive and all core components (VContextmenu, VContextmenuItem, VContextmenuDivider, VContextmenuSubmenu, and VContextmenuGroup) globally.

    import { createApp } from 'vue';
    import VContextmenu from 'v-contextmenu';
    
    const app = createApp(App);
    
    // Register the plugin
    app.use(VContextmenu);
    
    app.mount('#app');
  2. Configure the `VContextmenu` root component

    main

    The VContextmenu component is the root container for your context menu. It manages visibility, positioning, and teleportation.

    Attributes:

    • model-value / v-model (boolean): Controls visibility. Defaults to false.
    • autoAdjustPlacement (boolean): If true, the menu automatically switches position if it would overflow the browser viewport. Defaults to true.
    • disabled (boolean): If true, the menu will not appear. Defaults to false.
    • teleport (string | Element): The target element to use for Teleport. Defaults to "body".
    • preventContextmenu (boolean): If true, prevents the default contextmenu event behavior. Defaults to true.

    Methods:

    • show({ top: number, left: number }): Displays the menu at the specified coordinates relative to the browser window.
    • hide(): Hides the menu.

    Events:

    • show: Emitted when the menu is displayed.
    • hide: Emitted when the menu is hidden.
    • contextmenu: Emitted when a contextmenu event occurs inside the menu, providing the event object.
    <v-contextmenu v-model="isVisible" auto-adjust-placement>
      <!-- items here -->
    </v-contextmenu>
  3. Use the `v-contextmenu:ref` directive

    main

    The v-contextmenu:ref directive allows you to link a target element to a VContextmenu instance. The directive expects the ref to point to a VContextmenu component instance.

    You can also pass configuration parameters to the directive to specify which events trigger the menu.

    <v-contextmenu ref="contextmenu">
      <v-contextmenu-item>Menu Item</v-contextmenu-item>
    </v-contextmenu>
    
    <!-- Basic usage: triggers on contextmenu event -->
    <div v-contextmenu:contextmenu></div>
    
    <!-- Advanced usage: triggers on both contextmenu and click events -->
    <div v-contextmenu:contextmenu="{ trigger: ['contextmenu', 'click'] }"></div>
  4. Create submenus with `VContextmenuSubmenu`

    main

    The VContextmenuSubmenu component allows for nested menu structures. It can be nested multiple times.

    Attributes:

    • title (String): The name of the submenu.
    • disabled (Boolean): Disables the submenu. Defaults to false.

    Slots:

    • title: Allows customizing the submenu title via a slot (alternative to the title attribute).

    Events:

    • mouseenter: Emitted when the mouse enters the submenu.
    • mouseleave: Emitted when the mouse leaves the submenu.
    <v-contextmenu-submenu title="Submenu Name">
      <v-contextmenu-item>Nested Item</v-contextmenu-item>
    </v-contextmenu-submenu>
  5. Use `VContextmenuItem` for menu entries

    main

    The VContextmenuItem represents a single clickable entry in the menu. It can only be used inside VContextmenu, VContextmenuSubmenu, or VContextmenuGroup.

    Attributes:

    • disabled (boolean): Disables the item. Defaults to false.
    • hideOnClick (boolean): If true, the menu automatically hides after the item is clicked. Defaults to true.
    • contextmenuAsClick (boolean): If true, treats the contextmenu event as a click. Defaults to true.

    Events:

    • click: Emitted when the item is clicked (provides event).
    • contextmenu: Emitted on contextmenu event (provides event).
    • mouseenter: Emitted when the mouse enters the item area.
    • mouseleave: Emitted when the mouse leaves the item area.
    <v-contextmenu-item @click="handleAction">Action</v-contextmenu-item>
  6. Group items with `VContextmenuGroup`

    main

    The VContextmenuGroup component is used to organize VContextmenuItem components into logical groups with a header.

    Attributes:

    • title (String): The title for the group.
    • maxWidth (Number | String): Sets the maximum width for the group.

    Slots:

    • title: Allows customizing the group title via a slot (alternative to the title attribute).
    <v-contextmenu-group title="Group Title">
      <v-contextmenu-item>Item 1</v-contextmenu-item>
      <v-contextmenu-item>Item 2</v-contextmenu-item>
    </v-contextmenu-group>
  7. Customize menu styles with themes and class names

    main

    The library provides three built-in themes:

    1. Default
    2. Bright (Light mode)
    3. Dark (Dark mode)

    You can also override styles by targeting the following CSS classes:

    • v-contextmenu: Root element
    • v-contextmenu-icon: Icon element
    • v-contextmenu-inner: Menu root element
    • v-contextmenu-divider: Divider line
    • v-contextmenu-item: Individual menu item
    • v-contextmenu-item--hover: Active/hover state for an item
    • v-contextmenu-item--disabled: Disabled state for an item
    • v-contextmenu-group: Group root element
    • v-contextmenu-group__title: Group title
    • v-contextmenu-group__menus: Group items container
    • v-contextmenu-submenu: Submenu container
    • v-contextmenu-submenu__title: Submenu title
    • v-contextmenu-submenu__menus: Submenu items container
    • v-contextmenu-submenu__menus--top: Submenu position: Top
    • v-contextmenu-submenu__menus--right: Submenu position: Right
    • v-contextmenu-submenu__menus--bottom: Submenu position: Bottom
    • v-contextmenu-submenu__menus--left: Submenu position: Left
    • v-contextmenu-submenu__arrow: Submenu title arrow icon
  8. Use ShowOptions to control menu positioning

    main

    When manually triggering the visibility of a context menu, you can use the ShowOptions interface to control its placement. This is useful when you need to specify exact coordinates or control automatic adjustment.

    Properties:

    • top: The vertical position of the menu.
    • left: The horizontal position of the menu.
    • autoAdjustPlacement: A boolean indicating whether the menu should automatically adjust its position to stay within the viewport.
  9. Use AddReferenceOptions to add manual triggers

    main

    When adding a reference to the context menu programmatically, you can use AddReferenceOptions to define how that reference is triggered.

    Properties:

    • trigger: Accepts a TriggerEventTypeOption, which can be a single event type ('contextmenu' or 'click') or an array of these types.
  10. Configure the v-contextmenu directive

    main

    When using the v-contextmenu directive, you can pass an options object to specify which events should trigger the context menu. The trigger option accepts a single event type or an array of event types.

    Supported trigger event types are:

    • 'contextmenu'
    • 'click'
    // Example usage in a Vue template
    // Trigger on both right-click and left-click
    <div v-contextmenu='{ trigger: ["contextmenu", "click"] }'>
      Right click or left click me
    </div>
  11. Use v-contextmenu components

    main

    The library exports several components for building structured context menus. When installed via the plugin, these are available globally:

    • Contextmenu: The root container for the menu.
    • ContextmenuItem: An individual clickable item within the menu.
    • ContextmenuDivider: A visual separator between items or groups.
    • ContextmenuSubmenu: A menu item that opens a nested submenu.
    • ContextmenuGroup: A logical grouping of menu items.
    <template>
      <VContextmenu>
        <VContextmenuItem @click="doSomething">Action 1</VContextmenuItem>
        <VContextmenuDivider />
        <VContextmenuGroup>
          <VContextmenuItem>Group Item</VContextmenuItem>
        </VContextmenuGroup>
        <VContextmenuSubmenu title="More">
          <VContextmenuItem>Sub Item</VContextmenuItem>
        </VContextmenuSubmenu>
      </VContextmenu>
    </template>
  12. Use the v-contextmenu directive

    main

    The library provides a contextmenu directive that can be applied to elements to trigger the context menu. This is registered via the install function.

    <!-- Example usage of the directive -->
    <div v-contextmenu>
      Right click me!
    </div>