Skip to content

Commit

Permalink
feat: export Primitive Prop Types (#330)
Browse files Browse the repository at this point in the history
changeset
  • Loading branch information
Yonom authored Jun 26, 2024
1 parent 68f1e15 commit d52c345
Show file tree
Hide file tree
Showing 59 changed files with 499 additions and 306 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-cooks-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat: Primitive Prop Types
7 changes: 5 additions & 2 deletions apps/www/pages/docs/advanced/Markdown.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ import { Steps, Callout } from "nextra/components";
npx assistant-ui@latest add markdown-text
```

### Use it in Thread.tsx
### Use it in `Thread.tsx`

```tsx
Pass the `MarkdownText` component to the `MessagePrimitive.Content` component

```tsx {1, 7}
import { MarkdownText } from "@/components/ui/assistant-ui/markdown-text";

const AssistantMessage = () => {
return (
<MessagePrimitive.Root>
...
<MessagePrimitive.Content components={{ Text: MarkdownText }} />
...
</MessagePrimitive.Root>
);
};
Expand Down
17 changes: 8 additions & 9 deletions apps/www/pages/docs/migrations/v0.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const UserMessage = () => {
return (
...
- <p className="whitespace-pre-line ...">
+ <div className="...">
<MessagePrimitive.Content />
- </p>
+ </div>
+ <div className="...">
<MessagePrimitive.Content />
- </p>
+ </div>
</div>
...
);
Expand All @@ -25,11 +25,10 @@ const AssistantMessage = () => {
return (
...
- <p className="whitespace-pre-line ...">
+ <div className="...">
<MessagePrimitive.Content />
- </p>
+ </div>
</div>
+ <div className="...">
<MessagePrimitive.Content />
- </p>
+ </div>
...
);
};
Expand Down
8 changes: 4 additions & 4 deletions apps/www/pages/reference/primitives/ActionBar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Containts all parts of the action bar.
This primitive renders a `<div>` element unless `asChild` is set.

<ParametersTable
type="ActionBarRootProps"
type="ActionBarPrimitiveRootProps"
parameters={[
{
name: "asChild"
Expand Down Expand Up @@ -107,7 +107,7 @@ Enables edit mode on user message.
This primitive renders a `<button>` element unless `asChild` is set.

<ParametersTable
type="ActionBarEditProps"
type="ActionBarPrimitiveEditProps"
parameters={[
{
name: "asChild",
Expand Down Expand Up @@ -143,7 +143,7 @@ This primitive renders a `<button>` element unless `asChild` is set.


<ParametersTable
type="ActionBarReloadProps"
type="ActionBarPrimitiveReloadProps"
parameters={[
{
name: "asChild",
Expand Down Expand Up @@ -178,7 +178,7 @@ Copies the message to the clipboard.
This primitive renders a `<button>` element unless `asChild` is set.

<ParametersTable
type="ActionBarCopyProps"
type="ActionBarPrimitiveCopyProps"
parameters={[
{
name: "asChild",
Expand Down
6 changes: 3 additions & 3 deletions apps/www/pages/reference/primitives/AssistantModal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Thread = () => (
Contains all parts of the assistant modal.

<ParametersTable
type="AssistantModalRootProps"
type="AssistantModalPrimitiveRootProps"
parameters={[
{
name: "defaultOpen",
Expand Down Expand Up @@ -64,7 +64,7 @@ A button that toggles the open state of the assistant modal. `AssistantModalPrim
This primitive renders a `<button>` element unless `asChild` is set.

<ParametersTable
type="AssistantModalTriggerProps"
type="AssistantModalPrimitiveTriggerProps"
parameters={[
{
name: "asChild",
Expand All @@ -88,7 +88,7 @@ The component that pops out when the assistant modal is open.
This primitive renders a `<div>` element unless `asChild` is set.

<ParametersTable
type="AssistantModalContentProps"
type="AssistantModalPrimitiveContentProps"
parameters={[
{
name: "asChild",
Expand Down
6 changes: 3 additions & 3 deletions apps/www/pages/reference/primitives/BranchPicker.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Containts all parts of the branch picker.
This primitive renders a `<div>` element unless `asChild` is set.

<ParametersTable
type="BranchPickerRootProps"
type="BranchPickerPrimitiveRootProps"
parameters={[
{
name: "asChild",
Expand Down Expand Up @@ -86,7 +86,7 @@ Navigates to the previous branch.
This primitive renders a `<button>` element unless `asChild` is set.

<ParametersTable
type="BranchPickerPreviousProps"
type="BranchPickerPrimitivePreviousProps"
parameters={[
{
name: "asChild",
Expand Down Expand Up @@ -118,7 +118,7 @@ Navigates to the next branch.
This primitive renders a `<button>` element unless `asChild` is set.

<ParametersTable
type="BranchPickerNextProps"
type="BranchPickerPrimitiveNextProps"
parameters={[
{
name: "asChild",
Expand Down
6 changes: 3 additions & 3 deletions apps/www/pages/reference/primitives/Composer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ The text input field for the user to type a new message.
This primitive renders a `<textarea>` element unless `asChild` is set.

<ParametersTable
type="ComposerInputProps"
type="ComposerPrimitiveInputProps"
parameters={[
{
name: "asChild",
Expand Down Expand Up @@ -114,7 +114,7 @@ The button to send the message.
This primitive renders a `<button>` element unless `asChild` is set.

<ParametersTable
type="ComposerSendProps"
type="ComposerPrimitiveSendProps"
parameters={[
{
name: "asChild",
Expand All @@ -132,7 +132,7 @@ In thread composers, this action stops the current run.
This primitive renders a `<button>` element unless `asChild` is set.

<ParametersTable
type="ComposerCancelProps"
type="ComposerPrimitiveCancelProps"
parameters={[
{
name: "asChild",
Expand Down
6 changes: 3 additions & 3 deletions apps/www/pages/reference/primitives/Message.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Containts all parts of the message.
This primitive renders a `<div>` element unless `asChild` is set.

<ParametersTable
type="MessageRootProps"
type="MessagePrimitiveRootProps"
parameters={[
{
name: "asChild",
Expand All @@ -51,7 +51,7 @@ This primitive renders a `<div>` element unless `asChild` is set.
The content of the message. This renders a separate component for each content part of the message.

<ParametersTable
type="MessageContentProps"
type="MessagePrimitiveContentProps"
parameters={[
{
name: "components",
Expand Down Expand Up @@ -113,7 +113,7 @@ The contents of this element will be portaled to `<ContentPartPrimitive.InProgre
This primitive renders a `<span>` element unless `asChild` is set.

<ParametersTable
type="MessageInProgressProps"
type="MessagePrimitiveInProgressProps"
parameters={[
{
name: "asChild",
Expand Down
8 changes: 4 additions & 4 deletions apps/www/pages/reference/primitives/Thread.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The scrollable area containing all messages. Anchors scroll to the bottom as new
This primitive renders a `<div>` element unless `asChild` is set.

<ParametersTable
type="ThreadViewportProps"
type="ThreadPrimitiveViewportProps"
parameters={[
{
name: "asChild",
Expand Down Expand Up @@ -81,7 +81,7 @@ const Viewport = () => {
Renders all messages. This primitive renders a separate component for each message.

<ParametersTable
type="ThreadMessagesProps"
type="ThreadPrimitiveMessagesProps"
parameters={[
{
name: "components",
Expand Down Expand Up @@ -141,7 +141,7 @@ A button to scroll the viewport to the bottom. Disabled when the viewport is alr
This primitive renders a `<button>` element unless `asChild` is set.

<ParametersTable
type="ThreadScrollToBottomProps"
type="ThreadPrimitiveScrollToBottomProps"
parameters={[
{
name: "asChild",
Expand Down Expand Up @@ -171,7 +171,7 @@ const ScrollToBottom = () => {
Renders children if a condition is met.

<ParametersTable
type="UseThreadIfProps"
type="ThreadPrimitiveIfProps"
parameters={[
{
name: "empty",
Expand Down
13 changes: 10 additions & 3 deletions packages/react/src/primitives/actionBar/ActionBarCopy.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
"use client";

import { useActionBarCopy } from "../../primitive-hooks/actionBar/useActionBarCopy";
import { createActionButton } from "../../utils/createActionButton";
import {
ActionButtonProps,
createActionButton,
} from "../../utils/createActionButton";

export const ActionBarCopy = createActionButton(
"ActionBarCopy",
export type ActionBarPrimitiveCopyProps = ActionButtonProps<
typeof useActionBarCopy
>;

export const ActionBarPrimitiveCopy = createActionButton(
"ActionBarPrimitive.Copy",
useActionBarCopy,
);
13 changes: 10 additions & 3 deletions packages/react/src/primitives/actionBar/ActionBarEdit.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
"use client";

import { useActionBarEdit } from "../../primitive-hooks/actionBar/useActionBarEdit";
import { createActionButton } from "../../utils/createActionButton";
import {
ActionButtonProps,
createActionButton,
} from "../../utils/createActionButton";

export const ActionBarEdit = createActionButton(
"ActionBarEdit",
export type ActionBarPrimitiveEditProps = ActionButtonProps<
typeof useActionBarEdit
>;

export const ActionBarPrimitiveEdit = createActionButton(
"ActionBarPrimitive.Edit",
useActionBarEdit,
);
13 changes: 10 additions & 3 deletions packages/react/src/primitives/actionBar/ActionBarReload.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
"use client";

import { useActionBarReload } from "../../primitive-hooks/actionBar/useActionBarReload";
import { createActionButton } from "../../utils/createActionButton";
import {
ActionButtonProps,
createActionButton,
} from "../../utils/createActionButton";

export const ActionBarReload = createActionButton(
"ActionBarReload",
export type ActionBarPrimitiveReloadProps = ActionButtonProps<
typeof useActionBarReload
>;

export const ActionBarPrimitiveReload = createActionButton(
"ActionBarPrimitive.Reload",
useActionBarReload,
);
66 changes: 11 additions & 55 deletions packages/react/src/primitives/actionBar/ActionBarRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,21 @@

import { Primitive } from "@radix-ui/react-primitive";
import { type ElementRef, forwardRef, ComponentPropsWithoutRef } from "react";
import { useMessageContext } from "../../context/react/MessageContext";
import { useThreadContext } from "../../context/react/ThreadContext";
import { useCombinedStore } from "../../utils/combined/useCombinedStore";
import {
useActionBarFloatStatus,
HideAndFloatStatus,
UseActionBarFloatStatusProps,
} from "./useActionBarFloatStatus";

type ActionBarRootElement = ElementRef<typeof Primitive.div>;
type ActionBarPrimitiveRootElement = ElementRef<typeof Primitive.div>;
type PrimitiveDivProps = ComponentPropsWithoutRef<typeof Primitive.div>;

enum HideAndFloatStatus {
Hidden = "hidden",
Floating = "floating",
Normal = "normal",
}

type UseActionBarFloatStatusProps = {
hideWhenRunning?: boolean | undefined;
autohide?: "always" | "not-last" | "never" | undefined;
autohideFloat?: "always" | "single-branch" | "never" | undefined;
};

const useActionBarFloatStatus = ({
hideWhenRunning,
autohide,
autohideFloat,
}: UseActionBarFloatStatusProps) => {
const { useThread } = useThreadContext();
const { useMessage, useMessageUtils } = useMessageContext();

return useCombinedStore(
[useThread, useMessage, useMessageUtils],
(t, m, mu) => {
if (hideWhenRunning && t.isRunning) return HideAndFloatStatus.Hidden;

const autohideEnabled =
autohide === "always" || (autohide === "not-last" && !m.isLast);

// normal status
if (!autohideEnabled) return HideAndFloatStatus.Normal;

// hidden status
if (!mu.isHovering) return HideAndFloatStatus.Hidden;

// floating status
if (
autohideFloat === "always" ||
(autohideFloat === "single-branch" && m.branches.length <= 1)
)
return HideAndFloatStatus.Floating;

return HideAndFloatStatus.Normal;
},
);
};

export type ActionBarRootProps = PrimitiveDivProps &
export type ActionBarPrimitiveRootProps = PrimitiveDivProps &
UseActionBarFloatStatusProps;

export const ActionBarRoot = forwardRef<
ActionBarRootElement,
ActionBarRootProps
export const ActionBarPrimitiveRoot = forwardRef<
ActionBarPrimitiveRootElement,
ActionBarPrimitiveRootProps
>(({ hideWhenRunning, autohide, autohideFloat, ...rest }, ref) => {
const hideAndfloatStatus = useActionBarFloatStatus({
hideWhenRunning,
Expand All @@ -81,4 +37,4 @@ export const ActionBarRoot = forwardRef<
);
});

ActionBarRoot.displayName = "ActionBarRoot";
ActionBarPrimitiveRoot.displayName = "ActionBarPrimitive.Root";
8 changes: 4 additions & 4 deletions packages/react/src/primitives/actionBar/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ActionBarRoot as Root } from "./ActionBarRoot";
export { ActionBarCopy as Copy } from "./ActionBarCopy";
export { ActionBarReload as Reload } from "./ActionBarReload";
export { ActionBarEdit as Edit } from "./ActionBarEdit";
export { ActionBarPrimitiveRoot as Root } from "./ActionBarRoot";
export { ActionBarPrimitiveCopy as Copy } from "./ActionBarCopy";
export { ActionBarPrimitiveReload as Reload } from "./ActionBarReload";
export { ActionBarPrimitiveEdit as Edit } from "./ActionBarEdit";
4 changes: 4 additions & 0 deletions packages/react/src/primitives/actionBar/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type { ActionBarPrimitiveRootProps } from "./ActionBarRoot";
export type { ActionBarPrimitiveEditProps } from "./ActionBarEdit";
export type { ActionBarPrimitiveReloadProps } from "./ActionBarReload";
export type { ActionBarPrimitiveCopyProps } from "./ActionBarCopy";
Loading

0 comments on commit d52c345

Please sign in to comment.