@supermousejs/magnetic 2.0.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/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @supermousejs/magnetic
2
+
3
+ ## 2.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - Initial v2.0.0 release
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies
12
+ - @supermousejs/utils@2.0.0
13
+ - @supermousejs/core@2.0.0
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Sijibomi Olusunmbola
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.
@@ -0,0 +1,13 @@
1
+ import { SupermousePlugin } from '../../core/src/index.ts';
2
+ import { ValueOrGetter } from '../../core/src/index.ts';
3
+
4
+ export declare const Magnetic: (options?: MagneticOptions) => SupermousePlugin;
5
+
6
+ export declare interface MagneticOptions {
7
+ name?: string;
8
+ isEnabled?: boolean;
9
+ attraction?: ValueOrGetter<number>;
10
+ distance?: ValueOrGetter<number>;
11
+ }
12
+
13
+ export { }
package/dist/index.mjs ADDED
@@ -0,0 +1,30 @@
1
+ import { normalize as u, definePlugin as h, dom as v, math as o } from "@supermousejs/utils";
2
+ const b = (i = {}) => {
3
+ let a = null, n = { x: 0, y: 0 }, s = !1;
4
+ const f = u(i.attraction, 0.3), y = u(i.distance, 100);
5
+ return h({
6
+ name: "magnetic",
7
+ priority: -10,
8
+ install(t) {
9
+ t.registerHoverTarget("[data-supermouse-magnetic]");
10
+ },
11
+ update(t) {
12
+ const c = t.state.hoverTarget, r = t.state.interaction.magnetic, l = r === !0 || r === "true" || typeof r == "number" && r > 0;
13
+ if (l && c !== a) {
14
+ a = c;
15
+ const e = v.projectRect(c, t.container);
16
+ n.x = e.left + e.width / 2, n.y = e.top + e.height / 2, s = !0;
17
+ } else !l && a && (a = null, s = !1);
18
+ if (s && a) {
19
+ const { x: e, y: g } = t.state.pointer, d = o.dist(e, g, n.x, n.y), x = y(t.state);
20
+ if (d < x) {
21
+ const m = typeof r == "number" ? r : f(t.state);
22
+ t.state.target.x = o.lerp(e, n.x, m), t.state.target.y = o.lerp(g, n.y, m);
23
+ }
24
+ }
25
+ }
26
+ }, i);
27
+ };
28
+ export {
29
+ b as Magnetic
30
+ };
@@ -0,0 +1 @@
1
+ (function(n,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("@supermousejs/utils")):typeof define=="function"&&define.amd?define(["exports","@supermousejs/utils"],e):(n=typeof globalThis<"u"?globalThis:n||self,e(n.SupermouseMagnetic={},n.SupermouseUtils))})(this,(function(n,e){"use strict";const l=(a={})=>{let o=null,r={x:0,y:0},c=!1;const d=e.normalize(a.attraction,.3),y=e.normalize(a.distance,100);return e.definePlugin({name:"magnetic",priority:-10,install(t){t.registerHoverTarget("[data-supermouse-magnetic]")},update(t){const u=t.state.hoverTarget,s=t.state.interaction.magnetic,f=s===!0||s==="true"||typeof s=="number"&&s>0;if(f&&u!==o){o=u;const i=e.dom.projectRect(u,t.container);r.x=i.left+i.width/2,r.y=i.top+i.height/2,c=!0}else!f&&o&&(o=null,c=!1);if(c&&o){const{x:i,y:g}=t.state.pointer,h=e.math.dist(i,g,r.x,r.y),x=y(t.state);if(h<x){const m=typeof s=="number"?s:d(t.state);t.state.target.x=e.math.lerp(i,r.x,m),t.state.target.y=e.math.lerp(g,r.y,m)}}}},a)};n.Magnetic=l,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
package/meta.json ADDED
@@ -0,0 +1,14 @@
1
+
2
+ {
3
+ "id": "magnetic",
4
+ "name": "Magnetic",
5
+ "package": "@supermousejs/magnetic",
6
+ "description": "Attracts the cursor to interactive elements using physics-based magnetism.",
7
+ "code": "app.use(Magnetic({ attraction: 0.4, distance: 100 }));",
8
+ "recipeId": "magnetic-button",
9
+ "icon": "magnetic",
10
+ "options": [
11
+ { "name": "attraction", "type": "number", "default": "0.3", "description": "Strength of the pull (0-1).", "reactive": true },
12
+ { "name": "distance", "type": "number", "default": "100", "description": "Radius in pixels where attraction begins.", "reactive": true }
13
+ ]
14
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@supermousejs/magnetic",
3
+ "version": "2.0.0",
4
+ "main": "dist/index.umd.js",
5
+ "module": "dist/index.mjs",
6
+ "types": "dist/index.d.ts",
7
+ "dependencies": {
8
+ "@supermousejs/core": "2.0.0",
9
+ "@supermousejs/utils": "2.0.0"
10
+ },
11
+ "devDependencies": {},
12
+ "peerDependencies": {},
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.mjs",
17
+ "require": "./dist/index.umd.js"
18
+ }
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "scripts": {
24
+ "build": "vite build"
25
+ }
26
+ }
package/src/index.ts ADDED
@@ -0,0 +1,57 @@
1
+ import type { ValueOrGetter } from '@supermousejs/core';
2
+ import { definePlugin, normalize, math, dom } from '@supermousejs/utils';
3
+
4
+ export interface MagneticOptions {
5
+ name?: string;
6
+ isEnabled?: boolean;
7
+ attraction?: ValueOrGetter<number>;
8
+ distance?: ValueOrGetter<number>;
9
+ }
10
+
11
+ export const Magnetic = (options: MagneticOptions = {}) => {
12
+ let lastTarget: HTMLElement | null = null;
13
+ let magnetCenter = { x: 0, y: 0 };
14
+ let isActive = false;
15
+
16
+ const getAttraction = normalize(options.attraction, 0.3);
17
+ const getDistance = normalize(options.distance, 100);
18
+
19
+ return definePlugin({
20
+ name: "magnetic",
21
+ priority: -10,
22
+
23
+ install(app) {
24
+ app.registerHoverTarget("[data-supermouse-magnetic]");
25
+ },
26
+
27
+ update(app) {
28
+ const target = app.state.hoverTarget;
29
+ const val = app.state.interaction.magnetic;
30
+ const isMagnetic = val === true || val === 'true' || (typeof val === 'number' && val > 0);
31
+
32
+ if (isMagnetic && target !== lastTarget) {
33
+ lastTarget = target!;
34
+ const rect = dom.projectRect(target!, app.container);
35
+ magnetCenter.x = rect.left + rect.width / 2;
36
+ magnetCenter.y = rect.top + rect.height / 2;
37
+ isActive = true;
38
+ }
39
+ else if (!isMagnetic && lastTarget) {
40
+ lastTarget = null;
41
+ isActive = false;
42
+ }
43
+
44
+ if (isActive && lastTarget) {
45
+ const { x, y } = app.state.pointer;
46
+ const dist = math.dist(x, y, magnetCenter.x, magnetCenter.y);
47
+ const range = getDistance(app.state);
48
+
49
+ if (dist < range) {
50
+ const attraction = typeof val === 'number' ? val : getAttraction(app.state);
51
+ app.state.target.x = math.lerp(x, magnetCenter.x, attraction);
52
+ app.state.target.y = math.lerp(y, magnetCenter.y, attraction);
53
+ }
54
+ }
55
+ },
56
+ }, options);
57
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "include": [
4
+ "src"
5
+ ],
6
+ "compilerOptions": {
7
+ "outDir": "dist",
8
+ "baseUrl": ".",
9
+ "paths": {
10
+ "@supermousejs/core": [
11
+ "../core/src/index.ts"
12
+ ],
13
+ "@supermousejs/utils": [
14
+ "../utils/src/index.ts"
15
+ ]
16
+ }
17
+ }
18
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { defineConfig } from 'vite';
2
+ import dts from 'vite-plugin-dts';
3
+ import path from 'path';
4
+
5
+ export default defineConfig({
6
+ build: {
7
+ lib: {
8
+ entry: path.resolve(__dirname, 'src/index.ts'),
9
+ name: 'SupermouseMagnetic',
10
+ fileName: (format) => format === 'es' ? 'index.mjs' : 'index.umd.js',
11
+ },
12
+ rollupOptions: {
13
+ external: ['@supermousejs/core', '@supermousejs/utils'],
14
+ output: {
15
+ globals: {
16
+ '@supermousejs/core': 'SupermouseCore'
17
+ , '@supermousejs/utils': 'SupermouseUtils'}
18
+ }
19
+ }
20
+ },
21
+ plugins: [dts({ rollupTypes: true })]
22
+ });