use-auto-width-input 0.0.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/README.md +77 -0
- package/dist/index.cjs +112 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +90 -0
- package/dist/index.js.map +1 -0
- package/license.md +9 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# use-auto-width-input
|
|
2
|
+
|
|
3
|
+
A lightweight React hook that automatically adjusts input width based on content.
|
|
4
|
+
|
|
5
|
+
> [!WARNING]
|
|
6
|
+
> This is a working in progress library and more features will be added in the future, like: more customization via `Option` parameter and function overloading for multiple parameters, giving the hook more flexibility.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install use-auto-width-input
|
|
12
|
+
# or
|
|
13
|
+
yarn add use-auto-width-input
|
|
14
|
+
# or
|
|
15
|
+
bun add use-auto-width-input
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
import { useRef } from 'react';
|
|
22
|
+
import { useAutoWidthInput } from 'use-auto-width-input';
|
|
23
|
+
|
|
24
|
+
function App() {
|
|
25
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
26
|
+
const { callbackRef } = useAutoWidthInput(inputRef);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<input
|
|
30
|
+
ref={callbackRef}
|
|
31
|
+
type="text"
|
|
32
|
+
placeholder="Type something..."
|
|
33
|
+
style={{ minWidth: "50px", maxWidth: "480px" }}
|
|
34
|
+
/>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## API
|
|
40
|
+
|
|
41
|
+
### `useAutoWidthInput(inputRef, options?)`
|
|
42
|
+
|
|
43
|
+
#### Parameters
|
|
44
|
+
|
|
45
|
+
- `inputRef`: A React ref object for the input element
|
|
46
|
+
- `options` (optional):
|
|
47
|
+
- `minWidth?: string` - Minimum width for the input
|
|
48
|
+
|
|
49
|
+
#### Returns
|
|
50
|
+
|
|
51
|
+
- `callbackRef`: Callback ref to attach to the input element
|
|
52
|
+
- `width`: Current width value
|
|
53
|
+
- `ref`: The original input ref
|
|
54
|
+
|
|
55
|
+
## How It Works
|
|
56
|
+
|
|
57
|
+
The hook creates an invisible "ghost" element that mirrors the input's text and font styles. As you type, it measures the ghost element's width and dynamically applies it to the actual input, creating a seamless auto-sizing effect.
|
|
58
|
+
|
|
59
|
+
## Controlling Width Constraints
|
|
60
|
+
|
|
61
|
+
You can easily control the minimum and maximum width by applying standard CSS styles directly to your input element:
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
<input
|
|
65
|
+
ref={callbackRef}
|
|
66
|
+
style={{
|
|
67
|
+
minWidth: "100px", // Won't shrink below this
|
|
68
|
+
maxWidth: "500px" // Won't grow beyond this
|
|
69
|
+
}}
|
|
70
|
+
/>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
This gives you full control over the input's width boundaries without needing additional hook configuration.
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
useAutoWidthInput: () => useAutoWidthInput
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/use-auto-width-input.ts
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
|
|
30
|
+
// src/helpers.ts
|
|
31
|
+
var applyFontStyles = (styles, target) => {
|
|
32
|
+
target.style.fontFamily = styles.fontFamily;
|
|
33
|
+
target.style.fontSize = styles.fontSize;
|
|
34
|
+
target.style.letterSpacing = styles.letterSpacing;
|
|
35
|
+
target.style.fontWeight = styles.fontWeight;
|
|
36
|
+
};
|
|
37
|
+
var createGhostElement = (options) => {
|
|
38
|
+
const tempElement = document.createElement("p");
|
|
39
|
+
if (options?.minWidth) {
|
|
40
|
+
tempElement.style.minWidth = options?.minWidth;
|
|
41
|
+
}
|
|
42
|
+
Object.assign(tempElement.style, {
|
|
43
|
+
position: "absolute",
|
|
44
|
+
whiteSpace: "pre",
|
|
45
|
+
top: "0",
|
|
46
|
+
left: "0",
|
|
47
|
+
visibility: "hidden",
|
|
48
|
+
height: "0",
|
|
49
|
+
overflow: "hidden",
|
|
50
|
+
pointerEvents: "none",
|
|
51
|
+
zIndex: -1e3,
|
|
52
|
+
margin: "0"
|
|
53
|
+
// Reset margin to avoid layout interference
|
|
54
|
+
});
|
|
55
|
+
tempElement.innerText = "";
|
|
56
|
+
tempElement.setAttribute("class", "ghost-paragraph-element");
|
|
57
|
+
return document.body.appendChild(tempElement);
|
|
58
|
+
};
|
|
59
|
+
var syncWidth = (inputRef, ghostElement) => {
|
|
60
|
+
if (inputRef.current)
|
|
61
|
+
inputRef.current.style.width = `${ghostElement.current?.getBoundingClientRect()?.width ?? 0}px`;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// src/use-auto-width-input.ts
|
|
65
|
+
function useAutoWidthInput(inputRef, options) {
|
|
66
|
+
const [width] = (0, import_react.useState)(options?.minWidth ?? 0);
|
|
67
|
+
const ghostElement = (0, import_react.useRef)(null);
|
|
68
|
+
const input = inputRef.current;
|
|
69
|
+
const handleInput = (event) => {
|
|
70
|
+
const target = event.target;
|
|
71
|
+
if (ghostElement.current) {
|
|
72
|
+
ghostElement.current.innerText = target.value;
|
|
73
|
+
syncWidth(inputRef, ghostElement);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const destroy = () => {
|
|
77
|
+
input?.removeEventListener("input", handleInput);
|
|
78
|
+
ghostElement.current?.remove();
|
|
79
|
+
ghostElement.current = null;
|
|
80
|
+
};
|
|
81
|
+
(0, import_react.useEffect)(
|
|
82
|
+
() => () => destroy(),
|
|
83
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
84
|
+
[]
|
|
85
|
+
);
|
|
86
|
+
const callbackRef = (0, import_react.useCallback)(
|
|
87
|
+
(element) => {
|
|
88
|
+
inputRef.current = element;
|
|
89
|
+
if (!element) destroy();
|
|
90
|
+
else if (element && inputRef.current) {
|
|
91
|
+
const styles = window.getComputedStyle(inputRef.current);
|
|
92
|
+
ghostElement.current = createGhostElement(options);
|
|
93
|
+
ghostElement.current.innerText = inputRef.current.value;
|
|
94
|
+
applyFontStyles(styles, ghostElement.current);
|
|
95
|
+
syncWidth(inputRef, ghostElement);
|
|
96
|
+
inputRef.current.addEventListener("input", handleInput);
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
100
|
+
[inputRef]
|
|
101
|
+
);
|
|
102
|
+
return {
|
|
103
|
+
width,
|
|
104
|
+
callbackRef,
|
|
105
|
+
ref: inputRef
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
109
|
+
0 && (module.exports = {
|
|
110
|
+
useAutoWidthInput
|
|
111
|
+
});
|
|
112
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/use-auto-width-input.ts","../src/helpers.ts"],"sourcesContent":["export * from \"./use-auto-width-input\";\n\nexport * from \"./types\";\n","import {\n useCallback,\n useEffect,\n useRef,\n useState,\n type RefObject,\n} from \"react\";\nimport type { AutWidthInputOptions } from \"./types\";\nimport { applyFontStyles, createGhostElement, syncWidth } from \"./helpers\";\n\nexport function useAutoWidthInput(\n inputRef: RefObject<HTMLInputElement | null>,\n options?: AutWidthInputOptions\n) {\n const [width] = useState(options?.minWidth ?? 0);\n const ghostElement = useRef<HTMLElement>(null);\n const input = inputRef.current;\n\n const handleInput = (event: Event) => {\n const target = event.target as HTMLInputElement;\n\n if (ghostElement.current) {\n ghostElement.current.innerText = target.value;\n\n syncWidth(inputRef, ghostElement);\n }\n };\n\n const destroy = () => {\n input?.removeEventListener(\"input\", handleInput);\n ghostElement.current?.remove();\n ghostElement.current = null;\n };\n\n useEffect(\n () => () => destroy(),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n []\n );\n\n const callbackRef = useCallback(\n (element: HTMLInputElement | null) => {\n inputRef.current = element;\n\n if (!element) destroy();\n else if (element && inputRef.current) {\n const styles = window.getComputedStyle(inputRef.current);\n ghostElement.current = createGhostElement(options);\n\n ghostElement.current.innerText = inputRef.current.value;\n applyFontStyles(styles, ghostElement.current);\n syncWidth(inputRef, ghostElement);\n\n inputRef.current.addEventListener(\"input\", handleInput);\n }\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [inputRef]\n );\n\n return {\n width,\n callbackRef,\n ref: inputRef,\n };\n}\n","import type { RefObject } from \"react\";\nimport type { AutWidthInputOptions } from \"./types\";\n\nexport const applyFontStyles = (\n styles: CSSStyleDeclaration,\n target: HTMLElement\n) => {\n target.style.fontFamily = styles.fontFamily;\n target.style.fontSize = styles.fontSize;\n target.style.letterSpacing = styles.letterSpacing;\n target.style.fontWeight = styles.fontWeight;\n};\n\nexport const createGhostElement = (options?: AutWidthInputOptions) => {\n const tempElement = document.createElement(\"p\");\n\n if (options?.minWidth) {\n tempElement.style.minWidth = options?.minWidth;\n }\n\n Object.assign(tempElement.style, {\n position: \"absolute\",\n whiteSpace: \"pre\",\n top: \"0\",\n left: \"0\",\n visibility: \"hidden\",\n height: \"0\",\n overflow: \"hidden\",\n pointerEvents: \"none\",\n zIndex: -1000,\n margin: \"0\", // Reset margin to avoid layout interference\n });\n\n tempElement.innerText = \"\";\n tempElement.setAttribute(\"class\", \"ghost-paragraph-element\");\n\n return document.body.appendChild(tempElement);\n};\n\nexport const syncWidth = (\n inputRef: RefObject<HTMLInputElement | null>,\n ghostElement: RefObject<HTMLElement | null>\n) => {\n if (inputRef.current)\n inputRef.current.style.width = `${\n ghostElement.current?.getBoundingClientRect()?.width ?? 0\n }px`;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAMO;;;ACHA,IAAM,kBAAkB,CAC7B,QACA,WACG;AACH,SAAO,MAAM,aAAa,OAAO;AACjC,SAAO,MAAM,WAAW,OAAO;AAC/B,SAAO,MAAM,gBAAgB,OAAO;AACpC,SAAO,MAAM,aAAa,OAAO;AACnC;AAEO,IAAM,qBAAqB,CAAC,YAAmC;AACpE,QAAM,cAAc,SAAS,cAAc,GAAG;AAE9C,MAAI,SAAS,UAAU;AACrB,gBAAY,MAAM,WAAW,SAAS;AAAA,EACxC;AAEA,SAAO,OAAO,YAAY,OAAO;AAAA,IAC/B,UAAU;AAAA,IACV,YAAY;AAAA,I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,MACxD;AAAA,IACF;AAAA;AAAA,IAEA,CAAC,QAAQ;AAAA,EACX;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,KAAK;AAAA,EACP;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { RefObject } from 'react';
|
|
2
|
+
|
|
3
|
+
type AutWidthInputOptions = {
|
|
4
|
+
minWidth?: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
declare function useAutoWidthInput(inputRef: RefObject<HTMLInputElement | null>, options?: AutWidthInputOptions): {
|
|
8
|
+
width: string | number;
|
|
9
|
+
callbackRef: (element: HTMLInputElement | null) => void;
|
|
10
|
+
ref: RefObject<HTMLInputElement | null>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { type AutWidthInputOptions, useAutoWidthInput };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { RefObject } from 'react';
|
|
2
|
+
|
|
3
|
+
type AutWidthInputOptions = {
|
|
4
|
+
minWidth?: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
declare function useAutoWidthInput(inputRef: RefObject<HTMLInputElement | null>, options?: AutWidthInputOptions): {
|
|
8
|
+
width: string | number;
|
|
9
|
+
callbackRef: (element: HTMLInputElement | null) => void;
|
|
10
|
+
ref: RefObject<HTMLInputElement | null>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { type AutWidthInputOptions, useAutoWidthInput };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// src/use-auto-width-input.ts
|
|
2
|
+
import {
|
|
3
|
+
useCallback,
|
|
4
|
+
useEffect,
|
|
5
|
+
useRef,
|
|
6
|
+
useState
|
|
7
|
+
} from "react";
|
|
8
|
+
|
|
9
|
+
// src/helpers.ts
|
|
10
|
+
var applyFontStyles = (styles, target) => {
|
|
11
|
+
target.style.fontFamily = styles.fontFamily;
|
|
12
|
+
target.style.fontSize = styles.fontSize;
|
|
13
|
+
target.style.letterSpacing = styles.letterSpacing;
|
|
14
|
+
target.style.fontWeight = styles.fontWeight;
|
|
15
|
+
};
|
|
16
|
+
var createGhostElement = (options) => {
|
|
17
|
+
const tempElement = document.createElement("p");
|
|
18
|
+
if (options?.minWidth) {
|
|
19
|
+
tempElement.style.minWidth = options?.minWidth;
|
|
20
|
+
}
|
|
21
|
+
Object.assign(tempElement.style, {
|
|
22
|
+
position: "absolute",
|
|
23
|
+
whiteSpace: "pre",
|
|
24
|
+
top: "0",
|
|
25
|
+
left: "0",
|
|
26
|
+
visibility: "hidden",
|
|
27
|
+
height: "0",
|
|
28
|
+
overflow: "hidden",
|
|
29
|
+
pointerEvents: "none",
|
|
30
|
+
zIndex: -1e3,
|
|
31
|
+
margin: "0"
|
|
32
|
+
// Reset margin to avoid layout interference
|
|
33
|
+
});
|
|
34
|
+
tempElement.innerText = "";
|
|
35
|
+
tempElement.setAttribute("class", "ghost-paragraph-element");
|
|
36
|
+
return document.body.appendChild(tempElement);
|
|
37
|
+
};
|
|
38
|
+
var syncWidth = (inputRef, ghostElement) => {
|
|
39
|
+
if (inputRef.current)
|
|
40
|
+
inputRef.current.style.width = `${ghostElement.current?.getBoundingClientRect()?.width ?? 0}px`;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/use-auto-width-input.ts
|
|
44
|
+
function useAutoWidthInput(inputRef, options) {
|
|
45
|
+
const [width] = useState(options?.minWidth ?? 0);
|
|
46
|
+
const ghostElement = useRef(null);
|
|
47
|
+
const input = inputRef.current;
|
|
48
|
+
const handleInput = (event) => {
|
|
49
|
+
const target = event.target;
|
|
50
|
+
if (ghostElement.current) {
|
|
51
|
+
ghostElement.current.innerText = target.value;
|
|
52
|
+
syncWidth(inputRef, ghostElement);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const destroy = () => {
|
|
56
|
+
input?.removeEventListener("input", handleInput);
|
|
57
|
+
ghostElement.current?.remove();
|
|
58
|
+
ghostElement.current = null;
|
|
59
|
+
};
|
|
60
|
+
useEffect(
|
|
61
|
+
() => () => destroy(),
|
|
62
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
63
|
+
[]
|
|
64
|
+
);
|
|
65
|
+
const callbackRef = useCallback(
|
|
66
|
+
(element) => {
|
|
67
|
+
inputRef.current = element;
|
|
68
|
+
if (!element) destroy();
|
|
69
|
+
else if (element && inputRef.current) {
|
|
70
|
+
const styles = window.getComputedStyle(inputRef.current);
|
|
71
|
+
ghostElement.current = createGhostElement(options);
|
|
72
|
+
ghostElement.current.innerText = inputRef.current.value;
|
|
73
|
+
applyFontStyles(styles, ghostElement.current);
|
|
74
|
+
syncWidth(inputRef, ghostElement);
|
|
75
|
+
inputRef.current.addEventListener("input", handleInput);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
79
|
+
[inputRef]
|
|
80
|
+
);
|
|
81
|
+
return {
|
|
82
|
+
width,
|
|
83
|
+
callbackRef,
|
|
84
|
+
ref: inputRef
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
export {
|
|
88
|
+
useAutoWidthInput
|
|
89
|
+
};
|
|
90
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/use-auto-width-input.ts","../src/helpers.ts"],"sourcesContent":["import {\n useCallback,\n useEffect,\n useRef,\n useState,\n type RefObject,\n} from \"react\";\nimport type { AutWidthInputOptions } from \"./types\";\nimport { applyFontStyles, createGhostElement, syncWidth } from \"./helpers\";\n\nexport function useAutoWidthInput(\n inputRef: RefObject<HTMLInputElement | null>,\n options?: AutWidthInputOptions\n) {\n const [width] = useState(options?.minWidth ?? 0);\n const ghostElement = useRef<HTMLElement>(null);\n const input = inputRef.current;\n\n const handleInput = (event: Event) => {\n const target = event.target as HTMLInputElement;\n\n if (ghostElement.current) {\n ghostElement.current.innerText = target.value;\n\n syncWidth(inputRef, ghostElement);\n }\n };\n\n const destroy = () => {\n input?.removeEventListener(\"input\", handleInput);\n ghostElement.current?.remove();\n ghostElement.current = null;\n };\n\n useEffect(\n () => () => destroy(),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n []\n );\n\n const callbackRef = useCallback(\n (element: HTMLInputElement | null) => {\n inputRef.current = element;\n\n if (!element) destroy();\n else if (element && inputRef.current) {\n const styles = window.getComputedStyle(inputRef.current);\n ghostElement.current = createGhostElement(options);\n\n ghostElement.current.innerText = inputRef.current.value;\n applyFontStyles(styles, ghostElement.current);\n syncWidth(inputRef, ghostElement);\n\n inputRef.current.addEventListener(\"input\", handleInput);\n }\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [inputRef]\n );\n\n return {\n width,\n callbackRef,\n ref: inputRef,\n };\n}\n","import type { RefObject } from \"react\";\nimport type { AutWidthInputOptions } from \"./types\";\n\nexport const applyFontStyles = (\n styles: CSSStyleDeclaration,\n target: HTMLElement\n) => {\n target.style.fontFamily = styles.fontFamily;\n target.style.fontSize = styles.fontSize;\n target.style.letterSpacing = styles.letterSpacing;\n target.style.fontWeight = styles.fontWeight;\n};\n\nexport const createGhostElement = (options?: AutWidthInputOptions) => {\n const tempElement = document.createElement(\"p\");\n\n if (options?.minWidth) {\n tempElement.style.minWidth = options?.minWidth;\n }\n\n Object.assign(tempElement.style, {\n position: \"absolute\",\n whiteSpace: \"pre\",\n top: \"0\",\n left: \"0\",\n visibility: \"hidden\",\n height: \"0\",\n overflow: \"hidden\",\n pointerEvents: \"none\",\n zIndex: -1000,\n margin: \"0\", // Reset margin to avoid layout interference\n });\n\n tempElement.innerText = \"\";\n tempElement.setAttribute(\"class\", \"ghost-paragraph-element\");\n\n return document.body.appendChild(tempElement);\n};\n\nexport const syncWidth = (\n inputRef: RefObject<HTMLInputElement | null>,\n ghostElement: RefObject<HTMLElement | null>\n) => {\n if (inputRef.current)\n inputRef.current.style.width = `${\n ghostElement.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AAW;AAC/C,iBAAa,SAAS,OAAO;AAC7B,iBAAa,UAAU;AAAA,EACzB;AAEA;AAAA,IACE,MAAM,MAAM,QAAQ;AAAA;AAAA,IAEpB,CAAC;AAAA,EACH;AAEA,QAAM,cAAc;AAAA,IAClB,CAAC,YAAqC;AACpC,eAAS,UAAU;AAEnB,UAAI,CAAC,QAAS,SAAQ;AAAA,eACb,WAAW,SAAS,SAAS;AACpC,cAAM,SAAS,OAAO,iBAAiB,SAAS,OAAO;AACvD,qBAAa,UAAU,mBAAmB,OAAO;AAEjD,qBAAa,QAAQ,YAAY,SAAS,QAAQ;AAClD,wBAAgB,QAAQ,aAAa,OAAO;AAC5C,kBAAU,UAAU,YAAY;AAEhC,iBAAS,QAAQ,iBAAiB,SAAS,WAAW;AAAA,MACxD;AAAA,IACF;AAAA;AAAA,IAEA,CAAC,QAAQ;AAAA,EACX;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,KAAK;AAAA,EACP;AACF;","names":[]}
|
package/license.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 [Luis Vanin]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "use-auto-width-input",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A React hook to automatically resize an input based on its content",
|
|
5
|
+
"author": "Luis Vanin",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"types": "/dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"type": "module",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"dev:app": "bun run --cwd playground dev",
|
|
15
|
+
"lint": "bun eslint",
|
|
16
|
+
"build": "tsup",
|
|
17
|
+
"prepare": "husky",
|
|
18
|
+
"prepublishOnly": "bun run build"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/bun": "latest",
|
|
22
|
+
"@types/react": "^19.2.7",
|
|
23
|
+
"@types/react-dom": "^19.2.3",
|
|
24
|
+
"eslint": "^9.39.2",
|
|
25
|
+
"eslint-plugin-react": "^7.37.5",
|
|
26
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
27
|
+
"globals": "^16.5.0",
|
|
28
|
+
"husky": "^9.1.7",
|
|
29
|
+
"react": "^19.2.3",
|
|
30
|
+
"react-dom": "^19.2.3",
|
|
31
|
+
"tsup": "^8.5.1",
|
|
32
|
+
"typescript-eslint": "^8.49.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"typescript": "^5.9.3",
|
|
36
|
+
"react": "^19.2.3",
|
|
37
|
+
"react-dom": "^19.2.3"
|
|
38
|
+
}
|
|
39
|
+
}
|