@hh.ru/magritte-ui-textarea 3.0.3 → 3.1.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.
package/index.js CHANGED
@@ -13,10 +13,11 @@ import '@hh.ru/magritte-ui-breakpoint';
13
13
  import '@hh.ru/magritte-ui-form-helper';
14
14
  import '@hh.ru/magritte-ui-icon/private';
15
15
  import '@hh.ru/magritte-ui-input/useInputAriaProps';
16
+ import '@hh.ru/magritte-ui-scrollbar';
16
17
  import './useFocus.js';
17
- import './useLimitGrow.js';
18
- import './useMinHeight.js';
18
+ import './useHeightObserver.js';
19
19
  import './types.js';
20
+ import './useLimitGrow.js';
20
21
  import '@hh.ru/magritte-ui-typography';
21
22
  import '@hh.ru/magritte-common-func-utils';
22
23
  //# sourceMappingURL=index.js.map
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hh.ru/magritte-ui-textarea",
3
- "version": "3.0.3",
3
+ "version": "3.1.0",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "sideEffects": [
@@ -19,16 +19,17 @@
19
19
  "watch": "yarn root:watch $(pwd)"
20
20
  },
21
21
  "dependencies": {
22
- "@hh.ru/magritte-common-func-utils": "1.3.12",
23
- "@hh.ru/magritte-common-use-disabled": "1.0.12",
24
- "@hh.ru/magritte-common-use-multiple-refs": "1.1.8",
22
+ "@hh.ru/magritte-common-func-utils": "1.3.13",
23
+ "@hh.ru/magritte-common-use-disabled": "1.0.13",
24
+ "@hh.ru/magritte-common-use-multiple-refs": "1.1.9",
25
25
  "@hh.ru/magritte-common-use-translation": "1.0.1",
26
- "@hh.ru/magritte-design-tokens": "23.2.0",
27
- "@hh.ru/magritte-ui-breakpoint": "6.0.0",
28
- "@hh.ru/magritte-ui-form-helper": "2.0.5",
29
- "@hh.ru/magritte-ui-icon": "12.3.1",
30
- "@hh.ru/magritte-ui-input": "9.0.9",
31
- "@hh.ru/magritte-ui-typography": "4.2.1"
26
+ "@hh.ru/magritte-design-tokens": "23.2.1",
27
+ "@hh.ru/magritte-ui-breakpoint": "6.0.1",
28
+ "@hh.ru/magritte-ui-form-helper": "2.0.6",
29
+ "@hh.ru/magritte-ui-icon": "12.3.2",
30
+ "@hh.ru/magritte-ui-input": "9.0.10",
31
+ "@hh.ru/magritte-ui-scrollbar": "1.0.1",
32
+ "@hh.ru/magritte-ui-typography": "4.2.2"
32
33
  },
33
34
  "peerDependencies": {
34
35
  "classnames": ">=2.3.2",
@@ -38,5 +39,5 @@
38
39
  "publishConfig": {
39
40
  "access": "public"
40
41
  },
41
- "gitHead": "fb6e7a383f8c329c0b5bc46df4d4bb11e44d9d24"
42
+ "gitHead": "8c9ac514d963c54434c7f9ed84d3ae347fc5f396"
42
43
  }
@@ -0,0 +1,14 @@
1
+ import { type RefObject } from 'react';
2
+ import { type TextAreaLayout, type TextAreaSize } from '@hh.ru/magritte-ui-textarea/types';
3
+ type UseHeightObserverArgs = {
4
+ size: TextAreaSize;
5
+ minHeight: number | undefined;
6
+ layout: TextAreaLayout;
7
+ contentCloneRef: RefObject<HTMLElement>;
8
+ onHeightChange?: VoidFunction;
9
+ };
10
+ export declare const useHeightObserver: ({ size, minHeight, layout, contentCloneRef, onHeightChange, }: UseHeightObserverArgs) => {
11
+ minHeight: number;
12
+ maxedHeight: boolean;
13
+ };
14
+ export {};
@@ -2,15 +2,18 @@ import './index.css';
2
2
  import { useState, useRef, useEffect } from 'react';
3
3
  import { MIN_HEIGHT_BY_SIZE } from './types.js';
4
4
 
