mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-09 10:59:50 +00:00
Create copyable textfield component (#24247)
This commit is contained in:
committed by
GitHub
parent
694bb3088c
commit
c5c067ef19
111
src/components/ha-copy-textfield.ts
Normal file
111
src/components/ha-copy-textfield.ts
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
import { customElement, property, state } from "lit/decorators";
|
||||||
|
import { css, html, LitElement, nothing } from "lit";
|
||||||
|
import { mdiContentCopy, mdiEye, mdiEyeOff } from "@mdi/js";
|
||||||
|
|
||||||
|
import "./ha-button";
|
||||||
|
import "./ha-icon-button";
|
||||||
|
import "./ha-svg-icon";
|
||||||
|
import "./ha-textfield";
|
||||||
|
import type { HomeAssistant } from "../types";
|
||||||
|
import { copyToClipboard } from "../common/util/copy-clipboard";
|
||||||
|
import { showToast } from "../util/toast";
|
||||||
|
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`
|
||||||
|
<div class="container">
|
||||||
|
<div class="textfield-container">
|
||||||
|
<ha-textfield
|
||||||
|
.value=${this._showMasked && this.maskedValue
|
||||||
|
? this.maskedValue
|
||||||
|
: this.value}
|
||||||
|
readonly
|
||||||
|
.suffix=${this.maskedValue
|
||||||
|
? html`<div style="width: 24px"></div>`
|
||||||
|
: nothing}
|
||||||
|
@click=${this._focusInput}
|
||||||
|
></ha-textfield>
|
||||||
|
${this.maskedValue
|
||||||
|
? html`<ha-icon-button
|
||||||
|
class="toggle-unmasked"
|
||||||
|
toggles
|
||||||
|
.label=${this.hass.localize(
|
||||||
|
`ui.common.${this._showMasked ? "show" : "hide"}`
|
||||||
|
)}
|
||||||
|
@click=${this._toggleMasked}
|
||||||
|
.path=${this._showMasked ? mdiEye : mdiEyeOff}
|
||||||
|
></ha-icon-button>`
|
||||||
|
: nothing}
|
||||||
|
</div>
|
||||||
|
<ha-button @click=${this._copy} unelevated>
|
||||||
|
<ha-svg-icon slot="icon" .path=${mdiContentCopy}></ha-svg-icon>
|
||||||
|
${this.label || this.hass.localize("ui.common.copy")}
|
||||||
|
</ha-button>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _focusInput(ev) {
|
||||||
|
const inputElement = ev.currentTarget as HaTextField;
|
||||||
|
inputElement.select();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _toggleMasked(): void {
|
||||||
|
this._showMasked = !this._showMasked;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async _copy(): Promise<void> {
|
||||||
|
await copyToClipboard(this.value);
|
||||||
|
showToast(this, {
|
||||||
|
message: this.hass.localize("ui.common.copied_clipboard"),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static styles = css`
|
||||||
|
.container {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,13 @@
|
|||||||
import { mdiContentCopy, mdiEye, mdiEyeOff, mdiHelpCircle } from "@mdi/js";
|
import { mdiHelpCircle } from "@mdi/js";
|
||||||
import { LitElement, css, html, nothing } from "lit";
|
import { LitElement, css, html, nothing } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property } from "lit/decorators";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import { copyToClipboard } from "../../../../common/util/copy-clipboard";
|
|
||||||
import "../../../../components/ha-alert";
|
import "../../../../components/ha-alert";
|
||||||
import "../../../../components/ha-button";
|
import "../../../../components/ha-button";
|
||||||
import "../../../../components/ha-card";
|
import "../../../../components/ha-card";
|
||||||
import "../../../../components/ha-expansion-panel";
|
import "../../../../components/ha-expansion-panel";
|
||||||
import "../../../../components/ha-formfield";
|
|
||||||
import "../../../../components/ha-radio";
|
|
||||||
import "../../../../components/ha-settings-row";
|
import "../../../../components/ha-settings-row";
|
||||||
import "../../../../components/ha-switch";
|
import "../../../../components/ha-switch";
|
||||||
import "../../../../components/ha-textfield";
|
|
||||||
|
|
||||||
import { formatDate } from "../../../../common/datetime/format_date";
|
import { formatDate } from "../../../../common/datetime/format_date";
|
||||||
import type { HaSwitch } from "../../../../components/ha-switch";
|
import type { HaSwitch } from "../../../../components/ha-switch";
|
||||||
@@ -25,6 +21,7 @@ import type { HomeAssistant } from "../../../../types";
|
|||||||
import { showToast } from "../../../../util/toast";
|
import { showToast } from "../../../../util/toast";
|
||||||
import { showCloudCertificateDialog } from "../dialog-cloud-certificate/show-dialog-cloud-certificate";
|
import { showCloudCertificateDialog } from "../dialog-cloud-certificate/show-dialog-cloud-certificate";
|
||||||
import { obfuscateUrl } from "../../../../util/url";
|
import { obfuscateUrl } from "../../../../util/url";
|
||||||
|
import "../../../../components/ha-copy-textfield";
|
||||||
|
|
||||||
@customElement("cloud-remote-pref")
|
@customElement("cloud-remote-pref")
|
||||||
export class CloudRemotePref extends LitElement {
|
export class CloudRemotePref extends LitElement {
|
||||||
@@ -34,8 +31,6 @@ export class CloudRemotePref extends LitElement {
|
|||||||
|
|
||||||
@property({ type: Boolean }) public narrow = false;
|
@property({ type: Boolean }) public narrow = false;
|
||||||
|
|
||||||
@state() private _unmaskedUrl = false;
|
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
if (!this.cloudStatus) {
|
if (!this.cloudStatus) {
|
||||||
return nothing;
|
return nothing;
|
||||||
@@ -139,37 +134,13 @@ export class CloudRemotePref extends LitElement {
|
|||||||
)}
|
)}
|
||||||
</p>
|
</p>
|
||||||
`}
|
`}
|
||||||
<div class="url-container">
|
|
||||||
<div class="textfield-container">
|
<ha-copy-textfield
|
||||||
<ha-textfield
|
.hass=${this.hass}
|
||||||
.value=${this._unmaskedUrl
|
.value=${`https://${remote_domain}`}
|
||||||
? `https://${remote_domain}`
|
.maskedValue=${obfuscateUrl(`https://${remote_domain}`)}
|
||||||
: obfuscateUrl(`https://${remote_domain}`)}
|
.label=${this.hass!.localize("ui.panel.config.common.copy_link")}
|
||||||
readonly
|
></ha-copy-textfield>
|
||||||
.suffix=${
|
|
||||||
// reserve some space for the icon.
|
|
||||||
html`<div style="width: 24px"></div>`
|
|
||||||
}
|
|
||||||
></ha-textfield>
|
|
||||||
<ha-icon-button
|
|
||||||
class="toggle-unmasked-url"
|
|
||||||
toggles
|
|
||||||
.label=${this.hass.localize(
|
|
||||||
`ui.panel.config.common.${this._unmaskedUrl ? "hide" : "show"}_url`
|
|
||||||
)}
|
|
||||||
@click=${this._toggleUnmaskedUrl}
|
|
||||||
.path=${this._unmaskedUrl ? mdiEyeOff : mdiEye}
|
|
||||||
></ha-icon-button>
|
|
||||||
</div>
|
|
||||||
<ha-button
|
|
||||||
.url=${`https://${remote_domain}`}
|
|
||||||
@click=${this._copyURL}
|
|
||||||
unelevated
|
|
||||||
>
|
|
||||||
<ha-svg-icon slot="icon" .path=${mdiContentCopy}></ha-svg-icon>
|
|
||||||
${this.hass.localize("ui.panel.config.common.copy_link")}
|
|
||||||
</ha-button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<ha-expansion-panel
|
<ha-expansion-panel
|
||||||
outlined
|
outlined
|
||||||
@@ -234,10 +205,6 @@ export class CloudRemotePref extends LitElement {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _toggleUnmaskedUrl(): void {
|
|
||||||
this._unmaskedUrl = !this._unmaskedUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async _toggleChanged(ev) {
|
private async _toggleChanged(ev) {
|
||||||
const toggle = ev.target as HaSwitch;
|
const toggle = ev.target as HaSwitch;
|
||||||
|
|
||||||
@@ -268,14 +235,6 @@ export class CloudRemotePref extends LitElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _copyURL(ev): Promise<void> {
|
|
||||||
const url = ev.currentTarget.url;
|
|
||||||
await copyToClipboard(url);
|
|
||||||
showToast(this, {
|
|
||||||
message: this.hass.localize("ui.common.copied_clipboard"),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
static styles = css`
|
static styles = css`
|
||||||
.preparing {
|
.preparing {
|
||||||
padding: 0 16px 16px;
|
padding: 0 16px 16px;
|
||||||
@@ -335,30 +294,6 @@ export class CloudRemotePref extends LitElement {
|
|||||||
display: block;
|
display: block;
|
||||||
margin-bottom: 16px;
|
margin-bottom: 16px;
|
||||||
}
|
}
|
||||||
.url-container {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 8px;
|
|
||||||
margin-top: 8px;
|
|
||||||
}
|
|
||||||
.textfield-container {
|
|
||||||
position: relative;
|
|
||||||
flex: 1;
|
|
||||||
}
|
|
||||||
.textfield-container ha-textfield {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
.toggle-unmasked-url {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
hr {
|
hr {
|
||||||
border: none;
|
border: none;
|
||||||
height: 1px;
|
height: 1px;
|
||||||
|
|||||||
@@ -1,27 +1,23 @@
|
|||||||
import "@material/mwc-button";
|
import "@material/mwc-button";
|
||||||
import { mdiContentCopy, mdiOpenInNew } from "@mdi/js";
|
import { mdiOpenInNew } from "@mdi/js";
|
||||||
import type { CSSResultGroup } from "lit";
|
import type { CSSResultGroup } from "lit";
|
||||||
import { css, html, LitElement, nothing } from "lit";
|
import { css, html, LitElement, nothing } from "lit";
|
||||||
import { query, state } from "lit/decorators";
|
import { state } from "lit/decorators";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import { copyToClipboard } from "../../../../common/util/copy-clipboard";
|
|
||||||
import { createCloseHeading } from "../../../../components/ha-dialog";
|
import { createCloseHeading } from "../../../../components/ha-dialog";
|
||||||
import "../../../../components/ha-textfield";
|
|
||||||
import type { HaTextField } from "../../../../components/ha-textfield";
|
|
||||||
import { showConfirmationDialog } from "../../../../dialogs/generic/show-dialog-box";
|
import { showConfirmationDialog } from "../../../../dialogs/generic/show-dialog-box";
|
||||||
import { haStyle, haStyleDialog } from "../../../../resources/styles";
|
import { haStyle, haStyleDialog } from "../../../../resources/styles";
|
||||||
import type { HomeAssistant } from "../../../../types";
|
import type { HomeAssistant } from "../../../../types";
|
||||||
import { documentationUrl } from "../../../../util/documentation-url";
|
import { documentationUrl } from "../../../../util/documentation-url";
|
||||||
import { showToast } from "../../../../util/toast";
|
|
||||||
import type { WebhookDialogParams } from "./show-dialog-manage-cloudhook";
|
import type { WebhookDialogParams } from "./show-dialog-manage-cloudhook";
|
||||||
|
|
||||||
|
import "../../../../components/ha-copy-textfield";
|
||||||
|
|
||||||
export class DialogManageCloudhook extends LitElement {
|
export class DialogManageCloudhook extends LitElement {
|
||||||
protected hass?: HomeAssistant;
|
protected hass?: HomeAssistant;
|
||||||
|
|
||||||
@state() private _params?: WebhookDialogParams;
|
@state() private _params?: WebhookDialogParams;
|
||||||
|
|
||||||
@query("ha-textfield") _input!: HaTextField;
|
|
||||||
|
|
||||||
public showDialog(params: WebhookDialogParams) {
|
public showDialog(params: WebhookDialogParams) {
|
||||||
this._params = params;
|
this._params = params;
|
||||||
}
|
}
|
||||||
@@ -82,21 +78,12 @@ export class DialogManageCloudhook extends LitElement {
|
|||||||
<ha-svg-icon .path=${mdiOpenInNew}></ha-svg-icon>
|
<ha-svg-icon .path=${mdiOpenInNew}></ha-svg-icon>
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
<ha-textfield
|
|
||||||
.label=${this.hass!.localize(
|
<ha-copy-textfield
|
||||||
"ui.panel.config.cloud.dialog_cloudhook.public_url"
|
.hass=${this.hass}
|
||||||
)}
|
|
||||||
.value=${cloudhook.cloudhook_url}
|
.value=${cloudhook.cloudhook_url}
|
||||||
iconTrailing
|
.label=${this.hass!.localize("ui.panel.config.common.copy_link")}
|
||||||
readOnly
|
></ha-copy-textfield>
|
||||||
@click=${this._focusInput}
|
|
||||||
>
|
|
||||||
<ha-icon-button
|
|
||||||
@click=${this._copyUrl}
|
|
||||||
slot="trailingIcon"
|
|
||||||
.path=${mdiContentCopy}
|
|
||||||
></ha-icon-button>
|
|
||||||
</ha-textfield>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<a
|
<a
|
||||||
@@ -137,24 +124,6 @@ export class DialogManageCloudhook extends LitElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private _focusInput(ev) {
|
|
||||||
const inputElement = ev.currentTarget as HaTextField;
|
|
||||||
inputElement.select();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async _copyUrl(ev): Promise<void> {
|
|
||||||
if (!this.hass) return;
|
|
||||||
ev.stopPropagation();
|
|
||||||
const inputElement = ev.target.parentElement as HaTextField;
|
|
||||||
inputElement.select();
|
|
||||||
const url = this.hass.hassUrl(inputElement.value);
|
|
||||||
|
|
||||||
await copyToClipboard(url);
|
|
||||||
showToast(this, {
|
|
||||||
message: this.hass.localize("ui.common.copied_clipboard"),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
static get styles(): CSSResultGroup {
|
||||||
return [
|
return [
|
||||||
haStyle,
|
haStyle,
|
||||||
@@ -163,13 +132,6 @@ export class DialogManageCloudhook extends LitElement {
|
|||||||
ha-dialog {
|
ha-dialog {
|
||||||
width: 650px;
|
width: 650px;
|
||||||
}
|
}
|
||||||
ha-textfield {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
ha-textfield > ha-icon-button {
|
|
||||||
--mdc-icon-button-size: 24px;
|
|
||||||
--mdc-icon-size: 18px;
|
|
||||||
}
|
|
||||||
button.link {
|
button.link {
|
||||||
color: var(--primary-color);
|
color: var(--primary-color);
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
|
|||||||
@@ -370,7 +370,10 @@
|
|||||||
"name": "Name",
|
"name": "Name",
|
||||||
"optional": "optional",
|
"optional": "optional",
|
||||||
"default": "Default",
|
"default": "Default",
|
||||||
"dont_save": "Don't save"
|
"dont_save": "Don't save",
|
||||||
|
"copy": "Copy",
|
||||||
|
"show": "Show",
|
||||||
|
"hide": "Hide"
|
||||||
},
|
},
|
||||||
"components": {
|
"components": {
|
||||||
"selectors": {
|
"selectors": {
|
||||||
|
|||||||
Reference in New Issue
Block a user