Skip to content

Commit 11fb489

Browse files
author
Adiatma Kamarudin
authored
Merge pull request #1 from tutorjs/develop
MODIFIED: new style
2 parents 5e00b71 + 0bef5ab commit 11fb489

File tree

7 files changed

+229
-44
lines changed

7 files changed

+229
-44
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
11
# Simple Modal
2+
3+
## Getting Started
4+
5+
### HTML
6+
7+
```html
8+
9+
// Button Trigger
10+
<button class="your-trigger-selector">Show Modal</button>
11+
12+
// Modal
13+
<div class="your-modal-selector">
14+
<div class="your-modal-content-selector">
15+
<h1> Hello World </h1>
16+
</div>
17+
</div>
18+
19+
20+
```
21+
22+
### Javascript
23+
24+
```javascript
25+
26+
new SimpleModal({
27+
modal: '.your-modal-selector', // root class selector
28+
content: '.your-modal-content-selector', // children of modal
29+
trigger: '.your-trigger-selector' // button action
30+
})
31+
32+
```
33+
34+
35+
Thanks.

index.html

+91-5
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,111 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Simple Modal</title>
6-
<link rel="stylesheet" href="src/css/style.css">
6+
<link rel="stylesheet" href="src/css/simple-modal.css">
7+
<style>
8+
9+
.container {
10+
max-width: 1020px;
11+
margin: 0 auto;
12+
}
13+
14+
.show-modal {
15+
padding: 10px 15px;
16+
border: none;
17+
background: #eee;
18+
cursor: pointer;
19+
font-size: 15px;
20+
}
21+
22+
.show-modal:hover {
23+
box-shadow: 2px 2px 2px #ddd;
24+
}
25+
26+
.show-outher-modal {
27+
padding: 10px 15px;
28+
border: none;
29+
background: #ddd;
30+
cursor: pointer;
31+
font-size: 15px;
32+
}
33+
34+
.show-outher-modal:hover {
35+
box-shadow: 2px 2px 2px #ddd;
36+
}
37+
38+
pre {
39+
background: #eee;
40+
}
41+
42+
</style>
743
</head>
844
<body>
945

1046
<div class="container">
1147
<!-- button show modal -->
1248
<h1>Example Show Modal</h1>
1349
<button class="show-modal">Show Modal</button>
50+
<button class="show-outher-modal">Show Outher Modal</button>
51+
52+
<pre>
53+
<code>
54+
55+
// Show Modal (Default Width 400px)
56+
new SimpleModal({
57+
modal: '.my-modal',
58+
content: '.my-content',
59+
trigger: '.show-modal',
60+
})
61+
62+
// Show Outher Modal (With 800px)
63+
new SimpleModal({
64+
modal: '.my-outher-modal',
65+
content: '.my-outher-content',
66+
trigger: '.show-outher-modal',
67+
width: 800 // 800px
68+
})
69+
70+
</code>
71+
</pre>
72+
1473
</div>
1574

1675
<!-- modal element -->
17-
<div class="modal">
18-
<div class="modal-content">
19-
<span class="close">&times;</span>
76+
<div class="my-modal">
77+
<div class="my-content">
2078
<h1>Hello World</h1>
79+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Porro totam tempora expedita ipsa. Quas dolorem, dolor tenetur inventore, cum excepturi ea ratione unde id, numquam natus blanditiis provident suscipit dolorum.</p>
80+
</div>
81+
</div>
82+
83+
84+
<div class="my-outher-modal">
85+
<div class="my-outher-content">
86+
<h1>Halo Dunia</h1>
87+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Porro totam tempora expedita ipsa. Quas dolorem, dolor tenetur inventore, cum excepturi ea ratione unde id, numquam natus blanditiis provident suscipit dolorum.</p>
2188
</div>
2289
</div>
2390

24-
<script src="src/js/index.js"></script>
91+
<script src="src/js/simple-modal.js"></script>
92+
93+
<script>
94+
95+
// My Modal
96+
new SimpleModal({
97+
modal: '.my-modal',
98+
content: '.my-content',
99+
trigger: '.show-modal'
100+
})
101+
102+
// My Outher Modal
103+
new SimpleModal({
104+
modal: '.my-outher-modal',
105+
content: '.my-outher-content',
106+
trigger: '.show-outher-modal',
107+
width: 800 // width 800px
108+
})
109+
110+
</script>
25111

