zasuiv0.1
GitHub
Menú

Botones

Button

Botón con las variantes de color, contorno y tamaño de ZAS Express.

Dependencias: @radix-ui/react-slot class-variance-authority

Sólidos

Contorno

Otros

Tamaños

Solo icono

Con icono

Animación grow

Como enlace (asChild)

Instalación

Con la CLI de zasui (ver Introducción): instala el componente con sus dependencias y queda listo para usar.

pnpm dlx @medialityc/zasui add button
Ver el código
import * as React from 'react';
import { Slot } from '@radix-ui/react-slot';
import { cva, type VariantProps } from 'class-variance-authority';

import { cn } from '@/lib/utils';

// Base y variantes sacadas de `.btn-*` de zasexpress (components.css) y de su ui/button de shadcn.
const buttonVariants = cva(
	"relative inline-flex shrink-0 items-center justify-center gap-2 rounded-md border border-transparent text-sm font-semibold whitespace-nowrap transition duration-300 outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-60 aria-invalid:border-destructive aria-invalid:ring-destructive/20 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
	{
		variants: {
			variant: {
				default: 'border-primary bg-primary text-white shadow-[0_10px_20px_-10px] shadow-primary/60 hover:shadow-none',
				secondary: 'border-secondary bg-secondary text-white shadow-[0_10px_20px_-10px] shadow-secondary/60 hover:shadow-none',
				success: 'border-success bg-success text-white shadow-[0_10px_20px_-10px] shadow-success/60 hover:shadow-none',
				danger: 'border-danger bg-danger text-white shadow-[0_10px_20px_-10px] shadow-danger/60 hover:shadow-none',
				warning: 'border-warning bg-warning text-white shadow-[0_10px_20px_-10px] shadow-warning/60 hover:shadow-none',
				info: 'border-info bg-info text-white shadow-[0_10px_20px_-10px] shadow-info/60 hover:shadow-none',
				dark: 'border-dark bg-dark text-white shadow-[0_10px_20px_-10px] shadow-dark/60 hover:shadow-none',
				'outline-primary': 'border-primary bg-background text-primary hover:bg-primary hover:text-white',
				'outline-secondary': 'border-secondary bg-background text-secondary hover:bg-secondary hover:text-white',
				'outline-success': 'border-success bg-background text-success hover:bg-success hover:text-white',
				'outline-danger': 'border-danger bg-background text-danger hover:bg-danger hover:text-white',
				'outline-warning': 'border-warning bg-background text-warning hover:bg-warning hover:text-white',
				'outline-info': 'border-info bg-background text-info hover:bg-info hover:text-white',
				'outline-dark': 'border-dark bg-background text-dark hover:bg-dark hover:text-white dark:border-white-dark dark:text-white-dark',
				outline: 'border-border bg-background shadow-xs hover:bg-accent hover:text-accent-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50',
				ghost: 'hover:bg-accent hover:text-accent-foreground dark:hover:bg-accent/50',
				link: 'text-primary underline-offset-4 hover:underline',
				gradient: 'bg-linear-to-r from-[#ff6201] to-[#ebb902] text-white hover:from-[#ebb902] hover:to-[#ff6201]',
			},
			size: {
				default: 'px-5 py-2',
				sm: 'gap-1.5 px-2.5 py-1.5 text-xs',
				lg: 'px-7 py-2.5 text-base',
				icon: 'size-9',
				'icon-sm': 'size-8',
				'icon-lg': 'size-10',
			},
			// `grow`: animación de los botones del header de zasexpress («Crear Orden», «Soporte»).
			effect: {
				none: '',
				grow: 'shadow-sm hover:scale-105 hover:shadow-md focus-visible:ring-4',
			},
		},
		defaultVariants: {
			variant: 'default',
			size: 'default',
			effect: 'none',
		},
	},
);

function Button({
	className,
	variant,
	size,
	effect,
	asChild = false,
	type = 'button',
	...props
}: React.ComponentProps<'button'> &
	VariantProps<typeof buttonVariants> & {
		asChild?: boolean;
	}) {
	const Comp = asChild ? Slot : 'button';

	return (
		<Comp
			data-slot='button'
			type={asChild ? undefined : type}
			className={cn(buttonVariants({ variant, size, effect, className }))}
			{...props}
		/>
	);
}

export { Button, buttonVariants };