npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-contextmenu

v1.0.2

Published

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`.

Downloads

211

Readme

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>