Skip to content

Commit 8044b1e

Browse files
Raven-Bookfu050409
andauthored
feat(prompt): cascade size properties (#30)
Co-authored-by: 苏向夜 <[email protected]>
1 parent 39f817d commit 8044b1e

File tree

4 files changed

+106
-32
lines changed

4 files changed

+106
-32
lines changed

.changes/fix-prompt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@matechat/react": patch:fix
3+
---
4+
5+
adjust title/description font-size to match the prompt component

playground/src/Chat.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MessageSquarePlus, MessageSquareWarning } from "lucide-react";
1+
import { MessageSquarePlus } from "lucide-react";
22
import { useState } from "react";
33
import { BubbleList } from "../../dist/bubble";
44
import { Button } from "../../dist/button";
@@ -73,21 +73,15 @@ export function Chat() {
7373
}
7474
/>
7575
{messages.length === 0 && (
76-
<Prompts className="mx-10">
77-
<Prompt size="md" className="max-w-xs">
78-
<PromptTitle>
79-
<MessageSquareWarning />
80-
Understanding the Transformer Model
81-
</PromptTitle>
76+
<Prompts>
77+
<Prompt>
78+
<PromptTitle>Understanding the Transformer Model</PromptTitle>
8279
<PromptDescription>
8380
Give a detailed analysis of the Transformer model.
8481
</PromptDescription>
8582
</Prompt>
86-
<Prompt className="max-w-xs">
87-
<PromptTitle>
88-
<MessageSquareWarning />
89-
Understanding the Attention Mechanism
90-
</PromptTitle>
83+
<Prompt size="xs">
84+
<PromptTitle>Understanding the Attention Mechanism</PromptTitle>
9185
<PromptDescription>
9286
Explain the attention mechanism in neural networks.
9387
</PromptDescription>

src/prompt.tsx

Lines changed: 93 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,31 @@ import clsx from "clsx";
55
import type * as React from "react";
66
import { twMerge } from "tailwind-merge";
77

8+
const promptsVariants = cva("flex", {
9+
variants: {
10+
size: {
11+
xs: "gap-2",
12+
sm: "gap-3",
13+
default: "gap-4",
14+
md: "gap-4",
15+
lg: "gap-5",
16+
},
17+
},
18+
defaultVariants: {
19+
size: "default",
20+
},
21+
});
22+
823
const promptVariants = cva(
9-
"flex flex-col gap-1 justify-center rounded-md border border-gray-300 shadow-sm",
24+
"flex flex-col justify-center bg-white border border-gray-200 rounded-lg hover:border-gray-300 hover:shadow-sm transition-all duration-150 cursor-pointer",
1025
{
1126
variants: {
1227
size: {
13-
default: "px-4 py-2",
14-
lg: "px-6 py-4 text-lg",
15-
md: "px-4 py-2 text-base",
16-
sm: "px-2 py-1 text-sm",
17-
xs: "px-1 py-0.5 text-xs",
28+
xs: "px-3 py-2 gap-1",
29+
sm: "px-4 py-2.5 gap-1.5",
30+
default: "px-4 py-3 gap-2",
31+
md: "px-5 py-3.5 gap-2",
32+
lg: "px-6 py-4 gap-2.5",
1833
},
1934
},
2035
defaultVariants: {
@@ -23,24 +38,59 @@ const promptVariants = cva(
2338
},
2439
);
2540

26-
export function Prompts({ className, ...props }: React.ComponentProps<"div">) {
41+
const promptTitleVariants = cva("font-medium text-gray-900", {
42+
variants: {
43+
size: {
44+
xs: "text-sm",
45+
sm: "text-base",
46+
default: "text-base",
47+
md: "text-lg",
48+
lg: "text-xl",
49+
},
50+
},
51+
defaultVariants: {
52+
size: "default",
53+
},
54+
});
55+
56+
const promptDescriptionVariants = cva("text-gray-600", {
57+
variants: {
58+
size: {
59+
xs: "text-xs",
60+
sm: "text-sm",
61+
default: "text-sm",
62+
md: "text-base",
63+
lg: "text-base",
64+
},
65+
},
66+
defaultVariants: {
67+
size: "default",
68+
},
69+
});
70+
71+
export function Prompts({
72+
className,
73+
size,
74+
...props
75+
}: React.ComponentProps<"div"> & VariantProps<typeof promptsVariants>) {
2776
return (
2877
<div
2978
data-slot="prompts"
30-
className={twMerge(clsx("flex flex-row flex-wrap gap-4", className))}
79+
className={twMerge(clsx(promptsVariants({ size }), className))}
3180
{...props}
3281
/>
3382
);
3483
}
3584

3685
export function Prompt({
3786
className,
38-
size,
87+
size = "default",
3988
...props
4089
}: React.ComponentProps<"div"> & VariantProps<typeof promptVariants>) {
4190
return (
4291
<div
4392
data-slot="prompt"
93+
data-size={size}
4494
className={twMerge(clsx(promptVariants({ size, className })))}
4595
{...props}
4696
/>
@@ -49,27 +99,51 @@ export function Prompt({
4999

50100
export function PromptTitle({
51101
className,
102+
size,
52103
...props
53-
}: React.ComponentProps<"h3">) {
104+
}: React.ComponentProps<"h3"> & VariantProps<typeof promptTitleVariants>) {
105+
const computedClassName = size
106+
? twMerge(clsx(promptTitleVariants({ size, className })))
107+
: twMerge(
108+
clsx(
109+
"font-medium text-gray-900",
110+
"[div[data-size='xs']_&]:text-sm",
111+
"[div[data-size='sm']_&]:text-base",
112+
"[div[data-size='default']_&]:text-base",
113+
"[div[data-size='md']_&]:text-lg",
114+
"[div[data-size='lg']_&]:text-xl",
115+
className,
116+
),
117+
);
118+
54119
return (
55-
<h3
56-
data-slot="prompt-title"
57-
className={twMerge(
58-
clsx("inline-flex items-center font-semibold gap-3", className),
59-
)}
60-
{...props}
61-
/>
120+
<h3 data-slot="prompt-title" className={computedClassName} {...props} />
62121
);
63122
}
64123

65124
export function PromptDescription({
66125
className,
126+
size,
67127
...props
68-
}: React.ComponentProps<"p">) {
128+
}: React.ComponentProps<"p"> & VariantProps<typeof promptDescriptionVariants>) {
129+
const computedClassName = size
130+
? twMerge(clsx(promptDescriptionVariants({ size, className })))
131+
: twMerge(
132+
clsx(
133+
"text-gray-600",
134+
"[div[data-size='xs']_&]:text-xs",
135+
"[div[data-size='sm']_&]:text-sm",
136+
"[div[data-size='default']_&]:text-sm",
137+
"[div[data-size='md']_&]:text-base",
138+
"[div[data-size='lg']_&]:text-base",
139+
className,
140+
),
141+
);
142+
69143
return (
70144
<p
71145
data-slot="prompt-description"
72-
className={twMerge(clsx("text-sm text-gray-500", className))}
146+
className={computedClassName}
73147
{...props}
74148
/>
75149
);

tests/index.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ test("single prompt", () => {
3030
);
3131
expect(titleElement).toBeInTheDocument();
3232
expect(descriptionElement).toBeInTheDocument();
33-
expect(titleElement).toHaveClass("inline-flex font-semibold");
33+
expect(titleElement).toHaveClass("font-medium", "text-gray-900");
3434
expect(titleElement.tagName).toBe("H3");
35+
expect(descriptionElement).toHaveClass("text-gray-600");
3536
});

0 commit comments

Comments
 (0)