Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ui): Playground: make editor component resizeable #3217

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ export const MessagePanel = ({
}
}, [message.content, contentRef?.current?.scrollHeight]);

// Set isShowingMore to true when editor is opened
useEffect(() => {
if (editorHeight !== null) {
setIsShowingMore(true);
}
}, [editorHeight]);

const isUser = message.role === 'user';
const isSystemPrompt = message.role === 'system';
const isTool = message.role === 'tool';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,12 @@ export const PlaygroundMessagePanelEditor: React.FC<
<div
className={classNames(
'w-full pt-[6px]',
isNested ? 'px-[4px]' : 'px-[8px]'
isNested ? 'px-[4px]' : 'px-[16px]'
)}>
<StyledTextArea
value={editedContent}
onChange={e => setEditedContent(e.target.value)}
autoGrow
maxHeight={160}
startHeight={320}
/>
{/* 6px vs. 8px to make up for extra padding from textarea field */}
<div className="z-100 mt-[6px] flex justify-end gap-[8px]">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import React, {forwardRef} from 'react';
type TextAreaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement> & {
autoGrow?: boolean;
maxHeight?: string | number;
startHeight?: string | number;
reset?: boolean;
};

export const StyledTextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
({className, autoGrow, maxHeight, reset, ...props}, ref) => {
({className, autoGrow, maxHeight, startHeight, reset, ...props}, ref) => {
const textareaRef = React.useRef<HTMLTextAreaElement>(null);

React.useEffect(() => {
Expand All @@ -26,11 +27,22 @@ export const StyledTextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
return;
}

// Disable resize when autoGrow is true
textareaElement.style.resize = 'none';
// Only disable resize when autoGrow is true
textareaElement.style.resize = autoGrow ? 'none' : 'vertical';

// Set initial height if provided
if (startHeight && textareaElement.value === '') {
textareaElement.style.height =
typeof startHeight === 'number' ? `${startHeight}px` : startHeight;
return;
}

if (reset || textareaElement.value === '') {
textareaElement.style.height = 'auto';
textareaElement.style.height = startHeight
? typeof startHeight === 'number'
? `${startHeight}px`
: startHeight
: 'auto';
return;
}

Expand Down Expand Up @@ -63,7 +75,7 @@ export const StyledTextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(

return () =>
textareaRefElement.removeEventListener('input', adjustHeight);
}, [autoGrow, maxHeight, reset]);
}, [autoGrow, maxHeight, reset, startHeight]);

return (
<Tailwind style={{display: 'contents'}}>
Expand All @@ -86,6 +98,7 @@ export const StyledTextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
'focus:outline-none',
'relative bottom-0 top-0 items-center rounded-sm',
'outline outline-1 outline-moon-250',
!autoGrow && 'resize-y',
props.disabled
? 'opacity-50'
: 'hover:outline hover:outline-2 hover:outline-teal-500/40 focus:outline-2',
Expand All @@ -94,6 +107,14 @@ export const StyledTextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
'placeholder-moon-500 dark:placeholder-moon-600',
className
)}
style={{
height: startHeight
? typeof startHeight === 'number'
? `${startHeight}px`
: startHeight
: undefined,
...props.style,
}}
{...props}
/>
</Tailwind>
Expand Down
Loading