import { mdiContentCopy, mdiEye, mdiEyeOff } from "@mdi/js"; import { css, html, LitElement, nothing } from "lit"; import { customElement, property, state } from "lit/decorators"; import { copyToClipboard } from "../common/util/copy-clipboard"; import type { HomeAssistant } from "../types"; import { showToast } from "../util/toast"; import "./ha-button"; import "./ha-icon-button"; import "./ha-svg-icon"; import "./ha-textfield"; import type { HaTextField } from "./ha-textfield"; @customElement("ha-copy-textfield") export class HaCopyTextfield extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: "value" }) public value!: string; @property({ attribute: "masked-value" }) public maskedValue?: string; @property({ attribute: "label" }) public label?: string; @state() private _showMasked = true; public render() { return html`
` : nothing} @click=${this._focusInput} > ${this.maskedValue ? html`` : nothing}
${this.label || this.hass.localize("ui.common.copy")} `; } private _focusInput(ev) { const inputElement = ev.currentTarget as HaTextField; inputElement.select(); } private _toggleMasked(): void { this._showMasked = !this._showMasked; } private async _copy(): Promise { await copyToClipboard(this.value); showToast(this, { message: this.hass.localize("ui.common.copied_clipboard"), }); } static styles = css` .container { display: flex; align-items: center; gap: var(--ha-space-2); margin-top: 8px; } .textfield-container { position: relative; flex: 1; } .textfield-container ha-textfield { display: block; } .toggle-unmasked { position: absolute; top: 8px; right: 8px; inset-inline-start: initial; inset-inline-end: 8px; --mdc-icon-button-size: 40px; --mdc-icon-size: 20px; color: var(--secondary-text-color); direction: var(--direction); } `; } declare global { interface HTMLElementTagNameMap { "ha-copy-textfield": HaCopyTextfield; } }