Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ContextChip.tsx

131 lines5.9 KB

1import { mergeProps } from "@base-ui/react/merge-props";
2import { useRender } from "@base-ui/react/use-render";
3import { cva, type VariantProps } from "class-variance-authority";
4import type * as React from "react";
5​
6import { cn } from "~/lib/utils";
7​
8/**
9 * The inline pill for one piece of chat context. It lives with the chat components rather than
10 * in components/ui because it is one feature's look, not a generic primitive.
11 *
12 * An inline pill for one piece of context (a file, mention, terminal excerpt, pull request,
13 * skill…) that sits in running text, both in the composer and in sent messages.
14 *
15 * Metrics are in em so the chip scales with the text around it (the composer honors the
16 * prompt font-size preference). Each kind keeps one restrained color identity: every accent
17 * shares a lightness so no kind reads heavier than another, and only hue carries identity.
18 * A consumer may override `--context-chip-accent` through `style` for a color that comes
19 * from content, such as an image's average color.
20 *
21 * Renders a span by default. Render it as a button, link or popover/tooltip trigger to make it
22 * interactive; the hover tint follows from that, not from a prop. Anything focusable (including a
23 * span with tabIndex for a tooltip) gets the focus outline.
24 */
25const contextChipVariants = cva(
26 "inline-flex h-[1.41em] max-w-full items-center gap-[0.33em] rounded-[0.5em] border px-[0.5em] align-middle font-medium text-[0.86em] leading-none [&_svg]:block [&_svg]:size-[1.17em] [&_svg]:shrink-0 [&_svg]:self-center [button&,a&,[data-popup-open]&]:cursor-pointer [button&,a&]:transition-colors [button&,a&]:motion-reduce:transition-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--contrast-foreground)] disabled:cursor-default",
27 {
28 defaultVariants: { kind: "neutral" },
29 variants: {
30 kind: {
31 neutral: "border-border/70 bg-accent/40 text-foreground",
32 image: "[--context-chip-accent:oklch(0.62_0.16_16)]",
33 video: "[--context-chip-accent:oklch(0.62_0.16_48)]",
34 file: "[--context-chip-accent:oklch(0.62_0.136_237)]",
35 mention: "[--context-chip-accent:oklch(0.62_0.11_215)]",
36 terminal: "[--context-chip-accent:oklch(0.62_0.134_163)]",
37 element: "[--context-chip-accent:oklch(0.62_0.134_70)]",
38 "preview-annotation": "[--context-chip-accent:oklch(0.62_0.134_70)]",
39 "review-comment": "[--context-chip-accent:oklch(0.62_0.16_292)]",
40 "pull-request": "[--context-chip-accent:oklch(0.62_0.16_277)]",
41 "pr-open": "[--context-chip-accent:oklch(0.62_0.134_163)]",
42 "pr-draft": "[--context-chip-accent:oklch(0.62_0.02_259)]",
43 "pr-merged": "[--context-chip-accent:oklch(0.62_0.16_292)]",
44 "pr-closed": "[--context-chip-accent:oklch(0.62_0.16_16)]",
45 skill: "[--context-chip-accent:oklch(0.62_0.16_322)]",
46 citation: "[--context-chip-accent:oklch(0.62_0.16_259)]",
47 },
48 // Colors live in compoundVariants below so they come after the kind colors.
49 state: {
50 unresolved: "border-dashed",
51 invalid: "",
52 },
53 },
54 compoundVariants: [
55 {
56 kind: [
57 "image",
58 "video",
59 "file",
60 "mention",
61 "terminal",
62 "element",
63 "preview-annotation",
64 "review-comment",
65 "pull-request",
66 "pr-open",
67 "pr-draft",
68 "pr-merged",
69 "pr-closed",
70 "skill",
71 "citation",
72 ],
73 className:
74 "border-[color-mix(in_oklab,var(--context-chip-accent)_34%,var(--contrast-border))] bg-[color-mix(in_oklab,var(--context-chip-accent)_11%,transparent)] text-[color-mix(in_oklab,var(--context-chip-accent)_22%,var(--contrast-foreground))] [button:enabled&,a&]:hover:border-[color-mix(in_oklab,var(--context-chip-accent)_48%,var(--contrast-border))] [button:enabled&,a&]:hover:bg-[color-mix(in_oklab,var(--context-chip-accent)_17%,transparent)]",
75 },
76 // State colors win over any kind.
77 { state: "unresolved", className: "text-foreground" },
78 { state: "invalid", className: "border-destructive/35 bg-destructive/8 text-destructive" },
79 ],
80 },
81);
82​
83type ContextChipKind = NonNullable<VariantProps<typeof contextChipVariants>["kind"]>;
84​
85interface ContextChipProps extends useRender.ComponentProps<"span"> {
86 kind?: ContextChipKind;
87 state?: "unresolved" | "invalid";
89​
90function ContextChip({ className, kind, state, render, ...props }: ContextChipProps) {
91 const defaultProps = {
92 className: cn(contextChipVariants({ kind, state }), className),
93 "data-slot": "context-chip",
94 "data-state": state,
95 };
96 return useRender({
97 defaultTagName: "span",
98 props: mergeProps<"span">(defaultProps, props),
99 render,
100 });
102​
103/** The chip's text. Truncates to the chip's width. */
104function ContextChipLabel({ className, ...props }: React.ComponentProps<"span">) {
105 return (
106 <span
107 className={cn("block min-w-0 self-center truncate leading-tight", className)}
108 data-slot="context-chip-label"
109 {...props}
110 />
111 );
113​
114/** An icon action inside a chip, such as editing a citation's comment. Tints with the chip's kind. */
115function ContextChipAction({ className, render, ...props }: useRender.ComponentProps<"button">) {
116 const defaultProps = {
117 className: cn(
118 "ml-[0.17em] inline-flex size-[1.17em] shrink-0 cursor-pointer items-center justify-center rounded-sm text-current transition-colors hover:bg-[color-mix(in_oklab,var(--context-chip-accent,var(--color-foreground))_17%,transparent)] focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring motion-reduce:transition-none [&_svg]:size-[0.85em]",
119 className,
120 ),
121 "data-slot": "context-chip-action",
122 type: render ? undefined : ("button" as const),
123 };
124 return useRender({
125 defaultTagName: "button",
126 props: mergeProps<"button">(defaultProps, props),
127 render,
128 });
130​
131export { ContextChip, ContextChipAction, ContextChipLabel, type ContextChipKind };