flicker-alerts 1.0.83 → 1.0.84

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.
Files changed (2) hide show
  1. package/index.js +134 -95
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,100 +1,147 @@
1
+ // Função genérica para obter ou criar containers
2
+ function getOrCreateContainer(containerId, style) {
3
+ let container = document.getElementById(containerId);
4
+ if (!container) {
5
+ container = document.createElement('div');
6
+ container.id = containerId;
7
+ Object.assign(container.style, style);
8
+ document.body.appendChild(container);
9
+ }
10
+ return container;
11
+ }
12
+
13
+ // Função para criar ícones de alertas
14
+ function createAlertIcon(type) {
15
+ const icon = document.createElement('div');
16
+ icon.className = 'icon';
17
+ const iconClass = {
18
+ success: 'fa-check',
19
+ info: 'fa-info-circle',
20
+ warning: 'fa-exclamation-triangle',
21
+ danger: 'fa-times-circle',
22
+ }[type];
23
+ icon.innerHTML = `<i class="fas ${iconClass}" style="font-size: 24px;"></i>`;
24
+ return icon;
25
+ }
26
+
27
+ // Função para criar conteúdo do alerta
28
+ function createAlertContent(title, message) {
29
+ const content = document.createElement('div');
30
+ content.innerHTML = `<div class="title">${title}</div><div class="message">${message}</div>`;
31
+ return content;
32
+ }
33
+
34
+ // Função para criar a barra de progresso
35
+ function createProgressBar(duration) {
36
+ const progressBar = document.createElement('div');
37
+ progressBar.className = 'progress-bar';
38
+ progressBar.style.animationDuration = `${duration}ms`;
39
+ return progressBar;
40
+ }
41
+
42
+ // Função para criar o botão de fechamento
43
+ function createCloseButton(alertBox, timeoutId) {
44
+ const closeButton = document.createElement('div');
45
+ closeButton.className = 'close';
46
+ closeButton.innerHTML = '<i class="fas fa-times"></i>';
47
+ closeButton.addEventListener('click', () => {
48
+ clearTimeout(timeoutId);
49
+ removeElementWithAnimation(alertBox);
50
+ });
51
+ return closeButton;
52
+ }
53
+
54
+ // Função genérica para remover elementos com animação
55
+ function removeElementWithAnimation(element) {
56
+ element.style.transition = 'transform 0.3s ease-in, opacity 0.3s ease-in';
57
+ element.style.opacity = '0';
58
+ element.style.transform = 'translateY(-100%)';
59
+ setTimeout(() => element.remove(), 300);
60
+ }
61
+
62
+ // Objeto FlickerAlerts para exibir os alertas
1
63
  const FlickerAlerts = {
2
64
  showAlert: function ({ type, title, message, duration = 3000, position = 'top-right' }) {
3
65
  if (typeof window !== 'undefined' && typeof document !== 'undefined') {
4
- let container = document.getElementById('alerts-container');
5
- if (!container) {
6
- container = document.createElement('div');
7
- container.id = 'alerts-container';
8
- container.style.position = 'fixed';
9
- container.style.pointerEvents = 'none';
10
- document.body.appendChild(container);
11
-
12
- // Define posição com base no parâmetro
13
- const positionStyles = {
14
- 'top-right': { top: '0', right: '0', flexDirection: 'column' },
15
- 'top-left': { top: '0', left: '0', flexDirection: 'column' },
16
- 'bottom-right': { bottom: '0', right: '0', flexDirection: 'column-reverse' },
17
- 'bottom-left': { bottom: '0', left: '0', flexDirection: 'column-reverse' },
18
- 'center': { top: '50%', left: '50%', transform: 'translate(-50%, -50%)', flexDirection: 'column' },
19
- 'top-center': { top: '1rem', left: '50%', transform: 'translateX(-50%)', flexDirection: 'column' },
20
- 'bottom-center': { bottom: '1rem', left: '50%', transform: 'translateX(-50%)', flexDirection: 'column-reverse' },
21
- };
22
- Object.assign(container.style, positionStyles[position] || positionStyles['top-right']);
23
- }
66
+ const container = getOrCreateContainer('alerts-container', {
67
+ position: 'fixed',
68
+ pointerEvents: 'none',
69
+ top: position.includes('top') ? '0' : 'auto',
70
+ bottom: position.includes('bottom') ? '0' : 'auto',
71
+ left: position.includes('left') ? '0' : 'auto',
72
+ right: position.includes('right') ? '0' : 'auto',
73
+ transform: position.includes('center') ? 'translateX(-50%)' : 'none',
74
+ zIndex: 9999,
75
+ flexDirection: ['top-right', 'top-left', 'bottom-right', 'bottom-left'].includes(position) ? 'column' : 'column-reverse',
76
+ });
24
77
 
25
78
  const alertBox = document.createElement('div');
26
79
  alertBox.className = `notification ${type}`;
80
+ alertBox.style.opacity = '0';
81
+ alertBox.style.transform = 'translateY(-100%)';
27
82
 
28
83
  // Ícone
29
- const icon = document.createElement('div');
30
- icon.className = 'icon';
31
- const iconClass = {
32
- success: 'fa-check',
33
- info: 'fa-info-circle',
34
- warning: 'fa-exclamation-triangle',
35
- danger: 'fa-times-circle',
36
- }[type];
37
- icon.innerHTML = `<i class="fas ${iconClass}" style="font-size: 24px;"></i>`;
38
- alertBox.appendChild(icon);
84
+ alertBox.appendChild(createAlertIcon(type));
85
+
86
+ // Divider (linha de separação)
39
87
  const divider = document.createElement('div');
40
88
  divider.className = 'divider';
41
89
  alertBox.appendChild(divider);
42
-
90
+
43
91
  // Conteúdo
44
- const content = document.createElement('div');
45
- content.innerHTML = `<div class="title">${title}</div><div class="message">${message}</div>`;
46
- alertBox.appendChild(content);
92
+ alertBox.appendChild(createAlertContent(title, message));
47
93
 
48
94
  // Barra de progresso
49
- const progressBar = document.createElement('div');
50
- progressBar.className = 'progress-bar';
51
- progressBar.style.animationDuration = `${duration}ms`; // sincroniza com a duração do alerta
52
- alertBox.appendChild(progressBar);
95
+ alertBox.appendChild(createProgressBar(duration));
53
96
 
54
97
  // Botão de fechamento
55
- const closeButton = document.createElement('div');
56
- closeButton.className = 'close';
57
- closeButton.innerHTML = '<i class="fas fa-times"></i>';
58
- closeButton.addEventListener('click', () => {
59
- clearTimeout(timeoutId); // limpa o timeout quando fechado manualmente
60
- removeAlert();
61
- });
98
+ alertBox.appendChild(createCloseButton(alertBox));
62
99
 
63
- alertBox.appendChild(closeButton);
64
-
65
- // Adiciona no contêiner
66
100
  container.prepend(alertBox);
67
101
 
68
- // Timeout para remover automaticamente o alerta
69
- const timeoutId = setTimeout(() => {
70
- removeAlert();
71
- }, duration);
102
+ setTimeout(() => {
103
+ alertBox.style.transition = 'transform 0.3s ease-out, opacity 0.3s ease-out';
104
+ alertBox.style.opacity = '1';
105
+ alertBox.style.transform = 'translateY(0)';
106
+ }, 0);
72
107
 
73
- function removeAlert() {
74
- alertBox.style.opacity = '0';
75
- setTimeout(() => alertBox.remove(), 300); // atraso para animação de fade-out
76
- }
108
+ const timeoutId = setTimeout(() => removeElementWithAnimation(alertBox), duration);
77
109
  }
78
110
  },
79
111
  };
