@utily-js/core 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -2
- package/main.js +1 -0
- package/modal/modal.js +121 -0
- package/modal/modalStyle.css +120 -0
- package/modules.js +7 -0
- package/notify/notify.js +65 -0
- package/notify/notifyStyle.css +73 -0
- package/package.json +29 -6
- package/utils/gid.js +4 -0
- package/utils/normalize.js +3 -0
- package/utils/numbers.js +17 -0
- package/utils/qrcode.js +37 -0
- package/utils/random.js +7 -0
package/README.md
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# utilidades-js
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Biblioteca JavaScript simples para uso direto pela Vercel.
|
|
4
|
+
|
|
5
|
+
## Versão estável:
|
|
6
|
+
1.0.3
|
|
7
|
+
|
|
8
|
+
## Documentação
|
|
9
|
+
|
|
10
|
+
https://utilidades-js.vercel.app/docs
|
|
11
|
+
|
|
12
|
+
## Import
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
import * as utils from "https://esm.sh/utilidades-js";
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Exemplos
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
utils.createNotify({
|
|
22
|
+
text: "Notificação",
|
|
23
|
+
time: 10,
|
|
24
|
+
type: "warning",
|
|
25
|
+
show: true,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
utils.gid({
|
|
29
|
+
id: "editor",
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
utils.randomInt({
|
|
33
|
+
min: 1,
|
|
34
|
+
max: 10,
|
|
35
|
+
});
|
|
36
|
+
```
|
package/main.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./modules.js";
|
package/modal/modal.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
export function modalContainer() {
|
|
2
|
+
let container = document.getElementById("modalContainer");
|
|
3
|
+
let overlay = document.getElementById("modalOverlay");
|
|
4
|
+
|
|
5
|
+
if (!container) {
|
|
6
|
+
container = document.createElement("div");
|
|
7
|
+
container.id = "modalContainer";
|
|
8
|
+
|
|
9
|
+
document.body.appendChild(container);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (!overlay) {
|
|
13
|
+
overlay = document.createElement("div");
|
|
14
|
+
overlay.id = "modalOverlay";
|
|
15
|
+
|
|
16
|
+
document.body.appendChild(overlay);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
get() {
|
|
21
|
+
return container;
|
|
22
|
+
},
|
|
23
|
+
getOverlay() {
|
|
24
|
+
return overlay;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function createModal({
|
|
30
|
+
title = "Meu modal",
|
|
31
|
+
content = "Modal exemplo",
|
|
32
|
+
description = "Modal feito com utilidades-js",
|
|
33
|
+
buttonText = "Enviar dados",
|
|
34
|
+
callback = () => console.log("Modal clicado!"),
|
|
35
|
+
inputs = [],
|
|
36
|
+
} = {}) {
|
|
37
|
+
const { get, getOverlay } = modalContainer();
|
|
38
|
+
|
|
39
|
+
const container = get();
|
|
40
|
+
const overlay = getOverlay();
|
|
41
|
+
|
|
42
|
+
container.innerHTML = "";
|
|
43
|
+
|
|
44
|
+
const modalElement = document.createElement("div");
|
|
45
|
+
modalElement.classList.add("modal");
|
|
46
|
+
|
|
47
|
+
modalElement.innerHTML = `
|
|
48
|
+
<form>
|
|
49
|
+
<div class="top">
|
|
50
|
+
<p>${title}</p>
|
|
51
|
+
<button id="close-modal">X</button>
|
|
52
|
+
</div>
|
|
53
|
+
|
|
54
|
+
<div class="content">
|
|
55
|
+
${content}
|
|
56
|
+
</div>
|
|
57
|
+
|
|
58
|
+
<div class="inputs"></div>
|
|
59
|
+
|
|
60
|
+
<div class="bottom">
|
|
61
|
+
${description}
|
|
62
|
+
<button type="submit" id="modal-action">
|
|
63
|
+
${buttonText}
|
|
64
|
+
</button>
|
|
65
|
+
</div>
|
|
66
|
+
</form>
|
|
67
|
+
`;
|
|
68
|
+
|
|
69
|
+
const inputsContainer = modalElement.querySelector(".inputs");
|
|
70
|
+
|
|
71
|
+
inputs.forEach((input) => {
|
|
72
|
+
const element = document.createElement("input");
|
|
73
|
+
|
|
74
|
+
element.id = input.id ?? "";
|
|
75
|
+
element.type = input.type ?? "text";
|
|
76
|
+
element.placeholder = input.placeholder ?? "";
|
|
77
|
+
element.required = input.required ?? "false";
|
|
78
|
+
|
|
79
|
+
inputsContainer.appendChild(element);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
container.appendChild(modalElement);
|
|
83
|
+
|
|
84
|
+
overlay.style.display = "block";
|
|
85
|
+
|
|
86
|
+
const closeButton = modalElement.querySelector("#close-modal");
|
|
87
|
+
|
|
88
|
+
const actionButton = modalElement.querySelector("#modal-action");
|
|
89
|
+
|
|
90
|
+
function closeModal() {
|
|
91
|
+
modalElement.remove();
|
|
92
|
+
overlay.style.display = "none";
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
closeButton.addEventListener("click", closeModal);
|
|
96
|
+
|
|
97
|
+
if (typeof callback === "function") {
|
|
98
|
+
actionButton.addEventListener("click", callback);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
actionButton.addEventListener("click", closeModal);
|
|
102
|
+
|
|
103
|
+
overlay.onclick = closeModal;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function loadStyle() {
|
|
107
|
+
if (document.getElementById("modalStyle")) return;
|
|
108
|
+
|
|
109
|
+
const style = document.createElement("link");
|
|
110
|
+
|
|
111
|
+
style.id = "modalStyle";
|
|
112
|
+
style.rel = "stylesheet";
|
|
113
|
+
style.href = new URL("../modal/modalStyle.css", import.meta.url).href;
|
|
114
|
+
document.head.appendChild(style);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function init() {
|
|
118
|
+
loadStyle();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
init();
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
#modalContainer {
|
|
2
|
+
position: fixed;
|
|
3
|
+
z-index: 100000000000000000000000;
|
|
4
|
+
top: 50%;
|
|
5
|
+
left: 50%;
|
|
6
|
+
transform: translate(-50%, -50%);
|
|
7
|
+
width: min(90%, 500px);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
#modalOverlay {
|
|
11
|
+
position: fixed;
|
|
12
|
+
inset: 0;
|
|
13
|
+
z-index: 1000000000;
|
|
14
|
+
background: rgba(0, 0, 0, 0.55);
|
|
15
|
+
backdrop-filter: blur(3px);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.modal {
|
|
19
|
+
width: 100%;
|
|
20
|
+
box-sizing: border-box;
|
|
21
|
+
padding: 24px;
|
|
22
|
+
background: #fff;
|
|
23
|
+
border-radius: 16px;
|
|
24
|
+
border: 1px solid #e5e5e5;
|
|
25
|
+
box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
|
|
26
|
+
font-family: Arial, sans-serif;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.top {
|
|
30
|
+
display: flex;
|
|
31
|
+
align-items: center;
|
|
32
|
+
justify-content: space-between;
|
|
33
|
+
gap: 15px;
|
|
34
|
+
margin-bottom: 20px;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.top p {
|
|
38
|
+
margin: 0;
|
|
39
|
+
font-size: 22px;
|
|
40
|
+
font-weight: 600;
|
|
41
|
+
color: #222;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.top #close-modal {
|
|
45
|
+
width: 36px;
|
|
46
|
+
height: 36px;
|
|
47
|
+
padding: 0;
|
|
48
|
+
border: none;
|
|
49
|
+
border-radius: 10px;
|
|
50
|
+
background: #f1f1f1;
|
|
51
|
+
color: #555;
|
|
52
|
+
font-size: 18px;
|
|
53
|
+
cursor: pointer;
|
|
54
|
+
transition: 0.2s;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
.top #close-modal:hover {
|
|
58
|
+
background: #e5e5e5;
|
|
59
|
+
color: #111;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.content {
|
|
63
|
+
margin-bottom: 20px;
|
|
64
|
+
color: #666;
|
|
65
|
+
line-height: 1.5;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.inputs {
|
|
69
|
+
display: flex;
|
|
70
|
+
flex-direction: column;
|
|
71
|
+
gap: 12px;
|
|
72
|
+
margin-bottom: 24px;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.inputs input {
|
|
76
|
+
width: 100%;
|
|
77
|
+
box-sizing: border-box;
|
|
78
|
+
padding: 12px 14px;
|
|
79
|
+
border: 1px solid #d6d6d6;
|
|
80
|
+
border-radius: 10px;
|
|
81
|
+
outline: none;
|
|
82
|
+
font-size: 15px;
|
|
83
|
+
transition: 0.2s;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.inputs input:focus {
|
|
87
|
+
border-color: #666;
|
|
88
|
+
box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.08);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
.bottom {
|
|
92
|
+
display: flex;
|
|
93
|
+
align-items: center;
|
|
94
|
+
justify-content: space-between;
|
|
95
|
+
gap: 15px;
|
|
96
|
+
padding-top: 18px;
|
|
97
|
+
border-top: 1px solid #eee;
|
|
98
|
+
color: #777;
|
|
99
|
+
font-size: 14px;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.bottom #modal-action {
|
|
103
|
+
width: 100%;
|
|
104
|
+
padding: 10px 18px;
|
|
105
|
+
border: none;
|
|
106
|
+
border-radius: 10px;
|
|
107
|
+
background: #222;
|
|
108
|
+
color: white;
|
|
109
|
+
font-size: 14px;
|
|
110
|
+
cursor: pointer;
|
|
111
|
+
transition: 0.2s;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.bottom #modal-action:hover {
|
|
115
|
+
background: #444;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.bottom #modal-action:active {
|
|
119
|
+
transform: scale(0.97);
|
|
120
|
+
}
|
package/modules.js
ADDED
package/notify/notify.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export function notifyContainer(style = {}) {
|
|
2
|
+
let container = document.getElementById("notifyContainer");
|
|
3
|
+
|
|
4
|
+
if (!container) {
|
|
5
|
+
container = document.createElement("div");
|
|
6
|
+
container.id = "notifyContainer";
|
|
7
|
+
document.body.appendChild(container);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
Object.assign(container.style, style);
|
|
11
|
+
|
|
12
|
+
return container;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function loadStyle() {
|
|
16
|
+
if (document.getElementById("notifyStyle")) return;
|
|
17
|
+
|
|
18
|
+
const style = document.createElement("link");
|
|
19
|
+
|
|
20
|
+
style.id = "notifyStyle";
|
|
21
|
+
style.rel = "stylesheet";
|
|
22
|
+
style.href = new URL("../notify/notifyStyle.css", import.meta.url).href;
|
|
23
|
+
|
|
24
|
+
document.head.appendChild(style);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function createNotify({
|
|
28
|
+
text = "Notificação",
|
|
29
|
+
time = 10,
|
|
30
|
+
type = "",
|
|
31
|
+
show = false,
|
|
32
|
+
} = {}) {
|
|
33
|
+
const container = notifyContainer();
|
|
34
|
+
const notify = document.createElement("div");
|
|
35
|
+
|
|
36
|
+
notify.className = `notify ${type}`;
|
|
37
|
+
|
|
38
|
+
let remaining = time;
|
|
39
|
+
|
|
40
|
+
const update = () => {
|
|
41
|
+
notify.innerHTML = `<p>${text}</p><p>${remaining}s</p>`;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
update();
|
|
45
|
+
container.appendChild(notify);
|
|
46
|
+
|
|
47
|
+
setTimeout(() => {
|
|
48
|
+
requestAnimationFrame(() => notify.classList.add("active"));
|
|
49
|
+
}, container.children.length * 100);
|
|
50
|
+
|
|
51
|
+
const interval = setInterval(() => {
|
|
52
|
+
update();
|
|
53
|
+
remaining--;
|
|
54
|
+
}, 1000);
|
|
55
|
+
|
|
56
|
+
setTimeout(() => {
|
|
57
|
+
clearInterval(interval);
|
|
58
|
+
notify.classList.remove("active");
|
|
59
|
+
setTimeout(() => notify.remove(), 300);
|
|
60
|
+
}, time * 1000);
|
|
61
|
+
|
|
62
|
+
return notify;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
loadStyle();
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#notifyContainer,
|
|
2
|
+
.notify {
|
|
3
|
+
box-sizing: border-box;
|
|
4
|
+
padding: 0;
|
|
5
|
+
margin: 0;
|
|
6
|
+
overflow-x: hidden;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
#notifyContainer {
|
|
10
|
+
width: 320px;
|
|
11
|
+
max-height: 160px;
|
|
12
|
+
position: fixed;
|
|
13
|
+
top: 20px;
|
|
14
|
+
right: 20px;
|
|
15
|
+
overflow-y: auto;
|
|
16
|
+
|
|
17
|
+
display: flex;
|
|
18
|
+
flex-direction: column;
|
|
19
|
+
gap: 12px;
|
|
20
|
+
padding: 5px;
|
|
21
|
+
scrollbar-width: thin;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.notify {
|
|
25
|
+
display: flex;
|
|
26
|
+
flex-direction: column;
|
|
27
|
+
justify-content: center;
|
|
28
|
+
min-height: 70px;
|
|
29
|
+
width: 100%;
|
|
30
|
+
padding: 16px 18px;
|
|
31
|
+
border-radius: 14px;
|
|
32
|
+
outline: 1px solid rgba(0, 0, 0, 0.2);
|
|
33
|
+
background-color: rgb(226, 226, 255);
|
|
34
|
+
color: #202035;
|
|
35
|
+
transform: translateX(120%);
|
|
36
|
+
opacity: 0;
|
|
37
|
+
transition:
|
|
38
|
+
transform 0.35s ease,
|
|
39
|
+
opacity 0.35s ease;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.notify.active {
|
|
43
|
+
transform: translateX(0);
|
|
44
|
+
opacity: 1;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.notify p {
|
|
48
|
+
margin: 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.notify p:first-child {
|
|
52
|
+
font-size: 15px;
|
|
53
|
+
font-weight: 600;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.notify p:last-child {
|
|
57
|
+
margin-top: 5px;
|
|
58
|
+
|
|
59
|
+
font-size: 12px;
|
|
60
|
+
opacity: 0.65;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.notify.success {
|
|
64
|
+
background-color: rgb(104, 255, 104);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.notify.warning {
|
|
68
|
+
background-color: orange;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.notify.error {
|
|
72
|
+
background-color: rgb(255, 52, 52);
|
|
73
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@utily-js/core",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Biblioteca JavaScript com utilitários para DOM, notificações, modais e números.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./main.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"import": "./main.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"main.js",
|
|
16
|
+
"modules.js",
|
|
17
|
+
"notify/",
|
|
18
|
+
"modal/",
|
|
19
|
+
"utils/"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"javascript",
|
|
23
|
+
"utilities",
|
|
24
|
+
"dom",
|
|
25
|
+
"notifications",
|
|
26
|
+
"modal"
|
|
27
|
+
],
|
|
28
|
+
"license": "MIT"
|
|
29
|
+
}
|
package/utils/gid.js
ADDED
package/utils/numbers.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function isPrime({ n } = {}) {
|
|
2
|
+
if (n < 2) return false;
|
|
3
|
+
|
|
4
|
+
for (let i = 2; i <= Math.sqrt(n); i++) {
|
|
5
|
+
if (n % i === 0) return false;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function isEven({ number } = {}) {
|
|
12
|
+
return number % 2 === 0;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function isOdd({ number } = {}) {
|
|
16
|
+
return number % 2 !== 0;
|
|
17
|
+
}
|
package/utils/qrcode.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export async function qrcode({
|
|
2
|
+
element = "#qrcode",
|
|
3
|
+
content = "https://google.com",
|
|
4
|
+
width = 128,
|
|
5
|
+
height = 128,
|
|
6
|
+
colorDark = "#000000",
|
|
7
|
+
colorLight = "#ffffff",
|
|
8
|
+
correct = "H",
|
|
9
|
+
} = {}) {
|
|
10
|
+
if (!element || !content) return;
|
|
11
|
+
|
|
12
|
+
const target = document.querySelector(element);
|
|
13
|
+
|
|
14
|
+
if (!target) return;
|
|
15
|
+
|
|
16
|
+
if (!window.QRCode) {
|
|
17
|
+
await new Promise((resolve, reject) => {
|
|
18
|
+
const script = document.createElement("script");
|
|
19
|
+
|
|
20
|
+
script.src =
|
|
21
|
+
"https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js";
|
|
22
|
+
script.onload = resolve;
|
|
23
|
+
script.onerror = reject;
|
|
24
|
+
|
|
25
|
+
document.head.appendChild(script);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return new window.QRCode(target, {
|
|
30
|
+
text: content,
|
|
31
|
+
width,
|
|
32
|
+
height,
|
|
33
|
+
colorDark,
|
|
34
|
+
colorLight,
|
|
35
|
+
correctLevel: window.QRCode.CorrectLevel[correct],
|
|
36
|
+
});
|
|
37
|
+
}
|
package/utils/random.js
ADDED