@scality/core-ui 0.182.0 → 0.184.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.
@@ -3,12 +3,13 @@ export declare const COPY_STATE_IDLE = "idle";
3
3
  export declare const COPY_STATE_SUCCESS = "success";
4
4
  export declare const COPY_STATE_UNSUPPORTED = "unsupported";
5
5
  export declare const useClipboard: () => {
6
- copy: (text: any) => void;
6
+ copy: (text: string, asHtml?: boolean) => void;
7
7
  copyStatus: string;
8
8
  };
9
- export declare const CopyButton: ({ label, textToCopy, variant, ...props }: {
9
+ export declare const CopyButton: ({ label, textToCopy, copyAsHtml, variant, ...props }: {
10
10
  label?: string;
11
11
  textToCopy: string;
12
+ copyAsHtml?: boolean;
12
13
  variant?: "outline" | "ghost";
13
14
  } & Props) => import("react/jsx-runtime").JSX.Element;
14
15
  //# sourceMappingURL=CopyButton.component.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"CopyButton.component.d.ts","sourceRoot":"","sources":["../../../src/lib/components/buttonv2/CopyButton.component.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAU,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAErD,eAAO,MAAM,eAAe,SAAS,CAAC;AACtC,eAAO,MAAM,kBAAkB,YAAY,CAAC;AAC5C,eAAO,MAAM,sBAAsB,gBAAgB,CAAC;AACpD,eAAO,MAAM,YAAY;;;CAuBxB,CAAC;AAEF,eAAO,MAAM,UAAU,6CAKpB;IACD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC;CAC/B,GAAG,KAAK,4CA+CR,CAAC"}
1
+ {"version":3,"file":"CopyButton.component.d.ts","sourceRoot":"","sources":["../../../src/lib/components/buttonv2/CopyButton.component.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAU,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAErD,eAAO,MAAM,eAAe,SAAS,CAAC;AACtC,eAAO,MAAM,kBAAkB,YAAY,CAAC;AAC5C,eAAO,MAAM,sBAAsB,gBAAgB,CAAC;AACpD,eAAO,MAAM,YAAY;iBASQ,MAAM,WAAW,OAAO;;CAoCxD,CAAC;AAEF,eAAO,MAAM,UAAU,yDAMpB;IACD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,OAAO,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC;CAC/B,GAAG,KAAK,4CA+CR,CAAC"}
@@ -13,20 +13,41 @@ export const useClipboard = () => {
13
13
  }, 2000);
14
14
  return () => clearTimeout(timer);
15
15
  }, [copyStatus]);
16
- const copyToClipboard = (text) => {
16
+ const copyToClipboard = (text, asHtml) => {
17
17
  if (!navigator || !navigator.clipboard) {
18
18
  setCopyStatus(COPY_STATE_UNSUPPORTED);
19
19
  return;
20
20
  }
21
- navigator.clipboard.writeText(text);
22
- setCopyStatus(COPY_STATE_SUCCESS);
21
+ if (asHtml) {
22
+ // Copy as HTML with plain text fallback
23
+ const el = document.createElement('div');
24
+ el.innerHTML = text;
25
+ const plainText = el.innerText;
26
+ const clipboardItem = new ClipboardItem({
27
+ 'text/html': new Blob([text], { type: 'text/html' }),
28
+ 'text/plain': new Blob([plainText], { type: 'text/plain' }),
29
+ });
30
+ navigator.clipboard
31
+ .write([clipboardItem])
32
+ .then(() => {
33
+ setCopyStatus(COPY_STATE_SUCCESS);
34
+ })
35
+ .catch(() => {
36
+ setCopyStatus(COPY_STATE_UNSUPPORTED);
37
+ });
38
+ }
39
+ else {
40
+ // Copy as plain text only
41
+ navigator.clipboard.writeText(text);
42
+ setCopyStatus(COPY_STATE_SUCCESS);
43
+ }
23
44
  };
24
45
  return {
25
46
  copy: copyToClipboard,
26
47
  copyStatus: copyStatus,
27
48
  };
28
49
  };
