Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

feat(dify-ui): add Alert (#45266)

yyh93891d061673
diff --git a/packages/dify-ui/README.md b/packages/dify-ui/README.md
index c261d398d06..5d2ee873ead 100644
--- a/packages/dify-ui/README.md
+++ b/packages/dify-ui/README.md
@@ -39,7 +39,7 @@ Import `styles.css` once from the consumer's root stylesheet or entrypoint.
| Actions | `./button`, `./icon-button`, `./toggle` |
| Controls | `./segmented-control` |
| Display | `./collapsible`, `./kbd` |
-| Feedback | `./meter`, `./progress`, `./spinner`, `./status-dot`, `./toast` |
+| Feedback | `./alert`, `./meter`, `./progress`, `./spinner`, `./status-dot`, `./toast` |
| Form | `./form`, `./field`, `./fieldset`, `./input`, `./input-group`, `./textarea`, `./checkbox`, `./checkbox-group`, `./radio-group`, `./number-field`, `./select`, `./slider`, `./switch` |
| Layout | `./scroll-area`, `./separator` |
| Media | `./avatar` |
diff --git a/packages/dify-ui/package.json b/packages/dify-ui/package.json
index 2d6cebf25e2..b0413c240fd 100644
--- a/packages/dify-ui/package.json
+++ b/packages/dify-ui/package.json
@@ -25,6 +25,10 @@
"types": "./src/merge-props.ts",
"import": "./src/merge-props.ts"
},
+ "./alert": {
+ "types": "./src/alert/index.tsx",
+ "import": "./src/alert/index.tsx"
+ },
"./alert-dialog": {
"types": "./src/alert-dialog/index.tsx",
"import": "./src/alert-dialog/index.tsx"
diff --git a/packages/dify-ui/src/alert/__tests__/index.spec.tsx b/packages/dify-ui/src/alert/__tests__/index.spec.tsx
new file mode 100644
index 00000000000..495d0810e88
--- /dev/null
+++ b/packages/dify-ui/src/alert/__tests__/index.spec.tsx
@@ -0,0 +1,35 @@
+import { render } from 'vitest-browser-react'
+import { Alert, AlertTitle } from '../index'
+
+describe('Alert', () => {
+ it('interrupts only for errors by default', async () => {
+ const screen = await render(
+ <div>
+ <Alert type="error">
+ <AlertTitle>Could not save the connection.</AlertTitle>
+ </Alert>
+ <Alert type="warning">
+ <AlertTitle>This connection was updated by someone else.</AlertTitle>
+ </Alert>
+ </div>,
+ )
+
+ await expect
+ .element(screen.getByRole('alert'))
+ .toHaveTextContent('Could not save the connection.')
+ await expect
+ .element(screen.getByRole('status'))
+ .toHaveTextContent('This connection was updated by someone else.')
+ })
+
+ it('lets a static banner opt out of the live region', async () => {
+ const screen = await render(
+ <Alert type="error" role="note">
+ <AlertTitle>Unavailable</AlertTitle>
+ </Alert>,
+ )
+
+ await expect.element(screen.getByRole('note')).toHaveTextContent('Unavailable')
+ expect(screen.container.querySelector('[role="alert"]')).toBeNull()
+ })
+})
diff --git a/packages/dify-ui/src/alert/index.stories.tsx b/packages/dify-ui/src/alert/index.stories.tsx
new file mode 100644
index 00000000000..79d229dd706
--- /dev/null
+++ b/packages/dify-ui/src/alert/index.stories.tsx
@@ -0,0 +1,115 @@
+import type { Meta, StoryObj } from '@storybook/react-vite'
+import * as React from 'react'
+import { expect, within } from 'storybook/test'
+import { Alert, AlertActions, AlertClose, AlertDescription, AlertTitle } from '.'
+import { Button } from '../button'
+
+const meta = {
+ title: 'Base/UI/Alert',
+ component: Alert,
+ parameters: {
+ layout: 'padded',
+ docs: {
+ description: {
+ component:
+ 'An in-page status message aligned with the Dify Design Kit Alert. `type` sets the status icon, the background halo, and the default role: `alert` for errors and `status` otherwise; pass `role` when an error is already present on page load and should not interrupt. `AlertTitle` is always present; add `AlertDescription` for detail and `AlertActions` for buttons below the text. `AlertClose` is a stateless close button with a required `aria-label`; the caller decides whether the alert stays hidden. The icon cannot be replaced, so a banner that needs a product or business icon is not an Alert.',
+ },
+ },
+ },
+ tags: ['autodocs'],
+ args: {
+ type: 'info',
+ },
+ argTypes: {
+ type: {
+ control: 'select',
+ options: ['info', 'success', 'warning', 'error'],
+ },
+ },
+ decorators: [
+ (Story) => (
+ <div className="w-[360px]">
+ <Story />
+ </div>
+ ),
+ ],
+} satisfies Meta<typeof Alert>
+
+export default meta
+type Story = StoryObj<typeof meta>
+
+export const Default: Story = {
+ render: (args) => (
+ <Alert {...args}>
+ <AlertTitle>Web app settings apply to all environments.</AlertTitle>
+ </Alert>
+ ),
+}
+
+export const Types: Story = {
+ render: () => (
+ <div className="flex flex-col gap-3">
+ <Alert type="info">
+ <AlertTitle>Some logs in this time range may have been archived.</AlertTitle>
+ </Alert>
+ <Alert type="success">
+ <AlertTitle>The connection is ready.</AlertTitle>
+ </Alert>
+ <Alert type="warning">
+ <AlertTitle>This connection was updated by someone else.</AlertTitle>
+ </Alert>
+ <Alert type="error">
+ <AlertTitle>Could not save the connection.</AlertTitle>
+ </Alert>
+ </div>
+ ),
+}
+
+export const WithDescription: Story = {
+ render: () => (
+ <Alert type="warning">
+ <AlertTitle>This connection was updated by someone else.</AlertTitle>
+ <AlertDescription>Review the latest configuration before saving.</AlertDescription>
+ </Alert>
+ ),
+}
+
+export const WithActions: Story = {
+ render: () => (
+ <Alert type="warning">
+ <AlertTitle>Current draft will be overwritten.</AlertTitle>
+ <AlertDescription>Export the workflow as a backup before importing.</AlertDescription>
+ <AlertActions>
+ <Button size="small" variant="primary">
+ Backup current draft
+ </Button>
+ <Button size="small">Import anyway</Button>
+ </AlertActions>
+ </Alert>
+ ),
+}
+
+function DismissibleAlert() {
+ const [visible, setVisible] = React.useState(true)
+
+ if (!visible) return <Button onClick={() => setVisible(true)}>Show again</Button>
+
+ return (
+ <Alert type="info">
+ <AlertTitle>
+ This is a sample app with limited trial messages. To keep using it, create your own copy.
+ </AlertTitle>
+ <AlertClose aria-label="Dismiss" onClick={() => setVisible(false)} />
+ </Alert>
+ )
+}
+
+export const Dismissible: Story = {
+ render: () => <DismissibleAlert />,
+ play: async ({ canvasElement, userEvent }) => {
+ const canvas = within(canvasElement)
+
+ await userEvent.click(canvas.getByRole('button', { name: 'Dismiss' }))
+ await expect(canvas.queryByRole('status')).not.toBeInTheDocument()
+ },
+}
diff --git a/packages/dify-ui/src/alert/index.tsx b/packages/dify-ui/src/alert/index.tsx
new file mode 100644
index 00000000000..e14b8172e27
--- /dev/null
+++ b/packages/dify-ui/src/alert/index.tsx
@@ -0,0 +1,117 @@
+'use client'
+
+import type * as React from 'react'
+import type { IconButtonProps } from '../icon-button'
+import { cn } from '../cn'
+import { IconButton } from '../icon-button'
+import { mergeProps } from '../merge-props'
+import { useRender } from '../use-render'
+
+type AlertProps = React.ComponentProps<'div'> & {
+ type: 'info' | 'success' | 'warning' | 'error'
+}
+type AlertTitleProps = useRender.ComponentProps<'div'>
+type AlertDescriptionProps = useRender.ComponentProps<'div'>
+type AlertActionsProps = React.ComponentProps<'div'>
+type AlertCloseProps = Omit<
+ IconButtonProps,
+ 'children' | 'size' | 'variant' | 'tone' | 'aria-label' | 'aria-labelledby'
+> & {
+ 'aria-label': string
+}
+
+function Alert({ type, className, children, role, ...props }: AlertProps) {
+ return (
+ <div
+ role={role ?? (type === 'error' ? 'alert' : 'status')}
+ data-type={type}
+ className={cn(
+ 'group/alert relative flex items-start gap-0.5 overflow-hidden rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur p-2 shadow-xs shadow-shadow-shadow-3 backdrop-blur-[5px]',
+ className,
+ )}
+ {...props}
+ >
+ {/* Same type-to-halo and type-to-icon mapping as Toast; change both together. */}
+ <div
+ aria-hidden="true"
+ className={cn(
+ 'absolute -inset-[0.5px] bg-linear-108 to-background-gradient-mask-transparent opacity-40',
+ 'group-data-[type=info]/alert:from-components-badge-status-light-normal-halo',
+ 'group-data-[type=success]/alert:from-components-badge-status-light-success-halo',
+ 'group-data-[type=warning]/alert:from-components-badge-status-light-warning-halo',
+ 'group-data-[type=error]/alert:from-components-badge-status-light-error-halo',
+ )}
+ />
+ <div className="relative flex shrink-0 items-center justify-center p-1">
+ <span
+ aria-hidden="true"
+ className={cn(
+ 'size-4',
+ 'group-data-[type=info]/alert:i-ri-information-2-fill group-data-[type=info]/alert:text-text-accent',
+ 'group-data-[type=success]/alert:i-ri-checkbox-circle-fill group-data-[type=success]/alert:text-text-success',
+ 'group-data-[type=warning]/alert:i-ri-alert-fill group-data-[type=warning]/alert:text-text-warning-secondary',
+ 'group-data-[type=error]/alert:i-ri-error-warning-fill group-data-[type=error]/alert:text-text-destructive',
+ )}
+ />
+ </div>
+ <div className="relative flex min-w-0 flex-1 flex-col gap-0.5 py-1 has-data-dify-alert-close:pe-6.5">
+ {children}
+ </div>
+ </div>
+ )
+}
+
+function AlertTitle({ className, render, ...props }: AlertTitleProps) {
+ return useRender({
+ defaultTagName: 'div',
+ render,
+ props: mergeProps<'div'>(
+ {
+ className: cn('system-xs-medium wrap-break-word text-text-primary', className),
+ },
+ props,
+ ),
+ })
+}
+
+function AlertDescription({ className, render, ...props }: AlertDescriptionProps) {
+ return useRender({
+ defaultTagName: 'div',
+ render,
+ props: mergeProps<'div'>(
+ {
+ className: cn('system-xs-regular wrap-break-word text-text-secondary', className),
+ },
+ props,
+ ),
+ })
+}
+
+function AlertActions({ className, ...props }: AlertActionsProps) {
+ return (
+ <div className={cn('flex flex-wrap items-start gap-1 pt-1 pb-0.5', className)} {...props} />
+ )
+}
+
+function AlertClose({ className, 'aria-label': ariaLabel, ...props }: AlertCloseProps) {
+ return (
+ <IconButton
+ aria-label={ariaLabel}
+ data-dify-alert-close=""
+ size="md"
+ className={cn('absolute inset-e-0 top-0', className)}
+ {...props}
+ >
+ <span aria-hidden="true" className="i-ri-close-line size-4" />
+ </IconButton>
+ )
+}
+
+export { Alert, AlertActions, AlertClose, AlertDescription, AlertTitle }
+export type {
+ AlertActionsProps,
+ AlertCloseProps,
+ AlertDescriptionProps,
+ AlertProps,
+ AlertTitleProps,
+}
diff --git a/packages/dify-ui/src/toast/index.tsx b/packages/dify-ui/src/toast/index.tsx
index 1217d41bc8b..1bc626e415a 100644
--- a/packages/dify-ui/src/toast/index.tsx
+++ b/packages/dify-ui/src/toast/index.tsx
@@ -162,6 +162,7 @@ function ToastCard({ toast: toastItem }: { toast: ToastObject<ToastData> }) {
)}
>
<div className="relative h-full overflow-hidden rounded-xl border border-components-panel-border bg-components-panel-bg-blur shadow-lg shadow-shadow-shadow-5 backdrop-blur-[5px]">
+ {/* Same type-to-halo and type-to-icon mapping as Alert; change both together. */}
<div
aria-hidden="true"
className={cn(
diff --git a/packages/tooling/lint.config.ts b/packages/tooling/lint.config.ts
index d0864232ca5..b24623dd055 100644
--- a/packages/tooling/lint.config.ts
+++ b/packages/tooling/lint.config.ts
@@ -314,6 +314,24 @@ export const stylingContractRules = {
message:
'"{{className}}" is outside the InfotipContent styling contract. Use the default hint surface, spacing, typography, and wrapping from packages/dify-ui/src/infotip/index.tsx; callers may constrain width or preserve content line breaks.',
},
+ {
+ pattern: '^Alert$',
+ allow: ['layout'],
+ message:
+ '"{{className}}" is outside the Alert styling contract. The alert owns its surface, spacing, icon and typography; callers may only place and size it.',
+ },
+ {
+ pattern: '^(AlertTitle|AlertDescription)$',
+ allow: ['whitespace-pre-line'],
+ message:
+ '"{{className}}" is outside the {{component}} styling contract. Use its default typography and colour; callers may keep line breaks in the message.',
+ },
+ {
+ pattern: '^(AlertActions|AlertClose)$',
+ allow: [],
+ message:
+ '"{{className}}" is outside the {{component}} styling contract. The part owns its spacing and position.',
+ },
{
pattern: '^(Dialog|AlertDialog)Content$',
allow: [