@xoong/controls 0.0.41 → 0.0.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +124 -23
- package/src/avatar.js +142 -0
- package/src/controls.css +7 -0
- package/src/date.js +4 -4
- package/src/index.d.ts +436 -0
- package/src/index.js +10 -0
- package/src/inline-edit.js +99 -0
- package/src/otp.js +152 -0
- package/src/progress.js +98 -0
- package/src/range-slider.js +83 -0
- package/src/segmented.js +73 -0
- package/src/slider.js +2 -1
- package/src/split-button.js +42 -0
- package/src/stat.js +58 -0
- package/src/stepper.js +9 -6
- package/src/steps.js +73 -0
- package/src/textarea.js +57 -0
package/src/textarea.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* <xo-textarea> is a textarea with a character counter and a thin progress bar toward `maxlength`.
|
|
4
|
+
*
|
|
5
|
+
* <xo-textarea name="excerpt" label="Excerpt" maxlength="160" rows="4"></xo-textarea>
|
|
6
|
+
*
|
|
7
|
+
* - The counter ("142 / 160") sits in `.hint` under the field; it is announced politely only when the text gets near the limit (the last 10%) or goes over.
|
|
8
|
+
* Over the limit the field is invalid with a message (the browser's `maxlength` is not set, so pasted text is kept and shown red, not cut silently).
|
|
9
|
+
* - The value is the string. `input` and `change` fire like a native textarea.
|
|
10
|
+
*/
|
|
11
|
+
import { XoControl, define, text } from '@xoong/core';
|
|
12
|
+
|
|
13
|
+
class XoTextarea extends XoControl {
|
|
14
|
+
static props = { ...XoControl.props, label: String, maxlength: Number, rows: Number, placeholder: String };
|
|
15
|
+
|
|
16
|
+
/** @type {HTMLTextAreaElement | null} */ #area = null;
|
|
17
|
+
/** @type {HTMLElement | null} */ #count = null;
|
|
18
|
+
/** @type {HTMLElement | null} */ #bar = null;
|
|
19
|
+
|
|
20
|
+
setValue(v) { super.setValue(v == null ? '' : String(v)); if (this.#area && this.#area.value !== super.value) this.#area.value = super.value; this.#paint(); }
|
|
21
|
+
get focusTarget() { return this.#area; }
|
|
22
|
+
validate(v) { return this.maxlength && String(v).length > this.maxlength ? text('xoong.controls.tooLong', 'Use at most {max} characters.', { max: this.maxlength }) : ''; }
|
|
23
|
+
|
|
24
|
+
connected() {
|
|
25
|
+
const wrap = document.createElement('div');
|
|
26
|
+
wrap.className = 'xo-textarea-meter';
|
|
27
|
+
const area = document.createElement('textarea');
|
|
28
|
+
area.addEventListener('input', (e) => { e.stopPropagation(); this.userInput(area.value); this.#paint(); });
|
|
29
|
+
area.addEventListener('change', (e) => { e.stopPropagation(); this.userChange(); });
|
|
30
|
+
const bar = document.createElement('div');
|
|
31
|
+
bar.className = 'xo-progress xo-progress--thin';
|
|
32
|
+
bar.append(document.createElement('i'));
|
|
33
|
+
const count = document.createElement('span');
|
|
34
|
+
count.className = 'hint';
|
|
35
|
+
wrap.append(area, bar);
|
|
36
|
+
this.#area = area; this.#bar = bar; this.#count = count;
|
|
37
|
+
this.replaceChildren(wrap, count);
|
|
38
|
+
queueMicrotask(() => { area.value = super.value; this.#paint(); });
|
|
39
|
+
}
|
|
40
|
+
attributeChangedCallback(attr, old, value) { super.attributeChangedCallback(attr, old, value); this.#paint(); }
|
|
41
|
+
|
|
42
|
+
#paint() {
|
|
43
|
+
const a = this.#area, c = this.#count, bar = this.#bar;
|
|
44
|
+
if (!a || !c || !bar) return;
|
|
45
|
+
const n = String(super.value).length, max = this.maxlength || 0;
|
|
46
|
+
a.rows = this.rows || 3; a.placeholder = this.placeholder ?? ''; a.disabled = this.isDisabled; a.readOnly = this.readonly;
|
|
47
|
+
a.setAttribute('aria-label', this.label || this.name || text('xoong.controls.text', 'Text'));
|
|
48
|
+
a.setAttribute('aria-invalid', String(!!max && n > max));
|
|
49
|
+
bar.hidden = !max; c.hidden = !max;
|
|
50
|
+
if (!max) return;
|
|
51
|
+
c.textContent = `${n} / ${max}`;
|
|
52
|
+
c.setAttribute('aria-live', n >= max * 0.9 ? 'polite' : 'off');
|
|
53
|
+
c.classList.toggle('error', n > max);
|
|
54
|
+
/** @type {HTMLElement} */ (bar.firstElementChild).style.width = `${Math.min(100, (n / max) * 100)}%`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
define('xo-textarea', XoTextarea);
|