Skip to content

Commit 2fcd1aa

Browse files
committed
refactor(alert): replace action prop with Alert.Actions compound component
- Remove action prop from Alert.Root - Add Alert.Actions subcomponent for action button area (below text, col-start-2) - Consistent with Dialog.Footer / Sheet.Footer compound component pattern - Update example and tests
1 parent 6e90670 commit 2fcd1aa

3 files changed

Lines changed: 24 additions & 24 deletions

File tree

examples/nextjs-app/src/modules/pages/primitives-demo.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,15 @@ export const primitiveComponentsDemoResource = defineResource({
200200
<Alert.Title>With callback</Alert.Title>
201201
<Alert.Description>Dismiss fires a callback.</Alert.Description>
202202
</Alert.Root>
203-
<Alert.Root
204-
variant="warning"
205-
action={
206-
<div style={{ display: "flex", gap: "0.5rem" }}>
207-
<Button size="sm" variant="outline">
208-
Dismiss
209-
</Button>
210-
<Button size="sm">Action</Button>
211-
</div>
212-
}
213-
>
203+
<Alert.Root variant="warning" dismissible>
214204
<Alert.Title>Multiple actions</Alert.Title>
215205
<Alert.Description>This alert has multiple action buttons.</Alert.Description>
206+
<Alert.Actions>
207+
<Button size="sm" variant="outline">
208+
Dismiss
209+
</Button>
210+
<Button size="sm">Action</Button>
211+
</Alert.Actions>
216212
</Alert.Root>
217213
</div>
218214
</div>

packages/core/src/components/alert.test.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ describe("Alert", () => {
106106
expect(onDismiss).toHaveBeenCalledOnce();
107107
});
108108

109-
it("renders custom action", () => {
109+
it("renders actions area", () => {
110110
render(
111-
<Alert.Root action={<button type="button">Undo</button>}>
111+
<Alert.Root>
112112
<Alert.Title>Action</Alert.Title>
113+
<Alert.Actions>
114+
<button type="button">Undo</button>
115+
</Alert.Actions>
113116
</Alert.Root>,
114117
);
115118
expect(screen.getByRole("button", { name: "Undo" })).toBeDefined();

packages/core/src/components/alert.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,13 @@ const variantIcons: Record<
4545

4646
type RootProps = React.ComponentProps<"div"> &
4747
VariantProps<typeof alertVariants> & {
48-
action?: React.ReactNode;
4948
dismissible?: boolean;
5049
onDismiss?: () => void;
5150
};
5251

5352
function Root({
5453
className,
5554
variant = "neutral",
56-
action,
5755
dismissible,
5856
onDismiss,
5957
children,
@@ -78,14 +76,6 @@ function Root({
7876
>
7977
<Icon />
8078
{children}
81-
{action && (
82-
<div
83-
data-slot="alert-action"
84-
className="astw:col-start-2 astw:flex astw:items-center astw:gap-2 astw:mt-2"
85-
>
86-
{action}
87-
</div>
88-
)}
8979
{dismissible && (
9080
<button
9181
data-slot="alert-dismiss"
@@ -130,6 +120,17 @@ function Description({ className, ...props }: React.ComponentProps<"div">) {
130120
}
131121
Description.displayName = "Alert.Description";
132122

123+
function Actions({ className, ...props }: React.ComponentProps<"div">) {
124+
return (
125+
<div
126+
data-slot="alert-actions"
127+
className={cn("astw:col-start-2 astw:flex astw:items-center astw:gap-2 astw:mt-2", className)}
128+
{...props}
129+
/>
130+
);
131+
}
132+
Actions.displayName = "Alert.Actions";
133+
133134
export type AlertProps = RootProps;
134-
export const Alert = { Root, Title, Description };
135+
export const Alert = { Root, Title, Description, Actions };
135136
export { alertVariants };

0 commit comments

Comments
 (0)