@ailuracode/alpine-battery 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) ailuracode
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/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # @ailuracode/alpine-battery
2
+
3
+ Battery status magic for Alpine.js.
4
+
5
+ **[Full documentation →](../../docs/battery.md)**
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @ailuracode/alpine-battery alpinejs
11
+ ```
12
+
13
+ ## Quick example
14
+
15
+ ```js
16
+ import Alpine from "alpinejs";
17
+ import battery from "@ailuracode/alpine-battery";
18
+
19
+ Alpine.plugin(battery);
20
+ Alpine.start();
21
+ ```
22
+
23
+ ```html
24
+ <div x-show="$battery.isAvailable">
25
+ Battery: <span x-text="Math.round($battery.level * 100)"></span>%
26
+ <span x-show="$battery.isCharging">(charging)</span>
27
+ </div>
28
+ ```
29
+
30
+ ## API summary
31
+
32
+ | | |
33
+ |-|-|
34
+ | **Magic** | `$battery` |
35
+ | **Properties** | `isAvailable`, `level`, `isCharging`, `chargingTime`, `dischargingTime` |
36
+
37
+ ## License
38
+
39
+ MIT
@@ -0,0 +1,17 @@
1
+ /// <reference types="@types/alpinejs" />
2
+
3
+ export interface BatteryMagic {
4
+ isAvailable: boolean;
5
+ level: number | null;
6
+ isCharging: boolean;
7
+ chargingTime: number | null;
8
+ dischargingTime: number | null;
9
+ }
10
+
11
+ declare global {
12
+ namespace Alpine {
13
+ interface Magics<T> {
14
+ $battery: BatteryMagic;
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,30 @@
1
+ import AlpineType from 'alpinejs';
2
+
3
+ interface BatteryMagic {
4
+ isAvailable: boolean;
5
+ level: number | null;
6
+ isCharging: boolean;
7
+ chargingTime: number | null;
8
+ dischargingTime: number | null;
9
+ }
10
+ interface BatteryManagerLike {
11
+ charging: boolean;
12
+ chargingTime: number;
13
+ dischargingTime: number;
14
+ level: number;
15
+ addEventListener(type: string, listener: EventListener): void;
16
+ dispatchEvent(event: Event): boolean;
17
+ }
18
+ /** Reads battery state from a BatteryManager, or unavailable defaults. */
19
+ declare function readBatteryState(manager?: BatteryManagerLike): BatteryMagic;
20
+ /** Alpine.js battery plugin. Registers reactive magic `$battery`. */
21
+ declare function batteryPlugin(Alpine: AlpineType.Alpine): void;
22
+ declare global {
23
+ namespace Alpine {
24
+ interface Magics<T> {
25
+ $battery: BatteryMagic;
26
+ }
27
+ }
28
+ }
29
+
30
+ export { type BatteryMagic, type BatteryManagerLike, batteryPlugin as default, readBatteryState };
package/dist/index.js ADDED
@@ -0,0 +1,46 @@
1
+ // src/index.ts
2
+ function normalizeTime(seconds) {
3
+ return Number.isFinite(seconds) ? seconds : null;
4
+ }
5
+ function readBatteryState(manager) {
6
+ if (!manager) {
7
+ return {
8
+ isAvailable: false,
9
+ level: null,
10
+ isCharging: false,
11
+ chargingTime: null,
12
+ dischargingTime: null
13
+ };
14
+ }
15
+ return {
16
+ isAvailable: true,
17
+ level: manager.level,
18
+ isCharging: manager.charging,
19
+ chargingTime: normalizeTime(manager.chargingTime),
20
+ dischargingTime: normalizeTime(manager.dischargingTime)
21
+ };
22
+ }
23
+ function batteryPlugin(Alpine) {
24
+ const state = Alpine.reactive(readBatteryState());
25
+ Alpine.magic("battery", () => state);
26
+ const nav = navigator;
27
+ if (typeof nav.getBattery !== "function") {
28
+ return;
29
+ }
30
+ nav.getBattery().then((battery) => {
31
+ const update = () => {
32
+ Object.assign(state, readBatteryState(battery));
33
+ };
34
+ update();
35
+ battery.addEventListener("chargingchange", update);
36
+ battery.addEventListener("levelchange", update);
37
+ battery.addEventListener("chargingtimechange", update);
38
+ battery.addEventListener("dischargingtimechange", update);
39
+ }).catch(() => {
40
+ });
41
+ }
42
+ export {
43
+ batteryPlugin as default,
44
+ readBatteryState
45
+ };
46
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type AlpineType from \"alpinejs\";\n\nexport interface BatteryMagic {\n isAvailable: boolean;\n level: number | null;\n isCharging: boolean;\n chargingTime: number | null;\n dischargingTime: number | null;\n}\n\nexport interface BatteryManagerLike {\n charging: boolean;\n chargingTime: number;\n dischargingTime: number;\n level: number;\n addEventListener(type: string, listener: EventListener): void;\n dispatchEvent(event: Event): boolean;\n}\n\ntype NavigatorWithBattery = Navigator & {\n getBattery?: () => Promise<BatteryManagerLike>;\n};\n\nfunction normalizeTime(seconds: number): number | null {\n return Number.isFinite(seconds) ? seconds : null;\n}\n\n/** Reads battery state from a BatteryManager, or unavailable defaults. */\nexport function readBatteryState(manager?: BatteryManagerLike): BatteryMagic {\n if (!manager) {\n return {\n isAvailable: false,\n level: null,\n isCharging: false,\n chargingTime: null,\n dischargingTime: null,\n };\n }\n\n return {\n isAvailable: true,\n level: manager.level,\n isCharging: manager.charging,\n chargingTime: normalizeTime(manager.chargingTime),\n dischargingTime: normalizeTime(manager.dischargingTime),\n };\n}\n\n/** Alpine.js battery plugin. Registers reactive magic `$battery`. */\nexport default function batteryPlugin(Alpine: AlpineType.Alpine): void {\n const state = Alpine.reactive<BatteryMagic>(readBatteryState());\n\n Alpine.magic(\"battery\", () => state);\n\n const nav = navigator as NavigatorWithBattery;\n if (typeof nav.getBattery !== \"function\") {\n return;\n }\n\n nav\n .getBattery()\n .then((battery) => {\n const update = () => {\n Object.assign(state, readBatteryState(battery));\n };\n\n update();\n battery.addEventListener(\"chargingchange\", update);\n battery.addEventListener(\"levelchange\", update);\n battery.addEventListener(\"chargingtimechange\", update);\n battery.addEventListener(\"dischargingtimechange\", update);\n })\n .catch(() => {\n // API present but unavailable — keep isAvailable false\n });\n}\n\ndeclare global {\n namespace Alpine {\n interface Magics<T> {\n $battery: BatteryMagic;\n }\n }\n}\n"],"mappings":";AAuBA,SAAS,cAAc,SAAgC;AACrD,SAAO,OAAO,SAAS,OAAO,IAAI,UAAU;AAC9C;AAGO,SAAS,iBAAiB,SAA4C;AAC3E,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,MACL,aAAa;AAAA,MACb,OAAO;AAAA,MACP,YAAY;AAAA,MACZ,cAAc;AAAA,MACd,iBAAiB;AAAA,IACnB;AAAA,EACF;AAEA,SAAO;AAAA,IACL,aAAa;AAAA,IACb,OAAO,QAAQ;AAAA,IACf,YAAY,QAAQ;AAAA,IACpB,cAAc,cAAc,QAAQ,YAAY;AAAA,IAChD,iBAAiB,cAAc,QAAQ,eAAe;AAAA,EACxD;AACF;AAGe,SAAR,cAA+B,QAAiC;AACrE,QAAM,QAAQ,OAAO,SAAuB,iBAAiB,CAAC;AAE9D,SAAO,MAAM,WAAW,MAAM,KAAK;AAEnC,QAAM,MAAM;AACZ,MAAI,OAAO,IAAI,eAAe,YAAY;AACxC;AAAA,EACF;AAEA,MACG,WAAW,EACX,KAAK,CAAC,YAAY;AACjB,UAAM,SAAS,MAAM;AACnB,aAAO,OAAO,OAAO,iBAAiB,OAAO,CAAC;AAAA,IAChD;AAEA,WAAO;AACP,YAAQ,iBAAiB,kBAAkB,MAAM;AACjD,YAAQ,iBAAiB,eAAe,MAAM;AAC9C,YAAQ,iBAAiB,sBAAsB,MAAM;AACrD,YAAQ,iBAAiB,yBAAyB,MAAM;AAAA,EAC1D,CAAC,EACA,MAAM,MAAM;AAAA,EAEb,CAAC;AACL;","names":[]}
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@ailuracode/alpine-battery",
3
+ "version": "0.1.0",
4
+ "description": "Alpine.js battery status magic",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "ailuracode",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/ailuracode/alpine.git",
14
+ "directory": "packages/battery"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/ailuracode/alpine/issues"
18
+ },
19
+ "homepage": "https://github.com/ailuracode/alpine/tree/master/packages/battery#readme",
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "./global": {
30
+ "types": "./dist/global.d.ts"
31
+ }
32
+ },
33
+ "types": "./dist/global.d.ts",
34
+ "peerDependencies": {
35
+ "alpinejs": "^3.0.0",
36
+ "@types/alpinejs": "^3.13.11"
37
+ },
38
+ "peerDependenciesMeta": {
39
+ "@types/alpinejs": {
40
+ "optional": true
41
+ }
42
+ },
43
+ "keywords": [
44
+ "alpinejs",
45
+ "alpine",
46
+ "plugin",
47
+ "battery",
48
+ "power",
49
+ "charging"
50
+ ],
51
+ "scripts": {
52
+ "build": "tsup src/index.ts --format esm --dts --clean --sourcemap --out-dir dist && cp src/global.d.ts dist/global.d.ts",
53
+ "test": "vitest run --config ../../vitest.config.js test"
54
+ }
55
+ }