@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 +57 -52
- package/dist/ContextMenu.svelte +7 -5
- package/dist/ContextMenu.svelte.d.ts +3 -0
- package/package.json +1 -1
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
+
```
|
package/dist/ContextMenu.svelte
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
<script>import {
|
|
2
|
-
import {
|
|
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" && (
|
|
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 && (
|
|
65
|
+
on:click|stopPropagation={() => menuItemClickCloses && close()}
|
|
64
66
|
on:keydown|preventDefault|stopPropagation={menuKeydownHandler}
|
|
65
67
|
bind:this={menu}
|
|
66
68
|
>
|