LibreChat/packages/client/src/components/Button.tsx
Marco Beretta c640d80539
🪟 fix: Opaque Header Controls Over the Chat Gradient (#15894)
* 🪟 fix: Opaque Header Controls Over the Chat Gradient

The mobile sidebar toggle let the conversation show through itself: the chat
header is a gradient fading to transparent with messages scrolling under it,
and the shared `header-action` variant carried `bg-transparent` while every
other control in that row sits on `bg-presentation`.

* 🔒 fix: Keep an Explicit Button Shape Over the Header Compound

A compound variant is emitted after the `shape` recipe, so the `header-action`
+ `size: 'sm'` corner repair outranked a caller that asked for
`shape="theme"` or `shape="round"` — `cn` kept the later `rounded-xl` and
dropped the requested radius. Gate it on `shape: 'unset'`, the way the
`subtle` compound already is, so it only repairs the default geometry.

Adds the mobile header row's acceptance scenarios to the mock harness: the
toggle's interior does not change as the conversation scrolls under it, its
fill and corner match the new-chat and overflow controls beside it, and the
drawer's close toggle is the same control by fill, corner and tap target.
All three fail against the previous transparent fill.

* 🧪 test: Rewind the Header Scenario Before Scrolling It

The scroll scenario sampled the toggle from wherever a freshly opened
conversation happened to land. That is the app's business — the newest message
with auto-scroll on, the top of a seeded transcript without it — and a landed
end leaves a wheel delta in that direction nothing to move, which would fail the
"the conversation did not move" guard rather than measure the fill.

Rewind to the top first, then bring the transcript down under the header, so the
wheel always has a known distance to travel. The guard stays: a run where the
conversation does not move still fails instead of passing vacuously.

* 🧪 test: Read the Header Toggles With the Pointer Off Them

`click()` leaves the pointer on the opener's coordinates, and the drawer slides
its own toggle onto those same coordinates, so one of the pair could be reading
`hover:bg-surface-active-alt` while the other sat on `bg-presentation` — an
equality that depended on when the 300ms slide settled.

Move the pointer off both controls, wait for the drawer, and assert the pointer
really is clear: each surface now reports its own `:hover` state, so a run that
measures a hovered control fails on that rather than on the colour.
2026-09-14 00:58:37 +02:00

195 lines
8.1 KiB
TypeScript

import * as React from 'react';
import { Slot } from '@radix-ui/react-slot';
import { ClassProp } from 'class-variance-authority/types';
import { cva, type VariantProps } from 'class-variance-authority';
import { cn } from '~/utils';
type ButtonVariantOptions =
| ({
variant?:
| 'default'
| 'link'
| 'submit'
| 'outline'
| 'choice'
| 'subtle'
| 'destructive'
| 'secondary'
| 'ghost'
| 'row-action'
| 'section-header'
| 'section-action'
| 'header-action'
| null
| undefined;
size?:
| 'default'
| 'icon'
| 'icon-sm'
| 'icon-xs'
| 'icon-theme'
| 'sm'
| 'lg'
| 'theme'
| null
| undefined;
shape?: 'default' | 'theme' | 'round' | null | undefined;
} & ClassProp)
| undefined;
const buttonVariantRecipe = cva(
'inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-lg text-sm font-medium ring-offset-surface-primary transition-colors duration-theme-fast focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-text-primary focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50',
{
variants: {
variant: {
default: 'bg-surface-inverted text-text-inverted hover:bg-surface-inverted-hover',
destructive:
'bg-surface-destructive text-text-on-status hover:bg-surface-destructive-hover',
outline:
'text-text-primary border border-border-light bg-transparent hover:bg-surface-hover hover:text-text-primary',
/**
* A selectable answer inside a question card. `outline` is wrong here:
* its `border-light` edge measures ~1.2:1 against the panel these sit
* on, well under WCAG 1.4.11's 3:1 for a UI component boundary, so a
* column of choices reads as flat text rather than as controls. Carries
* its own fill so the answers are a different colour from the prompt,
* and drops to `font-normal` so the question above stays the heading.
*/
choice:
'border border-border-xheavy bg-surface-tertiary font-normal text-text-primary hover:bg-surface-hover hover:text-text-primary',
subtle:
'border border-border-light bg-transparent text-text-primary hover:bg-surface-secondary focus-visible:ring-text-primary focus-visible:ring-offset-0',
secondary: 'bg-surface-secondary text-text-primary hover:bg-surface-hover',
ghost: 'hover:bg-surface-hover hover:text-text-primary',
/**
* A compact action living inside a list row — a pinned row's unpin
* badge, a conversation's overflow trigger, a table row's controls. The
* rows stay `rounded-lg`; this sits one step inside them, so it
* overrides the base radius rather than matching its host.
*/
'row-action': 'rounded-md hover:bg-surface-hover-alt hover:text-text-primary',
link: 'text-text-primary underline-offset-4 hover:underline',
submit: 'bg-surface-submit text-text-on-status hover:bg-surface-submit-hover',
/**
* The toggle that heads a collapsible sidebar section, such as Chats,
* Projects and Pinned. It stays a quiet label rather than a control:
* no hover fill, because a heading that lights up competes with the
* rows it heads. Its ring is inset because these sit flush against the
* section body, and it carries its own metrics through the compound
* below, since a section heading is sized by its text.
*/
'section-header':
'justify-start gap-1 rounded-lg px-1 py-2 text-xs font-bold text-text-secondary focus-visible:ring-inset focus-visible:ring-offset-0',
/**
* A quiet icon action sitting beside a section heading in the sidebar.
* Unlike `row-action`, it recedes until hovered so the heading stays
* the thing being read, and its ring sits inside the control because
* these sit close enough that an offset one would cross a neighbour.
* One radius step inside the heading row, like every other control that
* sits on one.
*/
'section-action':
'rounded-md text-text-secondary hover:bg-surface-active-alt hover:text-text-primary focus-visible:ring-inset focus-visible:ring-offset-0',
/**
* A control floating on the presentation surface — the sidebar
* toggle in the chat header and its mirror in the mobile drawer
* header, so the pair reads as one persistent button across views.
* The fill is opaque and not transparent: the chat header is a
* gradient that fades to nothing while the conversation scrolls
* underneath, so a see-through control has message text moving
* through it, and every neighbour in that row — model selector, new
* chat, overflow menu — already sits on `bg-presentation`.
* `duration-0` makes the hover fill instant: these sit over a
* scrolling gradient, where the shared color transition reads as
* lag rather than polish.
*/
'header-action':
'rounded-xl border border-border-light bg-presentation text-text-primary duration-0 hover:bg-surface-active-alt hover:text-text-primary',
},
size: {
default: 'h-10 px-4 py-2',
sm: 'h-9 rounded-lg px-3',
lg: 'h-11 rounded-lg px-8',
icon: 'size-10',
'icon-sm': 'size-8 p-0',
'icon-xs': 'size-7',
/**
* A square icon control on the theme's control height — the size of
* every button in the composer's action row, for a control that has to
* line up with them.
*/
'icon-theme': 'size-theme-control p-0',
theme: 'h-theme-control gap-theme-compact px-theme-normal',
},
shape: {
default: 'rounded-lg',
theme: 'rounded-theme-control',
round: 'rounded-theme-control-round',
unset: '',
},
},
compoundVariants: [
{
variant: 'subtle',
shape: 'unset',
class: 'rounded-xl',
},
/* A section heading is sized by its own text, so it opts out of the
* default size recipe that every other caller supplies explicitly.
* Without this the default `h-10 px-4` is emitted after the variant and
* wins the merge, giving a 40px control in a 32px header row. */
{
variant: 'section-header',
size: 'default',
class: 'h-auto px-1 py-2',
},
/* `size: 'sm'` brings its own `rounded-lg`, emitted after the variant
* and so winning the merge. A text-bearing header control keeps the
* row's `rounded-xl` corner, matching the icon-sized ones beside it.
* Gated on `shape: 'unset'` like `subtle` above: a compound is emitted
* after the shape recipe, so an ungated one would silently outrank a
* caller that asked for `shape="theme"` or `shape="round"`. */
{
variant: 'header-action',
size: 'sm',
shape: 'unset',
class: 'rounded-xl',
},
],
defaultVariants: {
variant: 'default',
size: 'default',
shape: 'unset',
},
},
);
const buttonVariants: (props?: ButtonVariantOptions) => string = (props) =>
buttonVariantRecipe(
props == null ? props : { ...props, shape: props.shape == null ? 'unset' : props.shape },
);
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean;
}
const Button: React.ForwardRefExoticComponent<
ButtonProps & React.RefAttributes<HTMLButtonElement>
> = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, shape, asChild = false, type = 'button', ...props }, ref) => {
const Comp = asChild ? Slot : 'button';
return (
<Comp
type={asChild ? undefined : type}
className={cn(buttonVariants({ variant, size, shape, className }))}
ref={ref}
{...props}
/>
);
},
);
Button.displayName = 'Button';
export { Button, buttonVariants };