5
- const useMinHeight = (size, minHeight, layout, contentCloneRef) => {
5
+ const useHeightObserver = ({ size, minHeight, layout, contentCloneRef, onHeightChange, }) => {
6
6
  const _minHeight = minHeight ?? MIN_HEIGHT_BY_SIZE[size][1];
7
7
  const isMaxSizeHeight = _minHeight === MIN_HEIGHT_BY_SIZE[size][1];
8
8
  const [actualMinHeight, setActualMinHeight] = useState(_minHeight);
9
9
  const lastProcessedSizeRef = useRef(size);
10
10
  const isSizeChanged = lastProcessedSizeRef.current !== size;
11
11
  lastProcessedSizeRef.current = size;
12
+ const onHeightChangeRef = useRef(onHeightChange);
13
+ onHeightChangeRef.current = onHeightChange;
14
+ const rectsLengthRef = useRef(null);
12
15
  useEffect(() => {
13
- if (!contentCloneRef.current || isMaxSizeHeight) {
16
+ if (!contentCloneRef.current) {
14
17
  return void 0;
15
18
  }
16
19
  const range = document.createRange();
@@ -20,11 +23,15 @@ const useMinHeight = (size, minHeight, layout, contentCloneRef) => {
20
23
  }
21
24
  range.selectNodeContents(contentCloneRef.current);
22
25
  const rects = range.getClientRects();
26
+ if (rectsLengthRef.current !== rects.length && rectsLengthRef.current !== null) {
27
+ onHeightChangeRef.current?.();
28
+ }
29
+ rectsLengthRef.current = rects.length;
23
30
  setActualMinHeight(MIN_HEIGHT_BY_SIZE[size][rects.length > 1 ? 1 : 0]);
24
31
  });
25
32
  observer.observe(contentCloneRef.current);
26
33
  return () => observer.disconnect();
27
- }, [layout, size, contentCloneRef, isMaxSizeHeight]);
34
+ }, [layout, size, contentCloneRef]);
28
35
  let resultMinHeight = isSizeChanged || isMaxSizeHeight ? _minHeight : actualMinHeight;
