|
| 1 | +import { mergeProps } from "@base-ui/react/merge-props"; |
| 2 | +import { Select as SelectPrimitive } from "@base-ui/react/select"; |
| 3 | +import { useRender } from "@base-ui/react/use-render"; |
1 | 4 | import type { ComponentProps } from "react"; |
2 | 5 | import { ChevronDownIcon, type LucideIcon } from "lucide-react"; |
3 | 6 |
|
4 | 7 | import { cn } from "~/lib/utils"; |
5 | | -import { Button } from "../ui/button"; |
6 | | -import { SelectTrigger } from "../ui/select"; |
7 | 8 | import { Separator } from "../ui/separator"; |
8 | 9 |
|
9 | 10 | export type ComposerControlSize = "sm" | "xs"; |
10 | 11 |
|
11 | | -type ComposerControlProps = Omit<ComponentProps<typeof Button>, "size"> & { |
12 | | - size?: ComposerControlSize; |
13 | | -}; |
| 12 | +/** |
| 13 | + * The composer toolbar's control look. `sm` is the expanded toolbar; `xs` is the dimmer resting |
| 14 | + * strip. `aria-pressed` marks a toggle that is on (plan mode). This is an app control, not a |
| 15 | + * restyled Button, so it owns its classes. |
| 16 | + */ |
| 17 | +function composerControlClassName(size: ComposerControlSize, className?: string) { |
| 18 | + return cn( |
| 19 | + "relative inline-flex shrink-0 cursor-pointer items-center justify-center whitespace-nowrap rounded-[var(--control-radius)] border border-transparent text-base outline-none hover:bg-accent data-pressed:bg-accent focus-visible:ring-2 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 [&_svg:not([class*='text-'])]:text-[var(--control-icon-color)]", |
| 20 | + size === "xs" |
| 21 | + ? "h-7 gap-1 px-[calc(--spacing(2)-1px)] font-normal text-muted-foreground/70 text-sm [--control-icon-color:currentColor] 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" |
| 22 | + : "h-7 gap-1.5 px-2.5 font-medium text-secondary-label [--control-icon-color:var(--contrast-muted-foreground)] hover:text-foreground sm:text-sm [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4", |
| 23 | + "aria-pressed:bg-accent aria-pressed:text-accent-foreground aria-pressed:hover:bg-accent/80", |
| 24 | + className, |
| 25 | + ); |
| 26 | +} |
14 | 27 |
|
15 | | -type ComposerSelectControlProps = Omit<ComponentProps<typeof SelectTrigger>, "size"> & { |
| 28 | +type ComposerControlProps = useRender.ComponentProps<"button"> & { |
16 | 29 | size?: ComposerControlSize; |
17 | 30 | }; |
18 | 31 |
|
19 | | -const composerControlClassName = |
20 | | - "rounded-[var(--control-radius)] text-secondary-label transition-none hover:text-foreground [&_svg[data-composer-control-chevron]]:-mx-0.5 [&_svg[data-composer-control-icon]]:mx-0"; |
21 | | -const expandedComposerControlClassName = "h-7 min-h-7 gap-1.5 px-2.5"; |
22 | | -const restingComposerControlClassName = |
23 | | - "[--control-icon-color:currentColor] font-normal text-muted-foreground/70 hover:text-foreground/80 [&_svg[data-composer-control-chevron]]:-me-1 [&_svg[data-composer-control-chevron]]:ms-0"; |
24 | | - |
25 | 32 | export function ComposerControl({ |
26 | 33 | className, |
27 | 34 | size = "sm", |
28 | | - variant = "ghost", |
| 35 | + render, |
29 | 36 | ...props |
30 | 37 | }: ComposerControlProps) { |
31 | | - return ( |
32 | | - <Button |
33 | | - className={cn( |
34 | | - composerControlClassName, |
35 | | - size === "xs" ? restingComposerControlClassName : expandedComposerControlClassName, |
36 | | - className, |
37 | | - )} |
38 | | - size={size} |
39 | | - variant={variant} |
40 | | - {...props} |
41 | | - /> |
42 | | - ); |
| 38 | + const defaultProps = { |
| 39 | + className: composerControlClassName(size, className), |
| 40 | + type: render ? undefined : ("button" as const), |
| 41 | + }; |
| 42 | + return useRender({ |
| 43 | + defaultTagName: "button", |
| 44 | + props: mergeProps<"button">(defaultProps, props), |
| 45 | + render, |
| 46 | + }); |
43 | 47 | } |
44 | 48 |
|
45 | 49 | export function ComposerControlIcon({ |
@@ -105,21 +109,19 @@ export function ComposerControlSeparator({ |
105 | 109 |
|
106 | 110 | export function ComposerSelectControl({ |
107 | 111 | className, |
| 112 | + children, |
108 | 113 | size = "sm", |
109 | | - variant = "ghost", |
110 | 114 | ...props |
111 | | -}: ComposerSelectControlProps) { |
| 115 | +}: Omit<SelectPrimitive.Trigger.Props, "className"> & { |
| 116 | + className?: string | undefined; |
| 117 | + size?: ComposerControlSize; |
| 118 | +}) { |
112 | 119 | return ( |
113 | | - <SelectTrigger |
114 | | - className={cn( |
115 | | - composerControlClassName, |
116 | | - size === "xs" ? restingComposerControlClassName : expandedComposerControlClassName, |
117 | | - className, |
118 | | - )} |
119 | | - icon={<ComposerControlChevron size={size} />} |
120 | | - size={size} |
121 | | - variant={variant} |
122 | | - {...props} |
123 | | - /> |
| 120 | + <SelectPrimitive.Trigger className={composerControlClassName(size, className)} {...props}> |
| 121 | + {children} |
| 122 | + <SelectPrimitive.Icon> |
| 123 | + <ComposerControlChevron size={size} /> |
| 124 | + </SelectPrimitive.Icon> |
| 125 | + </SelectPrimitive.Trigger> |
124 | 126 | ); |
125 | 127 | } |
0 commit comments