Battery api provide an easy way to handle battery status in browser.
Battery api can provide you following information:-
Battery charging level between 0.00 to 1.00 in decimal number. You can easily convert this decimal number into percentage by multiplying with 100.
Charging or Discharging boolean status
Charging time in seconds
Discharging time in seconds
SolidJS
Solid is a declarative JavaScript library for creating user interfaces. It does not use a Virtual DOM. Instead it opts to compile its templates down to real DOM nodes and wrap updates in fine grained reactions. This way when your state updates only the code that depends on it runs. SolidJS Documentation
Init new Solidjs project from Javascript template
> npx degit solidjs/templates/js my-app
> cd my-app
> pnpm i
> pnpm run dev
onMount is a lifecycle function in solidjs which run only once when component mount you can run side effect inside this function like api call. We use this function to add event listener for battery event and initialise store with initial value provided by battery api.
The createStore call takes the initial value (here we provide battery related fields) and returns a read/write tuple. The first element is the store proxy which is readonly, and the second is a setter function.
navigator.getBattery() call web battery api and return promise this resolves to BatteryManager Object contains information about charging level, charging time etc...
If browser does not support battery web api then catch block will execute and it update isSupported to false and on ui screen we can show alert box.
chargingchange event fired when you connect or disconnect your device from charger.
levelchange event fired when your device battery level change. Suppose your device is charging and their level change from 0.79 to 0.80 then this event will fired.
chargingtimechange event fired when battery charging time will updated
dischargingtimechange fired when battery discharging time will updated.
Lets build the ui to show the battery related data in App.js
import{Alert,AlertDescription,AlertIcon,AlertTitle,Box,CircularProgress,CircularProgressIndicator,CircularProgressLabel,Container,GridItem,Heading,HopeProvider,HStack,SimpleGrid,Tag,Text,VStack,}from"@hope-ui/solid";import{Show}from"solid-js";importThemeSwitcherfrom"./components/ThemeSwitcher";importuseBatteryfrom"./hooks/useBattery";exportdefaultfunctionApp(){const{store}=useBattery();return (<HopeProviderconfig={{initialColorMode:"dark"}}><BoxminH={"100vh"}h="$full"w={"$full"}py="$10"><VStackspacing={"$3"}><HeadingtextAlign={"center"}fontSize={"$6xl"}>
Battery <Boxas="span"color={"$primary10"}>
Monitor
</Box></Heading><HStackspacing={"$2"}><Showwhen={store.isSupported}><TagcolorScheme={store.charging?"success":"danger"}size={"lg"}variant="dot"dotPlacement="start"><Showwhen={store.charging}fallback="Discharging">
Charging
</Show></Tag></Show><ThemeSwitcher/></HStack></VStack><Containermt={"$10"}><Showwhen={store.isSupported}fallback={<Alertstatus="danger"variant="subtle"flexDirection="column"justifyContent="center"textAlign="center"height="200px"><AlertIconboxSize="40px"mr="0"/><AlertTitlemt="$4"mb="$1"fontSize="$lg">
Unsupported Browser
</AlertTitle><AlertDescriptionmaxWidth="$sm">
Your browser does not support Web Battery API
</AlertDescription></Alert>}><SimpleGridw={"$full"}columns={{"@initial":1,"@sm":2,"@md":3}}justifyContent="center"><GridItemmx={"auto"}><CircularProgressthickness={"$0_5"}size={"$xs"}value={100}><CircularProgressIndicatorcolor={"$warning10"}/><CircularProgressLabel><VStackspacing={"$6"}><HeadingfontSize={"$xl"}> Charging Time</Heading><TextfontSize={"$xl"}>{Math.round(store.chargingTime/60)} Minutes
</Text></VStack></CircularProgressLabel></CircularProgress></GridItem><GridItemmx={"auto"}><CircularProgresssize={"$xs"}value={store.level*100}><CircularProgressIndicatorcolor={"$success10"}/><CircularProgressLabel><VStackspacing={"$6"}><HeadingfontSize={"$xl"}> Battery Level</Heading><TextfontSize={"$xl"}>{store.level*100} %</Text></VStack></CircularProgressLabel></CircularProgress></GridItem><GridItemmx={"auto"}><CircularProgressthickness={"$0_5"}size={"$xs"}value={100}><CircularProgressIndicatorcolor={"$primary10"}/><CircularProgressLabel><VStackspacing={"$6"}><HeadingfontSize={"$xl"}> Discharging Time</Heading><TextfontSize={"$xl"}>{Math.round(store.dischargingTime/60)} Minutes
</Text></VStack></CircularProgressLabel></CircularProgress></GridItem></SimpleGrid></Show></Container></Box></HopeProvider>);}
ThemeSwitcher.jsx component handle switching between dark and light mode.
Those templates dependencies are maintained via pnpm via pnpm up -Lri.
This is the reason you see a pnpm-lock.yaml. That being said, any package manager will work. This file can be safely be removed once you clone a template.