React MyUI - Modal

1. Design

Flexibility Through Props

The component is designed to be highly customizable through props:

1.1 Visual Customization
  • size: Provides 4 size variants (‘small‘ | ‘medium‘ | ‘large‘ | ‘full‘), allowing developers to choose the appropriate modal size for different use cases
  • animation: Supports 3 animation styles (‘scale‘ | ‘fade‘ | ‘slide‘), enhancing user experience with smooth transitions
  • className: Accepts custom className prop for style overrides and additional customization
1.2 Content Structure Flexibility
  • title: The title prop accepts ReactNode type, meaning it can be text, custom components, or even complex JSX
  • children: Uses the children pattern for maximum flexibility in modal body content
  • footer: Also accepts ReactNode for footer, allowing custom button layouts or any footer content
  • centeredFooter: Controls footer alignment (left-aligned vs centered)
1.3 Behavior Configuration
  • closeOnOutsideClick: Whether to close the modal when clicking outside
  • closeOnEscape: Whether to close the modal when pressing Escape key
  • showCloseButton: Whether to show the close button in the header
  • isOpen: Whether the modal is visible
  • onClose: Callback function when modal is closed

CSS Variable System

The component uses CSS variables for theming:

1
2
3
4
--color-background
--color-border
--color-text
--shadow-default

2. Implementation (css and tsx)

Modal.css
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
.myui-modal-overlay {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: flex;
align-items: center;
justify-content: center;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1000;
opacity: 0;
visibility: hidden;
transition: opacity 0.3s ease, visibility 0.3s ease;
}

.myui-modal-overlay--visible {
opacity: 1;
visibility: visible;
}

.myui-modal {
position: relative;
display: flex;
flex-direction: column;
max-height: calc(100vh - 2rem);
margin: 1rem;
background-color: var(--color-background);
border-radius: 8px;
box-shadow: var(--shadow-default);
opacity: 0;
transform: scale(0.95);
transition: opacity 0.3s ease, transform 0.3s ease;
overflow: hidden;
z-index: 1001;
}

.myui-modal--visible {
opacity: 1;
transform: scale(1);
}

.myui-modal--small {
width: 100%;
max-width: 400px;
}

.myui-modal--medium {
width: 100%;
max-width: 600px;
}

.myui-modal--large {
width: 100%;
max-width: 800px;
}

.myui-modal--full {
width: 100%;
max-width: 95vw;
height: 95vh;
max-height: 95vh;
}

.myui-modal__header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 1rem 1.25rem;
border-bottom: 1px solid var(--color-border);
}

.myui-modal__title {
margin: 0;
font-size: 1.25rem;
font-weight: 600;
color: var(--color-text);
}

.myui-modal__close {
display: flex;
align-items: center;
justify-content: center;
width: 2rem;
height: 2rem;
padding: 0;
border: none;
background: transparent;
color: var(--color-text);
cursor: pointer;
opacity: 0.7;
transition: opacity 0.2s ease;
}

.myui-modal__close:hover {
opacity: 1;
}

.myui-modal__body {
flex: 1;
padding: 1.25rem;
overflow-y: auto;
}

.myui-modal__footer {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 0.75rem;
padding: 1rem 1.25rem;
border-top: 1px solid var(--color-border);
}

/* Centered variant */
.myui-modal--centered .myui-modal__footer {
justify-content: center;
}

/* Animation variants */
.myui-modal--fade {
transform: scale(1);
}

.myui-modal--slide {
transform: translateY(20px);
}

.myui-modal--slide.myui-modal--visible {
transform: translateY(0);
}
Modal.tsx
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
import React, { useEffect, useRef } from 'react';
import './Modal.css';

export type ModalSize = 'small' | 'medium' | 'large' | 'full';
export type ModalAnimation = 'scale' | 'fade' | 'slide';

