Skip to content

Commit

Permalink
Updated Playground editor component (#3217)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-rgba authored Dec 12, 2024
1 parent a61a50e commit 99d07b4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
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

0 comments on commit 99d07b4

Please sign in to comment.