Battery
Web Battery API 封装。
- Methods: getLevel(), isCharging(), onLevelChange(cb), onChargingChange(cb)
- Availability depends on browser support
TSXBattery — React
1import { useEffect, useState } from "react";
2import { Battery } from "@quan-erp/shared-frontend-core";
3
4export function BatteryStatus() {
5 const battery = Battery.instance;
6 const [level, setLevel] = useState<number | null>(null);
7 const [charging, setCharging] = useState(false);
8
9 useEffect(() => {
10 void (async () => {
11 setLevel(await battery.getLevel());
12 setCharging(await battery.isCharging());
13 })();
14
15 const onLevel = (next: number) => setLevel(next);
16 const onCharging = (next: boolean) => setCharging(next);
17 battery.onLevelChange(onLevel);
18 battery.onChargingChange(onCharging);
19
20 return () => {
21 // Prefer off* helpers if your package version exposes them.
22 };
23 }, [battery]);
24
25 return (
26 <span>
27 {level == null ? "—" : `${Math.round(level * 100)}%`}
28 {charging ? " (charging)" : ""}
29 </span>
30 );
31}