Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free

wired-textarea

v1.0.0arrow_drop_down
v1.0.0
v0.9.2
STATUS
Passing
DOWNLOADS
18
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
A year ago
SIZE
12 KB
Hand-drawn sketchy multi-line text input web component.
1 contributor
Install wired-textarea as a package?
Copied
npm i @bit/wiredjs.wired-elements.wired-textarea
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
Files
wired-textarea/src/wired-textarea.ts
204 Lines(177 sloc)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { WiredBase, customElement, property, TemplateResult, html, css, CSSResult, PropertyValues } from 'wired-lib/lib/wired-base';
import { rectangle } from '../../wired-lib/src/wired-lib';

@customElement('wired-textarea')
export class WiredTextarea extends WiredBase {
  @property({ type: Number }) rows = 1;
  @property({ type: Number }) maxrows = 0;
  @property({ type: String }) autocomplete = '';
  @property({ type: Boolean }) autofocus = false;
  @property({ type: Boolean, reflect: true }) disabled = false;
  @property({ type: String }) inputmode = '';
  @property({ type: String }) placeholder = '';
  @property({ type: Boolean }) required = false;
  @property({ type: Boolean }) readonly = false;
  @property({ type: Number }) minlength?: number;
  @property({ type: Number }) maxlength?: number;

  private tokens: string[] = [];
  private prevHeight = 0;

  static get styles(): CSSResult {
    return css`
    :host {
      display: inline-block;
      position: relative;
      font-family: sans-serif;
      width: 400px;
      outline: none;
      opacity: 0;
    }

    :host(.wired-rendered) {
      opacity: 1;
    }
  
    :host(.wired-disabled) {
      opacity: 0.6 !important;
      cursor: default;
      pointer-events: none;
    }
  
    :host(.wired-disabled) svg {
      background: rgba(0, 0, 0, 0.07);
    }
  
    .fit {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
    }
  
    .overlay {
      pointer-events: none;
    }
  
    svg {
      display: block;
    }
  
    path {
      stroke: currentColor;
      stroke-width: 0.7;
      fill: transparent;
    }
  
    .mirror-text {
      visibility: hidden;
      word-wrap: break-word;
    }

    #mirror {
      padding: 10px;
    }
  
    textarea {
      position: relative;
      outline: none;
      border: none;
      resize: none;
      background: inherit;
      color: inherit;
      width: 100%;
      height: 100%;
      font-size: inherit;
      font-family: inherit;
      line-height: inherit;
      text-align: inherit;
      padding: 10px;
      box-sizing: border-box;
    }
    `;
  }

  render(): TemplateResult {
    return html`
    <div id="mirror" class="mirror-text">&#160;</div>
    <div class="fit">
      <textarea id="textarea" autocomplete="${this.autocomplete}" ?autofocus="${this.autofocus}" inputmode="${this.inputmode}"
        placeholder="${this.placeholder}" ?readonly="${this.readonly}" ?required="${this.required}" ?disabled="${this.disabled}"
        rows="${this.rows}" minlength="${this.minlength}" maxlength="${this.maxlength}" @input="${this.onInput}"></textarea>
    </div>
    <div class="fit overlay">
      <svg id="svg"></svg>
    </div>
    `;
  }

  createRenderRoot() {
    return this.attachShadow({ mode: 'open', delegatesFocus: true });
  }

  get textarea(): HTMLTextAreaElement | null {
    if (this.shadowRoot) {
      return this.shadowRoot.getElementById('textarea') as HTMLTextAreaElement;
    }
    return null;
  }

  private get mirror(): HTMLDivElement {
    return this.shadowRoot!.getElementById('mirror') as HTMLDivElement;
  }

  get value(): string {
    const input = this.textarea;
    return (input && input.value) || '';
  }

  set value(v: string) {
    const textarea = this.textarea;
    if (!textarea) {
      return;
    }
    if (textarea.value !== v) {
      textarea.value = v || '';
    }
    this.mirror.innerHTML = this.valueForMirror();
    this.requestUpdate();
  }

  private valueForMirror(): string {
    const input = this.textarea;
    if (!input) {
      return '';
    }
    this.tokens = (input && input.value) ? input.value.replace(/&/gm, '&amp;').replace(/"/gm, '&quot;').replace(/'/gm, '&#39;').replace(/</gm, '&lt;').replace(/>/gm, '&gt;').split('\n') : [''];
    return this.constrain(this.tokens);
  }

  private constrain(tokens: string[]) {
    let _tokens: string[];
    tokens = tokens || [''];
    if (this.maxrows > 0 && tokens.length > this.maxrows) {
      _tokens = tokens.slice(0, this.maxrows);
    } else {
      _tokens = tokens.slice(0);
    }
    while (this.rows > 0 && _tokens.length < this.rows) {
      _tokens.push('');
    }
    return _tokens.join('<br/>') + '&#160;';
  }

  private refreshDisabledState() {
    if (this.disabled) {
      this.classList.add('wired-disabled');
    } else {
      this.classList.remove('wired-disabled');
    }
  }

  firstUpdated() {
    this.value = this.value || this.getAttribute('value') || '';
  }

  updated(changed: PropertyValues) {
    if (changed.has('disabled')) {
      this.refreshDisabledState();
    }
    const svg = (this.shadowRoot!.getElementById('svg') as any) as SVGSVGElement;
    const s = this.getBoundingClientRect();

    if (this.prevHeight !== s.height) {
      while (svg.hasChildNodes()) {
        svg.removeChild(svg.lastChild!);
      }
      svg.setAttribute('width', `${s.width}`);
      svg.setAttribute('height', `${s.height}`);
      rectangle(svg, 2, 2, s.width - 2, s.height - 2);
      this.prevHeight = s.height;
      this.classList.add('wired-rendered');
      this.updateCached();
    }
  }

  private updateCached() {
    this.mirror.innerHTML = this.constrain(this.tokens);
  }

  private onInput() {
    this.value = this.textarea!.value;
  }
}