Files
openclaw/ui/src/components/theme-mode-toggle.ts
Peter Steinberger b8cdb93f6f refactor(ui): simplify Lit state ownership (#102848)
Co-authored-by: Peter Steinberger <peter@steipete.me>
2026-07-09 07:15:19 -07:00

56 lines
1.6 KiB
TypeScript

import { html } from "lit";
import { property } from "lit/decorators.js";
import type { ThemeMode } from "../app/theme.ts";
import { t } from "../i18n/index.ts";
import { OpenClawLightDomContentsElement } from "../lit/openclaw-element.ts";
import { icons } from "./icons.ts";
import "./tooltip.ts";
export type ThemeModeChangeDetail = {
mode: ThemeMode;
element: HTMLElement;
};
class ThemeModeToggle extends OpenClawLightDomContentsElement {
@property({ attribute: false }) mode: ThemeMode = "system";
private readonly handleModeChange = (event: Event) => {
const mode = this.mode === "system" ? "light" : this.mode === "light" ? "dark" : "system";
this.dispatchEvent(
new CustomEvent<ThemeModeChangeDetail>("theme-change", {
detail: { mode, element: event.currentTarget as HTMLElement },
bubbles: true,
composed: true,
}),
);
};
override render() {
const labelKey =
this.mode === "system"
? "common.system"
: this.mode === "light"
? "common.light"
: "common.dark";
const label = t(labelKey);
const tooltip = t("common.colorModeOption", { mode: label });
return html`
<openclaw-tooltip .content=${tooltip}>
<button
type="button"
class="theme-mode-toggle"
aria-label=${tooltip}
@click=${this.handleModeChange}
>
${this.mode === "system" ? icons.monitor : this.mode === "light" ? icons.sun : icons.moon}
</button>
</openclaw-tooltip>
`;
}
}
if (!customElements.get("openclaw-theme-mode-toggle")) {
customElements.define("openclaw-theme-mode-toggle", ThemeModeToggle);
}