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

ComposerControl.tsx

122 lines4.1 KB

1import { mergeProps } from "@base-ui/react/merge-props";
2import { Select as SelectPrimitive } from "@base-ui/react/select";
3import { useRender } from "@base-ui/react/use-render";
4import type { ComponentProps, ComponentType, SVGProps } from "react";
5import { ChevronDownIcon } from "lucide-react";
6​
7import { cn } from "~/lib/utils";
8import { Separator } from "../ui/separator";
9​
10export type ComposerControlSize = "sm" | "xs";
11​
12function composerControlClassName(size: ComposerControlSize, className?: string) {
13 return cn(
14 "relative inline-flex shrink-0 cursor-pointer items-center justify-center whitespace-nowrap rounded-(--control-radius) border border-transparent text-base outline-none hover:bg-accent data-pressed:bg-accent focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-64 data-disabled:pointer-events-none data-disabled:opacity-64 pointer-coarse:after:absolute pointer-coarse:after:size-full pointer-coarse:after:min-h-11 pointer-coarse:after:min-w-11 [&:active:not([aria-haspopup])]:scale-[0.97] [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg]:-mx-0.5 [&_svg[data-composer-control-icon]]:mx-0",
15 size === "xs"
16 ? "h-7 gap-1 px-1.75 font-normal text-muted-foreground/70 text-sm hover:text-foreground/80 sm:h-6 sm:text-xs [&_svg:not([class*='size-'])]:size-4 sm:[&_svg:not([class*='size-'])]:size-3.5 [&_svg[data-composer-control-chevron]]:ms-0 [&_svg[data-composer-control-chevron]]:-me-1"
17 : "h-7 gap-1.5 px-2.5 font-medium text-secondary-label [&_svg:not([class*='text-'])]:text-muted-foreground hover:text-foreground sm:text-sm [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4",
18 "aria-pressed:bg-accent aria-pressed:text-accent-foreground aria-pressed:hover:bg-accent/80",
19 className,
20 );
22​
23type ComposerControlProps = useRender.ComponentProps<"button"> & {
24 size?: ComposerControlSize;
25};
26​
27export function ComposerControl({
28 className,
29 size = "sm",
30 render,
31 ...props
32}: ComposerControlProps) {
33 const defaultProps = {
34 className: composerControlClassName(size, className),
35 type: render ? undefined : ("button" as const),
36 };
37 return useRender({
38 defaultTagName: "button",
39 props: mergeProps<"button">(defaultProps, props),
40 render,
41 });
43​
44export function ComposerControlIcon({
45 icon: Icon,
46 className,
47 opticalSize = "default",
48 size = "sm",
49}: {
50 icon: ComponentType<SVGProps<SVGSVGElement>>;
51 className?: string | undefined;
52 opticalSize?: "default" | "large";
53 size?: ComposerControlSize;
54}) {
55 return (
56 <Icon
57 aria-hidden="true"
58 className={cn(
59 "shrink-0",
60 size === "xs" ? "size-3" : opticalSize === "large" ? "size-4.5" : "size-4",
61 className,
62 )}
63 data-composer-control-icon
64 />
65 );
67​
68export function ComposerControlChevron({
69 className,
70 size = "sm",
71}: {
72 className?: string;
73 size?: ComposerControlSize;
74} = {}) {
75 return (
76 <ChevronDownIcon
77 aria-hidden="true"
78 className={cn(
79 "shrink-0",
80 size === "xs" ? "size-3 text-current opacity-50" : "size-3.5 text-icon-muted",
81 className,
82 )}
83 data-composer-control-chevron
84 strokeWidth={2.25}
85 />
86 );
88​
89export function ComposerControlSeparator({
90 className,
91 size = "sm",
92 ...props
93}: Omit<ComponentProps<typeof Separator>, "orientation"> & {
94 size?: ComposerControlSize;
95}) {
96 return (
97 <Separator
98 orientation="vertical"
99 className={cn("mx-0.5 hidden sm:block", size === "xs" ? "h-3.5!" : "h-4", className)}
100 {...props}
101 />
102 );
104​
105export function ComposerSelectControl({
106 className,
107 children,
108 size = "sm",
109 ...props
110}: Omit<SelectPrimitive.Trigger.Props, "className"> & {
111 className?: string | undefined;
112 size?: ComposerControlSize;
113}) {
114 return (
115 <SelectPrimitive.Trigger className={composerControlClassName(size, className)} {...props}>
116 {children}
117 <SelectPrimitive.Icon>
118 <ComposerControlChevron size={size} />
119 </SelectPrimitive.Icon>
120 </SelectPrimitive.Trigger>
121 );