-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathme-textarea.ts
More file actions
150 lines (130 loc) · 3.56 KB
/
Copy pathme-textarea.ts
File metadata and controls
150 lines (130 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { css, html, LitElement } from 'lit'
import { customElement, property } from 'lit/decorators.js'
/**
* A standard-compliant, form-associated textarea component.
*/
@customElement('me-textarea')
export class MeTextarea extends LitElement {
static formAssociated = true
@property({ reflect: true }) label = ''
@property({ reflect: true }) name = ''
@property({ type: Number, reflect: true }) rows = 4
@property({ reflect: true }) value = ''
@property({ type: Boolean, reflect: true }) disabled = false
@property({ type: Boolean, reflect: true }) required = false
@property({ reflect: true }) placeholder = ''
private _internals: ElementInternals
private _inputId = `me-input-${Math.random().toString(36).slice(2, 9)}`
constructor() {
super()
this._internals = this.attachInternals()
}
protected createRenderRoot() {
return this.attachShadow({ mode: 'open', delegatesFocus: true })
}
formResetCallback() {
this.value = ''
this._syncInternals()
}
formDisabledCallback(disabled: boolean) {
this.disabled = disabled
}
private _onInput(e: Event) {
const input = e.target as HTMLTextAreaElement
this.value = input.value
this._syncInternals()
this.dispatchEvent(
new CustomEvent('change', {
detail: input.value,
bubbles: true,
composed: true,
}),
)
}
protected updated(changedProperties: Map<PropertyKey, unknown>) {
if (changedProperties.has('value')) {
this._syncInternals()
}
}
private _syncInternals() {
this._internals.setFormValue(this.value ?? '')
const input = this.shadowRoot?.querySelector('textarea')
if (input) {
this._internals.setValidity(
input.validity,
input.validationMessage,
input,
)
this._internals.ariaInvalid = input.checkValidity() ? 'false' : 'true'
this._internals.ariaRequired = this.required ? 'true' : 'false'
}
}
render() {
return html`
<div class="field">
${
this.label
? html`<label class="label" for=${this._inputId} part="label">${this.label}</label>`
: null
}
<textarea
id=${this._inputId}
part="textarea"
.name=${this.name}
.rows=${this.rows}
.value=${this.value ?? ''}
.placeholder=${this.placeholder}
?disabled=${this.disabled}
?required=${this.required}
@input=${this._onInput}
></textarea>
</div>
`
}
static styles = css`
:host {
display: block;
}
.field {
display: grid;
gap: 6px;
}
.label {
font-size: 13px;
font-weight: 400;
color: var(--color-text-secondary);
cursor: pointer;
}
textarea {
width: 100%;
padding: 10px 12px;
border: 1px solid var(--color-border);
background: #ffffff;
color: var(--color-text-primary);
font-family: inherit;
font-size: 14px;
line-height: 1.6;
border-radius: 4px;
transition: border-color 0.2s ease, box-shadow 0.2s ease;
outline: none;
resize: vertical;
}
textarea:focus {
border-color: var(--admin-accent);
box-shadow: 0 0 0 1px var(--admin-accent);
}
:host([disabled]) textarea {
background: var(--color-bg-deep);
color: var(--color-text-tertiary);
cursor: not-allowed;
}
textarea:invalid:not(:placeholder-shown) {
border-color: var(--color-danger);
}
`
}
declare global {
interface HTMLElementTagNameMap {
'me-textarea': MeTextarea
}
}