Skip to content

Commit 6b09705

Browse files
committed
Add Critical battery state
1 parent 2bb1134 commit 6b09705

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

obs.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,21 @@
184184
// Get battery level and charging status.
185185
const { level, charging } = battery;
186186

187-
// The API doesn’t report Low Power Mode or similar, so let’s just assume
188-
// that a device with less than 20% charge is considered ‘low’.
187+
// The API doesn’t report Low Power Mode or similar. Treat ≤5% as
188+
// ‘critical’ and ≤20% as ‘low’.
189+
const critical = Number.isFinite(level) ? level <= 0.05 : null;
190+
window.obs.batteryCritical = critical;
191+
189192
const low = Number.isFinite(level) ? level <= 0.2 : null;
190193
window.obs.batteryLow = low;
191194

192-
// Add low-battery class to `html` element.
193-
html.classList.toggle('has-battery-low', !!low);
195+
// Add most urgent battery class.
196+
['critical','low'].forEach(t => html.classList.remove(`has-battery-${t}`));
197+
if (critical) {
198+
html.classList.add('has-battery-critical');
199+
} else if (low) {
200+
html.classList.add('has-battery-low');
201+
}
194202

195203
// Add a class to the `html` element if the device is currently charging
196204
const isCharging = !!charging;

0 commit comments

Comments
 (0)