Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update button.tsx #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 71 additions & 10 deletions components/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import { cva, type VariantProps } from "class-variance-authority";

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


const buttonVariants = cva(
"inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
"inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 disabled:pointer-events-none disabled:opacity-50",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
destructive:
"bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline:
"border border-input bg-background hover:bg-accent hover:text-accent-foreground",
secondary:
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline: "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
Expand All @@ -27,31 +25,94 @@ const buttonVariants = cva(
xs: "h-7 rounded-md px-2",
},
},
compoundVariants: [
{
variant: "outline",
size: "icon",
className: "p-2", // Specific adjustment for outlined icon buttons.
},
],
defaultVariants: {
variant: "default",
size: "default",
},
}
);


export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean;
isLoading?: boolean;
loadingText?: string;
}


const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, asChild = false, ...props }, ref) => {
(
{
className,
variant,
size,
asChild = false,
isLoading = false,
loadingText,
disabled,
children,
...props
},
ref
) => {
const Comp = asChild ? Slot : "button";

return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
className={cn(
buttonVariants({ variant, size, className }),
isLoading && "cursor-wait opacity-75"
)}
ref={ref}
disabled={disabled || isLoading}
{...props}
/>
>
{isLoading ? (
<span className="flex items-center gap-2">
<span className="loader" aria-hidden="true" />
{loadingText || "Loading..."}
</span>
) : (
children
)}
</Comp>
);
}
);

Button.displayName = "Button";

export { Button, buttonVariants };


const loaderStyles = `
.loader {
width: 1rem;
height: 1rem;
border: 2px solid transparent;
border-top: 2px solid currentColor;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}

@keyframes spin {
to {
transform: rotate(360deg);
}
}
`;

if (typeof document !== "undefined") {
const style = document.createElement("style");
style.innerHTML = loaderStyles;
document.head.appendChild(style);
}