26112
</body>
27113
</html>

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = reqiure('./src/js/index.js');

package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "simple-modal",
3+
"version": "0.1.0",
4+
"description": "Simple Modal",
5+
"main": "index.js",
6+
"repository": "https://github.com/tutorjs/simple-modal.git",
7+
"author": "adiatma <adiatma9024@gmail.com>",
8+
"license": "MIT"
9+
}

src/css/style.css renamed to src/css/simple-modal.css

+5-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Simple Modal
3+
* Author adiatma9024@gmail.com
4+
*/
5+
16
*,
27
*:after,
38
*:before {
@@ -10,23 +15,6 @@ body {
1015
margin: 0;
1116
}
1217

13-
.container {
14-
max-width: 1020px;
15-
margin: 0 auto;
16-
}
17-
18-
.show-modal {
19-
padding: 10px 15px;
20-
border: none;
21-
background: #eee;
22-
cursor: pointer;
23-
font-size: 15px;
24-
}
25-
26-
.show-modal:hover {
27-
box-shadow: 2px 2px 2px #ddd;
28-
}
29-
3018
.modal {
3119
display: none;
3220
position: fixed;
@@ -43,7 +31,6 @@ body {
4331
background: #fff;
4432
padding: 10px;
4533
margin: 15% auto;
46-
width: 80%;
4734
}
4835

4936
.close {

src/js/index.js

-21
This file was deleted.

src/js/simple-modal.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Simple Modal
3+
* Author adiatma9024@gmail.com
4+
*/
5+
6+
;(function(root, factory) {
7+
root.SimpleModal = factory();
8+
})(this, function () {
9+
10+
/**
11+
* write with es2015
12+
*/
13+
14+
function el(selector) {
15+
return document.querySelector(selector);
16+
}
17+
18+
function findChildren(element, selector) {
19+
return element.querySelectorAll(selector);
20+
}
21+
22+
function createModalContent() {
23+
var modalContent = document.createElement('div');
24+
modalContent.classList.add('modal-content');
25+
return modalContent;
26+
}
27+
28+
function createSpanClose() {
29+
var spanClose = document.createElement('span');
30+
spanClose.classList.add('close');
31+
spanClose.innerHTML = '&times;';
32+
return spanClose;
33+
}
34+
35+
function configModal(options) {
36+
37+
this.options = Object.assign({}, options);
38+
39+
this.width = this.options.width || 400;
40+
41+
this.wrapModalContent = this.wrapModalContent.bind(this);
42+
43+
this.makeModal();
44+
45+
};
46+
47+
configModal.prototype.makeModal = function makeModal() {
48+
49+
// Add class modal
50+
var modal = el(this.options.modal);
51+
modal.classList.add('modal');
52+
53+
// Add modal content
54+
var content = findChildren(modal, this.options.content);
55+
var modalContent = this.wrapModalContent(content);
56+
57+
// Add action close
58+
var close = findChildren(modalContent, '.close')[0];
59+
close.addEventListener('click', function() {
60+
modal.style.display = 'none';
61+
});
62+
63+
// Add trigger action
64+
var trigger = el(this.options.trigger);
65+
trigger.addEventListener('click', function() {
66+
modal.style.display = 'block';
67+
});
68+
69+
modal.appendChild(modalContent);
70+
71+
}
72+
73+
configModal.prototype.wrapModalContent = function wrapModalContent(children) {
74+
var fragmentModalContent = document.createDocumentFragment();
75+
var modalContent = createModalContent();
76+
modalContent.style.width = this.width + 'px';
77+
78+
Array.prototype.forEach.call(children, function(e) {
79+
modalContent.appendChild(createSpanClose());
80+
modalContent.appendChild(e);
81+
fragmentModalContent.appendChild(modalContent);
82+
});
83+
84+
return fragmentModalContent;
85+
}
86+
87+
return configModal;
88+
89+
});

0 commit comments

Comments
 (0)