Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions src/lib/components/textarea/TextArea.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
forwardRef,
TextareaHTMLAttributes,
ForwardedRef,
useEffect,
useRef,
useImperativeHandle,
useCallback,
} from 'react';
import styled, { css } from 'styled-components';
import { spacing } from '../../spacing';
Expand All @@ -12,13 +16,20 @@ type Props = TextareaHTMLAttributes<HTMLTextAreaElement> & {
variant?: TextAreaVariant;
width?: CSSProperties['width'];
height?: CSSProperties['height'];
/**
* Automatically adjust height to fit content
* When enabled, the textarea will grow/shrink to show all content
* It disables the resize property
*/
autoGrow?: boolean;
};
type RefType = HTMLTextAreaElement | null;

const TextAreaContainer = styled.textarea<{
variant: TextAreaVariant;
width?: CSSProperties['width'];
height?: CSSProperties['height'];
autoGrow?: boolean;
}>`
padding: ${spacing.r12} ${spacing.r8};
border-radius: 4px;
Expand Down Expand Up @@ -46,6 +57,13 @@ const TextAreaContainer = styled.textarea<{
height: ${props.height};
`}

${(props) =>
props.autoGrow &&
css`
resize: none;
overflow: hidden;
`}

&:placeholder-shown {
font-style: italic;
}
Expand Down Expand Up @@ -77,18 +95,55 @@ const TextAreaContainer = styled.textarea<{
`;

function TextAreaElement(
{ rows = 3, cols = 20, width, height, variant = 'code', ...rest }: Props,
{
rows = 3,
cols = 20,
width,
height,
variant = 'code',
autoGrow = false,
value,
defaultValue,
onChange,
...rest
}: Props,
ref: ForwardedRef<RefType>,
) {
const internalRef = useRef<HTMLTextAreaElement>(null);

// Expose the textarea element to parent components via forwarded ref
useImperativeHandle(ref, () => internalRef.current as HTMLTextAreaElement);

const adjustHeight = useCallback(() => {
const textarea = internalRef.current;
if (!textarea || !autoGrow) return;

// Reset height to auto to get the correct scrollHeight
textarea.style.height = 'auto';

// Set the height to match the content
const newHeight = textarea.scrollHeight;
textarea.style.height = `${newHeight}px`;
}, [autoGrow]);

// Adjust height on mount to fit initial content
useEffect(() => {
adjustHeight();
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that this autoGrow implementation is not reacting to any value change.
It should be able to work when the textearea is acting as a controlled component or not.


if (width || height) {
return (
<TextAreaContainer
className="sc-textarea"
width={width}
height={height}
variant={variant}
autoGrow={autoGrow}
value={value}
defaultValue={defaultValue}
onChange={onChange}
{...rest}
ref={ref}
ref={internalRef}
/>
);
}
Expand All @@ -99,8 +154,12 @@ function TextAreaElement(
rows={rows}
cols={cols}
variant={variant}
autoGrow={autoGrow}
value={value}
defaultValue={defaultValue}
onChange={onChange}
{...rest}
ref={ref}
ref={internalRef}
/>
);
}
Expand Down
56 changes: 56 additions & 0 deletions stories/textarea.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,59 @@ export const RowsAndColsSet = {
placeholder: 'With rows = 20 and cols = 40',
},
};

/**
* Auto-growing textarea adjusts its height based on content
* Perfect for displaying commands or long text where you want the entire content visible
* Simply set autoGrow={true} and the textarea will grow to show all content
*/
export const AutoGrowTextArea = {
args: {
autoGrow: true,
placeholder:
'Type or paste content here...\nThe textarea will automatically grow to fit all the content.',
value: `docker run -d \\
--name my-container \\
-p 8080:80 \\
-v /host/path:/container/path \\
-e ENV_VAR=value \\
my-image:latest`,
width: '500px',
readOnly: true,
},
};

/**
* Auto-growing textarea with long command example
* The entire command is visible without scrolling
*/
export const AutoGrowWithLongCommand = {
args: {
autoGrow: true,
variant: 'code',
value: `kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
app: myapp
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
env:
- name: DATABASE_URL
value: "postgresql://user:password@localhost:5432/db"
- name: API_KEY
valueFrom:
secretKeyRef:
name: api-secret
key: api-key
EOF`,
width: '600px',
readOnly: true,
},
};
Loading