29
- export const CopyButton = ({ label, textToCopy, variant, ...props }) => {
50
+ export const CopyButton = ({ label, textToCopy, copyAsHtml, variant, ...props }) => {
30
51
  const { copy, copyStatus } = useClipboard();
31
52
  return (_jsx(Button, { ...props, variant: variant === 'outline' ? 'outline' : undefined, style: {
32
53
  minWidth:
@@ -38,7 +59,7 @@ export const CopyButton = ({ label, textToCopy, variant, ...props }) => {
38
59
  ? copyStatus === COPY_STATE_SUCCESS
39
60
  ? `Copied${label ? ' ' + label + '' : ''}!`
40
61
  : `Copy${label ? ' ' + label : ''}`
41
- : undefined, icon: _jsx(Icon, { name: copyStatus === COPY_STATE_SUCCESS ? 'Check' : 'Copy', color: copyStatus === COPY_STATE_SUCCESS ? 'statusHealthy' : undefined }), disabled: copyStatus === COPY_STATE_SUCCESS || props.disabled, onClick: () => copy(textToCopy), type: "button", tooltip: variant !== 'outline'
62
+ : undefined, icon: _jsx(Icon, { name: copyStatus === COPY_STATE_SUCCESS ? 'Check' : 'Copy', color: copyStatus === COPY_STATE_SUCCESS ? 'statusHealthy' : undefined }), disabled: copyStatus === COPY_STATE_SUCCESS || props.disabled, onClick: () => copy(textToCopy, copyAsHtml), type: "button", tooltip: variant !== 'outline'
42
63
  ? {
43
64
  overlay: copyStatus === COPY_STATE_SUCCESS
44
65
  ? 'Copied !'
@@ -2,7 +2,8 @@ type Props = {
2
2
  title: string | React.ReactNode;
3
3
  content: React.ReactNode;
4
4
  link?: string;
5
+ linkText?: string;
5
6
  };
6
- export declare const InfoMessage: ({ title, content, link }: Props) => import("react/jsx-runtime").JSX.Element;
7
+ export declare const InfoMessage: ({ title, content, link, linkText }: Props) => import("react/jsx-runtime").JSX.Element;
7
8
  export {};
8
9
  //# sourceMappingURL=InfoMessage.component.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"InfoMessage.component.d.ts","sourceRoot":"","sources":["../../../src/lib/components/infomessage/InfoMessage.component.tsx"],"names":[],"mappings":"AAMA,KAAK,KAAK,GAAG;IACX,KAAK,EAAE,MAAM,GAAG,KAAK,CAAC,SAAS,CAAC;IAChC,OAAO,EAAE,KAAK,CAAC,SAAS,CAAC;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAYF,eAAO,MAAM,WAAW,6BAA8B,KAAK,4CAuB1D,CAAC"}
1
+ {"version":3,"file":"InfoMessage.component.d.ts","sourceRoot":"","sources":["../../../src/lib/components/infomessage/InfoMessage.component.tsx"],"names":[],"mappings":"AAMA,KAAK,KAAK,GAAG;IACX,KAAK,EAAE,MAAM,GAAG,KAAK,CAAC,SAAS,CAAC;IAChC,OAAO,EAAE,KAAK,CAAC,SAAS,CAAC;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAYF,eAAO,MAAM,WAAW,uCAAwC,KAAK,4CAuBpE,CAAC"}
@@ -13,8 +13,8 @@ const InfoMessageContainer = styled.div `
13
13
  gap: 0.5rem;
14
14
  color: white;
15
15
  `;
16
- export const InfoMessage = ({ title, content, link }) => {
16
+ export const InfoMessage = ({ title, content, link, linkText }) => {
17
17
  const { containerRef, backgroundColor } = useComputeBackgroundColor();
18
18
  const theme = useTheme();
19
- return (_jsxs(InfoMessageContainer, { ref: containerRef, style: { backgroundColor: backgroundColor }, children: [_jsxs(Stack, { children: [_jsx(Icon, { name: "Info-circle", color: theme.infoPrimary, size: "lg" }), typeof title === 'string' ? _jsx(Text, { isEmphazed: true, children: title }) : title] }), _jsx(Text, { color: "textSecondary", isGentleEmphazed: true, children: content }), link && (_jsxs(Link, { href: link, target: "_blank", style: { alignSelf: 'flex-end' }, children: ["More info ", _jsx(Icon, { name: "External-link" })] }))] }));
19
+ return (_jsxs(InfoMessageContainer, { ref: containerRef, style: { backgroundColor: backgroundColor }, children: [_jsxs(Stack, { children: [_jsx(Icon, { name: "Info-circle", color: theme.infoPrimary, size: "lg" }), typeof title === 'string' ? _jsx(Text, { isEmphazed: true, children: title }) : title] }), _jsx(Text, { color: "textSecondary", isGentleEmphazed: true, children: content }), link && (_jsxs(Link, { href: link, target: "_blank", style: { alignSelf: 'flex-end' }, children: [linkText || 'More info', " ", _jsx(Icon, { name: "External-link" })] }))] }));
20
20
  };
@@ -5,6 +5,11 @@ export declare const TextArea: import("react").ForwardRefExoticComponent<Textare
5
5
  variant?: TextAreaVariant;
6
6
  width?: CSSProperties["width"];
7
7
  height?: CSSProperties["height"];
8
+ /**
9
+ * Automatically adjust height to fit content
10
+ * When enabled, the textarea will grow/shrink to show all content
11
+ */
12
+ autoGrow?: boolean;
8
13
  } & import("react").RefAttributes<RefType>>;
9
14
  export {};
10
15
  //# sourceMappingURL=TextArea.component.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"TextArea.component.d.ts","sourceRoot":"","sources":["../../../src/lib/components/textarea/TextArea.component.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EAEb,sBAAsB,EAEvB,MAAM,OAAO,CAAC;AAIf,KAAK,eAAe,GAAG,MAAM,GAAG,MAAM,CAAC;AAMvC,KAAK,OAAO,GAAG,mBAAmB,GAAG,IAAI,CAAC;AA4F1C,eAAO,MAAM,QAAQ;cAhGT,eAAe;YACjB,aAAa,CAAC,OAAO,CAAC;aACrB,aAAa,CAAC,QAAQ,CAAC;2CA8FiC,CAAC"}
1
+ {"version":3,"file":"TextArea.component.d.ts","sourceRoot":"","sources":["../../../src/lib/components/textarea/TextArea.component.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EAEb,sBAAsB,EAMvB,MAAM,OAAO,CAAC;AAIf,KAAK,eAAe,GAAG,MAAM,GAAG,MAAM,CAAC;AAWvC,KAAK,OAAO,GAAG,mBAAmB,GAAG,IAAI,CAAC;AA0J1C,eAAO,MAAM,QAAQ;cAnKT,eAAe;YACjB,aAAa,CAAC,OAAO,CAAC;aACrB,aAAa,CAAC,QAAQ,CAAC;IAChC;;;OAGG;eACQ,OAAO;2CA4J+C,CAAC"}
@@ -1,11 +1,11 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { forwardRef, } from 'react';
2
+ import { forwardRef, useEffect, useRef, useImperativeHandle, useCallback, } from 'react';
3
3
  import styled, { css } from 'styled-components';
4
4
  import { spacing } from '../../spacing';
5
5
  const TextAreaContainer = styled.textarea `
6
6
  padding: ${spacing.r12} ${spacing.r8};
7
7
  border-radius: 4px;
8
- resize: vertical;
8
+ resize: ${(props) => (props.autoGrow ? 'none' : 'vertical')};
9
9
  font-family: ${(props) => props.variant === 'code' ? 'Courier New' : 'Lato'};
10
10
  font-size: ${spacing.f14};
11
11
 
@@ -19,12 +19,18 @@ const TextAreaContainer = styled.textarea `
19
19
  css `
20
20
  width: ${props.width};
21
21
  `}
22
-
22
+
23
23
  ${(props) => props.height &&
24
24
  css `
25
25
  height: ${props.height};
26
26
  `}
27
27
 
28
+ ${(props) => props.autoGrow &&
29
+ css `
30
+ overflow: hidden;
31
+ box-sizing: border-box;
32
+ `}
33
+
28
34
  &:placeholder-shown {
29
35
  font-style: italic;
30
36
  }
@@ -52,10 +58,36 @@ const TextAreaContainer = styled.textarea `
52
58
  `;
53
59
  }}
54
60
  `;
55
- function TextAreaElement({ rows = 3, cols = 20, width, height, variant = 'code', ...rest }, ref) {
61
+ function TextAreaElement({ rows = 3, cols = 20, width, height, variant = 'code', autoGrow = false, value, defaultValue, onChange, ...rest }, ref) {
62
+ const internalRef = useRef(null);
63
+ // Expose the textarea element to parent components via forwarded ref
64
+ useImperativeHandle(ref, () => internalRef.current);
65
+ // Adjust height on mount and when value changes (for controlled components)
66
+ const adjustHeight = useCallback(() => {
67
+ const textarea = internalRef.current;
68
+ if (!textarea || !autoGrow)
69
+ return;
70
+ // Reset height to auto to get the correct scrollHeight
71
+ textarea.style.height = '0px';
72
+ // Set the height to match the content
73
+ const newHeight = textarea.scrollHeight;
74
+ textarea.style.height = `${newHeight}px`;
75
+ }, [autoGrow]);
76
+ useEffect(() => {
77
+ adjustHeight();
78
+ }, [adjustHeight, value]);
79
+ // Handle onChange to support both controlled and uncontrolled components
80
+ const handleChange = useCallback((event) => {
81
+ if (autoGrow) {
82
+ adjustHeight();
83
+ }
84
+ if (onChange) {
85
+ onChange(event);
86
+ }
87
+ }, [autoGrow, adjustHeight, onChange]);
56
88
  if (width || height) {
57
- return (_jsx(TextAreaContainer, { className: "sc-textarea", width: width, height: height, variant: variant, ...rest, ref: ref }));
89
+ return (_jsx(TextAreaContainer, { className: "sc-textarea", width: width, height: height, variant: variant, autoGrow: autoGrow, value: value, defaultValue: defaultValue, onChange: handleChange, ...rest, ref: internalRef }));
58
90
  }
59
- return (_jsx(TextAreaContainer, { className: "sc-textarea", rows: rows, cols: cols, variant: variant, ...rest, ref: ref }));
91
+ return (_jsx(TextAreaContainer, { className: "sc-textarea", rows: rows, cols: cols, variant: variant, autoGrow: autoGrow, value: value, defaultValue: defaultValue, onChange: handleChange, ...rest, ref: internalRef }));
60
92
  }
61
93
  export const TextArea = forwardRef(TextAreaElement);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scality/core-ui",
3
- "version": "0.182.0",
3
+ "version": "0.184.0",
4
4
  "description": "Scality common React component library",
5
5
  "author": "Scality Engineering",
6
6
  "license": "SEE LICENSE IN LICENSE",
@@ -14,14 +14,36 @@ export const useClipboard = () => {
14
14
  return () => clearTimeout(timer);
15
15
  }, [copyStatus]);
16
16
 
17
- const copyToClipboard = (text) => {
17
+ const copyToClipboard = (text: string, asHtml?: boolean) => {
18
18
  if (!navigator || !navigator.clipboard) {
19
19
  setCopyStatus(COPY_STATE_UNSUPPORTED);
20
20
  return;
21
21
  }
22
22
 
23
- navigator.clipboard.writeText(text);
24
- setCopyStatus(COPY_STATE_SUCCESS);
23
+ if (asHtml) {
24
+ // Copy as HTML with plain text fallback
25
+ const el = document.createElement('div');
26
+ el.innerHTML = text;
27
+ const plainText = el.innerText;
28
+
29
+ const clipboardItem = new ClipboardItem({
30
+ 'text/html': new Blob([text], { type: 'text/html' }),
31
+ 'text/plain': new Blob([plainText], { type: 'text/plain' }),
32
+ });
33
+
34
+ navigator.clipboard
35
+ .write([clipboardItem])
36
+ .then(() => {
37
+ setCopyStatus(COPY_STATE_SUCCESS);
38
+ })
39
+ .catch(() => {
40
+ setCopyStatus(COPY_STATE_UNSUPPORTED);
41
+ });
42
+ } else {
43
+ // Copy as plain text only
44
+ navigator.clipboard.writeText(text);
45
+ setCopyStatus(COPY_STATE_SUCCESS);
46
+ }
25
47
  };
26
48
 
27
49
  return {
@@ -33,11 +55,13 @@ export const useClipboard = () => {
33
55
  export const CopyButton = ({
34
56
  label,
35
57
  textToCopy,
58
+ copyAsHtml,
36
59
  variant,
37
60
  ...props
38
61
  }: {
39
62
  label?: string;
40
63
  textToCopy: string;
64
+ copyAsHtml?: boolean;
41
65
  variant?: 'outline' | 'ghost';
42
66
  } & Props) => {
43
67
  const { copy, copyStatus } = useClipboard();
@@ -68,7 +92,7 @@ export const CopyButton = ({
68
92
  />
69
93
  }
70
94
  disabled={copyStatus === COPY_STATE_SUCCESS || props.disabled}
71
- onClick={() => copy(textToCopy)}
95
+ onClick={() => copy(textToCopy, copyAsHtml)}
72
96
  type="button"
73
97
  tooltip={
74
98
  variant !== 'outline'
@@ -8,6 +8,7 @@ type Props = {
8
8
  title: string | React.ReactNode;
9
9
  content: React.ReactNode;
10
10
  link?: string;
11
+ linkText?: string;
11
12
  };
12
13
 
13
14
  const InfoMessageContainer = styled.div`
@@ -20,7 +21,7 @@ const InfoMessageContainer = styled.div`
20
21
  color: white;
21
22
  `;
22
23
 
23
- export const InfoMessage = ({ title, content, link }: Props) => {
24
+ export const InfoMessage = ({ title, content, link, linkText }: Props) => {
24
25
  const { containerRef, backgroundColor } = useComputeBackgroundColor();
25
26
  const theme = useTheme();
26
27
 
@@ -38,7 +39,7 @@ export const InfoMessage = ({ title, content, link }: Props) => {
38
39
  </Text>
39
40
  {link && (
40
41
  <Link href={link} target="_blank" style={{ alignSelf: 'flex-end' }}>
41
- More info <Icon name="External-link"></Icon>
42
+ {linkText || 'More info'} <Icon name="External-link"></Icon>
42
43
  </Link>
43
44
  )}
44
45
  </InfoMessageContainer>
@@ -1,8 +1,66 @@
1
+ import React from 'react';
1
2
  import '@testing-library/jest-dom';
2
- import { render } from '@testing-library/react';
3
+ import { render, screen } from '@testing-library/react';
3
4
  import { coreUIAvailableThemes } from '../../style/theme';
4
5
  import { CoreUiThemeProvider } from '../coreuithemeprovider/CoreUiThemeProvider';
5
6
  import { useComputeBackgroundColor } from './InfoMessageUtils';
7
+ import { InfoMessage } from './InfoMessage.component';
8
+ import { getWrapper } from '../../testUtils';
9
+
10
+ describe('InfoMessage', () => {
11
+ const selectors = {
12
+ title: () => screen.getByText('Title'),
13
+ content: () => screen.getByText('Content'),
14
+ defaultLinkText: () => screen.getByText('More info'),
15
+ link: () => screen.getByRole('link'),
16
+ linkText: () => screen.getByText('Link text'),
17
+ };
18
+ it('should render', () => {
19
+ const { Wrapper } = getWrapper();
20
+ render(<InfoMessage title="Title" content="Content" />, {
21
+ wrapper: Wrapper,
22
+ });
23
+ expect(selectors.title()).toBeInTheDocument();
24
+ expect(selectors.content()).toBeInTheDocument();
25
+ });
26
+ it('should render with link and default link text', () => {
27
+ const { Wrapper } = getWrapper();
28
+ render(
29
+ <InfoMessage
30
+ title="Title"
31
+ content="Content"
32
+ link="https://www.google.com"
33
+ />,
34
+ {
35
+ wrapper: Wrapper,
36
+ },
37
+ );
38
+ expect(selectors.title()).toBeInTheDocument();
39
+ expect(selectors.content()).toBeInTheDocument();
40
+ expect(selectors.link()).toBeInTheDocument();
41
+ expect(selectors.defaultLinkText()).toBeInTheDocument();
42
+ expect(selectors.link()).toHaveAttribute('href', 'https://www.google.com');
43
+ });
44
+ it('should render with correct link text', () => {
45
+ const { Wrapper } = getWrapper();
46
+ render(
47
+ <InfoMessage
48
+ title="Title"
49
+ content="Content"
50
+ link="https://www.google.com"
51
+ linkText="Link text"
52
+ />,
53
+ {
54
+ wrapper: Wrapper,
55
+ },
56
+ );
57
+ expect(selectors.title()).toBeInTheDocument();
58
+ expect(selectors.content()).toBeInTheDocument();
59
+ expect(selectors.link()).toBeInTheDocument();
60
+ expect(selectors.linkText()).toBeInTheDocument();
61
+ expect(selectors.link()).toHaveAttribute('href', 'https://www.google.com');
62
+ });
63
+ });
6
64
 
7
65
  describe('useComputeBackgroundColor', () => {
8
66
  const SUT = jest.fn();
@@ -3,6 +3,10 @@ import {
3
3
  forwardRef,
4
4
  TextareaHTMLAttributes,
5
5
  ForwardedRef,
6
+ useEffect,
7
+ useRef,
8
+ useImperativeHandle,
9
+ useCallback,
6
10
  } from 'react';
7
11
  import styled, { css } from 'styled-components';
8
12
  import { spacing } from '../../spacing';
@@ -12,6 +16,11 @@ type Props = TextareaHTMLAttributes<HTMLTextAreaElement> & {
12
16
  variant?: TextAreaVariant;
13
17
  width?: CSSProperties['width'];
14
18
  height?: CSSProperties['height'];
19
+ /**
20
+ * Automatically adjust height to fit content
21
+ * When enabled, the textarea will grow/shrink to show all content
22
+ */
23
+ autoGrow?: boolean;
15
24
  };
16
25
  type RefType = HTMLTextAreaElement | null;
17
26
 
@@ -19,10 +28,11 @@ const TextAreaContainer = styled.textarea<{
19
28
  variant: TextAreaVariant;
20
29
  width?: CSSProperties['width'];
21
30
  height?: CSSProperties['height'];
31
+ autoGrow?: boolean;
22
32
  }>`
23
33
  padding: ${spacing.r12} ${spacing.r8};
24
34
  border-radius: 4px;
25
- resize: vertical;
35
+ resize: ${(props) => (props.autoGrow ? 'none' : 'vertical')};
26
36
  font-family: ${(props) =>
27
37
  props.variant === 'code' ? 'Courier New' : 'Lato'};
28
38
  font-size: ${spacing.f14};
@@ -39,13 +49,20 @@ const TextAreaContainer = styled.textarea<{
39
49
  css`
40
50
  width: ${props.width};
41
51
  `}
42
-
52
+
43
53
  ${(props) =>
44
54
  props.height &&
45
55
  css`
46
56
  height: ${props.height};
47
57
  `}
48
58
 
59
+ ${(props) =>
60
+ props.autoGrow &&
61
+ css`
62
+ overflow: hidden;
63
+ box-sizing: border-box;
64
+ `}
65
+
49
66
  &:placeholder-shown {
50
67
  font-style: italic;
51
68
  }
@@ -77,9 +94,55 @@ const TextAreaContainer = styled.textarea<{
77
94
  `;
78
95
 
79
96
  function TextAreaElement(
80
- { rows = 3, cols = 20, width, height, variant = 'code', ...rest }: Props,
97
+ {
98
+ rows = 3,
99
+ cols = 20,
100
+ width,
101
+ height,
102
+ variant = 'code',
103
+ autoGrow = false,
104
+ value,
105
+ defaultValue,
106
+ onChange,
107
+ ...rest
108
+ }: Props,
81
109
  ref: ForwardedRef<RefType>,
82
110
  ) {
111
+ const internalRef = useRef<HTMLTextAreaElement>(null);
112
+
113
+ // Expose the textarea element to parent components via forwarded ref
114
+ useImperativeHandle(ref, () => internalRef.current as HTMLTextAreaElement);
115
+
116
+ // Adjust height on mount and when value changes (for controlled components)
117
+ const adjustHeight = useCallback(() => {
118
+ const textarea = internalRef.current;
119
+ if (!textarea || !autoGrow) return;
120
+
121
+ // Reset height to auto to get the correct scrollHeight
122
+ textarea.style.height = '0px';
123
+
124
+ // Set the height to match the content
125
+ const newHeight = textarea.scrollHeight;
126
+ textarea.style.height = `${newHeight}px`;
127
+ }, [autoGrow]);
128
+
129
+ useEffect(() => {
130
+ adjustHeight();
131
+ }, [adjustHeight, value]);
132
+
133
+ // Handle onChange to support both controlled and uncontrolled components
134
+ const handleChange = useCallback(
135
+ (event: React.ChangeEvent<HTMLTextAreaElement>) => {
136
+ if (autoGrow) {
137
+ adjustHeight();
138
+ }
139
+ if (onChange) {
140
+ onChange(event);
141
+ }
142
+ },
143
+ [autoGrow, adjustHeight, onChange],
144
+ );
145
+
83
146
  if (width || height) {
84
147
  return (
85
148
  <TextAreaContainer
@@ -87,8 +150,12 @@ function TextAreaElement(
87
150
  width={width}
88
151
  height={height}
89
152
  variant={variant}
153
+ autoGrow={autoGrow}
154
+ value={value}
155
+ defaultValue={defaultValue}
156
+ onChange={handleChange}
90
157
  {...rest}
91
- ref={ref}
158
+ ref={internalRef}
92
159
  />
93
160
  );
94
161
  }
@@ -99,8 +166,12 @@ function TextAreaElement(
99
166
  rows={rows}
100
167
  cols={cols}
101
168
  variant={variant}
169
+ autoGrow={autoGrow}
170
+ value={value}
171
+ defaultValue={defaultValue}
172
+ onChange={handleChange}
102
173
  {...rest}
103
- ref={ref}
174
+ ref={internalRef}
104
175
  />
105
176
  );
106
177
  }
@@ -32,14 +32,14 @@ export const Playground = {};
32
32
 
33
33
  export const DefaultTextArea = {
34
34
  args: {
35
- value: 'Some text',
35
+ defaultValue: 'Some text',
36
36
  },
37
37
  };
38
38
 
39
39
  export const TextVariantTextArea = {
40
40
  args: {
41
41
  variant: 'text',
42
- value: 'Text area with "text" variant',
42
+ defaultValue: 'Text area with "text" variant',
43
43
  },
44
44
  };
45
45
 
@@ -67,3 +67,65 @@ export const RowsAndColsSet = {
67
67
  placeholder: 'With rows = 20 and cols = 40',
68
68
  },
69
69
  };
70
+
71
+ export const AutoGrowShortText = {
72
+ args: {
73
+ autoGrow: true,
74
+ defaultValue: 'Hello World!',
75
+ width: '400px',
76
+ },
77
+ };
78
+
79
+ /**
80
+ * Auto-growing textarea adjusts its height based on content
81
+ * Perfect for displaying commands or long text where you want the entire content visible
82
+ * Simply set autoGrow={true} and the textarea will grow to show all content
83
+ */
84
+ export const AutoGrowTextArea = {
85
+ args: {
86
+ autoGrow: true,
87
+ placeholder:
88
+ 'Type or paste content here...\nThe textarea will automatically grow to fit all the content.',
89
+ defaultValue: `docker run -d \\
90
+ --name my-container \\
91
+ -p 8080:80 \\
92
+ -v /host/path:/container/path \\
93
+ -e ENV_VAR=value \\
94
+ my-image:latest`,
95
+ width: '500px',
96
+ },
97
+ };
98
+
99
+ /**
100
+ * Auto-growing textarea with long command example
101
+ * The entire command is visible without scrolling
102
+ */
103
+ export const AutoGrowWithLongCommand = {
104
+ args: {
105
+ autoGrow: true,
106
+ variant: 'code',
107
+ defaultValue: `kubectl apply -f - <<EOF
108
+ apiVersion: v1
109
+ kind: Pod
110
+ metadata:
111
+ name: my-pod
112
+ labels:
113
+ app: myapp
114
+ spec:
115
+ containers:
116
+ - name: nginx
117
+ image: nginx:1.14.2
118
+ ports:
119
+ - containerPort: 80
120
+ env:
121
+ - name: DATABASE_URL
122
+ value: "postgresql://user:password@localhost:5432/db"
123
+ - name: API_KEY
124
+ valueFrom:
125
+ secretKeyRef:
126
+ name: api-secret
127
+ key: api-key
128
+ EOF`,
129
+ width: '600px',
130
+ },
131
+ };