@supermousejs/stick 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 +13 -0
- package/LICENSE.md +21 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.mjs +38 -0
- package/dist/index.umd.js +1 -0
- package/meta.json +13 -0
- package/package.json +26 -0
- package/src/index.ts +73 -0
- package/tsconfig.json +18 -0
- package/vite.config.ts +22 -0
package/CHANGELOG.md
ADDED
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.
|
package/dist/index.d.ts
ADDED
|
@@ -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 Stick: (options?: StickOptions) => SupermousePlugin;
|
|
5
|
+
|
|
6
|
+
export declare interface StickOptions {
|
|
7
|
+
name?: string;
|
|
8
|
+
isEnabled?: boolean;
|
|
9
|
+
/** Extra padding around the element when calculating shape. Default 10. */
|
|
10
|
+
padding?: ValueOrGetter<number>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { }
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { normalize as c, definePlugin as h, dom as g } from "@supermousejs/utils";
|
|
2
|
+
const m = (a = {}) => {
|
|
3
|
+
const d = c(a.padding, 10);
|
|
4
|
+
let s = null, e = null;
|
|
5
|
+
return h({
|
|
6
|
+
name: a.name || "stick",
|
|
7
|
+
// Logic plugin: must run before visuals to set the shape state
|
|
8
|
+
priority: -10,
|
|
9
|
+
install(t) {
|
|
10
|
+
t.registerHoverTarget("[data-supermouse-stick]");
|
|
11
|
+
},
|
|
12
|
+
update(t) {
|
|
13
|
+
const r = t.state.hoverTarget, l = t.state.interaction.stick === !0 || t.state.interaction.stick === "true";
|
|
14
|
+
if (r && l) {
|
|
15
|
+
if (r !== s || !e) {
|
|
16
|
+
s = r;
|
|
17
|
+
const i = g.projectRect(r, t.container), o = window.getComputedStyle(r), n = d(t.state);
|
|
18
|
+
e = {
|
|
19
|
+
width: i.width + n,
|
|
20
|
+
height: i.height + n,
|
|
21
|
+
radius: parseFloat(o.borderRadius) || 0,
|
|
22
|
+
x: i.left + i.width / 2,
|
|
23
|
+
y: i.top + i.height / 2
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
e && (t.state.target.x = e.x, t.state.target.y = e.y, t.state.shape = {
|
|
27
|
+
width: e.width,
|
|
28
|
+
height: e.height,
|
|
29
|
+
borderRadius: e.radius
|
|
30
|
+
});
|
|
31
|
+
} else
|
|
32
|
+
s && (s = null, e = null), t.state.shape = null;
|
|
33
|
+
}
|
|
34
|
+
}, a);
|
|
35
|
+
};
|
|
36
|
+
export {
|
|
37
|
+
m as Stick
|
|
38
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(i,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("@supermousejs/utils")):typeof define=="function"&&define.amd?define(["exports","@supermousejs/utils"],s):(i=typeof globalThis<"u"?globalThis:i||self,s(i.SupermouseStick={},i.SupermouseUtils))})(this,(function(i,s){"use strict";const a=(u={})=>{const c=s.normalize(u.padding,10);let o=null,t=null;return s.definePlugin({name:u.name||"stick",priority:-10,install(e){e.registerHoverTarget("[data-supermouse-stick]")},update(e){const r=e.state.hoverTarget,l=e.state.interaction.stick===!0||e.state.interaction.stick==="true";if(r&&l){if(r!==o||!t){o=r;const n=s.dom.projectRect(r,e.container),h=window.getComputedStyle(r),d=c(e.state);t={width:n.width+d,height:n.height+d,radius:parseFloat(h.borderRadius)||0,x:n.left+n.width/2,y:n.top+n.height/2}}t&&(e.state.target.x=t.x,e.state.target.y=t.y,e.state.shape={width:t.width,height:t.height,borderRadius:t.radius})}else o&&(o=null,t=null),e.state.shape=null}},u)};i.Stick=a,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|
package/meta.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
{
|
|
3
|
+
"id": "stick",
|
|
4
|
+
"name": "Stick",
|
|
5
|
+
"package": "@supermousejs/stick",
|
|
6
|
+
"description": "Morphs the cursor shape to match the bounding box of hovered elements.",
|
|
7
|
+
"code": "app.use(Stick({ padding: 10 }));",
|
|
8
|
+
"recipeId": "sticky-element",
|
|
9
|
+
"icon": "stick",
|
|
10
|
+
"options": [
|
|
11
|
+
{ "name": "padding", "type": "number", "default": "10", "description": "Extra padding around the target element in pixels.", "reactive": true }
|
|
12
|
+
]
|
|
13
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@supermousejs/stick",
|
|
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,73 @@
|
|
|
1
|
+
|
|
2
|
+
import type { ValueOrGetter } from '@supermousejs/core';
|
|
3
|
+
import { definePlugin, normalize, dom } from '@supermousejs/utils';
|
|
4
|
+
|
|
5
|
+
export interface StickOptions {
|
|
6
|
+
name?: string;
|
|
7
|
+
isEnabled?: boolean;
|
|
8
|
+
/** Extra padding around the element when calculating shape. Default 10. */
|
|
9
|
+
padding?: ValueOrGetter<number>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const Stick = (options: StickOptions = {}) => {
|
|
13
|
+
const getPadding = normalize(options.padding, 10);
|
|
14
|
+
|
|
15
|
+
let lastTarget: HTMLElement | null = null;
|
|
16
|
+
let cache: { width: number; height: number; radius: number; x: number; y: number } | null = null;
|
|
17
|
+
|
|
18
|
+
return definePlugin({
|
|
19
|
+
name: options.name || 'stick',
|
|
20
|
+
// Logic plugin: must run before visuals to set the shape state
|
|
21
|
+
priority: -10,
|
|
22
|
+
|
|
23
|
+
install(app) {
|
|
24
|
+
app.registerHoverTarget('[data-supermouse-stick]');
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
update(app) {
|
|
28
|
+
// Check normalized interaction key (camelCase, no prefix)
|
|
29
|
+
const target = app.state.hoverTarget;
|
|
30
|
+
const isSticky = app.state.interaction.stick === true || app.state.interaction.stick === 'true';
|
|
31
|
+
|
|
32
|
+
if (target && isSticky) {
|
|
33
|
+
|
|
34
|
+
// Recalculate only if target changed or on first frame
|
|
35
|
+
if (target !== lastTarget || !cache) {
|
|
36
|
+
lastTarget = target;
|
|
37
|
+
|
|
38
|
+
const rect = dom.projectRect(target, app.container);
|
|
39
|
+
const style = window.getComputedStyle(target);
|
|
40
|
+
const padding = getPadding(app.state);
|
|
41
|
+
|
|
42
|
+
cache = {
|
|
43
|
+
width: rect.width + padding,
|
|
44
|
+
height: rect.height + padding,
|
|
45
|
+
radius: parseFloat(style.borderRadius) || 0,
|
|
46
|
+
x: rect.left + rect.width / 2,
|
|
47
|
+
y: rect.top + rect.height / 2
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (cache) {
|
|
52
|
+
// 1. Override Target (Physics) - Snap target to center of element
|
|
53
|
+
app.state.target.x = cache.x;
|
|
54
|
+
app.state.target.y = cache.y;
|
|
55
|
+
|
|
56
|
+
// 2. Set Shape (Visuals) - Visual plugins like Ring will read this
|
|
57
|
+
app.state.shape = {
|
|
58
|
+
width: cache.width,
|
|
59
|
+
height: cache.height,
|
|
60
|
+
borderRadius: cache.radius
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
// Reset
|
|
65
|
+
if (lastTarget) {
|
|
66
|
+
lastTarget = null;
|
|
67
|
+
cache = null;
|
|
68
|
+
}
|
|
69
|
+
app.state.shape = null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}, options);
|
|
73
|
+
};
|
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: 'SupermouseStick',
|
|
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
|
+
});
|