@zag-js/auto-resize 0.9.2 → 0.10.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.
Files changed (2) hide show
  1. package/package.json +4 -3
  2. package/src/index.ts +71 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/auto-resize",
3
- "version": "0.9.2",
3
+ "version": "0.10.1",
4
4
  "description": "Autoresize utilities for the web",
5
5
  "keywords": [
6
6
  "js",
@@ -13,7 +13,8 @@
13
13
  "repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/auto-resize",
14
14
  "sideEffects": false,
15
15
  "files": [
16
- "dist/**/*"
16
+ "dist",
17
+ "src"
17
18
  ],
18
19
  "publishConfig": {
19
20
  "access": "public"
@@ -22,7 +23,7 @@
22
23
  "url": "https://github.com/chakra-ui/zag/issues"
23
24
  },
24
25
  "dependencies": {
25
- "@zag-js/dom-query": "0.9.2"
26
+ "@zag-js/dom-query": "0.10.1"
26
27
  },
27
28
  "devDependencies": {
28
29
  "clean-package": "2.2.0"
package/src/index.ts ADDED
@@ -0,0 +1,71 @@
1
+ import { getDocument, getWindow } from "@zag-js/dom-query"
2
+
3
+ function copyVisualStyles(fromEl: HTMLElement | null, toEl: HTMLElement) {
4
+ if (!fromEl) return
5
+
6
+ const win = getWindow(fromEl)
7
+ const el = win.getComputedStyle(fromEl)
8
+
9
+ // prettier-ignore
10
+ const cssText = 'box-sizing:' + el.boxSizing +
11
+ ';border-left:' + el.borderLeftWidth + ' solid red' +
12
+ ';border-right:' + el.borderRightWidth + ' solid red' +
13
+ ';font-family:' + el.fontFamily +
14
+ ';font-feature-settings:' + el.fontFeatureSettings +
15
+ ';font-kerning:' + el.fontKerning +
16
+ ';font-size:' + el.fontSize +
17
+ ';font-stretch:' + el.fontStretch +
18
+ ';font-style:' + el.fontStyle +
19
+ ';font-variant:' + el.fontVariant +
20
+ ';font-variant-caps:' + el.fontVariantCaps +
21
+ ';font-variant-ligatures:' + el.fontVariantLigatures +
22
+ ';font-variant-numeric:' + el.fontVariantNumeric +
23
+ ';font-weight:' + el.fontWeight +
24
+ ';letter-spacing:' + el.letterSpacing +
25
+ ';margin-left:' + el.marginLeft +
26
+ ';margin-right:' + el.marginRight +
27
+ ';padding-left:' + el.paddingLeft +
28
+ ';padding-right:' + el.paddingRight +
29
+ ';text-indent:' + el.textIndent +
30
+ ';text-transform:' + el.textTransform
31
+
32
+ toEl.style.cssText += cssText
33
+ }
34
+
35
+ function createGhostElement(doc: Document) {
36
+ var el = doc.createElement("div")
37
+ el.id = "ghost"
38
+ el.style.cssText =
39
+ "display:inline-block;height:0;overflow:hidden;position:absolute;top:0;visibility:hidden;white-space:nowrap;"
40
+ doc.body.appendChild(el)
41
+ return el
42
+ }
43
+
44
+ export function autoResizeInput(input: HTMLInputElement | null) {
45
+ if (!input) return
46
+ const doc = getDocument(input)
47
+ const win = getWindow(input)
48
+
49
+ const ghost = createGhostElement(doc)
50
+
51
+ copyVisualStyles(input, ghost)
52
+
53
+ function resize() {
54
+ win.requestAnimationFrame(() => {
55
+ ghost.innerHTML = input!.value
56
+ const rect = win.getComputedStyle(ghost)
57
+ input?.style.setProperty("width", rect.width)
58
+ })
59
+ }
60
+
61
+ resize()
62
+
63
+ input?.addEventListener("input", resize)
64
+ input?.addEventListener("change", resize)
65
+
66
+ return () => {
67
+ doc.body.removeChild(ghost)
68
+ input?.removeEventListener("input", resize)
69
+ input?.removeEventListener("change", resize)
70
+ }
71
+ }