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/use expandable textarea #58

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
51 changes: 51 additions & 0 deletions apps/postgres-new/components/ExpandableTextArea.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use client'

import React, { forwardRef, useEffect, useImperativeHandle, useRef } from 'react'
import { cn } from '~/lib/utils'
import { TextArea } from './ui/textarea'

export interface ExpandingTextAreaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
/* The value of the textarea. Required to calculate the height of the textarea. */
value: string
}

/**
* This is a custom TextArea component that expands based on the content.
*/
const ExpandingTextArea = forwardRef<HTMLTextAreaElement, ExpandingTextAreaProps>(
({ className, value, ...props }, ref) => {
const textAreaRef = useRef<HTMLTextAreaElement>(null)

// Expose the ref to the parent component
useImperativeHandle(ref, () => textAreaRef.current!)
saltcod marked this conversation as resolved.
Show resolved Hide resolved
/**
* This effect is used to resize the textarea based on the content
*/
useEffect(() => {
if (textAreaRef) {
if (textAreaRef.current && !value) {
textAreaRef.current.style.height = '40px'
saltcod marked this conversation as resolved.
Show resolved Hide resolved
} else if (textAreaRef && textAreaRef.current) {
textAreaRef.current.style.height = 'auto'
const newHeight = textAreaRef.current.scrollHeight + 'px'
textAreaRef.current.style.height = newHeight
}
}
}, [value, textAreaRef])

return (
<TextArea
ref={textAreaRef}
rows={1}
aria-expanded={false}
className={cn('transition-all resize-none leading-6 box-border', className)}
value={value}
{...props}
/>
)
}
)

ExpandingTextArea.displayName = 'ExpandingTextArea'

export { ExpandingTextArea }
31 changes: 28 additions & 3 deletions apps/postgres-new/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useApp } from './app-provider'
import ChatMessage from './chat-message'
import SignInButton from './sign-in-button'
import { useWorkspace } from './workspace'
import { ExpandingTextArea } from './ExpandableTextArea'

export function getInitialMessages(tables: TablesData): Message[] {
return [
Expand Down Expand Up @@ -457,12 +458,36 @@ export default function Chat() {
>
<Paperclip size={16} strokeWidth={1.3} />
</Button>
<textarea

<ExpandingTextArea
ref={inputRef}
autoFocus
autoComplete="off"
disabled={!user}
className="bg-muted/50 border-none"
placeholder="Message AI or write SQL"
spellCheck={false}
value={input}
onChange={handleInputChange}
onKeyDown={(e) => {
if (!(e.target instanceof HTMLTextAreaElement)) {
return
}

if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault()
if (!isLoading && isSubmitEnabled) {
handleFormSubmit(e)
}
}
}}
/>
{/* <textarea
saltcod marked this conversation as resolved.
Show resolved Hide resolved
ref={inputRef}
id="input"
name="prompt"
autoComplete="off"
className="flex-grow border-none focus-visible:ring-0 text-base placeholder:text-muted-foreground/50 bg-transparent resize-none outline-none"
className="flex-grow border-none focus-visible:ring-0 text-base placeholder:text-muted-foreground/50 bg-transparent outline-none"
value={input}
onChange={handleInputChange}
placeholder="Message AI or write SQL"
Expand All @@ -487,7 +512,7 @@ export default function Chat() {
}
}
}}
/>
/> */}
{isLoading ? (
<Button
className="rounded-full w-8 h-8 p-1.5 my-1 text-neutral-50 bg-neutral-800"
Expand Down
27 changes: 27 additions & 0 deletions apps/postgres-new/components/ui/textarea.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as React from 'react'
import { cn } from '~/lib/utils'

export interface TextAreaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {}

const customClasses = ['bg-control']

const TextArea = React.forwardRef<HTMLTextAreaElement, TextAreaProps>(
({ className, ...props }, ref) => {
return (
<textarea
className={cn(
'flex min-h-10 w-full rounded-md border border-control bg-control px-3 py-2 text-sm file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-foreground-muted',
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-background-control focus-visible:ring-offset-2 focus-visible:ring-offset-foreground-muted disabled:cursor-not-allowed disabled:opacity-50',
...customClasses,
className
)}
ref={ref}
{...props}
/>
)
}
)

TextArea.displayName = 'TextArea'

export { TextArea }