Skip to content

Commit 0756496

Browse files
committed
Update AlertDialog component to manage open state
1 parent c0fa2c4 commit 0756496

File tree

8 files changed

+47
-25
lines changed

8 files changed

+47
-25
lines changed

src/components/ui/AlertDialog/AlertDialog.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import AlertDialogRoot from './shards/AlertDialogRoot';
33
import AlertDialogContent from './shards/AlertDialogContent';
44
import AlertDialogTrigger from './shards/AlertDialogTrigger';
@@ -10,9 +10,10 @@ export type AlertDialogProps = {
1010
content: React.ReactNode;
1111
}
1212

13-
const AlertDialog = ({ children, content } : AlertDialogProps) => {
13+
const AlertDialog = ({ children, open, onOpenChange, content } : AlertDialogProps) => {
14+
const [isOpen, setIsOpen] = useState(open);
1415
return (
15-
<AlertDialogRoot>
16+
<AlertDialogRoot open={isOpen} onOpenChange={onOpenChange}>
1617
<AlertDialogTrigger>
1718
{children}
1819
</AlertDialogTrigger>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import { createContext } from 'react';
22

3-
export const AlertDialogContext = createContext({});
3+
export const AlertDialogContext = createContext({
4+
5+
});

src/components/ui/AlertDialog/shards/AlertDialogCancel.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export type AlertDialogCancelProps = {
77
}
88

99
const AlertDialogCancel = ({ children } : AlertDialogCancelProps) => {
10-
const { setOpen } = useContext(AlertDialogContext);
10+
const { handleOpenChange } = useContext(AlertDialogContext);
1111
return (
12-
<ButtonPrimitive onClick={() => setOpen(false)}>
12+
<ButtonPrimitive onClick={() => handleOpenChange(false)}>
1313
{children}
1414
</ButtonPrimitive>
1515
);

src/components/ui/AlertDialog/shards/AlertDialogContent.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export type AlertDialogContentProps = {
66
}
77

88
const AlertDialogContent = ({ children } : AlertDialogContentProps) => {
9-
const { open } = useContext(AlertDialogContext);
9+
const { isOpen } = useContext(AlertDialogContext);
1010

1111
return (
1212
<>
13-
{open && (
13+
{isOpen && (
1414
<div className="alert-content fixed left-1/2 top-1/2 z-50 -translate-x-1/2 -translate-y-1/2 bg-gray-900/50">
1515
{children}
1616
</div>

src/components/ui/AlertDialog/shards/AlertDialogOverlay.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import Floater from '~/core/primitives/Floater';
33
import { AlertDialogContext } from '../contexts/AlertDialogContext';
44

55
const AlertDialogOverlay = () => {
6-
const { open } = useContext(AlertDialogContext);
6+
const { isOpen } = useContext(AlertDialogContext);
77
return (
88
<>
9-
{open && (
9+
{isOpen && (
1010
<Floater.Overlay className="bg-black/50 relative z-50">
1111

1212
</Floater.Overlay>

src/components/ui/AlertDialog/shards/AlertDialogRoot.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@ import Floater from '~/core/primitives/Floater';
77
export type AlertDialogRootProps = {
88
children: React.ReactNode;
99
customRootClass?: string;
10+
open: boolean;
11+
onOpenChange: (open: boolean) => void;
1012
}
1113

1214
const COMPONENT_NAME = 'AlertDialog';
1315

14-
const AlertDialogRoot = ({ children, customRootClass = '' } : AlertDialogRootProps) => {
16+
const AlertDialogRoot = ({ children, customRootClass = '', open, onOpenChange } : AlertDialogRootProps) => {
1517
const { context: floaterContext } = Floater.useFloating();
1618
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);
17-
const [open, setOpen] = useState(false);
19+
const [isOpen, setIsOpen] = useState(open);
1820

19-
const props = { open, setOpen, floaterContext };
21+
const handleOpenChange = (open: boolean) => {
22+
setIsOpen(open);
23+
onOpenChange(open);
24+
};
25+
26+
const props = { isOpen, handleOpenChange, floaterContext };
2027
return (
2128
<AlertDialogContext.Provider value={props}>
2229
<div className={rootClass}>

src/components/ui/AlertDialog/shards/AlertDialogTrigger.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ export type AlertDialogTriggerProps = {
88
}
99

1010
const AlertDialogTrigger = ({ children, ...props } : AlertDialogTriggerProps) => {
11-
const { floaterContext } = useContext(AlertDialogContext);
12-
const { open, setOpen } = useContext(AlertDialogContext);
11+
const { isOpen, handleOpenChange, floaterContext } = useContext(AlertDialogContext);
1312

1413
return (
15-
<ButtonPrimitive onClick={() => setOpen(true)} {...props}>
14+
<ButtonPrimitive onClick={() => handleOpenChange(true)} {...props}>
1615
{children}
1716
</ButtonPrimitive>
1817
);

src/components/ui/AlertDialog/stories/AlertDialog.stories.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22

33
import AlertDialog from '../AlertDialog';
44
import SandboxEditor from '~/components/tools/SandboxEditor/SandboxEditor';
@@ -7,14 +7,27 @@ import SandboxEditor from '~/components/tools/SandboxEditor/SandboxEditor';
77
export default {
88
title: 'UI/Data-Display/AlertDialog',
99
component: AlertDialog,
10-
render: (args:any) => <SandboxEditor>
11-
<AlertDialog
12-
{...args} content={
13-
<div className="flex flex-col gap-4 text-gray-100">
14-
<h1>This is content</h1>
15-
</div>
16-
} />
17-
</SandboxEditor>
10+
render: (args:any) => {
11+
const [isOpen, setIsOpen] = useState(false);
12+
13+
const handleOpenChange = (open: boolean) => {
14+
console.log('open', open);
15+
setIsOpen(open);
16+
};
17+
18+
return (
19+
<SandboxEditor>
20+
<AlertDialog
21+
open={isOpen}
22+
onOpenChange={handleOpenChange}
23+
{...args} content={
24+
<div className="flex flex-col gap-4 text-gray-100">
25+
<h1>This is content</h1>
26+
</div>
27+
} />
28+
</SandboxEditor>
29+
);
30+
}
1831
};
1932

2033
// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args

0 commit comments

Comments
 (0)