Spaces:
Build error
Build error
File size: 7,415 Bytes
0bfe2e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
import { cva, VariantProps } from 'class-variance-authority';
import * as React from 'react';
import { cn, ComponentAnatomy, defineStyleAnatomy } from '../core/styling';
/* -------------------------------------------------------------------------------------------------
* Anatomy
* -----------------------------------------------------------------------------------------------*/
export const AlertAnatomy = defineStyleAnatomy({
root: cva(
['UI-Alert__root', 'py-3 px-4 flex justify-between rounded-[--radius]'],
{
variants: {
intent: {
info: 'bg-blue-50 text-blue-500 dark:bg-opacity-10 dark:text-blue-200',
success:
'bg-green-50 text-green-500 dark:bg-opacity-10 dark:text-green-200',
warning:
'bg-orange-50 text-orange-500 dark:bg-opacity-10 dark:text-orange-200',
alert: 'bg-red-50 text-red-500 dark:bg-opacity-10 dark:text-red-200',
'info-basic':
'bg-white text-gray-800 border dark:bg-gray-800 dark:text-gray-200',
'success-basic':
'bg-white text-gray-800 border dark:bg-gray-800 dark:text-gray-200',
'warning-basic':
'bg-white text-gray-800 border dark:bg-gray-800 dark:text-gray-200',
'alert-basic':
'bg-white text-gray-800 border dark:bg-gray-800 dark:text-gray-200',
},
},
defaultVariants: {
intent: 'info',
},
}
),
detailsContainer: cva(['UI-Alert__detailsContainer', 'flex']),
textContainer: cva([
'UI-Alert__textContainer',
'flex flex-col self-center ml-3 gap-.5',
]),
title: cva(['UI-Alert__title', 'font-bold']),
description: cva(['UI-Alert__description']),
icon: cva(['UI-Alert__icon', 'text-2xl content-evenly'], {
variants: {
intent: {
'info-basic': 'text-blue-500',
'success-basic': 'text-green-500',
'warning-basic': 'text-orange-500',
'alert-basic': 'text-red-500',
info: 'text-blue-500',
success: 'text-green-500',
warning: 'text-orange-500',
alert: 'text-red-500',
},
},
defaultVariants: {
intent: 'info-basic',
},
}),
closeButton: cva([
'UI-Alert__closeButton',
'flex-none self-start text-2xl hover:opacity-50 transition ease-in cursor-pointer h-5 w-5',
]),
});
/* -------------------------------------------------------------------------------------------------
* Alert
* -----------------------------------------------------------------------------------------------*/
export type AlertProps = React.ComponentPropsWithRef<'div'> &
VariantProps<typeof AlertAnatomy.root> &
ComponentAnatomy<typeof AlertAnatomy> & {
/**
* The title of the alert
*/
title?: string;
/**
* The description text or content of the alert
*/
description?: React.ReactNode;
/**
* Replace the default icon with a custom icon
*
* - `iconClass` does not apply to custom icons
*/
icon?: React.ReactNode;
/**
* If true, a close button will be rendered
*/
isClosable?: boolean;
/**
* Callback invoked when the close button is clicked
*/
onClose?: () => void;
};
export const Alert = React.forwardRef<HTMLDivElement, AlertProps>(
(props, ref) => {
const {
children,
className,
title,
description,
isClosable,
onClose,
intent = 'info-basic',
iconClass,
detailsContainerClass,
textContainerClass,
titleClass,
descriptionClass,
closeButtonClass,
icon,
...rest
} = props;
let Icon: any = null;
if (intent === 'info-basic' || intent === 'info') {
Icon = (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<circle cx="12" cy="12" r="10"></circle>
<path d="M12 16v-4"></path>
<path d="M12 8h.01"></path>
</svg>
);
} else if (intent === 'alert-basic' || intent === 'alert') {
Icon = (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<circle cx="12" cy="12" r="10"></circle>
<line x1="12" x2="12" y1="8" y2="12"></line>
<line x1="12" x2="12.01" y1="16" y2="16"></line>
</svg>
);
} else if (intent === 'warning-basic' || intent === 'warning') {
Icon = (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z"></path>
<line x1="12" x2="12" y1="9" y2="13"></line>
<line x1="12" x2="12.01" y1="17" y2="17"></line>
</svg>
);
} else if (intent === 'success-basic' || intent === 'success') {
Icon = (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2 2 6.477 2 12s4.477 10 10 10z"></path>
<path d="m9 12 2 2 4-4"></path>
</svg>
);
}
return (
<div
className={cn(AlertAnatomy.root({ intent }), className)}
{...rest}
ref={ref}
>
<div
className={cn(AlertAnatomy.detailsContainer(), detailsContainerClass)}
>
{icon ? (
icon
) : (
<div
className={cn(AlertAnatomy.icon({ intent: intent }), iconClass)}
>
{Icon && Icon}
</div>
)}
<div className={cn(AlertAnatomy.textContainer(), textContainerClass)}>
<span className={cn(AlertAnatomy.title(), titleClass)}>
{title}
</span>
{!!(description || children) && (
<div className={cn(AlertAnatomy.description(), descriptionClass)}>
{description || children}
</div>
)}
</div>
</div>
{onClose && (
<button
className={cn(AlertAnatomy.closeButton(), closeButtonClass)}
onClick={onClose}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<line x1="18" x2="6" y1="6" y2="18"></line>
<line x1="6" x2="18" y1="6" y2="18"></line>
</svg>
</button>
)}
</div>
);
}
);
Alert.displayName = 'Alert';
|