@isoftdata/svelte-context-menu 0.0.1

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 ADDED
@@ -0,0 +1,29 @@
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
+ | | | | |
13
+ | | | | |
14
+ | | | | |
15
+
16
+ ## Slots
17
+ * default - A brief description of the slot, if there is one
18
+
19
+ ## Events
20
+ * exampleEvent - A brief description of the event, if there are any
21
+
22
+ ## Example
23
+ ```html
24
+ <script lang="ts">
25
+ import ContextMenu from '@isoftdata/svelte-context-menu'
26
+ </script>
27
+
28
+ <ContextMenu prop='foo' />
29
+ ```
@@ -0,0 +1,84 @@
1
+ <script>import { onMount } from "svelte";
2
+ export let id = "menu";
3
+ export let show = false;
4
+ export let positionLeft = 0;
5
+ export let positionTop = 0;
6
+ let mounted = false;
7
+ function getPosition(e) {
8
+ let posX = 0;
9
+ let posY = 0;
10
+ if (e.pageX || e.pageY) {
11
+ posX = e.pageX;
12
+ posY = e.pageY;
13
+ } else if (e.clientX || e.clientY) {
14
+ posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
15
+ posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
16
+ }
17
+ return {
18
+ x: posX,
19
+ y: posY
20
+ };
21
+ }
22
+ function clickInsideElement(event, className) {
23
+ const el = event.target.closest(`.${className}`);
24
+ return el || false;
25
+ return false;
26
+ }
27
+ function handleMenuClick(event) {
28
+ if (id && mounted) {
29
+ const element = document.getElementById(id);
30
+ if (element && !element.contains(event.target)) {
31
+ show = false;
32
+ }
33
+ }
34
+ }
35
+ onMount(() => {
36
+ mounted = true;
37
+ });
38
+ export function contextMenu(event, property) {
39
+ let menu;
40
+ if (mounted) {
41
+ menu = document.getElementById(id);
42
+ }
43
+ if (property && menu && property === menu.id) {
44
+ event.preventDefault();
45
+ const { x, y } = getPosition(event);
46
+ const menuWidth = menu.offsetWidth + 4;
47
+ const menuHeight = menu.offsetHeight + 4;
48
+ const windowWidth = window.innerWidth;
49
+ const windowHeight = window.innerHeight;
50
+ if (windowWidth - x < menuWidth) {
51
+ positionLeft = windowWidth - menuWidth;
52
+ } else {
53
+ positionLeft = x;
54
+ }
55
+ if (windowHeight - y < menuHeight) {
56
+ positionTop = menuHeight * -1;
57
+ } else {
58
+ positionTop = 40;
59
+ }
60
+ show = true;
61
+ } else {
62
+ show = false;
63
+ }
64
+ }
65
+ </script>
66
+
67
+ <svelte:window on:click={event => handleMenuClick(event)} />
68
+
69
+ <div
70
+ {id}
71
+ class="dropdown-menu shadow bg-white rounded"
72
+ class:show
73
+ style="position: absolute; left:{{ positionLeft }}px; top:{{ positionTop }}px;"
74
+ on:click|stopPropagation={() => (show = false)}
75
+ role="menu"
76
+ tabindex="0"
77
+ on:keydown|preventDefault|stopPropagation={e => {
78
+ if (e.key === "Escape") {
79
+ show = false
80
+ }
81
+ }}
82
+ >
83
+ <slot />
84
+ </div>
@@ -0,0 +1,23 @@
1
+ import { SvelteComponent } from "svelte";
2
+ declare const __propDef: {
3
+ props: {
4
+ id?: string | undefined;
5
+ show?: boolean | undefined;
6
+ positionLeft?: number | undefined;
7
+ positionTop?: number | undefined;
8
+ contextMenu?: ((event: MouseEvent, property: string) => void) | undefined;
9
+ };
10
+ events: {
11
+ [evt: string]: CustomEvent<any>;
12
+ };
13
+ slots: {
14
+ default: {};
15
+ };
16
+ };
17
+ export type ContextMenuProps = typeof __propDef.props;
18
+ export type ContextMenuEvents = typeof __propDef.events;
19
+ export type ContextMenuSlots = typeof __propDef.slots;
20
+ export default class ContextMenu extends SvelteComponent<ContextMenuProps, ContextMenuEvents, ContextMenuSlots> {
21
+ get contextMenu(): (event: MouseEvent, property: string) => void;
22
+ }
23
+ export {};
@@ -0,0 +1 @@
1
+ export { default } from "./ContextMenu.svelte";
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export {default as default} from './ContextMenu.svelte'
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@isoftdata/svelte-context-menu",
3
+ "version": "0.0.1",
4
+ "scripts": {
5
+ "dev": "vite dev",
6
+ "build": "vite build && npm run package",
7
+ "preview": "vite preview",
8
+ "package": "svelte-kit sync && svelte-package && publint",
9
+ "prepublishOnly": "npm run package",
10
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
11
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
12
+ "lint": "prettier --plugin-search-dir . --check . && eslint .",
13
+ "format": "prettier --plugin-search-dir . --write ."
14
+ },
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "svelte": "./dist/index.js"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "!dist/**/*.test.*",
24
+ "!dist/**/*.spec.*"
25
+ ],
26
+ "peerDependencies": {
27
+ "svelte": "^4.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "@isoftdata/prettier-config": "^1.0.1",
31
+ "@sveltejs/adapter-auto": "^2.0.0",
32
+ "@sveltejs/kit": "^1.20.4",
33
+ "@sveltejs/package": "^2.0.0",
34
+ "@typescript-eslint/eslint-plugin": "^6.0.0",
35
+ "@typescript-eslint/parser": "^6.0.0",
36
+ "eslint": "^8.28.0",
37
+ "eslint-config-prettier": "^8.5.0",
38
+ "eslint-plugin-svelte": "^2.30.0",
39
+ "prettier": "^2.8.0",
40
+ "prettier-plugin-svelte": "^2.10.1",
41
+ "publint": "^0.1.9",
42
+ "svelte": "^4.0.5",
43
+ "svelte-check": "^3.4.3",
44
+ "tslib": "^2.4.1",
45
+ "typescript": "^5.0.0",
46
+ "vite": "^4.4.2"
47
+ },
48
+ "svelte": "./dist/index.js",
49
+ "types": "./dist/index.d.ts",
50
+ "type": "module",
51
+ "prettier": "@isoftdata/prettier-config"
52
+ }