Files
FitOS/admin/src/components/ui.tsx

147 lines
4.0 KiB
TypeScript

import type {
ButtonHTMLAttributes,
InputHTMLAttributes,
ReactNode,
SelectHTMLAttributes,
TextareaHTMLAttributes,
} from 'react';
import { cva } from 'class-variance-authority';
import { cn } from '../lib/utils';
import { Button as UIButton } from './ui/button';
import { Card as UICard } from './ui/card';
import { Badge as UIBadge } from './ui/badge';
import { Input as UIInput } from './ui/input';
import { Textarea as UITextarea } from './ui/textarea';
import { Label as UILabel } from './ui/label';
import { Field as UIField } from './ui/field';
import { Skeleton as UISkeleton } from './ui/skeleton';
import { Progress as UIProgress } from './ui/progress';
import { ErrorText as UIErrorText, SuccessText as UISuccessText } from './ui/alert';
export * from './ui/typography';
export * from './ui/content';
export * from './ui/dialogs';
export * from './ui/theme-toggle';
export * from './ui/language-toggle';
/* Button */
export type ButtonVariant = 'primary' | 'secondary' | 'danger' | 'ghost';
export type ButtonSize = 'md' | 'sm';
export function Button({
variant,
size,
className,
...props
}: ButtonHTMLAttributes<HTMLButtonElement> & { variant?: ButtonVariant; size?: ButtonSize }) {
return <UIButton variant={variant} size={size} className={className} {...props} />;
}
/* Card */
export function Card({ className, children }: { className?: string; children: ReactNode }) {
return <UICard className={className}>{children}</UICard>;
}
/* Badge */
export type BadgeColor = 'green' | 'gray' | 'red' | 'yellow' | 'blue';
export function Badge({
color,
className,
children,
}: {
color?: BadgeColor;
className?: string;
children: ReactNode;
}) {
return (
<UIBadge color={color} className={className}>
{children}
</UIBadge>
);
}
/* Form fields */
export type FieldSize = 'md' | 'sm';
const FIELD_BASE =
'w-full bg-input border border-line rounded-[10px] text-ink font-[inherit] shadow-(--inset-shadow) transition-shadow duration-150 focus:outline-none focus:border-accent-text focus:ring-[3px] focus:ring-accent/18 disabled:cursor-not-allowed disabled:opacity-50';
const FIELD_SIZES: Record<FieldSize, string> = {
md: 'h-10 px-3 text-[0.95rem]',
// matches the height of <Button size="sm"> (header / context menu)
sm: 'h-8 px-3 text-[0.875rem]',
};
export function Input({
size = 'md',
className,
...props
}: Omit<InputHTMLAttributes<HTMLInputElement>, 'size'> & { size?: FieldSize }) {
return <UIInput size={size} className={className} {...props} />;
}
const selectVariants = cva(FIELD_BASE, {
variants: { size: FIELD_SIZES },
defaultVariants: { size: 'md' },
});
export function Select({
size = 'md',
className,
children,
...props
}: Omit<SelectHTMLAttributes<HTMLSelectElement>, 'size'> & { size?: FieldSize }) {
return (
<select data-slot="select" className={cn(selectVariants({ size }), className)} {...props}>
{children}
</select>
);
}
export function Textarea({
size = 'md',
className,
...props
}: TextareaHTMLAttributes<HTMLTextAreaElement> & { size?: FieldSize }) {
return <UITextarea size={size} className={className} {...props} />;
}
export function Label({ children, className }: { children: ReactNode; className?: string }) {
return <UILabel className={className}>{children}</UILabel>;
}
export function Field({
label,
children,
className,
}: {
label: string;
children: ReactNode;
className?: string;
}) {
return (
<UIField label={label} className={className}>
{children}
</UIField>
);
}
/* Text helpers */
export function ErrorText({ children }: { children: ReactNode }) {
return <UIErrorText>{children}</UIErrorText>;
}
export function SuccessText({ children }: { children: ReactNode }) {
return <UISuccessText>{children}</UISuccessText>;
}
/* Skeleton */
export function Skeleton({ className = '' }: { className?: string }) {
return <UISkeleton className={className} />;
}
/* Progress bar */
export function ProgressBar({ percent, className = '' }: { percent: number; className?: string }) {
return <UIProgress percent={percent} className={className} />;
}