export interface ModalProps {
/**
* Whether the modal is visible
* @default false
*/
isOpen: boolean;

/**
* Callback when the modal is closed
*/
onClose: () => void;

/**
* Modal title displayed in the header
*/
title?: React.ReactNode;

/**
* Modal content
*/
children: React.ReactNode;

/**
* Footer content (usually buttons)
*/
footer?: React.ReactNode;

/**
* Modal size
* @default 'medium'
*/
size?: ModalSize;

/**
* Whether to close the modal when clicking outside
* @default true
*/
closeOnOutsideClick?: boolean;

/**
* Whether to close the modal when pressing Escape key
* @default true
*/
closeOnEscape?: boolean;

/**
* Center align the footer content
* @default false
*/
centeredFooter?: boolean;

/**
* Animation type
* @default 'scale'
*/
animation?: ModalAnimation;

/**
* Additional class name
*/
className?: string;

/**
* Whether to show the close button in the header
* @default true
*/
showCloseButton?: boolean;
}

export const Modal: React.FC<ModalProps> = ({
isOpen = false,
onClose,
title,
children,
footer,
size = 'medium',
closeOnOutsideClick = true,
closeOnEscape = true,
centeredFooter = false,
animation = 'scale',
className = '',
showCloseButton = true,
}) => {
const modalRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const handleEscapeKey = (event: KeyboardEvent) => {
if (isOpen && closeOnEscape && event.key === 'Escape') {
onClose();
}
};

document.addEventListener('keydown', handleEscapeKey);

// Lock body scroll when modal is open
if (isOpen) {
document.body.style.overflow = 'hidden';
}

return () => {
document.removeEventListener('keydown', handleEscapeKey);
document.body.style.overflow = '';
};
}, [isOpen, closeOnEscape, onClose]);

const handleOverlayClick = (event: React.MouseEvent) => {
if (closeOnOutsideClick && modalRef.current && !modalRef.current.contains(event.target as Node)) {
onClose();
}
};

const overlayClasses = [
'myui-modal-overlay',
isOpen ? 'myui-modal-overlay--visible' : '',
].filter(Boolean).join(' ');

const modalClasses = [
'myui-modal',
`myui-modal--${size}`,
`myui-modal--${animation}`,
isOpen ? 'myui-modal--visible' : '',
centeredFooter ? 'myui-modal--centered' : '',
className
].filter(Boolean).join(' ');

return (
<div className={overlayClasses} onClick={handleOverlayClick} aria-hidden={!isOpen}>
<div className={modalClasses} ref={modalRef} role="dialog" aria-modal={isOpen}>
{(title || showCloseButton) && (
<div className="myui-modal__header">
{title && <h2 className="myui-modal__title">{title}</h2>}
{showCloseButton && (
<button
type="button"
className="myui-modal__close"
onClick={onClose}
aria-label="Close"
>
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
</button>
)}
</div>
)}
<div className="myui-modal__body">
{children}
</div>
{footer && <div className="myui-modal__footer">{footer}</div>}
</div>
</div>
);
};

export default Modal;

3. Export Config

package/core/src/components/Modal/index.ts

1
2
export * from './Modal'
export { default } from './Modal'

packages/core/src/index.ts
Add

1
export * from './components/Modal';

4. StoryBook

Button.stories.tsx
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
import React, { useState } from 'react';
import { Meta, StoryObj } from '@storybook/react';
import { Modal } from '@hardyhu-ui/core';
import { Button } from '@hardyhu-ui/core';

const meta: Meta<typeof Modal> = {
title: 'Components/Modal',
component: Modal,
argTypes: {
isOpen: {
control: 'boolean',
description: 'Controls modal visibility',
table: {
type: { summary: 'boolean' },
defaultValue: { summary: 'false' },
},
},
size: {
control: { type: 'select' },
options: ['small', 'medium', 'large', 'full'],
description: 'Modal size',
table: {
type: { summary: 'string' },
defaultValue: { summary: 'medium' },
},
},
animation: {
control: { type: 'select' },
options: ['scale', 'fade', 'slide'],
description: 'Animation type',
table: {
type: { summary: 'string' },
defaultValue: { summary: 'scale' },
},
},
title: {
control: 'text',
description: 'Modal title',
},
closeOnOutsideClick: {
control: 'boolean',
description: 'Whether to close when clicking outside',
table: {
type: { summary: 'boolean' },
defaultValue: { summary: 'true' },
},
},
closeOnEscape: {
control: 'boolean',
description: 'Whether to close when pressing Escape key',
table: {
type: { summary: 'boolean' },
defaultValue: { summary: 'true' },
},
},
centeredFooter: {
control: 'boolean',
description: 'Center aligns footer content',
table: {
type: { summary: 'boolean' },
defaultValue: { summary: 'false' },
},
},
showCloseButton: {
control: 'boolean',
description: 'Show close button in header',
table: {
type: { summary: 'boolean' },
defaultValue: { summary: 'true' },
},
},
onClose: { action: 'closed' },
},
parameters: {
docs: {
description: {
component: 'A customizable modal component with different sizes and animations.',
},
},
},
};

