Skip to content

Commit b962a17

Browse files
author
James Crowson
committed
New interface.
1 parent 74a26c4 commit b962a17

File tree

3 files changed

+105
-24
lines changed

3 files changed

+105
-24
lines changed

examples/src/BasicExample.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@ import Modali, { useModali } from '../../src';
77
import Button from "./Button";
88

99
const BasicExample = () => {
10-
const [exampleModal, toggleExampleModal] = useModali();
10+
const [exampleModal, toggleExampleModal] = useModali({
11+
animated: true,
12+
title: 'Are you sure?',
13+
message: 'Deleting this user will be permanent.',
14+
buttons: [
15+
<Modali.Button
16+
label="Cancel"
17+
isStyleCancel
18+
onClick={() => toggleExampleModal()}
19+
/>,
20+
<Modali.Button
21+
label="Delete"
22+
isStyleDestructive
23+
onClick={() => toggleExampleModal()}
24+
/>,
25+
],
26+
});
1127
return (
1228
<div className="row mt-5">
1329
<div className="col-12">
@@ -23,15 +39,7 @@ const BasicExample = () => {
2339
<div className="col-12">
2440
<SyntaxHighlighter language='jsx' style={okaidia}>{basicExample}</SyntaxHighlighter>
2541
</div>
26-
<Modali {...exampleModal}>
27-
<div className="row my-3">
28-
<div className="col-12 d-flex justify-content-center">
29-
<h3>
30-
I'm a basic Modal Dialog 👍
31-
</h3>
32-
</div>
33-
</div>
34-
</Modali>
42+
<Modali {...exampleModal} />
3543
</div>
3644
)
3745
};

src/index.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import React, { useState, useEffect } from 'react';
22
import ReactDOM from 'react-dom';
33
import './modali.css';
44

5+
const Button = ({ onClick, label, isStyleDefault, isStyleCancel, isStyleDestructive }) => (
6+
<button
7+
className={`modali-button ${isStyleCancel && 'modali-button-cancel'} ${isStyleDefault && 'modali-button-default'} ${isStyleDestructive && 'modali-button-destructive'}`}
8+
onClick={onClick}
9+
>
10+
{label}
11+
</button>
12+
);
13+
514
const Modali = ({ isShown, hide, options, children }) => {
615

716
function handleOverlayClicked(e) {
@@ -20,6 +29,33 @@ const Modali = ({ isShown, hide, options, children }) => {
2029
}
2130
}
2231

32+
function renderBody() {
33+
if (children) {
34+
return children;
35+
} else if (options !== undefined && options.message !== undefined) {
36+
return (
37+
<div className="modali-body-style">
38+
{options.message}
39+
</div>
40+
);
41+
}
42+
}
43+
44+
function renderFooter() {
45+
const { buttons } = options;
46+
return (
47+
<div className="modali-footer">
48+
{buttons.map((button, index) => (
49+
<React.Fragment
50+
key={`modali-button-${index}`}
51+
>
52+
{button}
53+
</React.Fragment>
54+
))}
55+
</div>
56+
);
57+
}
58+
2359
return isShown ? ReactDOM.createPortal(
2460
<React.Fragment>
2561
<div className="modali-overlay"/>
@@ -28,21 +64,28 @@ const Modali = ({ isShown, hide, options, children }) => {
2864
<div className="modali-content">
2965
{options !== undefined && options.closeButton === false ? null : (
3066
<div className="modali-header">
67+
{options !== undefined && options.title !== undefined && (
68+
<div className="modali-title">
69+
{options.title}
70+
</div>
71+
)}
3172
<button type="button" className="modali-close-button" data-dismiss="modal" aria-label="Close" onClick={hide}>
3273
<span aria-hidden="true">&times;</span>
3374
</button>
3475
</div>
3576
)}
3677
<div className="modali-body">
37-
{children}
78+
{renderBody()}
3879
</div>
80+
{options !== undefined && options.buttons !== undefined && options.buttons.length > 0 && renderFooter()}
3981
</div>
4082
</div>
4183
</div>
4284
</React.Fragment>, document.body
4385
) : null;
4486
};
4587

88+
Modali.Button = Button;
4689
export default Modali;
4790

4891
export const useModali = (options) => {

src/modali.css

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,26 @@
3636

3737
.modali-header {
3838
display: flex;
39-
-ms-flex-align: start;
40-
align-items: flex-start;
41-
-ms-flex-pack: justify;
42-
justify-content: space-between;
43-
padding: 1rem 1rem;
44-
border-top-left-radius: .3rem;
45-
border-top-right-radius: .3rem;
39+
align-items: start;
40+
padding: 1.5rem 1.5rem .5rem 1.5rem;
4641
}
4742

48-
.modali-header .modali-close-button {
49-
padding: 1rem 1rem;
50-
margin: -1rem -1rem -1rem auto;
43+
.modali-header .modali-title {
44+
font-weight: 700;
45+
font-size: 1.2rem;
5146
}
5247

5348
.modali-close-button {
49+
padding: 1rem;
50+
margin: -1rem -1rem -1rem auto;
5451
font-size: 1.4rem;
5552
font-weight: 700;
5653
line-height: 1;
5754
color: #000;
5855
opacity: .3;
5956
cursor: pointer;
6057
border: none;
61-
outline: 0;
58+
outline: 0 !important;
6259
}
6360

6461
.modali-close-button:hover {
@@ -68,15 +65,23 @@
6865

6966
.modali-body {
7067
position: relative;
71-
display: -ms-flexbox;
7268
display: flex;
73-
-ms-flex-direction: column;
7469
flex-direction: column;
7570
width: 100%;
7671
background-color: #fff;
7772
border-radius: .3rem;
7873
}
7974

75+
.modali-body-style {
76+
padding: .5rem 1.5rem;
77+
}
78+
79+
.modali-footer {
80+
padding: 1rem 1.5rem;
81+
display: flex;
82+
justify-content: flex-end;
83+
}
84+
8085
.modali-open {
8186
overflow: hidden;
8287
}
@@ -91,6 +96,31 @@
9196
max-width: 800px;
9297
}
9398

99+
/* Button Classes */
100+
101+
.modali-button {
102+
font-size: .9rem;
103+
font-weight: 700;
104+
border: none;
105+
border-radius: 3px;
106+
padding: .3rem 1rem;
107+
margin-left: .5rem;
108+
}
109+
110+
.modali-button-cancel {
111+
background: #fff;
112+
color: #000;
113+
}
114+
115+
.modali-button-destructive {
116+
background: #FF1654;
117+
color: #fff;
118+
}
119+
120+
.modali-button-default {
121+
background: #247BA0;
122+
color: #fff;
123+
}
94124

95125
/* Animation Classes */
96126

0 commit comments

Comments
 (0)