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

186 lines
5.2 KiB
TypeScript

import { forwardRef } from 'react';
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/week-picker';
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 (forwardRef — np. do scrollowania okna listy w wątku komunikatora) */
export const Card = forwardRef<HTMLDivElement, { className?: string; children: ReactNode }>(
({ className, children }, ref) => (
<UICard ref={ref} 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>
);
}
/* Compact labeled input for dense grids (set rows) */
export function MiniInput({
label,
value,
onChange,
disabled,
placeholder,
inputMode,
}: {
label: string;
value: string;
onChange: (v: string) => void;
disabled?: boolean;
placeholder?: string;
inputMode?: 'numeric' | 'decimal';
}) {
return (
<label className="block min-w-0">
<span className="mb-1 block text-[10.5px] font-bold uppercase tracking-[0.11em] text-dim">
{label}
</span>
<input
value={value}
onChange={(e) => onChange(e.target.value)}
disabled={disabled}
placeholder={placeholder}
inputMode={inputMode}
className="w-full rounded-[10px] border border-line bg-input px-2.5 py-1.5 text-[0.9rem] text-ink shadow-(--inset-shadow) focus:border-accent-text focus:ring-[3px] focus:ring-accent/18 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50"
/>
</label>
);
}
/* 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} />;
}