@synerise/ds-input 1.7.10 → 1.7.12
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/CHANGELOG.md +8 -0
- package/dist/AutosizeInput/AutosizeInput.d.ts +6 -7
- package/dist/AutosizeInput/AutosizeInput.js +39 -116
- package/dist/AutosizeInput/AutosizeInput.types.d.ts +5 -18
- package/dist/AutosizeInput/useAutosizeWidth.d.ts +18 -0
- package/dist/AutosizeInput/useAutosizeWidth.js +145 -0
- package/dist/AutosizeInput/useAutosizeWidth.types.d.ts +29 -0
- package/dist/AutosizeInput/useAutosizeWidth.types.js +1 -0
- package/dist/AutosizeInput/useStretchToFit.d.ts +26 -0
- package/dist/AutosizeInput/useStretchToFit.js +31 -0
- package/dist/Input.d.ts +13 -3
- package/dist/Input.js +170 -81
- package/dist/Input.styles.d.ts +32 -11
- package/dist/Input.styles.js +64 -38
- package/dist/Input.types.d.ts +23 -7
- package/dist/InputGroup.d.ts +1 -1
- package/dist/InputGroup.js +11 -4
- package/dist/InputGroup.styles.d.ts +1 -0
- package/dist/InputGroup.styles.js +9 -2
- package/dist/InputGroup.types.d.ts +22 -3
- package/dist/InputMultivalue/InputMultivalue.styles.js +1 -1
- package/dist/TextArea.d.ts +2 -2
- package/dist/TextArea.js +4 -5
- package/dist/Textarea/Textarea.d.ts +4 -3
- package/dist/Textarea/Textarea.js +33 -4
- package/dist/Textarea/Textarea.styles.js +2 -2
- package/dist/Textarea/Textarea.types.d.ts +13 -8
- package/dist/components/ExpandableWrapper.d.ts +1 -2
- package/dist/components/index.d.ts +0 -1
- package/dist/components/index.js +0 -2
- package/dist/hooks/useElementFocus.d.ts +3 -2
- package/dist/hooks/useInputAddonHeight.d.ts +1 -2
- package/dist/hooks/useInputAddonHeight.js +1 -1
- package/dist/hooks/useTextareaAutosize.d.ts +53 -0
- package/dist/hooks/useTextareaAutosize.js +82 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.js +8 -3
- package/dist/utils/getCharCount.d.ts +3 -2
- package/package.json +8 -12
- package/dist/assets/style/index-tn0RQdqM.css +0 -0
- package/dist/components/AutosizeWrapper.d.ts +0 -17
- package/dist/components/AutosizeWrapper.js +0 -18
- package/dist/style/index.css +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.7.12](https://github.com/synerise/synerise-design/compare/@synerise/ds-input@1.7.11...@synerise/ds-input@1.7.12) (2026-07-09)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @synerise/ds-input
|
|
9
|
+
|
|
10
|
+
## [1.7.11](https://github.com/synerise/synerise-design/compare/@synerise/ds-input@1.7.10...@synerise/ds-input@1.7.11) (2026-06-17)
|
|
11
|
+
|
|
12
|
+
**Note:** Version bump only for package @synerise/ds-input
|
|
13
|
+
|
|
6
14
|
## [1.7.10](https://github.com/synerise/synerise-design/compare/@synerise/ds-input@1.7.9...@synerise/ds-input@1.7.10) (2026-06-11)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @synerise/ds-input
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { default as React, CSSProperties } from 'react';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Native auto-width `<input>`: renders its own input plus a hidden sizer, and
|
|
4
|
+
* sizes the input to the text width via `useAutosizeWidth`. The forwarded ref
|
|
5
|
+
* points at the underlying `<input>`.
|
|
6
|
+
*/
|
|
3
7
|
declare const AutosizeInput: React.ForwardRefExoticComponent<Omit<React.InputHTMLAttributes<HTMLInputElement>, "size"> & {
|
|
4
8
|
placeholderIsMinWidth?: boolean;
|
|
5
9
|
wrapperClassName?: string;
|
|
@@ -7,10 +11,5 @@ declare const AutosizeInput: React.ForwardRefExoticComponent<Omit<React.InputHTM
|
|
|
7
11
|
extraWidth?: number | string;
|
|
8
12
|
minWidth?: number | string;
|
|
9
13
|
onAutosize?: (newWidth: number) => void;
|
|
10
|
-
|
|
11
|
-
transformRef?: (element: HTMLElement) => HTMLElement;
|
|
12
|
-
transformWrapperRef?: (element: HTMLElement) => HTMLElement;
|
|
13
|
-
handleInputRef?: (element: HTMLInputElement, originalElement: HTMLElement) => HTMLElement;
|
|
14
|
-
children: React.ReactNode;
|
|
15
|
-
} & React.RefAttributes<AutosizeInputRefType>>;
|
|
14
|
+
} & React.RefAttributes<HTMLInputElement>>;
|
|
16
15
|
export default AutosizeInput;
|
|
@@ -1,128 +1,51 @@
|
|
|
1
1
|
import { jsxs, jsx } from "react/jsx-runtime";
|
|
2
|
-
import { forwardRef, useRef
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
const
|
|
6
|
-
position: "
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
overflow: "scroll",
|
|
12
|
-
whiteSpace: "pre"
|
|
2
|
+
import { forwardRef, useRef } from "react";
|
|
3
|
+
import { useMergeRefs } from "@floating-ui/react";
|
|
4
|
+
import { useAutosizeWidth, SIZER_STYLE } from "./useAutosizeWidth.js";
|
|
5
|
+
const wrapperBaseStyle = {
|
|
6
|
+
position: "relative",
|
|
7
|
+
display: "inline-block"
|
|
8
|
+
};
|
|
9
|
+
const inputBaseStyle = {
|
|
10
|
+
boxSizing: "content-box"
|
|
13
11
|
};
|
|
14
|
-
const FONT_CSS_PROPS = ["fontSize", "fontFamily", "fontWeight", "fontFeatureSettings", "fontStyle", "letterSpacing", "fontStretch", "textTransform"];
|
|
15
12
|
const AutosizeInput = forwardRef(({
|
|
16
|
-
|
|
13
|
+
minWidth,
|
|
14
|
+
extraWidth,
|
|
15
|
+
placeholderIsMinWidth,
|
|
17
16
|
wrapperClassName,
|
|
18
|
-
wrapperStyle
|
|
17
|
+
wrapperStyle,
|
|
19
18
|
onAutosize,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
minWidth = 0,
|
|
23
|
-
handleInputRef,
|
|
24
|
-
children,
|
|
25
|
-
transformWrapperRef,
|
|
26
|
-
transformRef,
|
|
27
|
-
placeholder,
|
|
19
|
+
className,
|
|
20
|
+
style,
|
|
28
21
|
value,
|
|
29
|
-
|
|
30
|
-
|
|
22
|
+
placeholder,
|
|
23
|
+
...inputProps
|
|
31
24
|
}, forwardedRef) => {
|
|
32
|
-
const usedValue = value ?? defaultValue ?? "";
|
|
33
25
|
const inputRef = useRef(null);
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const placeholderSizerRef = useRef(null);
|
|
37
|
-
const wrapperRef = useRef(null);
|
|
38
|
-
const transformWrapper = useMemo(() => {
|
|
39
|
-
return transformWrapperRef || ((element) => element);
|
|
40
|
-
}, [transformWrapperRef]);
|
|
41
|
-
const transformElement = useMemo(() => {
|
|
42
|
-
return transformRef || ((element) => element);
|
|
43
|
-
}, [transformRef]);
|
|
44
|
-
const contentWidth = useRef();
|
|
45
|
-
const placeholderWidth = useRef();
|
|
46
|
-
const applyNewWidth = useCallback(() => {
|
|
47
|
-
const calculatedWidth = calculateInputWidth({
|
|
48
|
-
sizerWidth: contentWidth.current,
|
|
49
|
-
hasValue: !!sizerRef.current?.textContent,
|
|
50
|
-
placeholderIsMinWidth,
|
|
51
|
-
placeholderWidth: placeholderWidth.current ?? placeholderSizerRef.current?.scrollWidth,
|
|
52
|
-
minWidth: +minWidth,
|
|
53
|
-
placeholder
|
|
54
|
-
});
|
|
55
|
-
if (inputWrapperRef.current) {
|
|
56
|
-
const finalWidth = calculatedWidth + +extraWidth;
|
|
57
|
-
if (finalWidth !== parseFloat(inputWrapperRef.current.style.width)) {
|
|
58
|
-
preAutosize && preAutosize(calculatedWidth);
|
|
59
|
-
inputWrapperRef.current.style.width = `${finalWidth}px`;
|
|
60
|
-
onAutosize && onAutosize(calculatedWidth);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}, [extraWidth, minWidth, onAutosize, placeholder, placeholderIsMinWidth, preAutosize]);
|
|
64
|
-
const resizeContentHandler = useCallback((newWidth) => {
|
|
65
|
-
contentWidth.current = newWidth.width;
|
|
66
|
-
applyNewWidth();
|
|
67
|
-
}, [applyNewWidth]);
|
|
68
|
-
const resizePlaceholderHandler = useCallback((newWidth) => {
|
|
69
|
-
placeholderWidth.current = newWidth.width;
|
|
70
|
-
applyNewWidth();
|
|
71
|
-
}, [applyNewWidth]);
|
|
72
|
-
useResizeObserver(sizerRef, resizeContentHandler);
|
|
73
|
-
useResizeObserver(placeholderSizerRef, resizePlaceholderHandler);
|
|
74
|
-
const copyInputStyles = () => {
|
|
75
|
-
if (inputRef.current) {
|
|
76
|
-
const computedStyle = window.getComputedStyle(inputRef.current);
|
|
77
|
-
FONT_CSS_PROPS.forEach((cssProperty) => {
|
|
78
|
-
if (sizerRef.current) {
|
|
79
|
-
sizerRef.current.style[cssProperty] = computedStyle[cssProperty];
|
|
80
|
-
}
|
|
81
|
-
if (placeholderSizerRef.current) {
|
|
82
|
-
placeholderSizerRef.current.style[cssProperty] = computedStyle[cssProperty];
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
useLayoutEffect(() => {
|
|
88
|
-
copyInputStyles();
|
|
89
|
-
}, []);
|
|
90
|
-
useEffect(() => {
|
|
91
|
-
if (sizerRef.current) {
|
|
92
|
-
const sizerSibling = sizerRef.current.nextElementSibling;
|
|
93
|
-
const inputElement = sizerSibling instanceof HTMLElement && transformElement(sizerSibling);
|
|
94
|
-
const inputWrapperElement = sizerSibling instanceof HTMLElement && transformWrapper(sizerSibling);
|
|
95
|
-
if (inputWrapperElement && inputWrapperElement instanceof HTMLElement) {
|
|
96
|
-
inputWrapperRef.current = inputWrapperElement;
|
|
97
|
-
}
|
|
98
|
-
if (inputElement && !inputRef.current) {
|
|
99
|
-
if (inputElement && inputElement instanceof HTMLInputElement) {
|
|
100
|
-
inputRef.current = inputElement;
|
|
101
|
-
inputRef.current.style.boxSizing = "content-box";
|
|
102
|
-
handleInputRef && handleInputRef(inputElement, sizerSibling);
|
|
103
|
-
copyInputStyles();
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}, [handleInputRef, transformElement, transformWrapper]);
|
|
108
|
-
useImperativeHandle(forwardedRef, () => ({
|
|
109
|
-
inputRef,
|
|
26
|
+
const measurableValue = typeof value === "string" || typeof value === "number" ? value : void 0;
|
|
27
|
+
const {
|
|
110
28
|
sizerRef,
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return /* @__PURE__ */ jsxs("div", {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
29
|
+
containerRef
|
|
30
|
+
} = useAutosizeWidth({
|
|
31
|
+
value: measurableValue,
|
|
32
|
+
placeholder,
|
|
33
|
+
minWidth,
|
|
34
|
+
extraWidth,
|
|
35
|
+
placeholderIsMinWidth,
|
|
36
|
+
inputRef,
|
|
37
|
+
onAutosize
|
|
38
|
+
});
|
|
39
|
+
const mergedInputRef = useMergeRefs([inputRef, containerRef, forwardedRef]);
|
|
40
|
+
return /* @__PURE__ */ jsxs("div", { className: wrapperClassName, style: {
|
|
41
|
+
...wrapperBaseStyle,
|
|
42
|
+
...wrapperStyle
|
|
43
|
+
}, children: [
|
|
44
|
+
/* @__PURE__ */ jsx("input", { ...inputProps, value, placeholder, ref: mergedInputRef, className, style: {
|
|
45
|
+
...inputBaseStyle,
|
|
46
|
+
...style
|
|
47
|
+
} }),
|
|
48
|
+
/* @__PURE__ */ jsx("span", { ref: sizerRef, style: SIZER_STYLE, "aria-hidden": true })
|
|
126
49
|
] });
|
|
127
50
|
});
|
|
128
51
|
export {
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import { CSSProperties, InputHTMLAttributes
|
|
1
|
+
import { CSSProperties, InputHTMLAttributes } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Native auto-width input props. The component renders its own `<input>` and a
|
|
4
|
+
* hidden sizer; the forwarded ref points at the `<input>` element.
|
|
5
|
+
*/
|
|
2
6
|
export type AutosizeInputProps = Omit<InputHTMLAttributes<HTMLInputElement>, 'size'> & {
|
|
3
7
|
placeholderIsMinWidth?: boolean;
|
|
4
8
|
wrapperClassName?: string;
|
|
@@ -6,21 +10,4 @@ export type AutosizeInputProps = Omit<InputHTMLAttributes<HTMLInputElement>, 'si
|
|
|
6
10
|
extraWidth?: number | string;
|
|
7
11
|
minWidth?: number | string;
|
|
8
12
|
onAutosize?: (newWidth: number) => void;
|
|
9
|
-
preAutosize?: (newWidth: number) => void;
|
|
10
|
-
transformRef?: (element: HTMLElement) => HTMLElement;
|
|
11
|
-
transformWrapperRef?: (element: HTMLElement) => HTMLElement;
|
|
12
|
-
handleInputRef?: (element: HTMLInputElement, originalElement: HTMLElement) => HTMLElement;
|
|
13
|
-
children: ReactNode;
|
|
14
|
-
};
|
|
15
|
-
export type AutosizeWrapperProps = AutosizeInputProps & {
|
|
16
|
-
autoResize?: boolean;
|
|
17
|
-
};
|
|
18
|
-
export type AutosizeInputRefType = {
|
|
19
|
-
inputRef: MutableRefObject<HTMLInputElement | null>;
|
|
20
|
-
sizerRef: RefObject<HTMLDivElement>;
|
|
21
|
-
wrapperRef: RefObject<HTMLDivElement>;
|
|
22
|
-
inputWrapperRef: RefObject<HTMLElement>;
|
|
23
|
-
placeholderSizerRef: RefObject<HTMLDivElement>;
|
|
24
|
-
copyInputStyles: () => void;
|
|
25
|
-
updateInputWidth: () => void;
|
|
26
13
|
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CSSProperties } from 'react';
|
|
2
|
+
import { UseAutosizeWidthParams, UseAutosizeWidthResult } from './useAutosizeWidth.types';
|
|
3
|
+
/**
|
|
4
|
+
* Styles for the hidden measuring `<span>`. It is taken out of layout flow
|
|
5
|
+
* (`position: absolute`, `height: 0`, `visibility: hidden`) so it never affects
|
|
6
|
+
* the surrounding layout, and `white-space: pre` preserves spacing exactly as
|
|
7
|
+
* typed. Font properties inherit by default; when the consuming input declares
|
|
8
|
+
* its font on the element itself (not an ancestor), pass `inputRef` to copy it.
|
|
9
|
+
*/
|
|
10
|
+
export declare const SIZER_STYLE: CSSProperties;
|
|
11
|
+
/**
|
|
12
|
+
* Headless auto-width measurement. The consumer renders a single hidden sizer
|
|
13
|
+
* `<span>` and an inline-block wrapper; this hook measures the text width
|
|
14
|
+
* synchronously in `useLayoutEffect` (before paint) and writes the resulting
|
|
15
|
+
* width to the wrapper imperatively — no ResizeObserver and no per-keystroke
|
|
16
|
+
* state update, so typing does not trigger extra React renders.
|
|
17
|
+
*/
|
|
18
|
+
export declare const useAutosizeWidth: <C extends HTMLElement = HTMLDivElement>({ value, placeholder, minWidth, extraWidth, placeholderIsMinWidth, inputRef, onAutosize, }: UseAutosizeWidthParams) => UseAutosizeWidthResult<C>;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { useRef, useCallback, useLayoutEffect, useEffect } from "react";
|
|
2
|
+
import calculateInputWidth from "./utils/calculateInputWidth.js";
|
|
3
|
+
const SIZER_STYLE = {
|
|
4
|
+
position: "absolute",
|
|
5
|
+
top: 0,
|
|
6
|
+
left: 0,
|
|
7
|
+
height: 0,
|
|
8
|
+
visibility: "hidden",
|
|
9
|
+
overflow: "scroll",
|
|
10
|
+
whiteSpace: "pre",
|
|
11
|
+
font: "inherit",
|
|
12
|
+
letterSpacing: "inherit",
|
|
13
|
+
textTransform: "inherit",
|
|
14
|
+
fontFeatureSettings: "inherit",
|
|
15
|
+
fontVariantNumeric: "inherit"
|
|
16
|
+
};
|
|
17
|
+
const FONT_CSS_PROPS = [
|
|
18
|
+
"fontSize",
|
|
19
|
+
"fontFamily",
|
|
20
|
+
"fontWeight",
|
|
21
|
+
"fontFeatureSettings",
|
|
22
|
+
// Mirror tabular/proportional figures onto the sizer — otherwise an input using
|
|
23
|
+
// `font-variant-numeric: tabular-nums` renders tabular but is measured proportional,
|
|
24
|
+
// clipping the value or leaving a gap.
|
|
25
|
+
"fontVariantNumeric",
|
|
26
|
+
"fontStyle",
|
|
27
|
+
"letterSpacing",
|
|
28
|
+
"fontStretch",
|
|
29
|
+
"textTransform"
|
|
30
|
+
];
|
|
31
|
+
const useAutosizeWidth = ({
|
|
32
|
+
value,
|
|
33
|
+
placeholder,
|
|
34
|
+
minWidth = 0,
|
|
35
|
+
extraWidth = 0,
|
|
36
|
+
placeholderIsMinWidth,
|
|
37
|
+
inputRef,
|
|
38
|
+
onAutosize
|
|
39
|
+
}) => {
|
|
40
|
+
const sizerRef = useRef(null);
|
|
41
|
+
const containerRef = useRef(null);
|
|
42
|
+
const copyFont = useCallback(() => {
|
|
43
|
+
const sizer = sizerRef.current;
|
|
44
|
+
const input = inputRef?.current;
|
|
45
|
+
if (!sizer || !input) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const computedStyle = window.getComputedStyle(input);
|
|
49
|
+
FONT_CSS_PROPS.forEach((cssProperty) => {
|
|
50
|
+
sizer.style[cssProperty] = computedStyle[cssProperty];
|
|
51
|
+
});
|
|
52
|
+
}, [inputRef]);
|
|
53
|
+
const measure = useCallback(() => {
|
|
54
|
+
const sizer = sizerRef.current;
|
|
55
|
+
const container = containerRef.current;
|
|
56
|
+
if (!sizer || !container) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const content = value === void 0 || value === null ? "" : String(value);
|
|
60
|
+
sizer.textContent = content;
|
|
61
|
+
const contentWidth = sizer.scrollWidth;
|
|
62
|
+
let placeholderWidth;
|
|
63
|
+
if (placeholder) {
|
|
64
|
+
sizer.textContent = placeholder;
|
|
65
|
+
placeholderWidth = sizer.scrollWidth;
|
|
66
|
+
sizer.textContent = content;
|
|
67
|
+
}
|
|
68
|
+
const computedWidth = calculateInputWidth({
|
|
69
|
+
sizerWidth: contentWidth,
|
|
70
|
+
hasValue: content.length > 0,
|
|
71
|
+
placeholderIsMinWidth,
|
|
72
|
+
placeholderWidth,
|
|
73
|
+
minWidth: +minWidth,
|
|
74
|
+
placeholder
|
|
75
|
+
});
|
|
76
|
+
const finalWidth = computedWidth + +extraWidth;
|
|
77
|
+
if (parseFloat(container.style.width) !== finalWidth) {
|
|
78
|
+
container.style.width = `${finalWidth}px`;
|
|
79
|
+
onAutosize && onAutosize(computedWidth);
|
|
80
|
+
}
|
|
81
|
+
}, [value, placeholder, placeholderIsMinWidth, minWidth, extraWidth, onAutosize]);
|
|
82
|
+
useLayoutEffect(() => {
|
|
83
|
+
copyFont();
|
|
84
|
+
}, [copyFont]);
|
|
85
|
+
useLayoutEffect(() => {
|
|
86
|
+
measure();
|
|
87
|
+
}, [measure]);
|
|
88
|
+
const measureRef = useRef(measure);
|
|
89
|
+
const copyFontRef = useRef(copyFont);
|
|
90
|
+
useLayoutEffect(() => {
|
|
91
|
+
measureRef.current = measure;
|
|
92
|
+
copyFontRef.current = copyFont;
|
|
93
|
+
});
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
const sizer = sizerRef.current;
|
|
96
|
+
if (!sizer || typeof ResizeObserver === "undefined") {
|
|
97
|
+
return void 0;
|
|
98
|
+
}
|
|
99
|
+
let frame = 0;
|
|
100
|
+
const hasRaf = typeof window.requestAnimationFrame === "function";
|
|
101
|
+
const observer = new ResizeObserver(() => {
|
|
102
|
+
if (hasRaf) {
|
|
103
|
+
window.cancelAnimationFrame(frame);
|
|
104
|
+
frame = window.requestAnimationFrame(() => measureRef.current());
|
|
105
|
+
} else {
|
|
106
|
+
measureRef.current();
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
observer.observe(sizer);
|
|
110
|
+
return () => {
|
|
111
|
+
observer.disconnect();
|
|
112
|
+
if (hasRaf) {
|
|
113
|
+
window.cancelAnimationFrame(frame);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}, []);
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
const fontSet = typeof document !== "undefined" ? document.fonts : void 0;
|
|
119
|
+
if (!fontSet) {
|
|
120
|
+
return void 0;
|
|
121
|
+
}
|
|
122
|
+
const remeasure = () => {
|
|
123
|
+
copyFontRef.current();
|
|
124
|
+
measureRef.current();
|
|
125
|
+
};
|
|
126
|
+
fontSet.ready.then(remeasure).catch(() => void 0);
|
|
127
|
+
fontSet.addEventListener("loadingdone", remeasure);
|
|
128
|
+
return () => {
|
|
129
|
+
fontSet.removeEventListener("loadingdone", remeasure);
|
|
130
|
+
};
|
|
131
|
+
}, []);
|
|
132
|
+
const recalculate = useCallback(() => {
|
|
133
|
+
copyFont();
|
|
134
|
+
measure();
|
|
135
|
+
}, [copyFont, measure]);
|
|
136
|
+
return {
|
|
137
|
+
sizerRef,
|
|
138
|
+
containerRef,
|
|
139
|
+
recalculate
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
export {
|
|
143
|
+
SIZER_STYLE,
|
|
144
|
+
useAutosizeWidth
|
|
145
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RefObject } from 'react';
|
|
2
|
+
export type UseAutosizeWidthParams = {
|
|
3
|
+
/** Current input value — drives the measured content width. */
|
|
4
|
+
value?: string | number;
|
|
5
|
+
/** Placeholder text — used as the measured width when there is no value. */
|
|
6
|
+
placeholder?: string;
|
|
7
|
+
/** Lower bound (px) applied to the computed content width. */
|
|
8
|
+
minWidth?: number | string;
|
|
9
|
+
/** Extra px added to the computed width (padding / affordances / icons). */
|
|
10
|
+
extraWidth?: number | string;
|
|
11
|
+
/** When true, the placeholder width is used as a minimum width. */
|
|
12
|
+
placeholderIsMinWidth?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Optional ref to the styled input. When provided, its resolved font is
|
|
15
|
+
* copied to the hidden sizer once on mount so measurement matches exactly,
|
|
16
|
+
* even when the font is declared on the input rather than an ancestor.
|
|
17
|
+
*/
|
|
18
|
+
inputRef?: RefObject<HTMLElement | null>;
|
|
19
|
+
/** Fired with the computed content width (excl. extraWidth) whenever it changes. */
|
|
20
|
+
onAutosize?: (width: number) => void;
|
|
21
|
+
};
|
|
22
|
+
export type UseAutosizeWidthResult<C extends HTMLElement = HTMLDivElement> = {
|
|
23
|
+
/** Attach to a single hidden, empty `<span>` styled with `SIZER_STYLE`. */
|
|
24
|
+
sizerRef: RefObject<HTMLSpanElement>;
|
|
25
|
+
/** Attach to the inline-block wrapper whose width the hook sets imperatively. */
|
|
26
|
+
containerRef: RefObject<C>;
|
|
27
|
+
/** Force a re-measure (e.g. after a web font loads or layout changes). */
|
|
28
|
+
recalculate: () => void;
|
|
29
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { RefObject } from 'react';
|
|
2
|
+
export type UseStretchToFitParams = {
|
|
3
|
+
/** When false the observer is disconnected and no max-width is applied. */
|
|
4
|
+
enabled: boolean;
|
|
5
|
+
/** Element whose `max-width` is clamped to the available width. */
|
|
6
|
+
targetRef: RefObject<HTMLElement | null>;
|
|
7
|
+
/**
|
|
8
|
+
* Maps the measured available width to the max-width to apply. Defaults to
|
|
9
|
+
* identity; consumers tweak it for their own padding/affordances
|
|
10
|
+
* (e.g. `(w) => w - paddingDiff` for Input, `(w) => w + 1` for Autocomplete).
|
|
11
|
+
*/
|
|
12
|
+
getMaxWidth?: (availableWidth: number) => number;
|
|
13
|
+
/** Runs before the max-width is applied (e.g. capture scroll position). */
|
|
14
|
+
onBeforeResize?: () => void;
|
|
15
|
+
/** Runs after the max-width is applied (e.g. restore scroll position). */
|
|
16
|
+
onAfterResize?: () => void;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Centralises the (previously triplicated) "stretch the autosize input to fill
|
|
20
|
+
* its container" behaviour. Attach the returned `outerRef` to the bounding
|
|
21
|
+
* element; its width is observed via `useResizeToFit` (no state churn) and the
|
|
22
|
+
* `targetRef` element's `max-width` is updated on resize.
|
|
23
|
+
*/
|
|
24
|
+
export declare const useStretchToFit: <T extends HTMLElement = HTMLDivElement>({ enabled, targetRef, getMaxWidth, onBeforeResize, onAfterResize, }: UseStretchToFitParams) => {
|
|
25
|
+
outerRef: RefObject<T>;
|
|
26
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useResizeToFit } from "@synerise/ds-utils";
|
|
3
|
+
const useStretchToFit = ({
|
|
4
|
+
enabled,
|
|
5
|
+
targetRef,
|
|
6
|
+
getMaxWidth = (availableWidth) => availableWidth,
|
|
7
|
+
onBeforeResize,
|
|
8
|
+
onAfterResize
|
|
9
|
+
}) => {
|
|
10
|
+
const handleResize = useCallback((availableWidth) => {
|
|
11
|
+
const target = targetRef.current;
|
|
12
|
+
if (!enabled || !target) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
onBeforeResize && onBeforeResize();
|
|
16
|
+
target.style.maxWidth = `${getMaxWidth(availableWidth)}px`;
|
|
17
|
+
onAfterResize && onAfterResize();
|
|
18
|
+
}, [enabled, targetRef, getMaxWidth, onBeforeResize, onAfterResize]);
|
|
19
|
+
const {
|
|
20
|
+
elementRef: outerRef
|
|
21
|
+
} = useResizeToFit({
|
|
22
|
+
onResize: handleResize,
|
|
23
|
+
autoObserve: enabled
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
outerRef
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
export {
|
|
30
|
+
useStretchToFit
|
|
31
|
+
};
|
package/dist/Input.d.ts
CHANGED
|
@@ -14,10 +14,20 @@ export declare const Input: React.ForwardRefExoticComponent<{
|
|
|
14
14
|
prefixel?: React.ReactNode;
|
|
15
15
|
suffixel?: React.ReactNode;
|
|
16
16
|
autoResize?: import('./Input.types').AutoResizeProp;
|
|
17
|
-
autoResizeProps?: Partial<Pick<import('
|
|
17
|
+
autoResizeProps?: Partial<Pick<import('.').AutosizeInputProps, "placeholderIsMinWidth" | "wrapperClassName" | "wrapperStyle" | "extraWidth">>;
|
|
18
18
|
expandable?: boolean;
|
|
19
19
|
expandableTooltip?: React.ReactNode;
|
|
20
|
-
} & Omit<import('@synerise/ds-form-field').ContentAboveProps, "id" | "rightSide"> & import('@synerise/ds-form-field').ContentBelowProps &
|
|
21
|
-
|
|
20
|
+
} & Omit<import('@synerise/ds-form-field').ContentAboveProps, "id" | "rightSide"> & import('@synerise/ds-form-field').ContentBelowProps & Omit<React.InputHTMLAttributes<HTMLInputElement>, "size" | "prefix"> & {
|
|
21
|
+
size?: import('./Input.types').InputSize;
|
|
22
|
+
allowClear?: boolean;
|
|
23
|
+
bordered?: boolean;
|
|
24
|
+
innerPrefix?: React.ReactNode;
|
|
25
|
+
prefix?: React.ReactNode;
|
|
26
|
+
suffix?: React.ReactNode;
|
|
27
|
+
onPressEnter?: React.KeyboardEventHandler<HTMLInputElement>;
|
|
28
|
+
addonBefore?: React.ReactNode;
|
|
29
|
+
addonAfter?: React.ReactNode;
|
|
30
|
+
} & React.RefAttributes<HTMLDivElement>>;
|
|
31
|
+
export declare const RawInput: ({ error, size, className, label, description, errorText, tooltip, tooltipConfig, counterLimit, renderCustomCounter, icon1, icon1Tooltip, icon2, icon2Tooltip, resetMargin, handleInputRef, prefixel, suffixel, autoResize, autoResizeProps, expandable, expandableTooltip, allowClear, prefix, suffix, bordered, onPressEnter, addonBefore, addonAfter, ...rest }: InputProps) => React.JSX.Element;
|
|
22
32
|
export { default as InputGroup } from './InputGroup';
|
|
23
33
|
export { default as InputMultivalue } from './InputMultivalue/InputMultivalue';
|