Code Monkey home page Code Monkey logo

svelte-contextmenu's Introduction

Vince's Svelte Context Menu

A simple, no-frills context menu that works out of the box with Bootstrap but allows total customization. This context menu can be set up to work with on:click and on:contextmenu.

By default, the context menu will appear to the bottom and right of where a click occurred. However, it automatically repositions itself if it overflows.

Live Demo

Install

https://www.npmjs.com/package/svelte-contextmenu

npm install --save-dev svelte-contextmenu

Basic Usage

<script>
  import ContextMenu, { Item, Divider, Settings } from "svelte-contextmenu";
  let myMenu;
</script>

<button class="btn btn-primary" on:click={myMenu.createHandler()}>
  Left Click Trigger
</button>
<button class="btn btn-primary" on:contextmenu={(e) => {
    console.log("Activating context menu");
    myMenu.show(e);
  }}>
  Right click Trigger
</button>

<ContextMenu bind:this={myMenu}>
  <Item href="https://" target={linkTarget}>This will be rendered with an anchor tag</Item>
  <Item>This will be rendered as a button</Item>
  <Divider />
  <Item>
</ContextMenu>

Using Custom Dropdown Items and Disabling Auto-Close Behavior

While svelte-context-menu provides Item and Divider components which should satisfy most use-cases, you are not limited to those. For example, you can add forms and checkboxes to dropdown menus.

Furthermore, while clicking on the menu will automatically close it, this behavior can be disabled for indivdual items. The Item component has an autoclose prop which may be set to false, while custom children may assign the data-autoclose attribute.

<script lang="ts">
  import ContextMenu, { Item, Divider, Settings } from "svelte-context-menu";

  let openInNewTab = false;
  let fireMenu : ContextMenu;
  
  $: linkTarget = openInNewTab ? "blank" : "";
</script>

<ContextMenu bind:this={fireMenu}>
  <Item href="https://youtu.be/PvF9PAxe5Ng?t=7" target={linkTarget}>โ›ฝ Give me fuel</Item>
  <Item href="https://youtu.be/PvF9PAxe5Ng?t=8" target={linkTarget}>๐Ÿ”ฅ Give me fire</Item>
  <Item href="https://youtu.be/PvF9PAxe5Ng?t=9" target={linkTarget}>๐Ÿงžโ€โ™€๏ธ Give me that which I desire</Item>
  <Divider />
  <form class="px-3 py-1" data-autoclose="false">
    <div class="form-check">
      <input type="checkbox" class="form-check-input" id="dropdownCheck" bind:checked={openInNewTab}>
      <label class="form-check-label" for="dropdownCheck">
        Open links in a new tab
      </label>
    </div>
  </form>
</ContextMenu>

Styling

For convenience, the context menus come with styling. However, you may override the default styling on a global or an individual basis.

CSS Variables

In most cases, the default styling can be changed to meet one's needs by defining CSS variables.

:root {
  --ctx-menu-background: #eeeeee;
  --ctx-menu-border: 1px solid #aaaaaa;
  --ctx-menu-border-radius: 0.5rem;
  --ctx-menu-hover-bg: #bbbbbb;
  --ctx-menu-font-size: 0.9rem;
  --ctx-menu-padding: 0.375rem 0.5rem;
}

More variables can be found by inspecting the rendered context menu.

Individual Styling

svelte-contextmenu comes with default CSS classes, and they may be appended to or replaced entirely via the Settings class.

<script>
  import ContextMenu, { Item, Divider, Settings } from "svelte-contextmenu";

  // Use default CSS class names *without* styling
  const boldSettings = new Settings();

  /* Alternative 1: Build off of default styling 
   * const boldSettings = Settings.DefaultCSS();
   */

  /* Alternative 2: Build off of Bootstrap styling 
   * const boldSettings = Settings.BootstrapCss();
   */

  boldSettings.Item.Class.push("fw-bold");
</script>

<ContextMenu bind:this={boldMenu} settings={boldSettings}>

Global Styling

In the above example, settings were created and applied to a single instance. However, settings can also be set globally using the defaultSettings store.

<script>
  import ContextMenu, { defaultSettings, Settings } from "svelte-contextmenu";

  const boldSettings = new Settings();
  boldSettings.ItemContent.Class.push("fw-bold");

  // Set global styles to `boldSettings`
  defaultSettings.set(boldSettings);
</script>

Bootstrap Styling: A shorthand

svelte-contextmenu contains a simple useBootstrap() function that may be called at the top of your app to make the menu use Bootstrap 5 classes everywhere.

<script>
  import ContextMenu, { useBootstrap } from "svelte-contextmenu";

  // Use Bootstrap 5 classes globally
  useBootstrap();
</script>

svelte-contextmenu's People

Contributors

vincentlaucsb avatar amerlander avatar robak08 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.