@isoftdata/svelte-context-menu 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,52 +1,57 @@
1
- # Svelte ContextMenu
2
-
3
- ## Install
4
-
5
- ```bash
6
- npm i @isoftdata/svelte-context-menu
7
- ```
8
-
9
- ## Props
10
- | Name | Type | Description | Default Value |
11
- | --- | --- | --- | --- |
12
- | id | `string` | The id of the context menu element. | "menu" |
13
- | menuItemClickCloses | `boolean` | Whether clicking a menu item will close the menu. | `true` |
14
- | placement | `string` | The placement of the context menu relative to the reference element. | "bottom-start" |
15
-
16
- ## Usage
17
-
18
- The main way you'll interact with this component is through calling the methods below. To access these methods on the component instance, you will have to use the `bind:this` directive on the `ContextMenu` in your template, and assign it to a variable, which you can then call the methods on. See Example for more details.
19
-
20
- ### Methods
21
-
22
- * open - Opens the context menu. Arguments below.
23
- * `event` - the mouse event
24
- * `targetId` (optional) the id of an element. If supplied, the menu will be positioned relative to this element. Otherwise, it will be positioned relative to the cursor.
25
- * close - Closes the context menu. Normally you'd close the context menu by clicking on an item within it, but this is also an option.
26
- ### Example
27
-
28
- ```html
29
- <script lang="ts">
30
- import ContextMenu from '@isoftdata/svelte-context-menu'
31
-
32
- let cm: ContextMenu
33
- </script>
34
-
35
- <button
36
- id="target"
37
- class="btn btn-primary btn-block"
38
- on:contextmenu|preventDefault={e => cm.open(e, "target")}
39
- >
40
- Right Click to Open Menu
41
- </button>
42
- <ContextMenu bind:this={cm}>
43
- <button
44
- class="dropdown-item"
45
- type="button"
46
- on:click={() => cmClicks++}
47
- >Click Me!
48
- </button>
49
- <div class="downdown-divider" />
50
- <h6 class="dropdown-header">Clicked {cmClicks} time(s)</h6>
51
- </ContextMenu>
52
- ```
1
+ # Svelte ContextMenu
2
+
3
+ ## Install
4
+
5
+ ```bash
6
+ npm i @isoftdata/svelte-context-menu
7
+ ```
8
+
9
+ ## Props
10
+ | Name | Type | Description | Default Value |
11
+ | --- | --- | --- | --- |
12
+ | id | `string` | The id of the context menu element. | "menu" |
13
+ | menuItemClickCloses | `boolean` | Whether clicking a menu item will close the menu. | `true` |
14
+ | placement | `string` | The placement of the context menu relative to the reference element. | "bottom-start" |
15
+
16
+ ## Usage
17
+
18
+ The main way you'll interact with this component is through calling the methods below. To access these methods on the component instance, you will have to use the `bind:this` directive on the `ContextMenu` in your template, and assign it to a variable, which you can then call the methods on. See Example for more details.
19
+
20
+ ### Methods
21
+
22
+ * open - Opens the context menu. Arguments below.
23
+ * `event` - the mouse event
24
+ * `targetId` (optional) the id of an element. If supplied, the menu will be positioned relative to this element. Otherwise, it will be positioned relative to the cursor.
25
+ * close - Closes the context menu. Normally you'd close the context menu by clicking on an item within it, but this is also an option.
26
+
27
+ ## Events
28
+ * open - Fired when the menu is opened
29
+ * close - Fired whe nthe menu is closed
30
+
31
+ ### Example
32
+
33
+ ```svelte
34
+ <script lang="ts">
35
+ import ContextMenu from '@isoftdata/svelte-context-menu'
36
+
37
+ let cm: ContextMenu
38
+ </script>
39
+
40
+ <button
41
+ id="target"
42
+ class="btn btn-primary btn-block"
43
+ on:contextmenu|preventDefault={e => cm.open(e, "target")}
44
+ >
45
+ Right Click to Open Menu
46
+ </button>
47
+ <ContextMenu bind:this={cm}>
48
+ <button
49
+ class="dropdown-item"
50
+ type="button"
51
+ on:click={() => cmClicks++}
52
+ >Click Me!
53
+ </button>
54
+ <div class="downdown-divider" />
55
+ <h6 class="dropdown-header">Clicked {cmClicks} time(s)</h6>
56
+ </ContextMenu>
57
+ ```
@@ -1,5 +1,6 @@
1
- <script>import { onMount } from "svelte";
2
- import { computePosition, flip } from "@floating-ui/dom";
1
+ <script>import { computePosition, flip } from "@floating-ui/dom";
2
+ import { createEventDispatcher } from "svelte";
3
+ const dispatch = createEventDispatcher();
3
4
  export let id = "menu";
4
5
  export let menuItemClickCloses = true;
5
6
  export let placement = "bottom-start";
@@ -7,7 +8,6 @@ let show = false;
7
8
  let positionLeft = 0;
8
9
  let positionTop = 0;
9
10
  let strategy = "absolute";
10
- let mounted = false;
11
11
  let menu;
12
12
  export async function open(event, targetId = "") {
13
13
  if (menu) {
@@ -33,12 +33,14 @@ export async function open(event, targetId = "") {
33
33
  positionTop = pos.y;
34
34
  strategy = pos.strategy;
35
35
  }
36
+ dispatch("open");
36
37
  } else {
37
38
  show = false;
38
39
  }
39
40
  }
40
41
  export function close() {
41
42
  show = false;
43
+ dispatch("close");
42
44
  }
43
45
  function menuKeydownHandler(event) {
44
46
  const target = event.target;
@@ -51,7 +53,7 @@ function menuKeydownHandler(event) {
51
53
  }
52
54
  </script>
53
55
 
54
- <svelte:window on:keydown={event => event.key === "Escape" && (show = false)} />
56
+ <svelte:window on:keydown={event => event.key === "Escape" && close()} />
55
57
 
56
58
  <div
57
59
  {id}
@@ -60,7 +62,7 @@ function menuKeydownHandler(event) {
60
62
  style="position: {strategy}; left:{positionLeft}px; top:{positionTop}px;"
61
63
  role="menu"
62
64
  tabindex="0"
63
- on:click|stopPropagation={() => menuItemClickCloses && (show = false)}
65
+ on:click|stopPropagation={() => menuItemClickCloses && close()}
64
66
  on:keydown|preventDefault|stopPropagation={menuKeydownHandler}
65
67
  bind:this={menu}
66
68
  >
@@ -9,6 +9,9 @@ declare const __propDef: {
9
9
  close?: (() => void) | undefined;
10
10
  };
11
11
  events: {
12
+ open: CustomEvent<any>;
13
+ close: CustomEvent<any>;
14
+ } & {
12
15
  [evt: string]: CustomEvent<any>;
13
16
  };
14
17
  slots: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoftdata/svelte-context-menu",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package",