@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
1 lines • 9.76 kB
Source Map (JSON)
{"version":3,"file":"Alert.mjs","names":[],"sources":["../../../src/base-ui/Alert/Alert.tsx"],"sourcesContent":["'use client';\n\nimport { cx } from 'antd-style';\nimport { AlertTriangle, CheckCircle, ChevronRight, Info, X, XCircle } from 'lucide-react';\nimport { memo, useEffect, useRef, useState } from 'react';\n\nimport Icon from '@/Icon';\n\nimport {\n extraHeaderVariants,\n extraVariants,\n integratedVariants,\n rootVariants,\n styles,\n toneVariants,\n} from './style';\nimport type { AlertCloseConfig, AlertProps, AlertType } from './type';\n\nconst typeIcons = {\n error: XCircle,\n info: Info,\n secondary: AlertTriangle,\n success: CheckCircle,\n warning: AlertTriangle,\n} satisfies Record<AlertType, typeof Info>;\n\nconst Alert = memo<AlertProps>(\n ({\n action,\n afterClose,\n banner = false,\n className,\n classNames,\n closable = false,\n closeIcon,\n closeText,\n colorfulText = false,\n description,\n extra,\n extraDefaultExpand = false,\n extraIsolate = false,\n glass = false,\n icon,\n iconProps,\n message,\n onClose,\n ref,\n role = 'alert',\n rootClassName,\n showIcon = true,\n style,\n styles: customStyles,\n text,\n title,\n type = 'info',\n variant = 'soft',\n ...rest\n }) => {\n const [closed, setClosed] = useState(false);\n const [extraExpanded, setExtraExpanded] = useState(extraDefaultExpand);\n const resolvedTitle = title ?? message;\n const hasDescription = description !== undefined && description !== null;\n const integratedExtra = Boolean(extra && !extraIsolate);\n const closeConfig: AlertCloseConfig = typeof closable === 'object' ? closable : {};\n const isClosable = Boolean(closable);\n const {\n afterClose: configuredAfterClose,\n closeIcon: configuredCloseIcon,\n className: closeClassName,\n disabled: closeDisabled,\n onClose: configuredOnClose,\n style: closeStyle,\n ...closeButtonProps\n } = closeConfig;\n const afterCloseRef = useRef(configuredAfterClose ?? afterClose);\n afterCloseRef.current = configuredAfterClose ?? afterClose;\n\n useEffect(() => {\n if (closed) afterCloseRef.current?.();\n }, [closed]);\n\n if (closed) return null;\n\n const handleClose: NonNullable<AlertCloseConfig['onClose']> = (event) => {\n (configuredOnClose ?? onClose)?.(event);\n setClosed(true);\n };\n\n const root = (\n <div\n {...rest}\n data-alert-type={type}\n data-alert-variant={variant}\n ref={ref}\n role={role}\n className={cx(\n toneVariants({ type }),\n rootVariants({\n banner,\n colorfulText,\n glass: integratedExtra ? false : glass,\n hasDescription,\n hasExtra: integratedExtra,\n variant,\n }),\n classNames?.root,\n classNames?.alert,\n rootClassName,\n className,\n )}\n style={{\n ...style,\n ...customStyles?.root,\n ...customStyles?.alert,\n }}\n >\n {showIcon && (\n <span\n aria-hidden=\"true\"\n className={cx(styles.icon, classNames?.icon)}\n style={customStyles?.icon}\n >\n <Icon icon={icon ?? typeIcons[type]} size={hasDescription ? 20 : 18} {...iconProps} />\n </span>\n )}\n <div\n className={cx(styles.content, classNames?.section, classNames?.content)}\n style={{ ...customStyles?.section, ...customStyles?.content }}\n >\n {resolvedTitle !== undefined && resolvedTitle !== null && (\n <div\n style={customStyles?.title}\n className={cx(\n styles.title,\n hasDescription && styles.titleDetailed,\n classNames?.title,\n )}\n >\n {resolvedTitle}\n </div>\n )}\n {hasDescription && (\n <div\n className={cx(styles.description, classNames?.description)}\n style={customStyles?.description}\n >\n {description}\n </div>\n )}\n </div>\n {action && (\n <div\n className={cx(styles.action, styles.wrappedAction, classNames?.action)}\n style={customStyles?.action}\n >\n {action}\n </div>\n )}\n {isClosable && (\n <button\n {...closeButtonProps}\n aria-label={closeButtonProps['aria-label'] ?? 'Close alert'}\n className={cx(styles.close, classNames?.close, closeClassName)}\n disabled={closeDisabled}\n style={{ ...customStyles?.close, ...closeStyle }}\n type=\"button\"\n onClick={handleClose}\n >\n {configuredCloseIcon ?? closeIcon ?? closeText ?? <X size={14} />}\n </button>\n )}\n </div>\n );\n\n if (!extra) return root;\n\n if (extraIsolate) {\n return (\n <div\n className={cx(styles.container, toneVariants({ type }), classNames?.container)}\n style={{ gap: 8, ...customStyles?.container }}\n >\n {root}\n {extra}\n </div>\n );\n }\n\n return (\n <div\n style={customStyles?.container}\n className={cx(\n styles.container,\n toneVariants({ type }),\n integratedVariants({ banner, glass, variant }),\n classNames?.container,\n )}\n >\n {root}\n <div\n className={cx(extraVariants({ banner, variant }), classNames?.extra)}\n style={customStyles?.extra}\n >\n <details\n open={extraExpanded}\n onToggle={(event) => setExtraExpanded(event.currentTarget.open)}\n >\n <summary\n className={cx(extraHeaderVariants({ variant }), classNames?.extraHeader)}\n style={customStyles?.extraHeader}\n >\n <ChevronRight\n aria-hidden=\"true\"\n className={cx(styles.extraIndicator, classNames?.extraIndicator)}\n size={14}\n style={customStyles?.extraIndicator}\n />\n <span>{text?.detail ?? 'Show Details'}</span>\n </summary>\n <div\n className={cx(styles.extraContent, classNames?.extraContent)}\n style={customStyles?.extraContent}\n >\n {extra}\n </div>\n </details>\n </div>\n </div>\n );\n },\n);\n\nAlert.displayName = 'BaseAlert';\n\nexport default Alert;\n"],"mappings":";;;;;;;;AAkBA,MAAM,YAAY;CAChB,OAAO;CACP,MAAM;CACN,WAAW;CACX,SAAS;CACT,SAAS;AACX;AAEA,MAAM,QAAQ,MACX,EACC,QACA,YACA,SAAS,OACT,WACA,YACA,WAAW,OACX,WACA,WACA,eAAe,OACf,aACA,OACA,qBAAqB,OACrB,eAAe,OACf,QAAQ,OACR,MACA,WACA,SACA,SACA,KACA,OAAO,SACP,eACA,WAAW,MACX,OACA,QAAQ,cACR,MACA,OACA,OAAO,QACP,UAAU,QACV,GAAG,WACC;CACJ,MAAM,CAAC,QAAQ,aAAa,SAAS,KAAK;CAC1C,MAAM,CAAC,eAAe,oBAAoB,SAAS,kBAAkB;CACrE,MAAM,gBAAgB,SAAS;CAC/B,MAAM,iBAAiB,gBAAgB,KAAA,KAAa,gBAAgB;CACpE,MAAM,kBAAkB,QAAQ,SAAS,CAAC,YAAY;CACtD,MAAM,cAAgC,OAAO,aAAa,WAAW,WAAW,CAAC;CACjF,MAAM,aAAa,QAAQ,QAAQ;C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