@sveltek/attachments 0.1.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/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025, Ivo Dolenc <https://github.com/ivodolenc>
4
+ Copyright (c) 2025, Sveltek <https://github.com/sveltek>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ <h1 align="center">@sveltek/attachments</h1>
2
+
3
+ <p align="center">A collection of custom attachments for Svelte.</p>
4
+
5
+ <br>
6
+
7
+ > [!NOTE]
8
+ >
9
+ > Since [attachments](https://svelte.dev/docs/svelte/svelte-attachments) are a new feature in **Svelte**, this project is in an early testing phase to experiment with their implementation. As a result, breaking changes are very likely before the first stable release.
10
+ >
11
+ > If you find any issues or bugs, please [report](https://github.com/sveltek/markdown/issues/new/choose) them so the project can be improved.
12
+
13
+ <br>
14
+
15
+ ## Installation
16
+
17
+ ```sh
18
+ # via pnpm
19
+ pnpm add @sveltek/attachments
20
+ ```
21
+
22
+ ```sh
23
+ # via npm
24
+ npm install @sveltek/attachments
25
+ ```
26
+
27
+ ## Attachments
28
+
29
+ > **Required**: `svelte`: `>=5.29.0`
30
+
31
+ - [clickOutside](./src/attachments/click-outside/README.md) → Triggers a callback when clicking outside the target element.
32
+ - [keyboard](./src/attachments/keyboard/README.md) → Triggers a callback when a specified keyboard event occurs.
33
+
34
+ ## Community
35
+
36
+ Feel free to ask questions or share new ideas.
37
+
38
+ Use the official [discussions](https://github.com/sveltek/attachments/discussions) to get involved.
39
+
40
+ ## License
41
+
42
+ Developed in 🇭🇷 Croatia, © Sveltek.
43
+
44
+ Released under the [MIT](LICENSE.txt) license.
@@ -0,0 +1,38 @@
1
+ import { Attachment } from 'svelte/attachments';
2
+
3
+ declare function clickOutside(callback: (event: MouseEvent) => void, options?: ClickOutsideOptions): Attachment;
4
+
5
+ interface ClickOutsideOptions {
6
+ event?: {
7
+ /**
8
+ * @default document
9
+ */
10
+ target?: Element | Document | Window;
11
+ };
12
+ /**
13
+ * @default undefined
14
+ */
15
+ trigger?: Element | null | (Element | null)[];
16
+ }
17
+
18
+ declare function keyboard(callback: (event: KeyboardEvent) => void, options?: KeyboardOptions): Attachment;
19
+
20
+ interface KeyboardOptions {
21
+ event?: {
22
+ /**
23
+ * @default 'keydown'
24
+ */
25
+ type?: 'keydown' | 'keyup';
26
+ /**
27
+ * @default this
28
+ */
29
+ target?: Element | Document | Window;
30
+ };
31
+ /**
32
+ * @default undefined
33
+ */
34
+ keys?: string[];
35
+ }
36
+
37
+ export { clickOutside, keyboard };
38
+ export type { ClickOutsideOptions, KeyboardOptions };
package/dist/index.mjs ADDED
@@ -0,0 +1,32 @@
1
+ function clickOutside(callback, options = {}) {
2
+ return (el) => {
3
+ const { event: { target = document } = {}, trigger } = options;
4
+ const triggers = Array.isArray(trigger) ? trigger : [trigger];
5
+ const handler = (event) => {
6
+ const target2 = event.target;
7
+ if (el.contains(target2) || triggers.some((v) => v?.contains(target2))) {
8
+ return;
9
+ }
10
+ callback(event);
11
+ };
12
+ target.addEventListener("click", handler, true);
13
+ return () => {
14
+ target.removeEventListener("click", handler, true);
15
+ };
16
+ };
17
+ }
18
+
19
+ function keyboard(callback, options = {}) {
20
+ return (el) => {
21
+ const { event: { type = "keydown", target = el } = {}, keys } = options;
22
+ const handler = (event) => {
23
+ if (!keys || keys.includes(event.key)) callback(event);
24
+ };
25
+ target.addEventListener(type, handler);
26
+ return () => {
27
+ target.removeEventListener(type, handler);
28
+ };
29
+ };
30
+ }
31
+
32
+ export { clickOutside, keyboard };
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@sveltek/attachments",
3
+ "version": "0.1.0",
4
+ "author": "Sveltek",
5
+ "description": "A collection of custom attachments for Svelte.",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/sveltek/attachments.git"
10
+ },
11
+ "homepage": "https://github.com/sveltek/attachments",
12
+ "type": "module",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.mts",
16
+ "import": "./dist/index.mjs"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "keywords": [
23
+ "sveltek",
24
+ "svelte",
25
+ "sveltekit",
26
+ "attachments",
27
+ "composables",
28
+ "attachment",
29
+ "functions",
30
+ "function",
31
+ "attach",
32
+ "utils"
33
+ ],
34
+ "peerDependencies": {
35
+ "@types/node": ">=20.0.0",
36
+ "svelte": ">=5.29.0",
37
+ "typescript": ">=5.0.0"
38
+ },
39
+ "peerDependenciesMeta": {
40
+ "@types/node": {
41
+ "optional": true
42
+ },
43
+ "typescript": {
44
+ "optional": true
45
+ }
46
+ },
47
+ "devDependencies": {
48
+ "@hypernym/bundler": "^0.14.4",
49
+ "@sveltejs/adapter-static": "^3.0.8",
50
+ "@sveltejs/kit": "^2.25.1",
51
+ "@sveltek/eslint-config": "^1.1.1",
52
+ "@sveltek/prettier-config": "^1.0.4",
53
+ "@types/node": "^24.0.15",
54
+ "eslint": "^9.31.0",
55
+ "prettier": "^3.6.2",
56
+ "svelte": "^5.36.13",
57
+ "typescript": "^5.8.3",
58
+ "vite": "^7.0.5"
59
+ },
60
+ "scripts": {
61
+ "build": "hyperbundler",
62
+ "dev": "vite",
63
+ "build:playground": "vite build",
64
+ "preview": "vite preview",
65
+ "sync": "svelte-kit sync",
66
+ "lint": "eslint .",
67
+ "lint:fix": "eslint --fix .",
68
+ "format": "prettier --write .",
69
+ "bump": "taze -f --interactive"
70
+ }
71
+ }