@isoftdata/svelte-context-menu 1.0.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,51 +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
- | `placement` | string | The placement of the context menu relative to the reference element. | "bottom-start" |
14
-
15
- ## Usage
16
-
17
- 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.
18
-
19
- ### Methods
20
-
21
- * open - Opens the context menu. Arguments below.
22
- * `event` - the mouse event
23
- * `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.
24
- * 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.
25
- ### Example
26
-
27
- ```html
28
- <script lang="ts">
29
- import ContextMenu from '@isoftdata/svelte-context-menu'
30
-
31
- let cm: ContextMenu
32
- </script>
33
-
34
- <button
35
- id="target"
36
- class="btn btn-primary btn-block"
37
- on:contextmenu|preventDefault={e => cm.open(e, "target")}
38
- >
39
- Right Click to Open Menu
40
- </button>
41
- <ContextMenu bind:this={cm}>
42
- <button
43
- class="dropdown-item"
44
- type="button"
45
- on:click={() => cmClicks++}
46
- >Click Me!
47
- </button>
48
- <div class="downdown-divider" />
49
- <h6 class="dropdown-header">Clicked {cmClicks} time(s)</h6>
50
- </ContextMenu>
51
- ```
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,21 +1,14 @@
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";
5
+ export let menuItemClickCloses = true;
4
6
  export let placement = "bottom-start";
5
7
  let show = false;
6
8
  let positionLeft = 0;
7
9
  let positionTop = 0;
8
10
  let strategy = "absolute";
9
- let mounted = false;
10
11
  let menu;
11
- function handleMenuClick(event) {
12
- if (id && mounted) {
13
- const element = document.getElementById(id);
14
- if (element && !element.contains(event.target)) {
15
- show = false;
16
- }
17
- }
18
- }
19
12
  export async function open(event, targetId = "") {
20
13
  if (menu) {
21
14
  show = true;
@@ -40,16 +33,27 @@ export async function open(event, targetId = "") {
40
33
  positionTop = pos.y;
41
34
  strategy = pos.strategy;
42
35
  }
36
+ dispatch("open");
43
37
  } else {
44
38
  show = false;
45
39
  }
46
40
  }
47
41
  export function close() {
48
42
  show = false;
43
+ dispatch("close");
44
+ }
45
+ function menuKeydownHandler(event) {
46
+ const target = event.target;
47
+ console.log(event.key, event.currentTarget, event.target);
48
+ if ((event.key === " " || event.key === "Enter") && target.tagName === "BUTTON") {
49
+ target.click();
50
+ } else if (target === event.currentTarget && event.key === "Tab") {
51
+ target.querySelector("button")?.focus();
52
+ }
49
53
  }
50
54
  </script>
51
55
 
52
- <svelte:window on:click={event => handleMenuClick(event)} />
56
+ <svelte:window on:keydown={event => event.key === "Escape" && close()} />
53
57
 
54
58
  <div
55
59
  {id}
@@ -58,12 +62,8 @@ export function close() {
58
62
  style="position: {strategy}; left:{positionLeft}px; top:{positionTop}px;"
59
63
  role="menu"
60
64
  tabindex="0"
61
- on:click|stopPropagation={() => (show = false)}
62
- on:keydown|preventDefault|stopPropagation={e => {
63
- if (e.key === "Escape") {
64
- show = false
65
- }
66
- }}
65
+ on:click|stopPropagation={() => menuItemClickCloses && close()}
66
+ on:keydown|preventDefault|stopPropagation={menuKeydownHandler}
67
67
  bind:this={menu}
68
68
  >
69
69
  <slot />
@@ -3,11 +3,15 @@ import { type Placement } from "@floating-ui/dom";
3
3
  declare const __propDef: {
4
4
  props: {
5
5
  id?: string | undefined;
6
+ menuItemClickCloses?: boolean | undefined;
6
7
  placement?: Placement | undefined;
7
8
  open?: ((event: MouseEvent, targetId?: string) => Promise<void>) | undefined;
8
9
  close?: (() => void) | undefined;
9
10
  };
10
11
  events: {
12
+ open: CustomEvent<any>;
13
+ close: CustomEvent<any>;
14
+ } & {
11
15
  [evt: string]: CustomEvent<any>;
12
16
  };
13
17
  slots: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoftdata/svelte-context-menu",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package",