@c-technology/adaptive-ui 0.1.1 → 0.1.3
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/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +87 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -44
package/dist/index.cjs
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/adaptive.ts","../src/react.ts","../src/signals/network.ts","../src/signals/battery.ts","../src/signals/behavior.ts","../src/strategies.ts"],"sourcesContent":["export { AdaptiveUI } from \"./adaptive\";\nexport { useAdaptive } from \"./react\";\nexport * from \"./strategies\";\n","export type Context =\n | \"slow-network\"\n | \"low-battery\"\n | \"impatient\"\n | \"new-user\";\n\ntype Strategy = {\n conditions: Context[];\n actions: () => void;\n};\n\nexport class AdaptiveUI {\n private context = new Set<Context>();\n private strategies: Strategy[] = [];\n\n addContext(ctx: Context) {\n this.context.add(ctx);\n }\n\n has(ctx: Context) {\n return this.context.has(ctx);\n }\n\n strategy(strategy: Strategy) {\n this.strategies.push(strategy);\n }\n\n run() {\n for (const s of this.strategies) {\n if (s.conditions.every(c => this.context.has(c))) {\n s.actions();\n }\n }\n }\n\n explain() {\n return {\n context: Array.from(this.context),\n strategies: this.strategies.length,\n };\n }\n}\n","import { useEffect, useState } from \"react\";\nimport { AdaptiveUI } from \"./adaptive\";\nimport { detectSlowNetwork } from \"./signals/network\";\nimport { detectLowBattery } from \"./signals/battery\";\nimport { detectImpatience } from \"./signals/behavior\";\n\nexport function useAdaptive() {\n const [adaptive] = useState(() => new AdaptiveUI());\n\n useEffect(() => {\n detectSlowNetwork(() => adaptive.addContext(\"slow-network\"));\n detectLowBattery(() => adaptive.addContext(\"low-battery\"));\n detectImpatience(() => adaptive.addContext(\"impatient\"));\n }, []);\n\n return adaptive;\n}\n","export function detectSlowNetwork(onDetect: () => void) {\n const connection = (navigator as any).connection;\n if (!connection) return;\n\n if (\n connection.effectiveType === \"2g\" ||\n connection.effectiveType === \"slow-2g\"\n ) {\n onDetect();\n }\n}\n","export async function detectLowBattery(onDetect: () => void) {\n if (!(\"getBattery\" in navigator)) return;\n\n const battery = await (navigator as any).getBattery();\n if (battery.level < 0.2) {\n onDetect();\n }\n}\n","export function detectImpatience(onDetect: () => void) {\n let clicks = 0;\n\n window.addEventListener(\"click\", () => {\n clicks++;\n if (clicks >= 6) {\n onDetect();\n }\n });\n}\n","export function liteUI() {\n document.body.classList.add(\"ui-lite\");\n}\n\nexport function focusMode() {\n document.body.classList.add(\"ui-focus\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACWO,IAAM,aAAN,MAAiB;AAAA,EAAjB;AACL,SAAQ,UAAU,oBAAI,IAAa;AACnC,SAAQ,aAAyB,CAAC;AAAA;AAAA,EAElC,WAAW,KAAc;AACvB,SAAK,QAAQ,IAAI,GAAG;AAAA,EACtB;AAAA,EAEA,IAAI,KAAc;AAChB,WAAO,KAAK,QAAQ,IAAI,GAAG;AAAA,EAC7B;AAAA,EAEA,SAAS,UAAoB;AAC3B,SAAK,WAAW,KAAK,QAAQ;AAAA,EAC/B;AAAA,EAEA,MAAM;AACJ,eAAW,KAAK,KAAK,YAAY;AAC/B,UAAI,EAAE,WAAW,MAAM,OAAK,KAAK,QAAQ,IAAI,CAAC,CAAC,GAAG;AAChD,UAAE,QAAQ;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAAA,EAEA,UAAU;AACR,WAAO;AAAA,MACL,SAAS,MAAM,KAAK,KAAK,OAAO;AAAA,MAChC,YAAY,KAAK,WAAW;AAAA,IAC9B;AAAA,EACF;AACF;;;ACzCA,mBAAoC;;;ACA7B,SAAS,kBAAkB,UAAsB;AACtD,QAAM,aAAc,UAAkB;AACtC,MAAI,CAAC,WAAY;AAEjB,MACE,WAAW,kBAAkB,QAC7B,WAAW,kBAAkB,WAC7B;AACA,aAAS;AAAA,EACX;AACF;;;ACVA,eAAsB,iBAAiB,UAAsB;AAC3D,MAAI,EAAE,gBAAgB,WAAY;AAElC,QAAM,UAAU,MAAO,UAAkB,WAAW;AACpD,MAAI,QAAQ,QAAQ,KAAK;AACvB,aAAS;AAAA,EACX;AACF;;;ACPO,SAAS,iBAAiB,UAAsB;AACrD,MAAI,SAAS;AAEb,SAAO,iBAAiB,SAAS,MAAM;AACrC;AACA,QAAI,UAAU,GAAG;AACf,eAAS;AAAA,IACX;AAAA,EACF,CAAC;AACH;;;AHHO,SAAS,cAAc;AAC5B,QAAM,CAAC,QAAQ,QAAI,uBAAS,MAAM,IAAI,WAAW,CAAC;AAElD,8BAAU,MAAM;AACd,sBAAkB,MAAM,SAAS,WAAW,cAAc,CAAC;AAC3D,qBAAiB,MAAM,SAAS,WAAW,aAAa,CAAC;AACzD,qBAAiB,MAAM,SAAS,WAAW,WAAW,CAAC;AAAA,EACzD,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;;;AIhBO,SAAS,SAAS;AACvB,WAAS,KAAK,UAAU,IAAI,SAAS;AACvC;AAEO,SAAS,YAAY;AAC1B,WAAS,KAAK,UAAU,IAAI,UAAU;AACxC;","names":[]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
type Context = "slow-network" | "low-battery" | "impatient" | "new-user";
|
|
2
|
+
type Strategy = {
|
|
3
|
+
conditions: Context[];
|
|
4
|
+
actions: () => void;
|
|
5
|
+
};
|
|
6
|
+
declare class AdaptiveUI {
|
|
7
|
+
private context;
|
|
8
|
+
private strategies;
|
|
9
|
+
addContext(ctx: Context): void;
|
|
10
|
+
has(ctx: Context): boolean;
|
|
11
|
+
strategy(strategy: Strategy): void;
|
|
12
|
+
run(): void;
|
|
13
|
+
explain(): {
|
|
14
|
+
context: Context[];
|
|
15
|
+
strategies: number;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare function useAdaptive(): AdaptiveUI;
|
|
20
|
+
|
|
21
|
+
declare function liteUI(): void;
|
|
22
|
+
declare function focusMode(): void;
|
|
23
|
+
|
|
24
|
+
export { AdaptiveUI, focusMode, liteUI, useAdaptive };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// src/adaptive.ts
|
|
2
|
+
var AdaptiveUI = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.context = /* @__PURE__ */ new Set();
|
|
5
|
+
this.strategies = [];
|
|
6
|
+
}
|
|
7
|
+
addContext(ctx) {
|
|
8
|
+
this.context.add(ctx);
|
|
9
|
+
}
|
|
10
|
+
has(ctx) {
|
|
11
|
+
return this.context.has(ctx);
|
|
12
|
+
}
|
|
13
|
+
strategy(strategy) {
|
|
14
|
+
this.strategies.push(strategy);
|
|
15
|
+
}
|
|
16
|
+
run() {
|
|
17
|
+
for (const s of this.strategies) {
|
|
18
|
+
if (s.conditions.every((c) => this.context.has(c))) {
|
|
19
|
+
s.actions();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
explain() {
|
|
24
|
+
return {
|
|
25
|
+
context: Array.from(this.context),
|
|
26
|
+
strategies: this.strategies.length
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// src/react.ts
|
|
32
|
+
import { useEffect, useState } from "react";
|
|
33
|
+
|
|
34
|
+
// src/signals/network.ts
|
|
35
|
+
function detectSlowNetwork(onDetect) {
|
|
36
|
+
const connection = navigator.connection;
|
|
37
|
+
if (!connection) return;
|
|
38
|
+
if (connection.effectiveType === "2g" || connection.effectiveType === "slow-2g") {
|
|
39
|
+
onDetect();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/signals/battery.ts
|
|
44
|
+
async function detectLowBattery(onDetect) {
|
|
45
|
+
if (!("getBattery" in navigator)) return;
|
|
46
|
+
const battery = await navigator.getBattery();
|
|
47
|
+
if (battery.level < 0.2) {
|
|
48
|
+
onDetect();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/signals/behavior.ts
|
|
53
|
+
function detectImpatience(onDetect) {
|
|
54
|
+
let clicks = 0;
|
|
55
|
+
window.addEventListener("click", () => {
|
|
56
|
+
clicks++;
|
|
57
|
+
if (clicks >= 6) {
|
|
58
|
+
onDetect();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/react.ts
|
|
64
|
+
function useAdaptive() {
|
|
65
|
+
const [adaptive] = useState(() => new AdaptiveUI());
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
detectSlowNetwork(() => adaptive.addContext("slow-network"));
|
|
68
|
+
detectLowBattery(() => adaptive.addContext("low-battery"));
|
|
69
|
+
detectImpatience(() => adaptive.addContext("impatient"));
|
|
70
|
+
}, []);
|
|
71
|
+
return adaptive;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/strategies.ts
|
|
75
|
+
function liteUI() {
|
|
76
|
+
document.body.classList.add("ui-lite");
|
|
77
|
+
}
|
|
78
|
+
function focusMode() {
|
|
79
|
+
document.body.classList.add("ui-focus");
|
|
80
|
+
}
|
|
81
|
+
export {
|
|
82
|
+
AdaptiveUI,
|
|
83
|
+
focusMode,
|
|
84
|
+
liteUI,
|
|
85
|
+
useAdaptive
|
|
86
|
+
};
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/adaptive.ts","../src/react.ts","../src/signals/network.ts","../src/signals/battery.ts","../src/signals/behavior.ts","../src/strategies.ts"],"sourcesContent":["export type Context =\n | \"slow-network\"\n | \"low-battery\"\n | \"impatient\"\n | \"new-user\";\n\ntype Strategy = {\n conditions: Context[];\n actions: () => void;\n};\n\nexport class AdaptiveUI {\n private context = new Set<Context>();\n private strategies: Strategy[] = [];\n\n addContext(ctx: Context) {\n this.context.add(ctx);\n }\n\n has(ctx: Context) {\n return this.context.has(ctx);\n }\n\n strategy(strategy: Strategy) {\n this.strategies.push(strategy);\n }\n\n run() {\n for (const s of this.strategies) {\n if (s.conditions.every(c => this.context.has(c))) {\n s.actions();\n }\n }\n }\n\n explain() {\n return {\n context: Array.from(this.context),\n strategies: this.strategies.length,\n };\n }\n}\n","import { useEffect, useState } from \"react\";\nimport { AdaptiveUI } from \"./adaptive\";\nimport { detectSlowNetwork } from \"./signals/network\";\nimport { detectLowBattery } from \"./signals/battery\";\nimport { detectImpatience } from \"./signals/behavior\";\n\nexport function useAdaptive() {\n const [adaptive] = useState(() => new AdaptiveUI());\n\n useEffect(() => {\n detectSlowNetwork(() => adaptive.addContext(\"slow-network\"));\n detectLowBattery(() => adaptive.addContext(\"low-battery\"));\n detectImpatience(() => adaptive.addContext(\"impatient\"));\n }, []);\n\n return adaptive;\n}\n","export function detectSlowNetwork(onDetect: () => void) {\n const connection = (navigator as any).connection;\n if (!connection) return;\n\n if (\n connection.effectiveType === \"2g\" ||\n connection.effectiveType === \"slow-2g\"\n ) {\n onDetect();\n }\n}\n","export async function detectLowBattery(onDetect: () => void) {\n if (!(\"getBattery\" in navigator)) return;\n\n const battery = await (navigator as any).getBattery();\n if (battery.level < 0.2) {\n onDetect();\n }\n}\n","export function detectImpatience(onDetect: () => void) {\n let clicks = 0;\n\n window.addEventListener(\"click\", () => {\n clicks++;\n if (clicks >= 6) {\n onDetect();\n }\n });\n}\n","export function liteUI() {\n document.body.classList.add(\"ui-lite\");\n}\n\nexport function focusMode() {\n document.body.classList.add(\"ui-focus\");\n}\n"],"mappings":";AAWO,IAAM,aAAN,MAAiB;AAAA,EAAjB;AACL,SAAQ,UAAU,oBAAI,IAAa;AACnC,SAAQ,aAAyB,CAAC;AAAA;AAAA,EAElC,WAAW,KAAc;AACvB,SAAK,QAAQ,IAAI,GAAG;AAAA,EACtB;AAAA,EAEA,IAAI,K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AS,SAAS;AACvB,WAAS,KAAK,UAAU,IAAI,SAAS;AACvC;AAEO,SAAS,YAAY;AAC1B,WAAS,KAAK,UAAU,IAAI,UAAU;AACxC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "@c-technology/adaptive-ui",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"files": [
|
|
17
|
-
|
|
18
|
-
],
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
2
|
+
"name": "@c-technology/adaptive-ui",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Human-aware UI adaptation layer for web apps",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs.js",
|
|
7
|
+
"module": "./dist/index.cjs.js",
|
|
8
|
+
"types": "./dist/index.d.cts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"require": "./dist/index.cjs.js",
|
|
12
|
+
"import": "./dist/index.cjs.js",
|
|
13
|
+
"types": "./dist/index.d.cts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup",
|
|
22
|
+
"dev": "tsup --watch",
|
|
23
|
+
"lint": "tsc --noEmit",
|
|
24
|
+
"prepublishOnly": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"react": ">=18"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/react": "^19.2.7",
|
|
31
|
+
"react": "^19.2.3",
|
|
32
|
+
"tsup": "^8.5.1",
|
|
33
|
+
"typescript": "^5.3.0"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"adaptive-ui",
|
|
37
|
+
"ux",
|
|
38
|
+
"accessibility",
|
|
39
|
+
"frontend",
|
|
40
|
+
"react",
|
|
41
|
+
"typescript"
|
|
42
|
+
],
|
|
43
|
+
"author": "Your Name",
|
|
44
|
+
"license": "MIT"
|
|
45
|
+
}
|