80
112
 
81
-
82
- // Botões de teste - Eles podem ser removidos se não forem necessários
113
+ // Botões de teste
83
114
  if (typeof document !== 'undefined') {
84
115
  document.getElementById('success-btn')?.addEventListener('click', () => {
85
- FlickerAlerts.showAlert({ type: 'success', title: 'Sucesso!', message: 'Essa mensagem é personalizável.' });
116
+ FlickerAlerts.showAlert({
117
+ type: 'success',
118
+ title: 'Sucesso!',
119
+ message: 'Essa mensagem é personalizável.',
120
+ });
86
121
  });
87
122
 
88
123
  document.getElementById('info-btn')?.addEventListener('click', () => {
89
- FlickerAlerts.showAlert({ type: 'info', title: 'Informação!', message: 'Essa mensagem é personalizável.' });
124
+ FlickerAlerts.showAlert({
125
+ type: 'info',
126
+ title: 'Informação!',
127
+ message: 'Essa mensagem é personalizável.',
128
+ });
90
129
  });
91
130
 
92
131
  document.getElementById('warning-btn')?.addEventListener('click', () => {
93
- FlickerAlerts.showAlert({ type: 'warning', title: 'Alerta!', message: 'Essa mensagem é personalizável.' });
132
+ FlickerAlerts.showAlert({
133
+ type: 'warning',
134
+ title: 'Alerta!',
135
+ message: 'Essa mensagem é personalizável.',
136
+ });
94
137
  });
95
138
 
96
139
  document.getElementById('error-btn')?.addEventListener('click', () => {
97
- FlickerAlerts.showAlert({ type: 'danger', title: 'Erro!', message: 'Essa mensagem é personalizável.' });
140
+ FlickerAlerts.showAlert({
141
+ type: 'danger',
142
+ title: 'Erro!',
143
+ message: 'Essa mensagem é personalizável.',
144
+ });
98
145
  });
99
146
  }
