@supermousejs/states 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/states
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,16 @@
1
+ import { SupermousePlugin } from '../../core/src/index.ts';
2
+
3
+ export declare const States: (options: StatesOptions) => SupermousePlugin;
4
+
5
+ export declare interface StatesOptions {
6
+ name?: string;
7
+ isEnabled?: boolean;
8
+ /** The default state (active when nothing else is hovered). */
9
+ default: string[];
10
+ /** Map of state names to lists of plugin names to enable. */
11
+ states: Record<string, string[]>;
12
+ /** CSS selector attribute to trigger state change. Default 'data-supermouse-state'. */
13
+ attribute?: string;
14
+ }
15
+
16
+ export { }
package/dist/index.mjs ADDED
@@ -0,0 +1,38 @@
1
+ import { definePlugin as f } from "@supermousejs/utils";
2
+ const v = (a) => {
3
+ const r = a.attribute || "data-supermouse-state", i = /* @__PURE__ */ new Set(), u = new Set(a.default);
4
+ Object.values(a.states).forEach((e) => {
5
+ e.forEach((l) => i.add(l));
6
+ });
7
+ let c = "default", d = !1;
8
+ return f({
9
+ name: "states",
10
+ // High priority: Run BEFORE visual plugins so they know if they are enabled/disabled this frame
11
+ priority: -999,
12
+ install(e) {
13
+ e.registerHoverTarget(`[${r}]`);
14
+ },
15
+ update(e) {
16
+ d || (i.forEach((t) => {
17
+ u.has(t) || e.disablePlugin(t);
18
+ }), u.forEach((t) => {
19
+ e.enablePlugin(t);
20
+ }), d = !0);
21
+ const l = e.state.hoverTarget;
22
+ let s = "default";
23
+ if (l && l.hasAttribute(r)) {
24
+ const t = l.getAttribute(r);
25
+ t && a.states[t] && (s = t);
26
+ }
27
+ if (s !== c) {
28
+ const t = s === "default" ? a.default : a.states[s];
29
+ (/* @__PURE__ */ new Set([...u, ...i])).forEach((n) => {
30
+ t.includes(n) ? e.enablePlugin(n) : e.disablePlugin(n);
31
+ }), c = s;
32
+ }
33
+ }
34
+ }, a);
35
+ };
36
+ export {
37
+ v as States
38
+ };
@@ -0,0 +1 @@
1
+ (function(s,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("@supermousejs/utils")):typeof define=="function"&&define.amd?define(["exports","@supermousejs/utils"],i):(s=typeof globalThis<"u"?globalThis:s||self,i(s.SupermouseStates={},s.SupermouseUtils))})(this,(function(s,i){"use strict";const h=u=>{const n=u.attribute||"data-supermouse-state",r=new Set,d=new Set(u.default);Object.values(u.states).forEach(t=>{t.forEach(a=>r.add(a))});let o="default",c=!1;return i.definePlugin({name:"states",priority:-999,install(t){t.registerHoverTarget(`[${n}]`)},update(t){c||(r.forEach(e=>{d.has(e)||t.disablePlugin(e)}),d.forEach(e=>{t.enablePlugin(e)}),c=!0);const a=t.state.hoverTarget;let l="default";if(a&&a.hasAttribute(n)){const e=a.getAttribute(n);e&&u.states[e]&&(l=e)}if(l!==o){const e=l==="default"?u.default:u.states[l];new Set([...d,...r]).forEach(f=>{e.includes(f)?t.enablePlugin(f):t.disablePlugin(f)}),o=l}}},u)};s.States=h,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
package/meta.json ADDED
@@ -0,0 +1,14 @@
1
+
2
+ {
3
+ "id": "states",
4
+ "name": "States",
5
+ "package": "@supermousejs/states",
6
+ "description": "Logic controller that enables/disables other plugins based on hover attributes.",
7
+ "code": "app.use(States({ default: ['dot'], states: { 'hover': ['ring'] } }));",
8
+ "icon": "context",
9
+ "options": [
10
+ { "name": "default", "type": "string[]", "default": "[]", "description": "List of plugin names active by default.", "reactive": false },
11
+ { "name": "states", "type": "Record<string, string[]>", "default": "{}", "description": "Map of state names to lists of active plugins.", "reactive": false },
12
+ { "name": "attribute", "type": "string", "default": "'data-supermouse-state'", "description": "DOM attribute to trigger state changes.", "reactive": false }
13
+ ]
14
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@supermousejs/states",
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,97 @@
1
+ import { definePlugin } from '@supermousejs/utils';
2
+
3
+ export interface StatesOptions {
4
+ name?: string;
5
+ isEnabled?: boolean;
6
+ /** The default state (active when nothing else is hovered). */
7
+ default: string[];
8
+ /** Map of state names to lists of plugin names to enable. */
9
+ states: Record<string, string[]>;
10
+ /** CSS selector attribute to trigger state change. Default 'data-supermouse-state'. */
11
+ attribute?: string;
12
+ }
13
+
14
+ export const States = (options: StatesOptions) => {
15
+ const attr = options.attribute || 'data-supermouse-state';
16
+
17
+ // Cache the relationships for fast lookups
18
+ const allManagedPlugins = new Set<string>(); // All plugins mentioned in 'states'
19
+ const defaultPlugins = new Set(options.default);
20
+
21
+ // Populate the set of managed plugins (excluding defaults for now)
22
+ Object.values(options.states).forEach(list => {
23
+ list.forEach(p => allManagedPlugins.add(p));
24
+ });
25
+
26
+ let currentState = 'default';
27
+ let hasInitialized = false;
28
+
29
+ return definePlugin({
30
+ name: 'states',
31
+ // High priority: Run BEFORE visual plugins so they know if they are enabled/disabled this frame
32
+ priority: -999,
33
+
34
+ install(app) {
35
+ // Register the trigger attribute
36
+ app.registerHoverTarget(`[${attr}]`);
37
+ },
38
+
39
+ update(app) {
40
+ // --- INITIALIZATION STEP ---
41
+ // We do this in the first update tick to ensure all other plugins
42
+ // have been registered via .use(), regardless of order.
43
+ if (!hasInitialized) {
44
+ // 1. Disable all "Special State" plugins initially
45
+ allManagedPlugins.forEach(name => {
46
+ if (!defaultPlugins.has(name)) {
47
+ app.disablePlugin(name);
48
+ }
49
+ });
50
+
51
+ // 2. Ensure default plugins are enabled
52
+ defaultPlugins.forEach(name => {
53
+ app.enablePlugin(name);
54
+ });
55
+
56
+ hasInitialized = true;
57
+ }
58
+
59
+ // --- STANDARD LOGIC ---
60
+ const target = app.state.hoverTarget;
61
+ let nextState = 'default';
62
+
63
+ // 1. Determine Desired State
64
+ if (target && target.hasAttribute(attr)) {
65
+ const val = target.getAttribute(attr);
66
+ if (val && options.states[val]) {
67
+ nextState = val;
68
+ }
69
+ }
70
+
71
+ // 2. Switch State (Only if changed)
72
+ if (nextState !== currentState) {
73
+
74
+ // A. Determine which plugins should be active
75
+ const activePlugins = nextState === 'default'
76
+ ? options.default
77
+ : options.states[nextState];
78
+
79
+ // B. Apply changes
80
+ // Note: We combine default + managed to ensure we cover everyone involved
81
+ const involved = new Set([...defaultPlugins, ...allManagedPlugins]);
82
+
83
+ involved.forEach(pluginName => {
84
+ const shouldBeActive = activePlugins.includes(pluginName);
85
+
86
+ if (shouldBeActive) {
87
+ app.enablePlugin(pluginName);
88
+ } else {
89
+ app.disablePlugin(pluginName);
90
+ }
91
+ });
92
+
93
+ currentState = nextState;
94
+ }
95
+ }
96
+ }, options);
97
+ };
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: 'SupermouseStates',
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
+ });