@isoftdata/svelte-context-menu 1.1.0 → 1.3.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,59 @@
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
+ A Context/Dropdown menu component, filled with [DropdownItem](DropdownItem.md)s
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i @isoftdata/svelte-context-menu
9
+ ```
10
+
11
+ ## Props
12
+ | Name | Type | Description | Default Value |
13
+ | --- | --- | --- | --- |
14
+ | id | `string` | The id of the context menu element. | "menu" |
15
+ | menuItemClickCloses | `boolean` | Whether clicking a menu item will close the menu. | `true` |
16
+ | placement | `string` | The placement of the context menu relative to the reference element. | "bottom-start" |
17
+
18
+ ## Usage
19
+
20
+ 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.
21
+
22
+ ### Methods
23
+
24
+ * open - Opens the context menu. Arguments below.
25
+ * `event` - the mouse event
26
+ * `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.
27
+ * 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.
28
+
29
+ ## Events
30
+ * open - Fired when the menu is opened
31
+ * close - Fired whe nthe menu is closed
32
+
33
+ ### Example
34
+
35
+ ```svelte
36
+ <script lang="ts">
37
+ import ContextMenu from '@isoftdata/svelte-context-menu'
38
+
39
+ let cm: ContextMenu
40
+ </script>
41
+
42
+ <button
43
+ id="target"
44
+ class="btn btn-primary btn-block"
45
+ on:contextmenu|preventDefault={e => cm.open(e, "target")}
46
+ >
47
+ Right Click to Open Menu
48
+ </button>
49
+ <ContextMenu bind:this={cm}>
50
+ <DropdownItem
51
+ icon="mouse"
52
+ on:click={() => alert("Dropdown Item Clicked!")}
53
+ >
54
+ Click Me!
55
+ </DropdownItem>
56
+ <div class="downdown-divider" />
57
+ <h6 class="dropdown-header">Clicked {cmClicks} time(s)</h6>
58
+ </ContextMenu>
59
+ ```
@@ -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,10 @@ function menuKeydownHandler(event) {
51
53
  }
52
54
  </script>
53
55
 
54
- <svelte:window on:keydown={event => event.key === "Escape" && (show = false)} />
56
+ <svelte:window
57
+ on:click={close}
58
+ on:keydown={event => event.key === "Escape" && close()}
59
+ />
55
60
 
56
61
  <div
57
62
  {id}
@@ -60,7 +65,7 @@ function menuKeydownHandler(event) {
60
65
  style="position: {strategy}; left:{positionLeft}px; top:{positionTop}px;"
61
66
  role="menu"
62
67
  tabindex="0"
63
- on:click|stopPropagation={() => menuItemClickCloses && (show = false)}
68
+ on:click|stopPropagation={() => menuItemClickCloses && close()}
64
69
  on:keydown|preventDefault|stopPropagation={menuKeydownHandler}
65
70
  bind:this={menu}
66
71
  >
@@ -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: {
@@ -0,0 +1,23 @@
1
+ <script>import Icon from "@isoftdata/svelte-icon";
2
+ export let icon = null;
3
+ function getIconProps(icon2) {
4
+ if (typeof icon2 === "string") {
5
+ return { icon: icon2, ...defaultIconProps };
6
+ } else {
7
+ return { ...icon2, ...defaultIconProps };
8
+ }
9
+ }
10
+ const defaultIconProps = {
11
+ prefix: "fas",
12
+ fixedWidth: true
13
+ };
14
+ $:
15
+ iconProps = getIconProps(icon);
16
+ </script>
17
+
18
+ <button
19
+ class="dropdown-item"
20
+ type="button"
21
+ on:click
22
+ >{#if $$props.icon}<span class="mr-1"><Icon {...iconProps} /> </span>{/if}<slot />
23
+ </button>
@@ -0,0 +1,23 @@
1
+ import { SvelteComponent } from "svelte";
2
+ import type { ComponentProps } from "svelte";
3
+ import Icon from "@isoftdata/svelte-icon";
4
+ declare const __propDef: {
5
+ props: {
6
+ [x: string]: any;
7
+ icon?: ComponentProps<Icon>["icon"] | ComponentProps<Icon>;
8
+ };
9
+ events: {
10
+ click: MouseEvent;
11
+ } & {
12
+ [evt: string]: CustomEvent<any>;
13
+ };
14
+ slots: {
15
+ default: {};
16
+ };
17
+ };
18
+ export type DropdownItemProps = typeof __propDef.props;
19
+ export type DropdownItemEvents = typeof __propDef.events;
20
+ export type DropdownItemSlots = typeof __propDef.slots;
21
+ export default class DropdownItem extends SvelteComponent<DropdownItemProps, DropdownItemEvents, DropdownItemSlots> {
22
+ }
23
+ export {};
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.3.0",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package",
@@ -50,6 +50,7 @@
50
50
  "type": "module",
51
51
  "prettier": "@isoftdata/prettier-config",
52
52
  "dependencies": {
53
- "@floating-ui/dom": "^1.5.3"
53
+ "@floating-ui/dom": "^1.5.3",
54
+ "@isoftdata/svelte-icon": "^1.1.0"
54
55
  }
55
56
  }