feat: add alert component

This commit is contained in:
nafees nazik
2023-09-19 17:24:33 +05:30
parent 257b4b0490
commit 6f57ef03d1
4 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
import { faTriangleExclamation } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import type { Meta, StoryObj } from "@storybook/react";
import { Alert, AlertDescription, AlertTitle } from "./Alert";
const meta: Meta<typeof Alert> = {
title: "Components/Alert",
component: Alert,
tags: ["v2"]
};
export default meta;
type Story = StoryObj<typeof Alert>;
const ExampleComponent = () => (
<>
<AlertTitle>this is a title</AlertTitle>
<AlertDescription>this is a description</AlertDescription>
</>
);
export const Default: Story = {
args: {
children: <ExampleComponent />
}
};
export const Warning: Story = {
args: {
children: <ExampleComponent />,
variant: "warning"
}
};
export const Destructive: Story = {
args: {
children: <ExampleComponent />,
variant: "destructive"
}
};
export const WithIcon: Story = {
args: {
children: (
<>
<FontAwesomeIcon icon={faTriangleExclamation} />
<ExampleComponent />
</>
),
variant: "warning"
}
};

View File

@@ -0,0 +1,51 @@
import * as React from "react";
import { type VariantProps, cva } from "cva";
import { cn } from "@app/helpers/classNames";
const alertVariants = cva(
"relative w-full bg-mineshaft-800 rounded-lg border px-4 py-3 text-sm [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground [&>svg~*]:pl-7",
{
variants: {
variant: {
default: "",
destructive: "text-red border-red",
warning: "text-yellow border-yellow"
}
},
defaultVariants: {
variant: "default"
}
}
);
const Alert = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof alertVariants>
>(({ className, variant, ...props }, ref) => (
<div ref={ref} role="alert" className={cn(alertVariants({ variant }), className)} {...props} />
));
Alert.displayName = "Alert";
const AlertTitle = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLHeadingElement>>(
({ className, children, ...props }, ref) => (
<h5
ref={ref}
className={cn("mb-1 font-medium leading-none tracking-tight", className)}
{...props}
>
{children}
</h5>
)
);
AlertTitle.displayName = "AlertTitle";
const AlertDescription = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, ...props }, ref) => (
<div ref={ref} className={cn("text-sm [&_p]:leading-relaxed", className)} {...props} />
));
AlertDescription.displayName = "AlertDescription";
export { Alert, AlertDescription, AlertTitle };

View File

@@ -0,0 +1 @@
export { Alert, AlertDescription, AlertTitle } from "./Alert";

View File

@@ -1,4 +1,5 @@
export * from "./Accordion";
export * from "./Alert";
export * from "./Button";
export * from "./Card";
export * from "./Checkbox";