export default meta;
type Story = StoryObj<typeof Modal>;

// Custom wrapper for interactive examples
const ModalExample = (args: any) => {
const [isOpen, setIsOpen] = useState(false);

return (
<>
<Button onClick={() => setIsOpen(true)}>Open Modal</Button>
<Modal
{...args}
isOpen={isOpen}
onClose={() => setIsOpen(false)}
footer={
<>
<Button variant="outline" onClick={() => setIsOpen(false)}>Cancel</Button>
<Button onClick={() => setIsOpen(false)}>Confirm</Button>
</>
}
>
{args.children}
</Modal>
</>
);
};

export const Basic: Story = {
render: (args) => (
<ModalExample {...args} title="Basic Modal">
<p>This is a basic modal with a title and footer buttons.</p>
<p>Click outside or press Escape to close it.</p>
</ModalExample>
),
};

export const Sizes: Story = {
render: () => (
<div style={{ display: 'flex', gap: '1rem' }}>
<ModalExample size="small" title="Small Modal">
<p>This is a small modal dialog.</p>
</ModalExample>

<ModalExample size="medium" title="Medium Modal">
<p>This is a medium modal dialog (default size).</p>
</ModalExample>

<ModalExample size="large" title="Large Modal">
<p>This is a large modal dialog with more space for content.</p>
</ModalExample>

<ModalExample size="full" title="Full Modal">
<p>This is a full-size modal that takes up most of the viewport.</p>
</ModalExample>
</div>
),
};

export const NoHeader: Story = {
render: () => (
<ModalExample showCloseButton={false}>
<p>This modal has no header or title.</p>
<p>You can close it by clicking outside or using the footer buttons.</p>
</ModalExample>
),
};

export const LongContent: Story = {
render: () => (
<ModalExample title="Modal with Scrollable Content">
<div>
<p>This modal contains long content that will scroll.</p>
{Array(20).fill(0).map((_, i) => (
<p key={i}>This is paragraph {i + 1} to demonstrate scrolling behavior.</p>
))}
</div>
</ModalExample>
),
};

export const CenteredFooter: Story = {
render: () => (
<ModalExample title="Centered Footer Buttons" centeredFooter={true}>
<p>This modal has its footer buttons centered instead of right-aligned.</p>
</ModalExample>
),
};

export const Animations: Story = {
render: () => (
<div style={{ display: 'flex', gap: '1rem' }}>
<ModalExample animation="scale" title="Scale Animation">
<p>This modal uses the default scale animation.</p>
</ModalExample>

<ModalExample animation="fade" title="Fade Animation">
<p>This modal uses a simple fade animation.</p>
</ModalExample>

<ModalExample animation="slide" title="Slide Animation">
<p>This modal slides up from below.</p>
</ModalExample>
</div>
),
};

export const CustomDialogExample: Story = {
render: () => (
<ModalExample
title="Confirm Action"
size="small"
footer={
<>
<Button variant="outline" onClick={() => alert('Action cancelled')}>No, Cancel</Button>
<Button variant="primary" onClick={() => alert('Action confirmed!')}>Yes, Proceed</Button>
</>
}
>
<div style={{ textAlign: 'center', padding: '1rem 0' }}>
<div style={{ fontSize: '3rem', marginBottom: '1rem' }}>⚠️</div>
<p style={{ fontWeight: 'bold' }}>Are you sure you want to proceed with this action?</p>
<p>This action cannot be undone.</p>
</div>
</ModalExample>
),
};

5. Jest


React MyUI - Modal
https://www.hardyhu.cn/2025/02/16/React-MyUI-Modal/
Author
John Doe
Posted on
February 16, 2025
Licensed under