100
147
 
@@ -104,23 +151,25 @@ if (typeof document !== 'undefined') {
104
151
  const FlickerModals = {
105
152
  showModal: function ({ type, title, message, onConfirm, onCancel, timer = 5000 }) {
106
153
  if (typeof window !== 'undefined' && typeof document !== 'undefined') {
107
- let container = document.getElementById('modals-container');
108
- if (!container) {
109
- container = document.createElement('div');
110
- container.id = 'modals-container';
111
- container.style.position = 'fixed';
112
- container.style.top = 0;
113
- container.style.left = 0;
114
- container.style.width = '100vw';
115
- container.style.height = '100vh';
116
- container.style.pointerEvents = 'none';
117
- document.body.appendChild(container);
118
- }
154
+ const container = getOrCreateContainer('modals-container', {
155
+ position: 'fixed',
156
+ top: 0,
157
+ left: 0,
158
+ width: '100vw',
159
+ height: '100vh',
160
+ pointerEvents: 'none',
161
+ display: 'flex',
162
+ justifyContent: 'center',
163
+ alignItems: 'center',
164
+ zIndex: 9999,
165
+ });
119
166
 
120
167
  const modalContainer = document.createElement('div');
121
168
  modalContainer.className = 'modal-container';
169
+ modalContainer.style.opacity = '0';
170
+ modalContainer.style.transform = 'scale(0.9)';
171
+ modalContainer.style.transition = 'transform 0.3s ease-out, opacity 0.3s ease-out';
122
172
 
123
- // Criar a estrutura para o modal
124
173
  const modalDialog = document.createElement('div');
125
174
  modalDialog.className = 'modal-dialog';
126
175
  modalDialog.setAttribute('role', 'document');
@@ -128,7 +177,6 @@ const FlickerModals = {
128
177
  const modalContent = document.createElement('div');
129
178
  modalContent.className = 'modal-content';
130
179
 
131
- // Header
132
180
  const modalHeader = document.createElement('div');
133
181
  modalHeader.className = 'modal-header';
134
182
  const closeButton = document.createElement('button');
@@ -137,12 +185,11 @@ const FlickerModals = {
137
185
  closeButton.setAttribute('aria-label', 'Close');
138
186
  closeButton.innerHTML = `<div><i class="fas fa-times"></i></div>`;
139
187
  closeButton.addEventListener('click', () => {
140
- removeModal();
188
+ removeElementWithAnimation(modalContainer);
141
189
  if (onCancel) onCancel();
142
190
  });
143
191
  modalHeader.appendChild(closeButton);
144
192
 
145
- // Body
146
193
  const modalBody = document.createElement('div');
147
194
  modalBody.className = 'modal-body';
148
195
  const icon = document.createElement('div');
@@ -161,7 +208,6 @@ const FlickerModals = {
161
208
  `;
162
209
  modalBody.appendChild(bodyContent);
163
210
 
164
- // Footer
165
211
  const modalFooter = document.createElement('div');
166
212
  modalFooter.className = 'modal-footer';
167
213
 
@@ -170,7 +216,7 @@ const FlickerModals = {
170
216
  cancelButton.className = 'btn btn-cancel';
171
217
  cancelButton.innerText = 'Não, cancelar';
172
218
  cancelButton.addEventListener('click', () => {
173
- removeModal();
219
+ removeElementWithAnimation(modalContainer);
174
220
  if (onCancel) onCancel();
175
221
  });
176
222
  modalFooter.appendChild(cancelButton);
@@ -180,7 +226,7 @@ const FlickerModals = {
180
226
  confirmButton.className = type === 'delete' ? 'btn btn-confirm-delete' : 'btn btn-confirm-warning';
181
227
  confirmButton.innerText = type === 'delete' ? 'Sim, deletar' : 'Sim, confirmar';
182
228
  confirmButton.addEventListener('click', () => {
183
- removeModal();
229
+ removeElementWithAnimation(modalContainer);
184
230
  if (onConfirm) onConfirm();
185
231
  });
186
232
  modalFooter.appendChild(confirmButton);
@@ -192,22 +238,15 @@ const FlickerModals = {
192
238
  modalContainer.appendChild(modalDialog);
193
239
  container.appendChild(modalContainer);
194
240
 
195
- // Timeout para remover o modal após o tempo especificado
196
- let timeoutId; // Mover a declaração para o início
197
- timeoutId = setTimeout(() => {
198
- removeModal();
199
- }, timer);
200
-
201
- // Função para remover o modal
202
- function removeModal() {
203
- modalContainer.style.opacity = '0';
204
- setTimeout(() => modalContainer.remove(), 300);
205
- clearTimeout(timeoutId); // Limpar timeout após remoção
206
- }
241
+ setTimeout(() => {
242
+ modalContainer.style.opacity = '1';
243
+ modalContainer.style.transform = 'scale(1)';
244
+ }, 0);
245
+
246
+ const timeoutId = setTimeout(() => removeElementWithAnimation(modalContainer), timer);
207
247
  }
208
- }
248
+ },
209
249
  };
210
-
211
250
  // Botões de teste - Eles podem ser removidos se não forem necessários
212
251
  if (typeof document !== 'undefined') {
213
252
  document.getElementById('modal-warning-btn')?.addEventListener('click', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flicker-alerts",
3
- "version": "1.0.83",
3
+ "version": "1.0.84",
4
4
  "repository": "https://www.linkedin.com/in/bruno-carneiro-9a53aa190/",
5
5
  "homepage": "https://flickeralerts.netlify.app/",
6
6
  "description": "Biblioteca para alertas animados",
@@ -18,6 +18,6 @@
18
18
  "author": "https://www.linkedin.com/in/bruno-carneiro-9a53aa190/",
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "flicker-alerts": "^1.0.83"
21
+ "flicker-alerts": "^1.0.84"
22
22
  }
23
23
  }