Collection
Life / Smart Home · v0.2.1Validated
See how many uses remain before the next descale.
Nespresso descaling · Preview
Loading widget…
Preparing source code…
Idle
Life / Smart Home · v0.2.1Validated
See how many uses remain before the next descale.
Loading widget…
Preparing source code…
Load the Lumo theme once in your app entry point. The example uses the local Lumo workspace packages.
import "@lumo/tokens/tokens.css";
import { CoffeeDescalingWidget } from "@lumo/widgets";
<CoffeeDescalingWidget
startDate="2026-10-27"
asOfDate="2026-11-20"
uses={95}
useLimit={100}
onLogUse={recordUse}
onDescaled={startNewCycle}
/>import "@lumo/tokens/tokens.css";
import { CoffeeDescalingDemo } from "./CoffeeDescalingDemo";
export default function Example() {
return <CoffeeDescalingDemo />;
}Props for integrating the React component in your app.
machineNamestringstartDate / asOfDatestring / stringuses / useLimitnumber | null / numberwarningThresholdnumberaverageUsesPerDaynumberlayout"full" | "glance"surface"card" | "embedded"onLogUse / onDescaled() => void / () => void{
"machineName": "Kitchen machine",
"startDate": "2026-10-27",
"asOfDate": "2026-11-20",
"uses": 95,
"useLimit": 100
}"use client";
import { useId, useRef, useState, type KeyboardEvent, type ReactNode } from "react";
import { AnimatedNumber, Button, Progress, WidgetCard, WidgetDialog, type WidgetCardProps } from "@lumo/ui";
import { MobilityStatus } from "../mobility/MobilityStatus";
import { CapsuleIcon, CapsuleOutline } from "./CapsuleOutline";
import styles from "./CoffeeDescalingWidget.module.css";
export interface CoffeeDescalingWidgetProps {
machineName?: string;
/** Date the current cycle began, as YYYY-MM-DD. */
startDate: string;
/** Date of the supplied usage reading, as YYYY-MM-DD. */
asOfDate: string;
uses: number | null;
/** Configured number of uses between descaling cycles. */
useLimit: number;
warningThreshold?: number;
/** Optional usage rate from a separate history. Otherwise calculated from the current cycle. */
averageUsesPerDay?: number;
/** Choose the full layout or a compact summary with a separate cycle-details view. */
layout?: "full" | "glance";
surface?: WidgetCardProps["surface"];
onLogUse?: () => void;
onDescaled?: () => void;
}
const dayMs = 86_400_000;
const dateLabel = new Intl.DateTimeFormat("en-GB", { day: "numeric", month: "short", year: "numeric", timeZone: "UTC" });
function calendarDay(value: string) {
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) return null;
const elapsed = Date.parse(value);
return Number.isFinite(elapsed) && new Date(elapsed).toISOString().slice(0, 10) === value ? elapsed : null;
}
function projectDate(start: number | null, observed: number | null, uses: number, remaining: number | null, rate?: number) {
if (start === null || observed === null || observed < start || remaining === null) return null;
if (remaining === 0) return observed;
const days = (observed - start) / dayMs;
const daily = rate !== undefined && Number.isFinite(rate) && rate > 0 ? rate : days > 0 ? uses / days : 0;
if (daily <= 0) return null;
const forecast = observed + Math.ceil(remaining / daily) * dayMs;
return Number.isFinite(new Date(forecast).getTime()) && forecast <= Date.UTC(9999, 11, 31) ? forecast : null;
}
function CupIcon() {
return <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.65" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<path d="M4 8h13v3a6.5 6.5 0 0 1-13 0V8Zm13 1h2a2 2 0 1 1 0 4h-2M3 19h17M8 3v2m5-2v2" />
</svg>;
}
function CoffeeMachine() {
return (
<svg className={styles.machine} viewBox="0 0 237 248" role="img" aria-label="Front view of a capsule coffee machine with a cup and three capsules">
<path className={styles.machineShell} d="M15 219V81a60 60 0 1 1 120 0v138Z" />
<path className={styles.machineShell} d="M70 113c-13-13-28-31-28-47a33 33 0 0 1 66 0c0 16-15 34-27 47Z" />
<path className={styles.detail} d="M46 17q29-12 58 0" />
<path className={styles.recess} d="M26 219V124h98v95Z" />
<path className={styles.detail} d="M26 190h98m-22-38h3a10 10 0 0 1 0 20h-7" />
<path className={styles.cup} d="M41 144h62c0 18-6 32-15 46H56c-9-14-15-28-15-46Z" />
<path className={styles.machineShell} d="M10 219h130l2 20H8l2-20Z" />
<g className={styles.capsulePile}>
<g transform="translate(150 200) scale(1.85)"><CapsuleOutline /></g>
<g transform="translate(191.5 200) scale(1.85)"><CapsuleOutline /></g>
<g transform="translate(168.5 158) scale(1.85)"><CapsuleOutline /></g>
</g>
</svg>
);
}
function CoffeeMachineIcon() {
return <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<path d="M4 21V10a8 8 0 1 1 16 0v11M3 21h18M9 8h6M8 12h8v3a4 4 0 0 1-8 0z" />
</svg>;
}
function CycleDate({ label, date }: { label: string; date: number | null }) {
return (
<div className={styles.dateTile}>
<dt>{label}</dt>
<dd>{date === null ? <span>—</span> : <time dateTime={new Date(date).toISOString().slice(0, 10)}>{dateLabel.format(date)}</time>}</dd>
</div>
);
}
type CycleState = "unknown" | "due" | "warning" | "ok";
type GlanceView = "summary" | "details" | "confirm";
function CoffeeGlance({ machineName, state, surface, usage, status, dates, onLogUse, onDescaled }: {
machineName: string;
state: CycleState;
surface: WidgetCardProps["surface"];
usage: ReactNode;
status: ReactNode;
dates: ReactNode;
onLogUse?: () => void;
onDescaled?: () => void;
}) {
const [view, setView] = useState<GlanceView>("summary");
const navigation = useRef<HTMLButtonElement>(null);
function goTo(next: GlanceView) {
setView(next);
navigation.current?.focus({ preventScroll: true });
}
function back() {
goTo(view === "confirm" ? "details" : "summary");
}
function handleEscape(event: KeyboardEvent) {
if (event.key !== "Escape" || view === "summary") return;
event.stopPropagation();
back();
}
return (
<WidgetCard className={`${styles.card} ${styles.glance}`} size="small" surface={surface} icon={<CoffeeMachineIcon />}
title={machineName} description="Descaling" action={
<Button type="button" ref={navigation} className={styles.glanceNav} size="xs" variant="ghost"
onClick={view === "summary" ? () => goTo("details") : back} onKeyDown={handleEscape}>
{view === "summary" ? "Details" : "Back"}
</Button>
}>
<div className={styles.glanceBody} data-state={state} onKeyDown={handleEscape}>
{view === "summary" && <>
{usage}
<div className={styles.glanceStatus} role="status"><span aria-hidden="true" />{status}</div>
{onLogUse && <Button size="xs" className={styles.glanceAction} type="button" onClick={onLogUse}><CapsuleIcon className={styles.capsule} />Log use</Button>}
</>}
{view === "details" && <>
{dates}
{onDescaled && <Button size="xs" className={styles.glanceAction} type="button" variant="secondary" onClick={() => goTo("confirm")}>Mark descaled</Button>}
</>}
{view === "confirm" && <>
<p className={styles.glanceQuestion}>Start a new cycle and reset the use counter?</p>
<div className={styles.glanceConfirm}>
<Button size="xs" className={styles.glanceAction} type="button" variant="secondary" onClick={back}>Cancel</Button>
<Button size="xs" className={styles.glanceAction} type="button" onClick={() => { onDescaled?.(); goTo("summary"); }}>Reset cycle</Button>
</div>
</>}
</div>
</WidgetCard>
);
}
export function CoffeeDescalingWidget({
machineName = "Nespresso", startDate, asOfDate, uses, useLimit, warningThreshold = 5, averageUsesPerDay,
layout = "full", surface, onLogUse, onDescaled,
}: CoffeeDescalingWidgetProps) {
const usageId = useId();
const [confirm, setConfirm] = useState(false);
const resetButton = useRef<HTMLButtonElement>(null);
const count = uses !== null && Number.isFinite(uses) && uses >= 0 ? Math.trunc(uses) : null;
const limit = Number.isFinite(useLimit) && useLimit >= 1 ? Math.trunc(useLimit) : null;
const warning = Number.isFinite(warningThreshold) ? Math.max(0, Math.trunc(warningThreshold)) : 5;
const remaining = limit === null || count === null ? null : Math.max(0, limit - count);
const start = calendarDay(startDate);
const estimated = count === null ? null : projectDate(start, calendarDay(asOfDate), count, remaining, averageUsesPerDay);
const state = remaining === null ? "unknown" : remaining === 0 ? "due" : remaining <= warning ? "warning" : "ok";
const glance = layout === "glance";
const usageText = `${remaining === 1 ? "use" : "uses"} left${glance ? "" : " before descaling"}`;
const status = state === "due" ? <strong>Descaling is due now</strong>
: count === null ? <span>Usage unavailable</span>
: remaining === null ? <span>Set the cycle limit to track descaling.</span>
: <span className={styles.statusMessage}>
<AnimatedNumber value={remaining} className={styles.statusCount} blur={false} />{" "}
<span>{usageText}</span>
</span>;
const usage = <div className={styles.usage}>
<div className={styles.caption} id={usageId}>Cycle uses</div>
<div className={styles.counter} role="group" aria-label="Uses since descaling">
{count === null ? <span aria-label="Uses unavailable">—</span> : <AnimatedNumber className={styles.digits} value={count} blur={false} />}
{glance ? limit !== null && <span className={styles.limit}>/ {limit}</span> : <>
<span className={styles.multiplier} aria-hidden="true">×</span>
<CapsuleIcon className={styles.capsule} />
</>}
</div>
{limit !== null && count !== null && <>
{!glance && <div className={styles.limit}>of {limit} in this cycle</div>}
<Progress className={styles.progress} value={count} max={limit} aria-labelledby={usageId} animateOnMount />
</>}
</div>;
const dates = <dl className={styles.dates}>
<CycleDate label="Cycle started" date={start} />
<CycleDate label="Estimated descaling" date={estimated} />
</dl>;
if (glance) return <CoffeeGlance machineName={machineName} state={state} surface={surface}
usage={usage} status={status} dates={dates} onLogUse={onLogUse} onDescaled={onDescaled} />;
const feedback = { due: ["Due", "danger"], unknown: ["Setup", "neutral"], warning: ["Check", "warning"], ok: ["OK", "success"] } as const;
const [label, tone] = feedback[state];
const card = (
<WidgetCard className={styles.card} surface={surface} icon={<CupIcon />} title={machineName} description="Descaling">
<div className={styles.cycle} data-state={state}>
<CoffeeMachine />
{usage}
</div>
{dates}
{(onLogUse || onDescaled) && <div className={styles.actions}>
{onLogUse && <Button size="xs" type="button" onClick={onLogUse}><CapsuleIcon className={styles.capsule} />Log use</Button>}
{onDescaled && <Button ref={resetButton} type="button" size="xs" variant="secondary" aria-haspopup="dialog" onClick={() => setConfirm(true)}>Mark descaled</Button>}
</div>}
<MobilityStatus label={label} tone={tone}>{status}</MobilityStatus>
</WidgetCard>
);
return onDescaled ? <WidgetDialog className={styles.frame} placement="bottom" open={confirm} title="Start a new cycle"
onClose={() => setConfirm(false)} returnFocusRef={resetButton} surface={card}>
<div className={styles.confirmation}>
<p>Start a new cycle and reset the use counter?</p>
<div className={styles.confirmationActions}>
<Button size="xs" type="button" variant="ghost" onClick={() => setConfirm(false)}>Cancel</Button>
<Button size="xs" type="button" onClick={() => { onDescaled(); setConfirm(false); }}>Reset cycle</Button>
</div>
</div>
</WidgetDialog> : card;
}
lucide-reactmotionreactreact-dom