@jaykay-design/flext 1.0.0

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.
@@ -0,0 +1,6 @@
1
+ interface IFlextInfo {
2
+ container?: HTMLElement | undefined;
3
+ textarea?: HTMLTextAreaElement | undefined;
4
+ }
5
+ declare function setup(e: CustomEvent<IFlextInfo>): void;
6
+ export { setup, IFlextInfo };
package/dist/index.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setup = setup;
4
+ function resize(element) {
5
+ const scrollY = window.scrollY, styles = getComputedStyle(element), paddingWidth = parseInt(styles.getPropertyValue('padding-top'), 10) + parseInt(styles.getPropertyValue('padding-bottom'), 10), borderWidths = parseInt(styles.getPropertyValue('border-top-width'), 10) + parseInt(styles.getPropertyValue('border-bottom-width'), 10), copy = document.getElementById(element.dataset.copy);
6
+ let minHeight = borderWidths;
7
+ if (element.closest('.form-floating'))
8
+ minHeight += 24 + paddingWidth;
9
+ copy.value = element.value;
10
+ let newHeight = copy.scrollHeight;
11
+ if (newHeight === 0)
12
+ newHeight = minHeight;
13
+ newHeight += paddingWidth + borderWidths;
14
+ element.style.height = newHeight + 'px';
15
+ window.scrollTo(window.scrollX, scrollY);
16
+ }
17
+ function setup(e) {
18
+ if (CSS.supports('field-sizing', 'content'))
19
+ return;
20
+ let items = [];
21
+ if (e.detail.textarea)
22
+ items.push(e.detail.textarea);
23
+ else if (e.detail.container)
24
+ items = Array.from(e.detail.container.querySelectorAll('textarea.growme'));
25
+ else
26
+ return;
27
+ items.forEach(el => {
28
+ const copy = document.createElement('textarea');
29
+ copy.style.height = '1px';
30
+ copy.style.width = '100%';
31
+ copy.style.visibility = 'hidden';
32
+ copy.style.border = '0';
33
+ copy.style.padding = '0';
34
+ copy.id = 'flext-' + Math.trunc(Math.random() * 100000000);
35
+ copy.value = el.value;
36
+ el.dataset.copy = copy.id;
37
+ el.insertAdjacentElement('afterend', copy);
38
+ resize(el);
39
+ if (el.hasAttribute('flext'))
40
+ return;
41
+ el.setAttribute('flext', 'done');
42
+ el.addEventListener('keyup', () => resize(el));
43
+ el.addEventListener('paste', () => resize(el));
44
+ });
45
+ }
package/index.js ADDED
File without changes
package/index.ts ADDED
@@ -0,0 +1,64 @@
1
+ interface IFlextInfo {
2
+ container?: HTMLElement | undefined;
3
+ textarea?: HTMLTextAreaElement | undefined;
4
+ }
5
+
6
+ function resize(element: HTMLTextAreaElement) {
7
+ const scrollY = window.scrollY,
8
+ styles = getComputedStyle(element),
9
+ paddingWidth = parseInt(styles.getPropertyValue('padding-top'), 10) + parseInt(styles.getPropertyValue('padding-bottom'), 10),
10
+ borderWidths = parseInt(styles.getPropertyValue('border-top-width'), 10) + parseInt(styles.getPropertyValue('border-bottom-width'), 10),
11
+ copy = document.getElementById(element.dataset.copy!)! as HTMLTextAreaElement;
12
+
13
+ let minHeight = borderWidths;
14
+
15
+ if (element.closest('.form-floating')) minHeight += 24 + paddingWidth;
16
+
17
+ copy.value = element.value;
18
+
19
+ let newHeight = copy.scrollHeight;
20
+ if (newHeight === 0) newHeight = minHeight;
21
+
22
+ newHeight += paddingWidth + borderWidths;
23
+
24
+ element.style.height = newHeight + 'px';
25
+
26
+ window.scrollTo(window.scrollX, scrollY);
27
+ }
28
+
29
+ function setup(e: CustomEvent<IFlextInfo>) {
30
+ if (CSS.supports('field-sizing', 'content')) return;
31
+
32
+ let items: HTMLTextAreaElement[] = [];
33
+
34
+ if (e.detail.textarea) items.push(e.detail.textarea);
35
+ else if (e.detail.container) items = Array.from(e.detail.container.querySelectorAll('textarea.growme'));
36
+ else return;
37
+
38
+ items.forEach(el => {
39
+ const copy = document.createElement('textarea');
40
+
41
+ copy.style.height = '1px';
42
+ copy.style.width = '100%';
43
+ copy.style.visibility = 'hidden';
44
+ copy.style.border = '0';
45
+ copy.style.padding = '0';
46
+
47
+ copy.id = 'flext-' + Math.trunc(Math.random() * 100000000);
48
+
49
+ copy.value = el.value;
50
+ el.dataset.copy = copy.id;
51
+ el.insertAdjacentElement('afterend', copy);
52
+
53
+ resize(el);
54
+
55
+ if (el.hasAttribute('flext')) return;
56
+ el.setAttribute('flext', 'done');
57
+
58
+ el.addEventListener('keyup', () => resize(el));
59
+ el.addEventListener('paste', () => resize(el));
60
+ });
61
+ }
62
+
63
+ export { setup, IFlextInfo };
64
+
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@jaykay-design/flext",
3
+ "version": "1.0.0",
4
+ "description": "HTML textbox polyfill for CSS field-sizing:content",
5
+ "license": "ISC",
6
+ "author": "",
7
+ "type": "commonjs",
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1",
12
+ "build": "tsc"
13
+ },
14
+ "devDependencies": {
15
+ "typescript": "^5.9.3"
16
+ }
17
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2015",
4
+ "module": "commonjs",
5
+ "declaration": true,
6
+ "outDir": "./dist",
7
+ "strict": true
8
+ }
9
+ }