Spaces:
Sleeping
Sleeping
Download packages/ui/src/components/Button.tsx from Benaah/MicProtocol: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/spaces/Benaah/MicProtocol/resolve/main/packages/ui/src/components/Button.tsx
- Command line
-
hf download hf://spaces/Benaah/MicProtocol/packages/ui/src/components/Button.tsx
-
curl -L -o Button.tsx https://huggingface.co/spaces/Benaah/MicProtocol/resolve/main/packages/ui/src/components/Button.tsx
5.01 kB
| import React from "react"; | |
| import { cn } from "../lib/cn"; | |
| import { cva, type VariantProps } from "../lib/cva"; | |
| export type ButtonVariant = | |
| | "primary" | |
| | "secondary" | |
| | "outline" | |
| | "ghost" | |
| | "destructive" | |
| | "glow"; | |
| export type ButtonSize = "xs" | "sm" | "md" | "lg"; | |
| export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> { | |
| variant?: ButtonVariant; | |
| size?: ButtonSize; | |
| /** Show loading spinner and disable interactions */ | |
| loading?: boolean; | |
| /** Icon to render before children */ | |
| leftIcon?: React.ReactNode; | |
| /** Icon to render after children */ | |
| rightIcon?: React.ReactNode; | |
| /** Stretch button to full container width */ | |
| fullWidth?: boolean; | |
| /** Render as square icon-only button */ | |
| iconOnly?: boolean; | |
| } | |
| const buttonVariants = cva( | |
| "inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-[var(--radius-md)] border border-solid font-sans font-semibold no-underline transition-[background-color,border-color,color,box-shadow,transform] duration-150 ease-out disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-60 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-primary)] focus-visible:ring-offset-2 focus-visible:ring-offset-[var(--bg-primary)]", | |
| { | |
| variants: { | |
| variant: { | |
| primary: | |
| "bg-[var(--color-primary)] text-[var(--text-inverse)] border-[var(--color-primary)] shadow-[var(--shadow-sm)] hover:bg-[var(--color-primary-dark)] hover:border-[var(--color-primary-dark)]", | |
| secondary: | |
| "bg-[var(--bg-tertiary)] text-[var(--text-primary)] border-[var(--border-color)] hover:bg-[var(--bg-elevated)] hover:border-[var(--text-tertiary)]", | |
| outline: | |
| "bg-transparent text-[var(--text-primary)] border-[var(--border-color)] hover:bg-[var(--bg-elevated)] hover:border-[var(--text-tertiary)]", | |
| ghost: | |
| "bg-transparent text-[var(--text-secondary)] border-transparent hover:bg-[var(--bg-tertiary)] hover:text-[var(--text-primary)]", | |
| destructive: | |
| "bg-[color-mix(in srgb, var(--color-error) 10%, transparent)] text-[var(--color-error)] border-[color-mix(in srgb, var(--color-error) 25%, transparent)] hover:bg-[color-mix(in srgb, var(--color-error) 16%, transparent)]", | |
| glow: "bg-[var(--color-primary)] text-[var(--text-inverse)] border-[var(--color-primary)] shadow-[var(--shadow-glow)] hover:bg-[var(--color-primary-dark)]", | |
| }, | |
| size: { | |
| xs: "px-2 text-[var(--text-xs)] h-7 gap-1.5", | |
| sm: "px-3 text-[var(--text-sm)] h-8", | |
| md: "px-4 text-[var(--text-sm)] h-10", | |
| lg: "px-5 text-[var(--text-base)] h-12 gap-2.5", | |
| }, | |
| }, | |
| defaultVariants: { | |
| variant: "primary", | |
| size: "md", | |
| }, | |
| }, | |
| ); | |
| const iconOnlyStyles: Record<ButtonSize, string> = { | |
| xs: "px-0 h-[26px] w-[26px] aspect-square", | |
| sm: "px-0 h-[32px] w-[32px] aspect-square", | |
| md: "px-0 h-[40px] w-[40px] aspect-square", | |
| lg: "px-0 h-[48px] w-[48px] aspect-square", | |
| }; | |
| /** | |
| * MicProtocol Button — consistent sizing, restrained states, and clear focus. | |
| * Built on cva + cn, classes reference CSS custom properties from @micprotocol/design-tokens. | |
| */ | |
| export const Button = React.forwardRef< | |
| HTMLButtonElement, | |
| ButtonProps & VariantProps<typeof buttonVariants> | |
| >( | |
| ( | |
| { | |
| variant = "primary", | |
| size = "md", | |
| loading = false, | |
| leftIcon, | |
| rightIcon, | |
| fullWidth = false, | |
| iconOnly = false, | |
| className, | |
| disabled, | |
| children, | |
| ...props | |
| }, | |
| ref, | |
| ) => { | |
| const isActuallyDisabled = disabled || loading; | |
| const classes = cn( | |
| buttonVariants({ variant, size }), | |
| iconOnly ? iconOnlyStyles[size as ButtonSize] : "", | |
| fullWidth && "w-full", | |
| className, | |
| ); | |
| return ( | |
| <button | |
| ref={ref} | |
| className={classes} | |
| disabled={isActuallyDisabled} | |
| aria-busy={loading} | |
| {...props} | |
| > | |
| {loading ? ( | |
| <svg | |
| style={{ | |
| animation: "spin 1s linear infinite", | |
| width: "1em", | |
| height: "1em", | |
| flexShrink: 0, | |
| }} | |
| viewBox="0 0 24 24" | |
| fill="none" | |
| stroke="currentColor" | |
| strokeWidth="2.5" | |
| strokeLinecap="round" | |
| strokeLinejoin="round" | |
| aria-hidden="true" | |
| > | |
| <circle cx="12" cy="12" r="10" strokeOpacity="0.25" /> | |
| <path d="M12 2a10 10 0 0 1 10 10" /> | |
| </svg> | |
| ) : ( | |
| leftIcon && ( | |
| <span style={{ display: "inline-flex", flexShrink: 0 }}> | |
| {leftIcon} | |
| </span> | |
| ) | |
| )} | |
| {children && (!iconOnly || !loading) && <span>{children}</span>} | |
| {!loading && rightIcon && ( | |
| <span style={{ display: "inline-flex", flexShrink: 0 }}> | |
| {rightIcon} | |
| </span> | |
| )} | |
| </button> | |
| ); | |
| }, | |
| ); | |
| Button.displayName = "Button"; | |