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

ContextChip.tsx

112 lines4.8 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 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-foreground disabled:cursor-default",
10 {
11 defaultVariants: { kind: "neutral" },
12 variants: {
13 kind: {
14 neutral: "border-border/70 bg-accent/40 text-foreground",
15 image: "[--context-chip-accent:oklch(0.62_0.16_16)]",
16 video: "[--context-chip-accent:oklch(0.62_0.16_48)]",
17 file: "[--context-chip-accent:oklch(0.62_0.136_237)]",
18 mention: "[--context-chip-accent:oklch(0.62_0.11_215)]",
19 terminal: "[--context-chip-accent:oklch(0.62_0.134_163)]",
20 element: "[--context-chip-accent:oklch(0.62_0.134_70)]",
21 "preview-annotation": "[--context-chip-accent:oklch(0.62_0.134_70)]",
22 "review-comment": "[--context-chip-accent:oklch(0.62_0.16_292)]",
23 "pull-request": "[--context-chip-accent:oklch(0.62_0.16_277)]",
24 "pr-open": "[--context-chip-accent:oklch(0.62_0.134_163)]",
25 "pr-draft": "[--context-chip-accent:oklch(0.62_0.02_259)]",
26 "pr-merged": "[--context-chip-accent:oklch(0.62_0.16_292)]",
27 "pr-closed": "[--context-chip-accent:oklch(0.62_0.16_16)]",
28 skill: "[--context-chip-accent:oklch(0.62_0.16_322)]",
29 thread: "[--context-chip-accent:oklch(0.62_0.12_190)]",
30 citation: "[--context-chip-accent:oklch(0.62_0.16_259)]",
31 },
32 state: {
33 unresolved: "border-dashed",
34 invalid: "",
35 },
36 },
37 compoundVariants: [
38 {
39 kind: [
40 "image",
41 "video",
42 "file",
43 "mention",
44 "terminal",
45 "element",
46 "preview-annotation",
47 "review-comment",
48 "pull-request",
49 "pr-open",
50 "pr-draft",
51 "pr-merged",
52 "pr-closed",
53 "skill",
54 "thread",
55 "citation",
56 ],
57 className:
58 "[--context-chip-border:color-mix(in_oklab,var(--context-chip-accent)_34%,var(--contrast-border))] [--context-chip-border-hover:color-mix(in_oklab,var(--context-chip-accent)_48%,var(--contrast-border))] [--context-chip-foreground:color-mix(in_oklab,var(--context-chip-accent)_22%,var(--contrast-foreground))] border-(--context-chip-border) bg-(--context-chip-accent)/11 text-(--context-chip-foreground) [button:enabled&,a&]:hover:border-(--context-chip-border-hover) [button:enabled&,a&]:hover:bg-(--context-chip-accent)/17",
59 },
60 { state: "unresolved", className: "text-foreground" },
61 { state: "invalid", className: "border-destructive/35 bg-destructive/8 text-destructive" },
62 ],
63 },
64);
65​
66type ContextChipKind = NonNullable<VariantProps<typeof contextChipVariants>["kind"]>;
67​
68interface ContextChipProps extends useRender.ComponentProps<"span"> {
69 kind?: ContextChipKind;
70 state?: "unresolved" | "invalid";
72​
73function ContextChip({ className, kind, state, render, ...props }: ContextChipProps) {
74 const defaultProps = {
75 className: cn(contextChipVariants({ kind, state }), className),
76 "data-slot": "context-chip",
77 "data-state": state,
78 };
79 return useRender({
80 defaultTagName: "span",
81 props: mergeProps<"span">(defaultProps, props),
82 render,
83 });
85​
86function ContextChipLabel({ className, ...props }: React.ComponentProps<"span">) {
87 return (
88 <span
89 className={cn("block min-w-0 self-center truncate leading-tight", className)}
90 data-slot="context-chip-label"
91 {...props}
92 />
93 );
95​
96function ContextChipAction({ className, render, ...props }: useRender.ComponentProps<"button">) {
97 const defaultProps = {
98 className: cn(
99 "ml-[0.17em] inline-flex size-[1.17em] 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]",
100 className,
101 ),
102 "data-slot": "context-chip-action",
103 type: render ? undefined : ("button" as const),
104 };
105 return useRender({
106 defaultTagName: "button",
107 props: mergeProps<"button">(defaultProps, props),
108 render,
109 });
111​
112export { ContextChip, ContextChipAction, ContextChipLabel, type ContextChipKind };