feat(webui): improve shared control ergonomics
Raise shared button and form-control defaults to provide clearer, more consistent interaction targets across the interface. Guarantee 44-pixel touch targets and 16-pixel form text on narrow screens, preserve explicit desktop size overrides, and prevent mobile browser focus zoom. Replace broad transitions with targeted press feedback, respect reduced-motion preferences, and allow callers to opt out without overriding their persistent transforms.
This commit is contained in:
@@ -4,7 +4,7 @@ import { cva, type VariantProps } from 'class-variance-authority';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
const buttonVariants = cva(
|
||||
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-all disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 [&_svg]:shrink-0 outline-none focus-visible:ring-[3px] focus-visible:ring-ring/40 focus-visible:border-ring cursor-pointer",
|
||||
"ui-button inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-[color,background-color,border-color,box-shadow,scale,opacity] duration-150 ease-out disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 [&_svg]:shrink-0 outline-none focus-visible:ring-[3px] focus-visible:ring-ring/40 focus-visible:border-ring cursor-pointer",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
@@ -16,25 +16,43 @@ const buttonVariants = cva(
|
||||
link: 'text-primary underline-offset-4 hover:underline',
|
||||
},
|
||||
size: {
|
||||
default: 'h-9 px-4 py-2',
|
||||
sm: 'h-8 rounded-md px-3 text-xs',
|
||||
lg: 'h-10 rounded-md px-6',
|
||||
icon: 'size-9',
|
||||
'icon-sm': 'size-8',
|
||||
default: 'h-10 px-4 py-2',
|
||||
sm: 'h-10 rounded-md px-3 text-xs',
|
||||
lg: 'h-12 rounded-md px-6',
|
||||
icon: 'ui-button-icon size-10',
|
||||
'icon-sm': 'ui-button-icon size-10',
|
||||
},
|
||||
press: {
|
||||
true: 'active:scale-[0.96] motion-reduce:active:scale-100',
|
||||
false: '',
|
||||
},
|
||||
},
|
||||
defaultVariants: { variant: 'default', size: 'default' },
|
||||
defaultVariants: { variant: 'default', size: 'default', press: true },
|
||||
}
|
||||
);
|
||||
|
||||
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement>, VariantProps<typeof buttonVariants> {
|
||||
type ButtonVariantProps = Omit<VariantProps<typeof buttonVariants>, 'press'>;
|
||||
|
||||
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement>, ButtonVariantProps {
|
||||
asChild?: boolean;
|
||||
/** Disable tactile press scaling when motion would distract from the action. */
|
||||
static?: boolean;
|
||||
}
|
||||
|
||||
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
|
||||
({ className, variant, size, asChild = false, ...props }, ref) => {
|
||||
({ className, variant, size, asChild = false, static: isStatic = false, ...props }, ref) => {
|
||||
const Comp = asChild ? Slot : 'button';
|
||||
return <Comp className={cn(buttonVariants({ variant, size }), className)} ref={ref} {...props} />;
|
||||
return (
|
||||
<Comp
|
||||
className={cn(
|
||||
buttonVariants({ variant, size, press: !isStatic }),
|
||||
className,
|
||||
)}
|
||||
ref={ref}
|
||||
data-press-scale={isStatic ? undefined : ''}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
);
|
||||
Button.displayName = 'Button';
|
||||
|
||||
@@ -6,8 +6,9 @@ const Input = React.forwardRef<HTMLInputElement, React.InputHTMLAttributes<HTMLI
|
||||
<input
|
||||
type={type}
|
||||
ref={ref}
|
||||
data-ui-control=""
|
||||
className={cn(
|
||||
'flex h-9 w-full min-w-0 rounded-md border border-border bg-transparent px-3 py-1 text-sm shadow-xs transition-[color,box-shadow] outline-none',
|
||||
'flex h-10 w-full min-w-0 rounded-md border border-border bg-transparent px-3 py-1 text-sm shadow-xs transition-[color,box-shadow] outline-none',
|
||||
'file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium',
|
||||
'placeholder:text-muted-foreground',
|
||||
'focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/40',
|
||||
|
||||
@@ -10,8 +10,9 @@ const Select = React.forwardRef<HTMLSelectElement, React.SelectHTMLAttributes<HT
|
||||
<div className="relative w-full">
|
||||
<select
|
||||
ref={ref}
|
||||
data-ui-control=""
|
||||
className={cn(
|
||||
'flex h-9 w-full min-w-0 appearance-none rounded-md border border-border bg-transparent px-3 py-1 pr-8 text-sm shadow-xs transition-[color,box-shadow] outline-none',
|
||||
'flex h-10 w-full min-w-0 appearance-none rounded-md border border-border bg-transparent px-3 py-1 pr-8 text-sm shadow-xs transition-[color,box-shadow] outline-none',
|
||||
'focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/40',
|
||||
'disabled:cursor-not-allowed disabled:opacity-50',
|
||||
className
|
||||
|
||||
@@ -8,8 +8,9 @@ const Textarea = React.forwardRef<HTMLTextAreaElement, React.TextareaHTMLAttribu
|
||||
({ className, ...props }, ref) => (
|
||||
<textarea
|
||||
ref={ref}
|
||||
data-ui-control=""
|
||||
className={cn(
|
||||
'flex w-full min-w-0 rounded-md border border-border bg-transparent px-3 py-2 text-sm shadow-xs transition-[color,box-shadow] outline-none',
|
||||
'flex min-h-10 w-full min-w-0 rounded-md border border-border bg-transparent px-3 py-2 text-sm shadow-xs transition-[color,box-shadow] outline-none',
|
||||
'placeholder:text-muted-foreground',
|
||||
'focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/40',
|
||||
'disabled:cursor-not-allowed disabled:opacity-50',
|
||||
|
||||
@@ -280,6 +280,36 @@ html[data-reduce-motion="1"] *::after {
|
||||
scroll-behavior: auto !important;
|
||||
}
|
||||
|
||||
/* Press feedback is useful motion, but never essential. The app-level reduce
|
||||
switch and the OS preference both disable it without changing hit areas. */
|
||||
html[data-reduce-motion="1"] :is(.ui-button, [data-press-scale]) {
|
||||
scale: 1 !important;
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
:is(.ui-button, [data-press-scale]) { scale: 1 !important; }
|
||||
}
|
||||
|
||||
/* Keep shared form controls at 16px on narrow touch screens even when a page
|
||||
supplies a smaller desktop typography utility. This prevents iOS Safari
|
||||
from zooming the viewport when the control receives focus. */
|
||||
@media (max-width: 767px) {
|
||||
.ui-button {
|
||||
min-width: max(2.75rem, 44px);
|
||||
min-height: max(2.75rem, 44px);
|
||||
}
|
||||
.ui-button-icon { min-width: max(2.75rem, 44px); }
|
||||
.topbar-control {
|
||||
width: 44px;
|
||||
min-width: 44px;
|
||||
height: 44px;
|
||||
min-height: 44px;
|
||||
}
|
||||
[data-ui-control] {
|
||||
min-height: max(2.75rem, 44px);
|
||||
font-size: max(1rem, 16px);
|
||||
}
|
||||
}
|
||||
|
||||
/* 关闭全部动效: harder than reduce — remove CSS animations outright (e.g. the
|
||||
pulsing status dots) rather than just collapsing their duration. Framer
|
||||
entrance fades are gated in components on `disableMotion`. */
|
||||
|
||||
Reference in New Issue
Block a user