Battery Status
The battery status indicates the current battery level. This status is updated when the component is connected and whenever there are changes.
In the following example, a Twig component will display the information received from the Stimulus Controller.
<?php
namespace App\Twig\Component;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveListener;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
#[AsLiveComponent('Battery')]
class Battery
{
use DefaultActionTrait;
use ComponentToolsTrait;
#[LiveProp]
public string $charging = '';
#[LiveProp]
public string $level = '';
#[LiveProp]
public string $chargingTime = '';
#[LiveProp]
public string $dischargingTime = '';
#[LiveListener('updated')]
public function onUpdate(
#[LiveArg] bool $charging,
#[LiveArg] float $level,
#[LiveArg] ?int $chargingTime,
#[LiveArg] ?int $dischargingTime,
): void {
$this->charging = $charging ? 'Yes' : 'No';
$this->level = ($level* 100) . '%';
$this->chargingTime = $this->formatSeconds($chargingTime);
$this->dischargingTime = $this->formatSeconds($dischargingTime);
}
private function formatSeconds(?float $s): string
{
if ($s === null ||!is_finite($s)) {
return '∞';
}
if ($s === 0.0) {
return 'done';
}
$h = str_pad((string) floor($s / 3600), 2, '0', STR_PAD_LEFT);
$m = str_pad((string) floor(($s % 3600) / 60), 2, '0', STR_PAD_LEFT);
$sec = str_pad((string) floor($s % 60), 2, '0', STR_PAD_LEFT);
return "$h:$m:$sec";
}
}Next, simply place the live component where needed in your templates.
Parameters
None
Actions
None
Targets
None
Events
Upon status change, the event pwa--battery:updated is triggered. The payload includes the following data:
charging: indicates whether the battery is charging or not.level: the current battery level.chargingTime: the time needed to recharge the battery from empty to full.dischargingTime: the time the battery can last before being completely discharged.
Last updated
Was this helpful?