The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
[Design System] Feat: Assistant Chat fragment component (#27952)
Jonathan Summers-Muir58779edaa657
diff --git a/apps/design-system/__registry__/index.tsx b/apps/design-system/__registry__/index.tsxindex 7e6779fef1b..ac171675bb0 100644--- a/apps/design-system/__registry__/index.tsx+++ b/apps/design-system/__registry__/index.tsx@@ -1831,6 +1831,28 @@ export const Index: Record<string, any> = {subcategory: "undefined",chunks: []},+ "assistant-chat-demo": {+ name: "assistant-chat-demo",+ type: "components:example",+ registryDependencies: undefined,+ component: React.lazy(() => import("@/registry/default/example/assistant-chat-demo")),+ source: "",+ files: ["registry/default/example/assistant-chat-demo.tsx"],+ category: "undefined",+ subcategory: "undefined",+ chunks: []+ },+ "assistant-chat-commands": {+ name: "assistant-chat-commands",+ type: "components:example",+ registryDependencies: undefined,+ component: React.lazy(() => import("@/registry/default/example/assistant-chat-commands")),+ source: "",+ files: ["registry/default/example/assistant-chat-commands.tsx"],+ category: "undefined",+ subcategory: "undefined",+ chunks: []+ },"color-usage-surface-www-and-docs": {name: "color-usage-surface-www-and-docs",type: "docs:example",diff --git a/apps/design-system/config/docs.ts b/apps/design-system/config/docs.tsindex caaf8c695f5..6a3cad91db9 100644--- a/apps/design-system/config/docs.ts+++ b/apps/design-system/config/docs.ts@@ -82,6 +82,11 @@ export const docsConfig: DocsConfig = {href: '/docs/fragments/admonition',items: [],},+ {+ title: 'Assistant Chat',+ href: '/docs/fragments/assistant-chat',+ items: [],+ },{title: 'Modal',href: '/docs/fragments/modal',diff --git a/apps/design-system/content/docs/fragments/assistant-chat.mdx b/apps/design-system/content/docs/fragments/assistant-chat.mdxnew file mode 100644index 00000000000..215ff3b60c8--- /dev/null+++ b/apps/design-system/content/docs/fragments/assistant-chat.mdx@@ -0,0 +1,52 @@+---+title: Assistant Chat+description: Bespoke Assistant chat text area, with support for a commands Popover.+component: true+---++<ComponentPreview name="assistant-chat-demo" peekCode wide />++## Usage++```tsx+import { AssistantChatForm } from 'ui-patterns/AssistantChat/AssistantChatForm'+```++```tsx+<AssistantChatForm+ icon={<Box strokeWidth={1.5} size={24} className="text-foreground-muted" />}+ value={value}+ loading={loading}+ disabled={loading}+ onValueChange={(e) => setValueState(e.target.value)}+ onSubmit={async (event) => {+ event.preventDefault()+ handleSubmit(event)+ }}+/>+```++## Commands++The Assistant Chat support a commands Popover that can be used to display a list of available commands.++It requires a useState and a ref though for it to work correctly.+This is so that the input is still in focus, and the user can still interact with the Popover with the keyboard.++```tsx+const [commandsOpen, setCommandsOpen] = useState<boolean>(false)+const textAreaRef = createRef<HTMLTextAreaElement>()++<AssistantCommandPopover+ open={commandsOpen}+ setOpen={setCommandsOpen}+ textAreaRef={textAreaRef}+>+ <AssistantChatForm+ commandsOpen={commandsOpen}+ setCommandsOpen={setCommandsOpen}+ />+</AssistantCommandPopover>+```++<ComponentPreview name="assistant-chat-commands" />diff --git a/apps/design-system/registry/default/example/assistant-chat-commands.tsx b/apps/design-system/registry/default/example/assistant-chat-commands.tsxnew file mode 100644index 00000000000..d09a27fdf74--- /dev/null+++ b/apps/design-system/registry/default/example/assistant-chat-commands.tsx@@ -0,0 +1,55 @@+import React, { useRef, useState } from 'react'+import { AssistantChatForm } from 'ui-patterns/AssistantChat/AssistantChatForm'+import { AssistantCommandsPopover } from 'ui-patterns/AssistantChat/AssistantCommandsPopover'++export default function AssistantChatCommands() {+ const [commandsOpen, setCommandsOpen] = useState<boolean>(false)+ const textAreaRef = useRef<HTMLTextAreaElement>(null)+ const [value, setValueState] = useState('')+ const [loading, setLoading] = useState(false)++ function handleSubmit(event: React.FormEvent) {+ // set loading for 3 seconds and then reset+ setLoading(true)+ setTimeout(() => {+ setLoading(false)+ }, 1500)+ }++ const suggestions = [+ 'Add policy for org Inserted User Access',+ 'Add policy for User-Specific Todo Access',+ 'Add policy for Org Update Restriction',+ ]++ return (+ <div className="flex flex-col gap-3">+ <AssistantCommandsPopover+ open={commandsOpen}+ setOpen={setCommandsOpen}+ textAreaRef={textAreaRef}+ value={value}+ setValue={(e) => setValueState(e)}+ suggestions={suggestions}+ >+ <AssistantChatForm+ textAreaRef={textAreaRef}+ value={value}+ loading={loading}+ disabled={loading}+ onValueChange={(e) => setValueState(e.target.value)}+ commandsOpen={commandsOpen}+ setCommandsOpen={setCommandsOpen}+ onSubmit={async (event) => {+ event.preventDefault()+ handleSubmit(event)+ }}+ />+ </AssistantCommandsPopover>+ <p className="text-xs mt-3 text-foreground-lighter">+ Press <span className="bg-surface-300 px-[3px] py-[2px] border rounded">/</span> to open+ commands+ </p>+ </div>+ )+}diff --git a/apps/design-system/registry/default/example/assistant-chat-demo.tsx b/apps/design-system/registry/default/example/assistant-chat-demo.tsxnew file mode 100644index 00000000000..54ad73a33f2--- /dev/null+++ b/apps/design-system/registry/default/example/assistant-chat-demo.tsx@@ -0,0 +1,30 @@+import { Box } from 'lucide-react'+import { useState } from 'react'+import { AssistantChatForm } from 'ui-patterns/AssistantChat/AssistantChatForm'++export default function AssistantChatDemo() {+ const [value, setValueState] = useState('')+ const [loading, setLoading] = useState(false)++ function handleSubmit(event: React.FormEvent) {+ // set loading for 3 seconds and then reset+ setLoading(true)+ setTimeout(() => {+ setLoading(false)+ }, 1500)+ }++ return (+ <AssistantChatForm+ icon={<Box strokeWidth={1.5} size={24} className="text-foreground-muted" />}+ value={value}+ loading={loading}+ disabled={loading}+ onValueChange={(e) => setValueState(e.target.value)}+ onSubmit={async (event) => {+ event.preventDefault()+ handleSubmit(event)+ }}+ />+ )+}diff --git a/apps/design-system/registry/examples.ts b/apps/design-system/registry/examples.tsindex f8e00260d70..035003469da 100644--- a/apps/design-system/registry/examples.ts+++ b/apps/design-system/registry/examples.ts@@ -1050,6 +1050,16 @@ export const examples: Registry = [type: 'components:example',files: ['example/modal-hide-footer.tsx'],},+ {+ name: 'assistant-chat-demo',+ type: 'components:example',+ files: ['example/assistant-chat-demo.tsx'],+ },+ {+ name: 'assistant-chat-commands',+ type: 'components:example',+ files: ['example/assistant-chat-commands.tsx'],+ },{name: 'color-usage-surface-www-and-docs',type: 'docs:example',diff --git a/packages/ui-patterns/AssistantChat/AssistantChatForm.tsx b/packages/ui-patterns/AssistantChat/AssistantChatForm.tsxindex 4f836f6c702..86a9b43d558 100644--- a/packages/ui-patterns/AssistantChat/AssistantChatForm.tsx+++ b/packages/ui-patterns/AssistantChat/AssistantChatForm.tsx@@ -1,13 +1,13 @@'use client'import { Loader2 } from 'lucide-react'-import React, { ChangeEvent, createRef, useEffect } from 'react'-import { TextArea } from 'ui/src/components/shadcn/ui/text-area'+import React, { ChangeEvent, useRef } from 'react'+import { ExpandingTextArea } from 'ui'import { cn } from 'ui/src/lib/utils'export interface FormProps extends React.FormHTMLAttributes<HTMLFormElement> {- /* The ref for the textarea */- textAreaRef: React.RefObject<HTMLTextAreaElement>+ /* The ref for the textarea, optional. Exposed for the CommandsPopover to attach events. */+ textAreaRef?: React.RefObject<HTMLTextAreaElement>/* The loading state of the form */loading: boolean/* The disabled state of the form */@@ -45,29 +45,8 @@ const AssistantChatForm = React.forwardRef<HTMLFormElement, FormProps>(},ref) => {- const formRef = createRef<HTMLFormElement>()- const submitRef = createRef<HTMLButtonElement>()-- /**- * This effect is used to resize the textarea based on the content- */- useEffect(() => {- if (textAreaRef) {- if (!value && textAreaRef && textAreaRef.current) {- textAreaRef.current.style.height = '40px'- } else if (textAreaRef && textAreaRef.current) {- const newHeight = textAreaRef.current.scrollHeight + 'px'- textAreaRef.current.style.height = newHeight- }- }- }, [value, textAreaRef])-- /**- * This effect is used to focus the textarea when the component mounts- */- useEffect(() => {- textAreaRef?.current?.focus()- }, [value, textAreaRef])+ const formRef = useRef<HTMLFormElement>(null)+ const submitRef = useRef<HTMLButtonElement>(null)/*** This function is used to handle the "Enter" key press@@ -95,22 +74,16 @@ const AssistantChatForm = React.forwardRef<HTMLFormElement, FormProps>({icon}</div>)}- {/* TODO: Replace this with ExpandingTextArea */}- <TextArea+ <ExpandingTextArearef={textAreaRef}autoFocus- rows={1}disabled={disabled}contentEditable- aria-expanded={false}- className={cn(- icon ? 'pl-12' : '',- 'transition-all text-sm pr-10 rounded-[18px] resize-none box-border leading-6'- )}+ className={cn(icon ? 'pl-12' : '', 'text-sm pr-10 rounded-[18px]')}placeholder={placeholder}spellCheck={false}value={value}- onChange={(event: ChangeEvent<HTMLTextAreaElement>) => onValueChange(event)}+ onChange={(event) => onValueChange(event)}onKeyDown={handleKeyDown}/><div className="absolute right-1.5 top-1.5 flex gap-3 items-center">diff --git a/packages/ui-patterns/AssistantChat/ExpandingTextArea.tsx b/packages/ui-patterns/AssistantChat/ExpandingTextArea.tsxdeleted file mode 100644index 226a3343079..00000000000--- a/packages/ui-patterns/AssistantChat/ExpandingTextArea.tsx+++ /dev/null@@ -1,53 +0,0 @@-'use client'--import React, { createRef, useEffect } from 'react'-import { TextArea } from 'ui/src/components/shadcn/ui/text-area'-import { cn } from 'ui/src/lib/utils'--export interface ExpandingTextAreaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {}--const ExpandingTextArea = ({- className,- disabled = false,- value,- onSubmit,- placeholder,- ...props-}: ExpandingTextAreaProps) => {- const textAreaRef = createRef<HTMLTextAreaElement>()-- /**- * This effect is used to resize the textarea based on the content- */- useEffect(() => {- if (textAreaRef) {- if (!value && textAreaRef && textAreaRef.current) {- textAreaRef.current.style.height = '40px'- } else if (textAreaRef && textAreaRef.current) {- const newHeight = textAreaRef.current.scrollHeight + 'px'- textAreaRef.current.style.height = newHeight- }- }- }, [value, textAreaRef])-- return (- <TextArea- ref={textAreaRef}- rows={1}- contentEditable- aria-expanded={false}- className={cn(- 'transition-all text-sm pr-10 rounded-[18px] resize-none box-border leading-6',- className- )}- placeholder={placeholder}- spellCheck={false}- value={value}- {...props}- />- )-}--ExpandingTextArea.displayName = 'AssistantChatTextArea'--export { ExpandingTextArea }diff --git a/packages/ui/src/components/ExpandingTextArea/index.tsx b/packages/ui/src/components/ExpandingTextArea/index.tsxindex 143c9c0282b..b867ecea92b 100644--- a/packages/ui/src/components/ExpandingTextArea/index.tsx+++ b/packages/ui/src/components/ExpandingTextArea/index.tsx@@ -1,6 +1,6 @@'use client'-import React, { createRef, useEffect } from 'react'+import React, { forwardRef, useEffect, useImperativeHandle, useRef } from 'react'import { cn } from '../../lib/utils'import { TextArea } from '../shadcn/ui/text-area'@@ -12,34 +12,38 @@ export interface ExpandingTextAreaProps extends React.TextareaHTMLAttributes<HTM/*** This is a custom TextArea component that expands based on the content.*/-const ExpandingTextArea = ({ className, value, ...props }: ExpandingTextAreaProps) => {- const textAreaRef = createRef<HTMLTextAreaElement>()+const ExpandingTextArea = forwardRef<HTMLTextAreaElement, ExpandingTextAreaProps>(+ ({ className, value, ...props }, ref) => {+ const textAreaRef = useRef<HTMLTextAreaElement>(null)- /**- * This effect is used to resize the textarea based on the content- */- useEffect(() => {- if (textAreaRef) {- if (!value && textAreaRef && textAreaRef.current) {- textAreaRef.current.style.height = '40px'- } else if (textAreaRef && textAreaRef.current) {- const newHeight = textAreaRef.current.scrollHeight + 'px'- textAreaRef.current.style.height = newHeight+ // Expose the ref to the parent component+ useImperativeHandle(ref, () => textAreaRef.current!)+ /**+ * This effect is used to resize the textarea based on the content+ */+ useEffect(() => {+ if (textAreaRef) {+ if (!value && textAreaRef && textAreaRef.current) {+ textAreaRef.current.style.height = '40px'+ } else if (textAreaRef && textAreaRef.current) {+ const newHeight = textAreaRef.current.scrollHeight + 'px'+ textAreaRef.current.style.height = newHeight+ }}- }- }, [value, textAreaRef])+ }, [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}- />- )-}+ 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'