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

ContextChip.tsx

97 lines4.7 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​
8const contextChipVariants = cva(
9 "inline-flex max-w-full items-center gap-[0.2em] font-medium leading-[normal] text-(--context-chip-ink) [--context-chip-ink:var(--context-chip-accent)] [--document-icon-stroke:var(--context-chip-ink)] [&_svg]:block [&_svg]:size-[1em] [&_svg]:shrink-0 [&_svg]:self-center [button&,a&,[data-popup-open]&]:cursor-pointer focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-current rounded-sm disabled:cursor-default",
10 {
11 defaultVariants: { kind: "neutral" },
12 variants: {
13 kind: {
14 neutral: "[--context-chip-accent:currentColor]",
15 image:
16 "rounded-lg border border-(--context-chip-accent)/30 bg-(--context-chip-accent)/10 px-1.5 [--context-chip-accent:var(--context-chip-image)]",
17 video: "[--context-chip-accent:var(--context-chip-video)]",
18 file: "[--context-chip-accent:var(--context-chip-file)]",
19 mention: "[--context-chip-accent:var(--context-chip-mention)]",
20 terminal: "[--context-chip-accent:var(--context-chip-terminal)]",
21 element: "[--context-chip-accent:var(--context-chip-element)]",
22 "preview-annotation": "[--context-chip-accent:var(--context-chip-preview-annotation)]",
23 "review-comment": "[--context-chip-accent:var(--context-chip-review-comment)]",
24 "pull-request": "[--context-chip-accent:var(--context-chip-pull-request)]",
25 "pr-open": "[--context-chip-accent:var(--context-chip-pr-open)]",
26 "pr-draft": "[--context-chip-accent:var(--context-chip-pr-draft)]",
27 "pr-merged": "[--context-chip-accent:var(--context-chip-pr-merged)]",
28 "pr-closed": "[--context-chip-accent:var(--context-chip-pr-closed)]",
29 skill: "[--context-chip-accent:var(--context-chip-skill)]",
30 thread: "[--context-chip-accent:var(--context-chip-thread)]",
31 environment: "[--context-chip-accent:var(--context-chip-environment)]",
32 citation: "[--context-chip-accent:var(--context-chip-citation)]",
33 "doc-pdf": "[--context-chip-accent:var(--context-chip-doc-pdf)]",
34 "doc-word": "[--context-chip-accent:var(--context-chip-doc-word)]",
35 "doc-spreadsheet": "[--context-chip-accent:var(--context-chip-doc-spreadsheet)]",
36 "doc-slides": "[--context-chip-accent:var(--context-chip-doc-slides)]",
37 "doc-epub": "[--context-chip-accent:var(--context-chip-doc-epub)]",
38 },
39 state: {
40 unresolved: "data-[state=unresolved]:[--context-chip-accent:currentColor]",
41 invalid:
42 "data-[state=invalid]:[--context-chip-accent:var(--context-chip-invalid,var(--destructive-foreground))]",
43 },
44 },
45 },
46);
47​
48type ContextChipKind = NonNullable<VariantProps<typeof contextChipVariants>["kind"]>;
49​
50interface ContextChipProps extends useRender.ComponentProps<"span"> {
51 kind?: ContextChipKind;
52 state?: "unresolved" | "invalid";
54​
55function ContextChip({ className, kind, state, render, ...props }: ContextChipProps) {
56 const defaultProps = {
57 className: cn(contextChipVariants({ kind, state }), className),
58 "data-slot": "context-chip",
59 "data-state": state,
60 };
61 return useRender({
62 defaultTagName: "span",
63 props: mergeProps<"span">(defaultProps, props),
64 render,
65 });
67​
68function ContextChipLabel({ className, ...props }: React.ComponentProps<"span">) {
69 return (
70 <span
71 className={cn(
72 "block min-w-0 self-baseline overflow-x-clip text-ellipsis whitespace-nowrap underline decoration-transparent decoration-1 underline-offset-[0.2em] transition-colors motion-reduce:transition-none [:is(button:enabled,a):hover>&]:decoration-current",
73 className,
74 )}
75 data-slot="context-chip-label"
76 {...props}
77 />
78 );
80​
81function ContextChipAction({ className, render, ...props }: useRender.ComponentProps<"button">) {
82 const defaultProps = {
83 className: cn(
84 "ml-[0.17em] inline-flex size-[1em] shrink-0 cursor-pointer items-center justify-center rounded-sm text-current transition-colors hover:bg-(--context-chip-accent,var(--color-foreground))/17 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-ring motion-reduce:transition-none [&_svg]:size-[0.85em]",
85 className,
86 ),
87 "data-slot": "context-chip-action",
88 type: render ? undefined : ("button" as const),
89 };
90 return useRender({
91 defaultTagName: "button",
92 props: mergeProps<"button">(defaultProps, props),
93 render,
94 });
96​
97export { ContextChip, ContextChipAction, ContextChipLabel, type ContextChipKind };