-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
225 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@assistant-ui/react": patch | ||
--- | ||
|
||
feat: AttachmentPrimitive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
--- | ||
title: ActionBar | ||
--- | ||
|
||
Buttons to interact with attachments. | ||
|
||
import { ParametersTable, DataAttributesTable } from "@/components/docs"; | ||
import { Code } from "@radix-ui/themes"; | ||
import { Callout } from "fumadocs-ui/components/callout"; | ||
|
||
<Callout> | ||
**Dual Use!** Attachments can appear in both messages and composers. | ||
</Callout> | ||
|
||
## Anatomy | ||
|
||
```tsx | ||
import { AttachmentPrimitive } from "@assistant-ui/react"; | ||
|
||
const MyMessageAttachment = () => ( | ||
<AttachmentPrimitive.Root> | ||
<AttachmentPrimitive.Thumbnail /> | ||
<AttachmentPrimitive.Name /> | ||
</AttachmentPrimitive.Root> | ||
); | ||
|
||
const MyComposerAttachment = () => ( | ||
<AttachmentPrimitive.Root> | ||
<AttachmentPrimitive.Thumbnail /> | ||
<AttachmentPrimitive.Name /> | ||
<AttachmentPrimitive.Remove /> | ||
</AttachmentPrimitive.Root> | ||
); | ||
``` | ||
|
||
## API Reference | ||
|
||
### Container | ||
|
||
Containts all parts of the attachment. | ||
|
||
This primitive renders a `<div>` element unless `asChild` is set. | ||
|
||
<ParametersTable | ||
type="AttachmentPrimitiveRootProps" | ||
parameters={[ | ||
{ | ||
name: "asChild", | ||
}, | ||
]} | ||
/> | ||
|
||
### Thumbnail | ||
|
||
The thumbnail of the attachment. | ||
|
||
This primitive renders a `<div>` element unless `asChild` is set. | ||
|
||
<ParametersTable | ||
type="AttachmentPrimitiveThumbnailProps" | ||
parameters={[ | ||
{ | ||
name: "asChild", | ||
}, | ||
]} | ||
/> | ||
|
||
### Name | ||
|
||
The name of the attachment. | ||
|
||
This primitive renders a `<div>` element unless `asChild` is set. | ||
|
||
<ParametersTable | ||
type="AttachmentPrimitiveNameProps" | ||
parameters={[ | ||
{ | ||
name: "asChild", | ||
}, | ||
]} | ||
/> | ||
|
||
### Remove | ||
|
||
Removes the attachment. | ||
|
||
This primitive renders a `<button>` element unless `asChild` is set. | ||
|
||
<ParametersTable | ||
type="AttachmentPrimitiveRemoveProps" | ||
parameters={[ | ||
{ | ||
name: "asChild", | ||
}, | ||
]} | ||
/> | ||
|
||
#### `useAttachmentRemove` | ||
|
||
Provides the `Remove` functionality as a hook. | ||
|
||
```tsx | ||
import { useAttachmentRemove } from "@assistant-ui/react"; | ||
|
||
const Remove = () => { | ||
const remove = useAttachmentRemove(); | ||
|
||
// remove action is not available | ||
if (!remove) return null; | ||
|
||
return <button onClick={remove}>Remove</button>; | ||
}; | ||
``` |
12 changes: 12 additions & 0 deletions
12
packages/react/src/primitive-hooks/attachment/useAttachmentRemove.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useCallback } from "react"; | ||
import { useAttachmentRuntime } from "../../context/react/AttachmentContext"; | ||
|
||
export const useAttachmentRemove = () => { | ||
const attachmentRuntime = useAttachmentRuntime(); | ||
|
||
const handleRemoveAttachment = useCallback(() => { | ||
attachmentRuntime.remove(); | ||
}, [attachmentRuntime]); | ||
|
||
return handleRemoveAttachment; | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/react/src/primitives/attachment/AttachmentName.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use client"; | ||
|
||
import type { FC } from "react"; | ||
import { useAttachment } from "../../context/react/AttachmentContext"; | ||
|
||
export namespace AttachmentPrimitiveName { | ||
export type Props = Record<string, never>; | ||
} | ||
|
||
export const AttachmentPrimitiveName: FC< | ||
AttachmentPrimitiveName.Props | ||
> = () => { | ||
const name = useAttachment((a) => a.name); | ||
return <>{name}</>; | ||
}; | ||
|
||
AttachmentPrimitiveName.displayName = "AttachmentPrimitive.Name"; |
16 changes: 16 additions & 0 deletions
16
packages/react/src/primitives/attachment/AttachmentRemove.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"use client"; | ||
|
||
import { useAttachmentRemove } from "../../primitive-hooks/attachment/useAttachmentRemove"; | ||
import { | ||
ActionButtonProps, | ||
createActionButton, | ||
} from "../../utils/createActionButton"; | ||
|
||
export namespace AttachmentPrimitiveRemove { | ||
export type Props = ActionButtonProps<typeof useAttachmentRemove>; | ||
} | ||
|
||
export const AttachmentPrimitiveRemove = createActionButton( | ||
"AttachmentPrimitive.Remove", | ||
useAttachmentRemove, | ||
); |
18 changes: 18 additions & 0 deletions
18
packages/react/src/primitives/attachment/AttachmentRoot.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Primitive } from "@radix-ui/react-primitive"; | ||
import { ComponentPropsWithoutRef, ElementRef, forwardRef } from "react"; | ||
|
||
type PrimitiveDivProps = ComponentPropsWithoutRef<typeof Primitive.div>; | ||
type AttachmentPrimitiveRootElement = ElementRef<typeof Primitive.div>; | ||
|
||
export namespace AttachmentPrimitiveRoot { | ||
export type Props = PrimitiveDivProps; | ||
} | ||
|
||
export const AttachmentPrimitiveRoot = forwardRef< | ||
AttachmentPrimitiveRootElement, | ||
AttachmentPrimitiveRoot.Props | ||
>((props, ref) => { | ||
return <Primitive.div {...props} ref={ref} />; | ||
}); | ||
|
||
AttachmentPrimitiveRoot.displayName = "AttachmentPrimitive.Root"; |
22 changes: 22 additions & 0 deletions
22
packages/react/src/primitives/attachment/AttachmentThumb.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"use client"; | ||
|
||
import { ComponentPropsWithoutRef, forwardRef, type ElementRef } from "react"; | ||
import { useAttachment } from "../../context/react/AttachmentContext"; | ||
import { Primitive } from "@radix-ui/react-primitive"; | ||
|
||
type AttachmentPrimitiveThumbElement = ElementRef<typeof Primitive.div>; | ||
type PrimitiveDivProps = ComponentPropsWithoutRef<typeof Primitive.div>; | ||
|
||
export namespace AttachmentPrimitiveThumb { | ||
export type Props = PrimitiveDivProps; | ||
} | ||
|
||
export const AttachmentPrimitiveThumb = forwardRef< | ||
AttachmentPrimitiveThumbElement, | ||
AttachmentPrimitiveThumb.Props | ||
>(() => { | ||
const ext = useAttachment((a) => a.name.split(".").pop()); | ||
return <Primitive.div>.{ext}</Primitive.div>; | ||
}); | ||
|
||
AttachmentPrimitiveThumb.displayName = "AttachmentPrimitive.Thumb"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { AttachmentPrimitiveRoot as Root } from "./AttachmentRoot"; | ||
export { AttachmentPrimitiveThumb as unstable_Thumb } from "./AttachmentThumb"; | ||
export { AttachmentPrimitiveName as Name } from "./AttachmentName"; | ||
export { AttachmentPrimitiveRemove as Remove } from "./AttachmentRemove"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters