@nectary/components 2.6.0 → 2.6.1
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 +1 -1
- package/textarea/index.js +72 -15
package/package.json
CHANGED
package/textarea/index.js
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
import { Context, defineCustomElement, getAttribute, getBooleanAttribute, getIntegerAttribute, getReactEventHandler, isAttrEqual, isAttrTrue, NectaryElement, setClass, updateAttribute, updateBooleanAttribute, updateExplicitBooleanAttribute } from '../utils';
|
|
1
|
+
import { Context, defineCustomElement, getAttribute, getBooleanAttribute, getIntegerAttribute, getReactEventHandler, getRect, isAttrEqual, isAttrTrue, NectaryElement, setClass, updateAttribute, updateBooleanAttribute, updateExplicitBooleanAttribute } from '../utils';
|
|
2
2
|
import { DEFAULT_SIZE } from '../utils/size';
|
|
3
|
-
const templateHTML = '<style>:host{all:initial;display:block}#wrapper{display:flex;flex-direction:column;position:relative;width:100%;box-sizing:border-box;background-color:var(--sinch-comp-textarea-color-default-background-initial);border-radius:var(--sinch-local-shape-radius);padding-right:2px;
|
|
3
|
+
const templateHTML = '<style>:host{all:initial;display:block}#wrapper{display:flex;flex-direction:column;position:relative;width:100%;box-sizing:border-box;background-color:var(--sinch-comp-textarea-color-default-background-initial);border-radius:var(--sinch-local-shape-radius);padding-right:2px;overflow:hidden;--sinch-local-shape-radius:var(--sinch-comp-textarea-shape-radius)}#input{all:initial;display:block;font:var(--sinch-comp-textarea-font-input);color:var(--sinch-comp-textarea-color-default-text-initial);resize:none;white-space:pre-wrap;overflow-wrap:break-word;padding:8px 10px 8px 12px;border:none;box-sizing:border-box}#input::placeholder{color:var(--sinch-comp-textarea-color-default-text-placeholder);opacity:1}#input:disabled{color:var(--sinch-comp-textarea-color-disabled-text-initial);-webkit-text-fill-color:var(--sinch-comp-textarea-color-disabled-text-initial)}#border{position:absolute;border:1px solid var(--sinch-comp-textarea-color-default-border-initial);border-radius:var(--sinch-local-shape-radius);inset:0;pointer-events:none}:host([invalid]) #border{border-color:var(--sinch-comp-textarea-color-invalid-border-initial)}#input:focus+#border{border-color:var(--sinch-comp-textarea-color-default-border-focus);border-width:2px}#input:disabled+#border{border-color:var(--sinch-comp-textarea-color-disabled-border-initial)}#bottom{display:flex;flex-direction:row;align-items:center;gap:8px;padding:12px 4px 4px}#bottom.empty{display:none}#resize-handle{display:none;position:absolute;width:20px;height:20px;bottom:36px;right:0;cursor:ns-resize}:host([resizable]) #resize-handle{display:block}#bottom.empty+#resize-handle{bottom:0}#resize-icon{display:block;pointer-events:none;fill:var(--sinch-comp-textarea-color-default-border-initial)}</style><div id="wrapper"><textarea id="input"></textarea><div id="border"></div><div id="bottom"><slot name="bottom"></slot></div><div id="resize-handle"><svg id="resize-icon" width="16" height="16"><path d="m14.833 4.724-9.61 9.61-.942-.944 9.61-9.609.942.943ZM15.443 10 10.5 14.943 9.557 14 14.5 9.057l.943.943Z"/></svg></div></div>';
|
|
4
4
|
const template = document.createElement('template');
|
|
5
5
|
template.innerHTML = templateHTML;
|
|
6
6
|
defineCustomElement('sinch-textarea', class extends NectaryElement {
|
|
7
7
|
#$input;
|
|
8
8
|
#$bottomSlot;
|
|
9
9
|
#$bottomWrapper;
|
|
10
|
+
#$resizeHandle;
|
|
10
11
|
#cursorPos = null;
|
|
11
12
|
#isPendingDk = false;
|
|
12
13
|
#controller = null;
|
|
13
14
|
#sizeContext;
|
|
14
|
-
#
|
|
15
|
+
#prevContentHeight = 0;
|
|
16
|
+
#dragStartY = 0;
|
|
17
|
+
#intersectionObserver = null;
|
|
15
18
|
constructor() {
|
|
16
19
|
super();
|
|
17
20
|
const shadowRoot = this.attachShadow({
|
|
@@ -21,6 +24,7 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
|
|
|
21
24
|
this.#$input = shadowRoot.querySelector('#input');
|
|
22
25
|
this.#$bottomSlot = shadowRoot.querySelector('slot[name="bottom"]');
|
|
23
26
|
this.#$bottomWrapper = shadowRoot.querySelector('#bottom');
|
|
27
|
+
this.#$resizeHandle = shadowRoot.querySelector('#resize-handle');
|
|
24
28
|
this.#sizeContext = new Context(this.#$bottomWrapper, 'size');
|
|
25
29
|
}
|
|
26
30
|
connectedCallback() {
|
|
@@ -37,19 +41,24 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
|
|
|
37
41
|
this.#$input.addEventListener('keydown', this.#onSelectionChange, options);
|
|
38
42
|
this.#$input.addEventListener('focus', this.#onInputFocus, options);
|
|
39
43
|
this.#$input.addEventListener('blur', this.#onInputBlur, options);
|
|
44
|
+
this.#$resizeHandle.addEventListener('mousedown', this.#onDragStart, options);
|
|
40
45
|
this.#$bottomSlot.addEventListener('slotchange', this.#onBottomSlotChange, options);
|
|
41
46
|
this.addEventListener('-change', this.#onChangeReactHandler, options);
|
|
42
47
|
this.addEventListener('-focus', this.#onFocusReactHandler, options);
|
|
43
48
|
this.addEventListener('-blur', this.#onBlurReactHandler, options);
|
|
44
49
|
this.#sizeContext.listen(this.#controller.signal);
|
|
45
50
|
this.#onBottomSlotChange();
|
|
46
|
-
this
|
|
51
|
+
this.#updateMinRows();
|
|
47
52
|
this.#onSizeUpdate();
|
|
48
53
|
}
|
|
49
54
|
disconnectedCallback() {
|
|
50
55
|
super.disconnectedCallback();
|
|
51
56
|
this.#controller.abort();
|
|
52
57
|
this.#controller = null;
|
|
58
|
+
if (this.#intersectionObserver !== null) {
|
|
59
|
+
this.#intersectionObserver.disconnect();
|
|
60
|
+
this.#intersectionObserver = null;
|
|
61
|
+
}
|
|
53
62
|
}
|
|
54
63
|
static get observedAttributes() {
|
|
55
64
|
return ['value', 'placeholder', 'invalid', 'disabled', 'rows', 'minrows', 'resizable'];
|
|
@@ -70,9 +79,9 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
|
|
|
70
79
|
this.#$input.style.removeProperty('height');
|
|
71
80
|
}
|
|
72
81
|
const nextContentHeight = this.#$input.scrollHeight;
|
|
73
|
-
if (isShrinkingContent || nextContentHeight !== this.#
|
|
74
|
-
this.#
|
|
75
|
-
this.#$input.style.setProperty('height', `${this.#
|
|
82
|
+
if (isShrinkingContent || nextContentHeight !== this.#prevContentHeight) {
|
|
83
|
+
this.#prevContentHeight = nextContentHeight;
|
|
84
|
+
this.#$input.style.setProperty('height', `${this.#prevContentHeight}px`);
|
|
76
85
|
}
|
|
77
86
|
}
|
|
78
87
|
if (!isPrevCursorEnd) {
|
|
@@ -114,7 +123,7 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
|
|
|
114
123
|
}
|
|
115
124
|
case 'minrows':
|
|
116
125
|
{
|
|
117
|
-
this
|
|
126
|
+
this.#updateMinRows();
|
|
118
127
|
break;
|
|
119
128
|
}
|
|
120
129
|
case 'resizable':
|
|
@@ -196,19 +205,37 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
|
|
|
196
205
|
blur() {
|
|
197
206
|
this.#$input.blur();
|
|
198
207
|
}
|
|
199
|
-
updateMinRows() {
|
|
200
|
-
if (!this.isDomConnected) {
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
208
|
+
#updateMinRows() {
|
|
203
209
|
const minRows = this.minRows;
|
|
204
210
|
if (minRows <= 0) {
|
|
205
211
|
this.#$input.style.removeProperty('min-height');
|
|
212
|
+
if (this.#intersectionObserver !== null) {
|
|
213
|
+
this.#intersectionObserver.disconnect();
|
|
214
|
+
this.#intersectionObserver = null;
|
|
215
|
+
}
|
|
206
216
|
} else {
|
|
207
|
-
this
|
|
208
|
-
|
|
209
|
-
|
|
217
|
+
if (this.isDomConnected) {
|
|
218
|
+
this.#calcMinRows();
|
|
219
|
+
}
|
|
220
|
+
if (this.#intersectionObserver === null) {
|
|
221
|
+
this.#intersectionObserver = new IntersectionObserver(this.#intersectionObserverCallback, {
|
|
222
|
+
root: null
|
|
223
|
+
});
|
|
224
|
+
this.#intersectionObserver.observe(this.#$input);
|
|
225
|
+
}
|
|
210
226
|
}
|
|
211
227
|
}
|
|
228
|
+
#intersectionObserverCallback = _ref => {
|
|
229
|
+
let [entry] = _ref;
|
|
230
|
+
if (entry != null && entry.isIntersecting) {
|
|
231
|
+
this.#calcMinRows();
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
#calcMinRows() {
|
|
235
|
+
this.#$input.rows = this.minRows;
|
|
236
|
+
this.#$input.style.setProperty('min-height', `${getRect(this.#$input).height}px`);
|
|
237
|
+
this.#$input.rows = this.rows;
|
|
238
|
+
}
|
|
212
239
|
#onCompositionStart = () => {
|
|
213
240
|
this.#isPendingDk = true;
|
|
214
241
|
};
|
|
@@ -236,6 +263,36 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
|
|
|
236
263
|
}));
|
|
237
264
|
}
|
|
238
265
|
};
|
|
266
|
+
#onDragStart = e => {
|
|
267
|
+
if (this.#dragStartY !== 0) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
this.#dragStartY = e.clientY;
|
|
271
|
+
this.#prevContentHeight = getRect(this.#$input).height;
|
|
272
|
+
window.addEventListener('mousemove', this.#onDragResize, {
|
|
273
|
+
signal: this.#controller.signal,
|
|
274
|
+
capture: true
|
|
275
|
+
});
|
|
276
|
+
window.addEventListener('mouseup', this.#onDragEnd, {
|
|
277
|
+
signal: this.#controller.signal,
|
|
278
|
+
capture: true
|
|
279
|
+
});
|
|
280
|
+
};
|
|
281
|
+
#onDragResize = e => {
|
|
282
|
+
const dy = e.clientY - this.#dragStartY;
|
|
283
|
+
const height = Math.round(Math.max(0, this.#prevContentHeight + dy));
|
|
284
|
+
this.#$input.style.setProperty('height', `${height}px`);
|
|
285
|
+
};
|
|
286
|
+
#onDragEnd = () => {
|
|
287
|
+
window.removeEventListener('mousemove', this.#onDragResize, {
|
|
288
|
+
capture: true
|
|
289
|
+
});
|
|
290
|
+
window.removeEventListener('mouseup', this.#onDragEnd, {
|
|
291
|
+
capture: true
|
|
292
|
+
});
|
|
293
|
+
this.#dragStartY = 0;
|
|
294
|
+
this.#prevContentHeight = 0;
|
|
295
|
+
};
|
|
239
296
|
#onInputFocus = () => {
|
|
240
297
|
this.dispatchEvent(new CustomEvent('-focus'));
|
|
241
298
|
};
|