svelte-link-interceptor 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 babu-ch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,60 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region ../core/dist/index.mjs
3
+ /**
4
+ * Intercept all `<a>` tag clicks on the document.
5
+ * Returns a cleanup function that removes the listener.
6
+ */
7
+ function interceptLinks(options) {
8
+ const handler = createClickHandler(options);
9
+ document.addEventListener("click", handler, true);
10
+ return () => document.removeEventListener("click", handler, true);
11
+ }
12
+ function findAnchorFromEvent(event) {
13
+ for (const el of event.composedPath()) if (el instanceof HTMLAnchorElement) return el;
14
+ return null;
15
+ }
16
+ function createClickHandler(options) {
17
+ return (event) => {
18
+ if (event.button !== 0) return;
19
+ const isModifierClick = event.metaKey || event.ctrlKey || event.shiftKey || event.altKey;
20
+ const anchor = findAnchorFromEvent(event);
21
+ if (!anchor) return;
22
+ const href = anchor.href;
23
+ if (!href) return;
24
+ let url;
25
+ try {
26
+ url = new URL(href);
27
+ } catch {
28
+ return;
29
+ }
30
+ if (url.protocol !== "http:" && url.protocol !== "https:") return;
31
+ const isExternal = url.origin !== window.location.origin;
32
+ const ctx = {
33
+ url,
34
+ anchor,
35
+ event,
36
+ get path() {
37
+ return this.url.pathname + this.url.search + this.url.hash;
38
+ },
39
+ isExternal,
40
+ isModifierClick,
41
+ preventDefault() {
42
+ event.preventDefault();
43
+ }
44
+ };
45
+ if (isExternal) options.onExternalLink?.(ctx);
46
+ else options.onInternalLink?.(ctx);
47
+ if (anchor.href !== url.toString()) anchor.href = url.toString();
48
+ };
49
+ }
50
+ //#endregion
51
+ //#region src/action.ts
52
+ /**
53
+ * Svelte action that intercepts all `<a>` tag clicks.
54
+ * Usage: `<div use:linkInterceptor={options}>`
55
+ */
56
+ function linkInterceptor(_node, options) {
57
+ return { destroy: interceptLinks(options) };
58
+ }
59
+ //#endregion
60
+ exports.linkInterceptor = linkInterceptor;
@@ -0,0 +1,40 @@
1
+ //#region ../core/dist/index.d.mts
2
+ //#region src/types.d.ts
3
+ interface LinkContext {
4
+ /** Parsed URL (mutable — changes are reflected on `anchor.href`) */
5
+ url: URL;
6
+ /** The clicked `<a>` element */
7
+ anchor: HTMLAnchorElement;
8
+ /** The original click event */
9
+ event: MouseEvent;
10
+ /** `url.pathname + url.search + url.hash` */
11
+ path: string;
12
+ /** Whether the link is external (different origin) */
13
+ isExternal: boolean;
14
+ /** Whether a modifier key (Ctrl/Meta/Shift/Alt) was held during the click */
15
+ isModifierClick: boolean;
16
+ /** Cancel the default navigation */
17
+ preventDefault(): void;
18
+ }
19
+ interface LinkInterceptorOptions {
20
+ /** Called when an internal (same-origin) link is clicked */
21
+ onInternalLink?: (ctx: LinkContext) => void;
22
+ /** Called when an external (different-origin) link is clicked */
23
+ onExternalLink?: (ctx: LinkContext) => void;
24
+ } //#endregion
25
+ //#region src/interceptor.d.ts
26
+ /**
27
+ * Intercept all `<a>` tag clicks on the document.
28
+ * Returns a cleanup function that removes the listener.
29
+ */
30
+ //#endregion
31
+ //#region src/action.d.ts
32
+ /**
33
+ * Svelte action that intercepts all `<a>` tag clicks.
34
+ * Usage: `<div use:linkInterceptor={options}>`
35
+ */
36
+ declare function linkInterceptor(_node: HTMLElement, options: LinkInterceptorOptions): {
37
+ destroy: () => void;
38
+ };
39
+ //#endregion
40
+ export { type LinkContext, type LinkInterceptorOptions, linkInterceptor };
@@ -0,0 +1,40 @@
1
+ //#region ../core/dist/index.d.mts
2
+ //#region src/types.d.ts
3
+ interface LinkContext {
4
+ /** Parsed URL (mutable — changes are reflected on `anchor.href`) */
5
+ url: URL;
6
+ /** The clicked `<a>` element */
7
+ anchor: HTMLAnchorElement;
8
+ /** The original click event */
9
+ event: MouseEvent;
10
+ /** `url.pathname + url.search + url.hash` */
11
+ path: string;
12
+ /** Whether the link is external (different origin) */
13
+ isExternal: boolean;
14
+ /** Whether a modifier key (Ctrl/Meta/Shift/Alt) was held during the click */
15
+ isModifierClick: boolean;
16
+ /** Cancel the default navigation */
17
+ preventDefault(): void;
18
+ }
19
+ interface LinkInterceptorOptions {
20
+ /** Called when an internal (same-origin) link is clicked */
21
+ onInternalLink?: (ctx: LinkContext) => void;
22
+ /** Called when an external (different-origin) link is clicked */
23
+ onExternalLink?: (ctx: LinkContext) => void;
24
+ } //#endregion
25
+ //#region src/interceptor.d.ts
26
+ /**
27
+ * Intercept all `<a>` tag clicks on the document.
28
+ * Returns a cleanup function that removes the listener.
29
+ */
30
+ //#endregion
31
+ //#region src/action.d.ts
32
+ /**
33
+ * Svelte action that intercepts all `<a>` tag clicks.
34
+ * Usage: `<div use:linkInterceptor={options}>`
35
+ */
36
+ declare function linkInterceptor(_node: HTMLElement, options: LinkInterceptorOptions): {
37
+ destroy: () => void;
38
+ };
39
+ //#endregion
40
+ export { type LinkContext, type LinkInterceptorOptions, linkInterceptor };
package/dist/index.mjs ADDED
@@ -0,0 +1,59 @@
1
+ //#region ../core/dist/index.mjs
2
+ /**
3
+ * Intercept all `<a>` tag clicks on the document.
4
+ * Returns a cleanup function that removes the listener.
5
+ */
6
+ function interceptLinks(options) {
7
+ const handler = createClickHandler(options);
8
+ document.addEventListener("click", handler, true);
9
+ return () => document.removeEventListener("click", handler, true);
10
+ }
11
+ function findAnchorFromEvent(event) {
12
+ for (const el of event.composedPath()) if (el instanceof HTMLAnchorElement) return el;
13
+ return null;
14
+ }
15
+ function createClickHandler(options) {
16
+ return (event) => {
17
+ if (event.button !== 0) return;
18
+ const isModifierClick = event.metaKey || event.ctrlKey || event.shiftKey || event.altKey;
19
+ const anchor = findAnchorFromEvent(event);
20
+ if (!anchor) return;
21
+ const href = anchor.href;
22
+ if (!href) return;
23
+ let url;
24
+ try {
25
+ url = new URL(href);
26
+ } catch {
27
+ return;
28
+ }
29
+ if (url.protocol !== "http:" && url.protocol !== "https:") return;
30
+ const isExternal = url.origin !== window.location.origin;
31
+ const ctx = {
32
+ url,
33
+ anchor,
34
+ event,
35
+ get path() {
36
+ return this.url.pathname + this.url.search + this.url.hash;
37
+ },
38
+ isExternal,
39
+ isModifierClick,
40
+ preventDefault() {
41
+ event.preventDefault();
42
+ }
43
+ };
44
+ if (isExternal) options.onExternalLink?.(ctx);
45
+ else options.onInternalLink?.(ctx);
46
+ if (anchor.href !== url.toString()) anchor.href = url.toString();
47
+ };
48
+ }
49
+ //#endregion
50
+ //#region src/action.ts
51
+ /**
52
+ * Svelte action that intercepts all `<a>` tag clicks.
53
+ * Usage: `<div use:linkInterceptor={options}>`
54
+ */
55
+ function linkInterceptor(_node, options) {
56
+ return { destroy: interceptLinks(options) };
57
+ }
58
+ //#endregion
59
+ export { linkInterceptor };
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "svelte-link-interceptor",
3
+ "version": "0.1.0",
4
+ "description": "Svelte action to intercept all <a> tag clicks in your SPA — handle internal routing and external link modifications with ease.",
5
+ "keywords": [
6
+ "anchor",
7
+ "interceptor",
8
+ "link",
9
+ "action",
10
+ "router",
11
+ "spa",
12
+ "svelte"
13
+ ],
14
+ "author": "babu-ch",
15
+ "license": "MIT",
16
+ "homepage": "https://github.com/babu-ch/link-interceptor/tree/main/packages/svelte",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/babu-ch/link-interceptor.git",
20
+ "directory": "packages/svelte"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/babu-ch/link-interceptor/issues"
24
+ },
25
+ "sideEffects": false,
26
+ "engines": {
27
+ "node": ">=18.0.0"
28
+ },
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "type": "module",
33
+ "main": "./dist/index.cjs",
34
+ "module": "./dist/index.mjs",
35
+ "types": "./dist/index.d.mts",
36
+ "exports": {
37
+ ".": {
38
+ "import": {
39
+ "types": "./dist/index.d.mts",
40
+ "default": "./dist/index.mjs"
41
+ },
42
+ "require": {
43
+ "types": "./dist/index.d.cts",
44
+ "default": "./dist/index.cjs"
45
+ }
46
+ }
47
+ },
48
+ "peerDependencies": {
49
+ "svelte": "^4.0.0 || ^5.0.0"
50
+ },
51
+ "devDependencies": {
52
+ "jsdom": "^29.0.1",
53
+ "svelte": "^5.0.0",
54
+ "typescript": "^5.8.2",
55
+ "vite-plus": "^0.1.14",
56
+ "link-interceptor": "0.1.0"
57
+ },
58
+ "scripts": {
59
+ "build": "vp pack",
60
+ "test": "vp test run",
61
+ "lint": "vp lint"
62
+ }
63
+ }