getBattery

Category Sensors

Provides information about the system's battery charge level and lets you be notified by events that are sent when the battery level or charging status change.

Demo

{
  "isSupported": false,
  "charging": 0,
  "chargingTime": 0,
  "dischargingTime": 0,
  "level": 1
}

Usage

<script>
	import { getBattery } from '@sv-use/core';
 
	const battery = getBattery();
</script>
Type definitions
import type { CleanupFunction } from '../../__internal__/types.js';
export interface BatteryManager extends EventTarget {
    readonly charging: number;
    readonly chargingTime: number;
    readonly dischargingTime: number;
    readonly level: number;
}
type GetBatteryOptions = {
    /**
     * Whether to auto-cleanup the event listener or not.
     *
     * If set to `true`, it must run in the component initialization lifecycle.
     * @default true
     */
    autoCleanup?: boolean;
};
type GetBatteryReturn = {
    /** Whether the {@link https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API | Battery Status API} is supported by the browser or not. */
    readonly isSupported: boolean;
    /** Whether the battery is currently being charged or not. */
    readonly charging: number;
    /** The remaining time in seconds until the battery is fully charged, or 0 if the battery is already fully charged. */
    readonly chargingTime: number;
    /** The remaining time in seconds until the battery is completely discharged and the system suspends. */
    readonly dischargingTime: number;
    /** The system's battery charge level scaled to a value between 0.0 and 1.0. */
    readonly level: number;
    /**
     * Cleans up the event listeners.
     * @note Is called automatically if `options.autoCleanup` is set to `true`.
     */
    cleanup: CleanupFunction;
};
/**
 * Retrieves information about the battery.
 * @see https://svelte-librarian.github.io/sv-use/docs/core/sensors/get-battery
 */
export declare function getBattery(options?: GetBatteryOptions): GetBatteryReturn;
export {};

Sources