From 22c5324f0c1681a3246bf5cfe701e5b77a4dcd76 Mon Sep 17 00:00:00 2001 From: Ivan Vasilov Date: Fri, 12 Jul 2024 11:32:20 +0200 Subject: [PATCH] feat: Use expanding text area in the Ai Assistant (SQL Editor) (#27930) * Add a new textarea component which autoexpands depending on number of content rows. * Use the new ExpandingTextArea in the AIAssistantPanel. * Remove old unused text area. * Move around some of the props in the ExpandingTextArea. * Make the value prop mandatory. * Fix minor stuff in the Design System app. * Add examples for Expanding Text Area. * add max height to the expanding text area. * Add todo comment in database-new app. --- apps/design-system/__registry__/index.tsx | 11 ++++ apps/design-system/app/layout.tsx | 2 +- apps/design-system/config/docs.ts | 5 ++ .../docs/components/expanding-textarea.mdx | 25 +++++++++ apps/design-system/next.config.mjs | 6 ++- .../example/expanding-textarea-demo.tsx | 16 ++++++ apps/design-system/registry/examples.ts | 5 ++ .../SQLEditor/AiAssistantPanel/index.tsx | 20 ++++--- .../AssistantChat/AssistantChatForm.tsx | 1 + .../AssistantChat/ExpandingTextArea.tsx | 53 +++++++++++++++++++ packages/ui/index.tsx | 5 +- .../components/ExpandingTextArea/index.tsx | 46 ++++++++++++++++ .../ui/src/components/Textarea/Textarea.css | 11 ---- .../ui/src/components/Textarea/Textarea.js | 41 -------------- .../src/components/Textarea/Textarea.spec.js | 15 ------ .../components/Textarea/Textarea.stories.js | 14 ----- packages/ui/src/components/Textarea/index.js | 1 - 17 files changed, 182 insertions(+), 95 deletions(-) create mode 100644 apps/design-system/content/docs/components/expanding-textarea.mdx create mode 100644 apps/design-system/registry/default/example/expanding-textarea-demo.tsx create mode 100644 packages/ui-patterns/AssistantChat/ExpandingTextArea.tsx create mode 100644 packages/ui/src/components/ExpandingTextArea/index.tsx delete mode 100644 packages/ui/src/components/Textarea/Textarea.css delete mode 100644 packages/ui/src/components/Textarea/Textarea.js delete mode 100644 packages/ui/src/components/Textarea/Textarea.spec.js delete mode 100644 packages/ui/src/components/Textarea/Textarea.stories.js delete mode 100644 packages/ui/src/components/Textarea/index.js diff --git a/apps/design-system/__registry__/index.tsx b/apps/design-system/__registry__/index.tsx index e17eb4f9c02..7e6779fef1b 100644 --- a/apps/design-system/__registry__/index.tsx +++ b/apps/design-system/__registry__/index.tsx @@ -1864,5 +1864,16 @@ export const Index: Record = { subcategory: "undefined", chunks: [] }, + "expanding-textarea-demo": { + name: "expanding-textarea-demo", + type: "docs:example", + registryDependencies: undefined, + component: React.lazy(() => import("@/registry/default/example/expanding-textarea-demo")), + source: "", + files: ["registry/default/example/expanding-textarea-demo.tsx"], + category: "undefined", + subcategory: "undefined", + chunks: [] + }, }, } diff --git a/apps/design-system/app/layout.tsx b/apps/design-system/app/layout.tsx index 7e8ff99a5a9..b0e13e68153 100644 --- a/apps/design-system/app/layout.tsx +++ b/apps/design-system/app/layout.tsx @@ -6,7 +6,7 @@ import { ThemeProvider } from './Providers' const inter = Inter({ subsets: ['latin'] }) export const metadata: Metadata = { - title: 'Create Next App', + title: 'Supabase Design System', description: 'Generated by create next app', } diff --git a/apps/design-system/config/docs.ts b/apps/design-system/config/docs.ts index 4e1a7be559d..caaf8c695f5 100644 --- a/apps/design-system/config/docs.ts +++ b/apps/design-system/config/docs.ts @@ -374,6 +374,11 @@ export const docsConfig: DocsConfig = { href: '/docs/components/tree-view', items: [], }, + { + title: 'Expanding Textarea', + href: '/docs/components/expanding-textarea', + items: [], + }, ], }, ], diff --git a/apps/design-system/content/docs/components/expanding-textarea.mdx b/apps/design-system/content/docs/components/expanding-textarea.mdx new file mode 100644 index 00000000000..dded1c63207 --- /dev/null +++ b/apps/design-system/content/docs/components/expanding-textarea.mdx @@ -0,0 +1,25 @@ +--- +title: Expanding Textarea +description: Displays a textarea which autoexpands (or shrinks) based on its content. +component: true +source: + shadcn: true +--- + + + +## Usage + +```tsx +import { ExpandingTextArea } from 'ui' +``` + +```tsx + +``` + +## Examples + +### Default + + diff --git a/apps/design-system/next.config.mjs b/apps/design-system/next.config.mjs index 3c67b4ac18e..2280b564d29 100644 --- a/apps/design-system/next.config.mjs +++ b/apps/design-system/next.config.mjs @@ -1,5 +1,7 @@ import { withContentlayer } from 'next-contentlayer2' +const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH || '/design-system' + /** @type {import('next').NextConfig} */ const nextConfig = { transpilePackages: ['ui', 'common', 'shared-data', 'icons', 'tsconfig'], @@ -22,11 +24,11 @@ const nextConfig = { }, async redirects() { return [ - ...(process.env.NEXT_PUBLIC_BASE_PATH?.length + ...(BASE_PATH.length ? [ { source: '/', - destination: process.env.NEXT_PUBLIC_BASE_PATH, + destination: BASE_PATH, basePath: false, permanent: false, }, diff --git a/apps/design-system/registry/default/example/expanding-textarea-demo.tsx b/apps/design-system/registry/default/example/expanding-textarea-demo.tsx new file mode 100644 index 00000000000..464d1db42ab --- /dev/null +++ b/apps/design-system/registry/default/example/expanding-textarea-demo.tsx @@ -0,0 +1,16 @@ +import { useState } from 'react' +import { ExpandingTextArea } from 'ui' + +export default function ExpandingTextareaDemo() { + const [value, setValue] = useState('') + + return ( + { + setValue(event.target.value) + }} + /> + ) +} diff --git a/apps/design-system/registry/examples.ts b/apps/design-system/registry/examples.ts index 26a7386efbe..f8e00260d70 100644 --- a/apps/design-system/registry/examples.ts +++ b/apps/design-system/registry/examples.ts @@ -1065,4 +1065,9 @@ export const examples: Registry = [ type: 'docs:example', files: ['example/color-usage-surface-studio-frame.tsx'], }, + { + name: 'expanding-textarea-demo', + type: 'docs:example', + files: ['example/expanding-textarea-demo.tsx'], + }, ] diff --git a/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx b/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx index 8f9c72a5bf8..e6e19e3fa80 100644 --- a/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx +++ b/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx @@ -15,12 +15,12 @@ import { IS_PLATFORM, LOCAL_STORAGE_KEYS, OPT_IN_TAGS } from 'lib/constants' import { useProfile } from 'lib/profile' import { useAppStateSnapshot } from 'state/app-state' import { + ExpandingTextArea, Button, FormControl_Shadcn_, FormField_Shadcn_, FormItem_Shadcn_, Form_Shadcn_, - Input_Shadcn_, cn, } from 'ui' import { AiIcon } from 'ui-patterns/Cmdk' @@ -86,6 +86,8 @@ export const AiAssistantPanel = ({ } }, [loading]) + const formRef = useRef(null) + return (
) => { onSubmit(data.chat) @@ -168,18 +171,23 @@ export const AiAssistantPanel = ({ control={form.control} name="chat" render={({ field }) => ( - +
- { + if (event.key === 'Enter' && !event.shiftKey) { + event.preventDefault() + formRef?.current?.requestSubmit() + } + }} placeholder="Ask a question about your SQL query" /> {loading && } diff --git a/packages/ui-patterns/AssistantChat/AssistantChatForm.tsx b/packages/ui-patterns/AssistantChat/AssistantChatForm.tsx index dedae876eb7..4f836f6c702 100644 --- a/packages/ui-patterns/AssistantChat/AssistantChatForm.tsx +++ b/packages/ui-patterns/AssistantChat/AssistantChatForm.tsx @@ -95,6 +95,7 @@ const AssistantChatForm = React.forwardRef( {icon}
)} + {/* TODO: Replace this with ExpandingTextArea */}