29
36
  if (layout !== 'hug') {
30
37
  resultMinHeight = MIN_HEIGHT_BY_SIZE[size][1];
@@ -32,5 +39,5 @@ const useMinHeight = (size, minHeight, layout, contentCloneRef) => {
32
39
  return { minHeight: resultMinHeight, maxedHeight: resultMinHeight === MIN_HEIGHT_BY_SIZE[size][1] };
33
40
  };
34
41
 
35
- export { useMinHeight };
36
- //# sourceMappingURL=useMinHeight.js.map
42
+ export { useHeightObserver };
43
+ //# sourceMappingURL=useHeightObserver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useHeightObserver.js","sources":["../src/useHeightObserver.ts"],"sourcesContent":["import { type RefObject, useState, useEffect, useRef } from 'react';\n\nimport { type TextAreaLayout, type TextAreaSize, MIN_HEIGHT_BY_SIZE } from '@hh.ru/magritte-ui-textarea/types';\n\ntype UseHeightObserverArgs = {\n size: TextAreaSize;\n minHeight: number | undefined;\n layout: TextAreaLayout;\n contentCloneRef: RefObject<HTMLElement>;\n onHeightChange?: VoidFunction;\n};\n\nexport const useHeightObserver = ({\n size,\n minHeight,\n layout,\n contentCloneRef,\n onHeightChange,\n}: UseHeightObserverArgs): { minHeight: number; maxedHeight: boolean } => {\n const _minHeight = minHeight ?? MIN_HEIGHT_BY_SIZE[size][1];\n const isMaxSizeHeight = _minHeight === MIN_HEIGHT_BY_SIZE[size][1];\n const [actualMinHeight, setActualMinHeight] = useState(_minHeight);\n const lastProcessedSizeRef = useRef<TextAreaSize>(size);\n const isSizeChanged = lastProcessedSizeRef.current !== size;\n lastProcessedSizeRef.current = size;\n const onHeightChangeRef = useRef(onHeightChange);\n onHeightChangeRef.current = onHeightChange;\n const rectsLengthRef = useRef<number | null>(null);\n\n useEffect(() => {\n if (!contentCloneRef.current) {\n return void 0;\n }\n\n const range = document.createRange();\n const observer = new ResizeObserver(() => {\n if (!contentCloneRef.current) {\n return;\n }\n\n range.selectNodeContents(contentCloneRef.current);\n const rects = range.getClientRects();\n if (rectsLengthRef.current !== rects.length && rectsLengthRef.current !== null) {\n onHeightChangeRef.current?.();\n }\n rectsLengthRef.current = rects.length;\n setActualMinHeight(MIN_HEIGHT_BY_SIZE[size][rects.length > 1 ? 1 : 0]);\n });\n observer.observe(contentCloneRef.current);\n return () => observer.disconnect();\n }, [layout, size, contentCloneRef]);\n\n let resultMinHeight = isSizeChanged || isMaxSizeHeight ? _minHeight : actualMinHeight;\n if (layout !== 'hug') {\n resultMinHeight = MIN_HEIGHT_BY_SIZE[size][1];\n }\n\n return { minHeight: resultMinHeight, maxedHe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OAAO,KAAK,KAAK,CAAC,MAAM,IAAI,cAAc,CAAC,OAAO,KAAK,IAAI,EAAE;AAC5E,gBAAA,iBAAiB,CAAC,OAAO,IAAI,CAAC;aACjC;AACD,YAAA,cAAc,CAAC,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC;YACtC,kBAAkB,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AAC3E,SAAC,CAAC,CAAC;AACH,QAAA,QAAQ,CAAC,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;AAC1C,QAAA,OAAO,MAAM,QAAQ,CAAC,UAAU,EAAE,CAAC;KACtC,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,eAAe,CAAC,CAAC,CAAC;AAEpC,IAAA,IAAI,eAAe,GAAG,aAAa,IAAI,eAAe,GAAG,UAAU,GAAG,eAAe,CAAC;AACtF,IAAA,IAAI,MAAM,KAAK,KAAK,EAAE;QAClB,eAAe,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;KACjD;AAED,IAAA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,WAAW,EAAE,eAAe,KAAK,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACxG;;;;"}
package/useMinHeight.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { type RefObject } from 'react';
2
- import { type TextAreaLayout, type TextAreaSize } from '@hh.ru/magritte-ui-textarea/types';
3
- export declare const useMinHeight: (size: TextAreaSize, minHeight: number | undefined, layout: TextAreaLayout, contentCloneRef: RefObject<HTMLElement>) => {
4
- minHeight: number;
5
- maxedHeight: boolean;
6
- };
@@ -1 +0,0 @@
1
- {"version":3,"file":"useMinHeight.js","sources":["../src/useMinHeight.ts"],"sourcesContent":["import { type RefObject, useState, useEffect, useRef } from 'react';\n\nimport { type TextAreaLayout, type TextAreaSize, MIN_HEIGHT_BY_SIZE } from '@hh.ru/magritte-ui-textarea/types';\n\nexport const useMinHeight = (\n size: TextAreaSize,\n minHeight: number | undefined,\n layout: TextAreaLayout,\n contentCloneRef: RefObject<HTMLElement>\n): { minHeight: number; maxedHeight: boolean } => {\n const _minHeight = minHeight ?? MIN_HEIGHT_BY_SIZE[size][1];\n const isMaxSizeHeight = _minHeight === MIN_HEIGHT_BY_SIZE[size][1];\n const [actualMinHeight, setActualMinHeight] = useState(_minHeight);\n const lastProcessedSizeRef = useRef<TextAreaSize>(size);\n const isSizeChanged = lastProcessedSizeRef.current !== size;\n lastProcessedSizeRef.current = size;\n\n useEffect(() => {\n if (!contentCloneRef.current || isMaxSizeHeight) {\n return void 0;\n }\n const range = document.createRange();\n\n const observer = new ResizeObserver(() => {\n if (!contentCloneRef.current) {\n return;\n }\n\n range.selectNodeContents(contentCloneRef.current);\n const rects = range.getClientRects();\n setActualMinHeight(MIN_HEIGHT_BY_SIZE[size][rects.length > 1 ? 1 : 0]);\n });\n observer.observe(contentCloneRef.current);\n return () => observer.disconnect();\n }, [layout, size, contentCloneRef, isMaxSizeHeight]);\n\n let resultMinHeight = isSizeChanged || isMaxSizeHeight ? _minHeight : actualMinHeight;\n if (layout !== 'hug') {\n resultMinHeight = MIN_HEIGHT_BY_SIZE[size][1];\n }\n\n return { minHeight: resultMinHeight, maxedHeight: resultMinHeight === MIN_HEIGHT_BY_SIZE[size][1] };\n};\n"],"names":[],"mappings":";;;AAIO,MAAM,YAAY,GAAG,CACxB,IAAkB,EAClB,SAA6B,EAC7B,MAAsB,EACtB,eAAuC,KACM;IAC7C,MAAM,UAAU,GAAG,SAAS,IAAI,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5D,MAAM,eAAe,GAAG,UAAU,KAAK,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IACnE,MAAM,CAAC,eAAe,EAAE,kBAAkB,CAAC,GAAG,Q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