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

ContextChip.tsx

96 lines4.6 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: "[--context-chip-accent:var(--context-chip-image)]",
16 video: "[--context-chip-accent:var(--context-chip-video)]",
17 file: "[--context-chip-accent:var(--context-chip-file)]",
18 mention: "[--context-chip-accent:var(--context-chip-mention)]",
19 terminal: "[--context-chip-accent:var(--context-chip-terminal)]",
20 element: "[--context-chip-accent:var(--context-chip-element)]",
21 "preview-annotation": "[--context-chip-accent:var(--context-chip-preview-annotation)]",
22 "review-comment": "[--context-chip-accent:var(--context-chip-review-comment)]",
23 "pull-request": "[--context-chip-accent:var(--context-chip-pull-request)]",
24 "pr-open": "[--context-chip-accent:var(--context-chip-pr-open)]",
25 "pr-draft": "[--context-chip-accent:var(--context-chip-pr-draft)]",
26 "pr-merged": "[--context-chip-accent:var(--context-chip-pr-merged)]",
27 "pr-closed": "[--context-chip-accent:var(--context-chip-pr-closed)]",
28 skill: "[--context-chip-accent:var(--context-chip-skill)]",
29 thread: "[--context-chip-accent:var(--context-chip-thread)]",
30 environment: "[--context-chip-accent:var(--context-chip-environment)]",
31 citation: "[--context-chip-accent:var(--context-chip-citation)]",
32 "doc-pdf": "[--context-chip-accent:var(--context-chip-doc-pdf)]",
33 "doc-word": "[--context-chip-accent:var(--context-chip-doc-word)]",
34 "doc-spreadsheet": "[--context-chip-accent:var(--context-chip-doc-spreadsheet)]",
35 "doc-slides": "[--context-chip-accent:var(--context-chip-doc-slides)]",
36 "doc-epub": "[--context-chip-accent:var(--context-chip-doc-epub)]",
37 },
38 state: {
39 unresolved: "data-[state=unresolved]:[--context-chip-accent:currentColor]",
40 invalid:
41 "data-[state=invalid]:[--context-chip-accent:var(--context-chip-invalid,var(--destructive-foreground))]",
42 },
43 },
44 },
45);
46​
47type ContextChipKind = NonNullable<VariantProps<typeof contextChipVariants>["kind"]>;
48​
49interface ContextChipProps extends useRender.ComponentProps<"span"> {
50 kind?: ContextChipKind;
51 state?: "unresolved" | "invalid";
53​
54function ContextChip({ className, kind, state, render, ...props }: ContextChipProps) {
55 const defaultProps = {
56 className: cn(contextChipVariants({ kind, state }), className),
57 "data-slot": "context-chip",
58 "data-state": state,
59 };
60 return useRender({
61 defaultTagName: "span",
62 props: mergeProps<"span">(defaultProps, props),
63 render,
64 });
66​
67function ContextChipLabel({ className, ...props }: React.ComponentProps<"span">) {
68 return (
69 <span
70 className={cn(
71 "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",
72 className,
73 )}
74 data-slot="context-chip-label"
75 {...props}
76 />
77 );
79​
80function ContextChipAction({ className, render, ...props }: useRender.ComponentProps<"button">) {
81 const defaultProps = {
82 className: cn(
83 "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]",
84 className,
85 ),
86 "data-slot": "context-chip-action",
87 type: render ? undefined : ("button" as const),
88 };
89 return useRender({
90 defaultTagName: "button",
91 props: mergeProps<"button">(defaultProps, props),
92 render,
93 });
95​
96export { ContextChip, ContextChipAction, ContextChipLabel, type ContextChipKind };