avalynx-alert 0.0.4 → 0.0.6

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 CHANGED
@@ -16,7 +16,9 @@ AvalynxAlert is a lightweight, dependency-free alert system designed for web app
16
16
  Here's a simple example of how to use AvalynxAlert in your project:
17
17
 
18
18
  * [Overview](https://avalynx-alert.jbs-newmedia.de/examples/index.html)
19
- * [Random Alerts](https://avalynx-alert.jbs-newmedia.de/examples/random-alerts.html)
19
+ * [Random alerts](https://avalynx-alert.jbs-newmedia.de/examples/random-alerts.html)
20
+ * [Callback on close](https://avalynx-alert.jbs-newmedia.de/examples/callback-onclose.html)
21
+ * [Random alerts every 3 seconds](https://avalynx-alert.jbs-newmedia.de/examples/random-alerts-every-3s.html)
20
22
 
21
23
  ## Installation
22
24
 
@@ -48,6 +50,8 @@ AvalynxAlert is also available via [jsDelivr](https://www.jsdelivr.com/). You ca
48
50
  <script src="https://cdn.jsdelivr.net/npm/avalynx-alert/dist/avalynx-alert.js"></script>
49
51
  ```
50
52
 
53
+ Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxAlert displays correctly.
54
+
51
55
  ## Installation via NPM
52
56
 
53
57
  AvalynxAlert is also available as a npm package. You can add it to your project with the following command:
@@ -63,7 +67,7 @@ import { AvalynxAlert } from 'avalynx-alert';
63
67
  import 'avalynx-alert/dist/css/avalynx-alert.min.css';
64
68
  ```
65
69
 
66
- Make sure to also include Bootstrap's CSS in your project to ensure AvalynxAlert displays correctly.
70
+ Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxAlert displays correctly.
67
71
 
68
72
  ## Installation via Symfony AssetMapper
69
73
 
@@ -78,7 +82,7 @@ import { AvalynxAlert } from 'avalynx-alert';
78
82
  import 'avalynx-alert/dist/css/avalynx-alert.min.css';
79
83
  ```
80
84
 
81
- Make sure to also include Bootstrap's CSS in your project to ensure AvalynxAlert displays correctly.
85
+ Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxAlert displays correctly.
82
86
 
83
87
  ## Usage
84
88
 
@@ -106,6 +110,7 @@ AvalynxAlert allows the following options for customization:
106
110
  - `closeable`: Whether the alert can be closed by the user (default: true).
107
111
  - `autoClose`: Whether the alert will close automatically after the duration (default: true).
108
112
  - `width`: The width of the alert (default: `400px`).
113
+ - `onClose`: A callback function to be called when the alert is closed.
109
114
 
110
115
  ## Contributing
111
116
 
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * A simple alert system for web applications. Based on Bootstrap >=5.3 without any framework dependencies.
5
5
  *
6
- * @version 0.0.4
6
+ * @version 0.0.6
7
7
  * @license MIT
8
8
  * @author https://github.com/avalynx/avalynx-alert/graphs/contributors
9
9
  * @website https://github.com/avalynx/
@@ -53,4 +53,4 @@
53
53
 
54
54
  .avalynx-alert.alert-dark .alert-timer {
55
55
  background-color: var(--bs-dark-border-subtle);
56
- }
56
+ }
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * A simple alert system for web applications. Based on Bootstrap >=5.3 without any framework dependencies.
5
5
  *
6
- * @version 0.0.4
6
+ * @version 0.0.6
7
7
  * @license MIT
8
8
  * @author https://github.com/avalynx/avalynx-alert/graphs/contributors
9
9
  * @website https://github.com/avalynx/
@@ -21,110 +21,119 @@
21
21
  */
22
22
 
23
23
  export class AvalynxAlert {
24
- constructor(message, type, options = {}) {
25
- this.message = message;
26
- this.type = type;
27
- this.duration = options.duration || 5000;
28
- this.position = options.position || 'top-center';
29
- this.closeable = options.closeable || true;
30
- this.autoClose = options.autoClose || true;
31
- this.width = options.width || '400px';
32
- if (!['primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark'].includes(this.type)) {
33
- this.type = 'info';
34
- }
35
- if (!['top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right'].includes(this.position)) {
36
- this.position = 'top-center';
37
- }
38
- if (this.autoClose !== false && this.autoClose !== true) {
39
- this.autoClose = true;
40
- }
41
- if ((this.closeable !== false && this.closeable !== true) || (this.autoClose === true)) {
42
- this.closeable = true;
43
- }
24
+ constructor(message, type, options = {}) {
25
+ this.message = message;
26
+ this.type = type;
27
+ this.options = {
28
+ duration: options.duration || 5000,
29
+ position: options.position || 'top-center',
30
+ closeable: options.closeable || true,
31
+ autoClose: options.autoClose || true,
32
+ width: options.width || '400px',
33
+ onClose: options.onClose || null,
34
+ ...options
35
+ };
36
+ if (!['primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark'].includes(this.type)) {
37
+ this.type = 'info';
38
+ }
39
+ if (!['top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right'].includes(this.options.position)) {
40
+ this.options.position = 'top-center';
41
+ }
42
+ if (this.options.autoClose !== false && this.options.autoClose !== true) {
43
+ this.options.autoClose = true;
44
+ }
45
+ if ((this.options.closeable !== false && this.options.closeable !== true) || (this.options.autoClose === true)) {
46
+ this.options.closeable = true;
47
+ }
44
48
 
45
- this.init();
46
- }
49
+ this.init();
50
+ }
47
51
 
48
- ensureAlertContainer() {
49
- if (!document.getElementById('avalynx-alert-container-' + this.position)) {
50
- var container = document.createElement('div');
51
- container.id = 'avalynx-alert-container-' + this.position;
52
- container.style.width = '100%';
53
- container.style.maxWidth = this.width;
54
- container.style.zIndex = '1000';
55
- container.classList.add('container-fluid');
52
+ ensureAlertContainer() {
53
+ if (!document.getElementById('avalynx-alert-container-' + this.options.position)) {
54
+ var container = document.createElement('div');
55
+ container.id = 'avalynx-alert-container-' + this.options.position;
56
+ container.style.width = '100%';
57
+ container.style.maxWidth = this.options.width;
58
+ container.style.zIndex = '1000';
59
+ container.classList.add('container-fluid');
56
60
 
57
- if (this.position === 'top-left') {
58
- container.style.position = 'fixed';
59
- container.style.top = '10px';
60
- container.style.left = '0px';
61
- } else if (this.position === 'top-center') {
62
- container.style.position = 'fixed';
63
- container.style.top = '10px';
64
- container.style.left = '50%';
65
- container.style.transform = 'translateX(-50%)';
66
- } else if (this.position === 'top-right') {
67
- container.style.position = 'fixed';
68
- container.style.top = '10px';
69
- container.style.right = '0px';
70
- } else if (this.position === 'bottom-left') {
71
- container.style.position = 'fixed';
72
- container.style.bottom = '10px';
73
- container.style.left = '0px';
74
- } else if (this.position === 'bottom-center') {
75
- container.style.position = 'fixed';
76
- container.style.bottom = '10px';
77
- container.style.left = '50%';
78
- container.style.transform = 'translateX(-50%)';
79
- } else if (this.position === 'bottom-right') {
80
- container.style.position = 'fixed';
81
- container.style.bottom = '10px';
82
- container.style.right = '0px';
83
- }
61
+ if (this.options.position === 'top-left') {
62
+ container.style.position = 'fixed';
63
+ container.style.top = '10px';
64
+ container.style.left = '0px';
65
+ } else if (this.options.position === 'top-center') {
66
+ container.style.position = 'fixed';
67
+ container.style.top = '10px';
68
+ container.style.left = '50%';
69
+ container.style.transform = 'translateX(-50%)';
70
+ } else if (this.options.position === 'top-right') {
71
+ container.style.position = 'fixed';
72
+ container.style.top = '10px';
73
+ container.style.right = '0px';
74
+ } else if (this.options.position === 'bottom-left') {
75
+ container.style.position = 'fixed';
76
+ container.style.bottom = '10px';
77
+ container.style.left = '0px';
78
+ } else if (this.options.position === 'bottom-center') {
79
+ container.style.position = 'fixed';
80
+ container.style.bottom = '10px';
81
+ container.style.left = '50%';
82
+ container.style.transform = 'translateX(-50%)';
83
+ } else if (this.options.position === 'bottom-right') {
84
+ container.style.position = 'fixed';
85
+ container.style.bottom = '10px';
86
+ container.style.right = '0px';
87
+ }
84
88
 
85
- document.body.appendChild(container);
86
- }
87
- }
89
+ document.body.appendChild(container);
90
+ }
91
+ }
88
92
 
89
- init() {
90
- this.ensureAlertContainer();
93
+ init() {
94
+ this.ensureAlertContainer();
91
95
 
92
- var alert = document.createElement('div');
93
- alert.className = `alert alert-${this.type}`;
94
- if (this.closeable) {
95
- alert.classList.add('alert-dismissible');
96
- }
97
- alert.classList.add('fade');
98
- alert.classList.add('show');
99
- alert.classList.add('p-0');
100
- alert.classList.add('overflow-hidden');
101
- alert.classList.add('avalynx-alert');
102
- alert.role = 'alert';
103
- alert.innerHTML = `<div class="alert-content">${this.message}`;
104
- if (this.closeable) {
105
- alert.innerHTML += `<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>`;
106
- }
107
- alert.innerHTML += `</div>`;
108
- if (this.autoClose) {
109
- alert.innerHTML += `<div class="alert-timer" style="height: 5px; width: 0;"></div>`;
110
- }
96
+ var alert = document.createElement('div');
97
+ alert.className = `alert alert-${this.type}`;
98
+ if (this.options.closeable) {
99
+ alert.classList.add('alert-dismissible');
100
+ }
101
+ alert.classList.add('fade');
102
+ alert.classList.add('show');
103
+ alert.classList.add('p-0');
104
+ alert.classList.add('overflow-hidden');
105
+ alert.classList.add('avalynx-alert');
106
+ alert.role = 'alert';
107
+ alert.innerHTML = `<div class="alert-content">${this.message}`;
108
+ if (this.options.closeable) {
109
+ alert.innerHTML += `<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>`;
110
+ }
111
+ alert.innerHTML += `</div>`;
112
+ if (this.options.autoClose) {
113
+ alert.innerHTML += `<div class="alert-timer" style="height: 5px; width: 0;"></div>`;
114
+ }
111
115
 
112
- document.getElementById('avalynx-alert-container-' + this.position).appendChild(alert);
116
+ document.getElementById('avalynx-alert-container-' + this.options.position).appendChild(alert);
113
117
 
114
- if (this.autoClose) {
115
- var timerBar = alert.querySelector('.alert-timer');
116
- timerBar.style.transition = `width ${this.duration}ms linear`;
118
+ if (this.options.autoClose) {
119
+ var timerBar = alert.querySelector('.alert-timer');
120
+ timerBar.style.transition = `width ${this.options.duration}ms linear`;
117
121
 
118
- requestAnimationFrame(() => {
119
- requestAnimationFrame(() => {
120
- timerBar.style.width = '100%';
121
- });
122
- });
122
+ requestAnimationFrame(() => {
123
+ requestAnimationFrame(() => {
124
+ timerBar.style.width = '100%';
125
+ });
126
+ });
123
127
 
124
- setTimeout(() => {
125
- alert.classList.remove('show');
126
- setTimeout(() => alert.remove(), 150);
127
- }, this.duration);
128
- }
129
- }
130
- }
128
+ setTimeout(() => {
129
+ alert.classList.remove('show');
130
+ setTimeout(() => {
131
+ alert.remove();
132
+ if (typeof this.options.onClose === 'function') {
133
+ this.options.onClose();
134
+ }
135
+ }, 150);
136
+ }, this.options.duration);
137
+ }
138
+ }
139
+ }
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * A simple alert system for web applications. Based on Bootstrap >=5.3 without any framework dependencies.
5
5
  *
6
- * @version 0.0.4
6
+ * @version 0.0.6
7
7
  * @license MIT
8
8
  * @author https://github.com/avalynx/avalynx-alert/graphs/contributors
9
9
  * @website https://github.com/avalynx/
@@ -18,113 +18,123 @@
18
18
  * @param {boolean} options.closeable - Whether the alert can be closed by the user. Default is true.
19
19
  * @param {boolean} options.autoClose - Whether the alert will close automatically after the duration. Default is true.
20
20
  * @param {string} options.width - The width of the alert. Default is '400px'.
21
+ * @param {function} options.onClose - A callback function to be called when the alert is closed.
21
22
  */
22
23
 
23
24
  class AvalynxAlert {
24
- constructor(message, type, options = {}) {
25
- this.message = message;
26
- this.type = type;
27
- this.duration = options.duration || 5000;
28
- this.position = options.position || 'top-center';
29
- this.closeable = options.closeable || true;
30
- this.autoClose = options.autoClose || true;
31
- this.width = options.width || '400px';
32
- if (!['primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark'].includes(this.type)) {
33
- this.type = 'info';
34
- }
35
- if (!['top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right'].includes(this.position)) {
36
- this.position = 'top-center';
37
- }
38
- if (this.autoClose !== false && this.autoClose !== true) {
39
- this.autoClose = true;
40
- }
41
- if ((this.closeable !== false && this.closeable !== true) || (this.autoClose === true)) {
42
- this.closeable = true;
43
- }
25
+ constructor(message, type, options = {}) {
26
+ this.message = message;
27
+ this.type = type;
28
+ this.options = {
29
+ duration: options.duration || 5000,
30
+ position: options.position || 'top-center',
31
+ closeable: options.closeable || true,
32
+ autoClose: options.autoClose || true,
33
+ width: options.width || '400px',
34
+ onClose: options.onClose || null,
35
+ ...options
36
+ };
37
+ if (!['primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark'].includes(this.type)) {
38
+ this.type = 'info';
39
+ }
40
+ if (!['top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right'].includes(this.options.position)) {
41
+ this.options.position = 'top-center';
42
+ }
43
+ if (this.options.autoClose !== false && this.options.autoClose !== true) {
44
+ this.options.autoClose = true;
45
+ }
46
+ if ((this.options.closeable !== false && this.options.closeable !== true) || (this.options.autoClose === true)) {
47
+ this.options.closeable = true;
48
+ }
44
49
 
45
- this.init();
46
- }
50
+ this.init();
51
+ }
47
52
 
48
- ensureAlertContainer() {
49
- if (!document.getElementById('avalynx-alert-container-' + this.position)) {
50
- var container = document.createElement('div');
51
- container.id = 'avalynx-alert-container-' + this.position;
52
- container.style.width = '100%';
53
- container.style.maxWidth = this.width;
54
- container.style.zIndex = '1000';
55
- container.classList.add('container-fluid');
53
+ ensureAlertContainer() {
54
+ if (!document.getElementById('avalynx-alert-container-' + this.options.position)) {
55
+ var container = document.createElement('div');
56
+ container.id = 'avalynx-alert-container-' + this.options.position;
57
+ container.style.width = '100%';
58
+ container.style.maxWidth = this.options.width;
59
+ container.style.zIndex = '1000';
60
+ container.classList.add('container-fluid');
56
61
 
57
- if (this.position === 'top-left') {
58
- container.style.position = 'fixed';
59
- container.style.top = '10px';
60
- container.style.left = '0px';
61
- } else if (this.position === 'top-center') {
62
- container.style.position = 'fixed';
63
- container.style.top = '10px';
64
- container.style.left = '50%';
65
- container.style.transform = 'translateX(-50%)';
66
- } else if (this.position === 'top-right') {
67
- container.style.position = 'fixed';
68
- container.style.top = '10px';
69
- container.style.right = '0px';
70
- } else if (this.position === 'bottom-left') {
71
- container.style.position = 'fixed';
72
- container.style.bottom = '10px';
73
- container.style.left = '0px';
74
- } else if (this.position === 'bottom-center') {
75
- container.style.position = 'fixed';
76
- container.style.bottom = '10px';
77
- container.style.left = '50%';
78
- container.style.transform = 'translateX(-50%)';
79
- } else if (this.position === 'bottom-right') {
80
- container.style.position = 'fixed';
81
- container.style.bottom = '10px';
82
- container.style.right = '0px';
83
- }
62
+ if (this.options.position === 'top-left') {
63
+ container.style.position = 'fixed';
64
+ container.style.top = '10px';
65
+ container.style.left = '0px';
66
+ } else if (this.options.position === 'top-center') {
67
+ container.style.position = 'fixed';
68
+ container.style.top = '10px';
69
+ container.style.left = '50%';
70
+ container.style.transform = 'translateX(-50%)';
71
+ } else if (this.options.position === 'top-right') {
72
+ container.style.position = 'fixed';
73
+ container.style.top = '10px';
74
+ container.style.right = '0px';
75
+ } else if (this.options.position === 'bottom-left') {
76
+ container.style.position = 'fixed';
77
+ container.style.bottom = '10px';
78
+ container.style.left = '0px';
79
+ } else if (this.options.position === 'bottom-center') {
80
+ container.style.position = 'fixed';
81
+ container.style.bottom = '10px';
82
+ container.style.left = '50%';
83
+ container.style.transform = 'translateX(-50%)';
84
+ } else if (this.options.position === 'bottom-right') {
85
+ container.style.position = 'fixed';
86
+ container.style.bottom = '10px';
87
+ container.style.right = '0px';
88
+ }
84
89
 
85
- document.body.appendChild(container);
86
- }
87
- }
90
+ document.body.appendChild(container);
91
+ }
92
+ }
88
93
 
89
- init() {
90
- this.ensureAlertContainer();
94
+ init() {
95
+ this.ensureAlertContainer();
91
96
 
92
- var alert = document.createElement('div');
93
- alert.className = `alert alert-${this.type}`;
94
- if (this.closeable) {
95
- alert.classList.add('alert-dismissible');
96
- }
97
- alert.classList.add('fade');
98
- alert.classList.add('show');
99
- alert.classList.add('p-0');
100
- alert.classList.add('overflow-hidden');
101
- alert.classList.add('avalynx-alert');
102
- alert.role = 'alert';
103
- alert.innerHTML = `<div class="alert-content">${this.message}`;
104
- if (this.closeable) {
105
- alert.innerHTML += `<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>`;
106
- }
107
- alert.innerHTML += `</div>`;
108
- if (this.autoClose) {
109
- alert.innerHTML += `<div class="alert-timer" style="height: 5px; width: 0;"></div>`;
110
- }
97
+ var alert = document.createElement('div');
98
+ alert.className = `alert alert-${this.type}`;
99
+ if (this.options.closeable) {
100
+ alert.classList.add('alert-dismissible');
101
+ }
102
+ alert.classList.add('fade');
103
+ alert.classList.add('show');
104
+ alert.classList.add('p-0');
105
+ alert.classList.add('overflow-hidden');
106
+ alert.classList.add('avalynx-alert');
107
+ alert.role = 'alert';
108
+ alert.innerHTML = `<div class="alert-content">${this.message}`;
109
+ if (this.options.closeable) {
110
+ alert.innerHTML += `<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>`;
111
+ }
112
+ alert.innerHTML += `</div>`;
113
+ if (this.options.autoClose) {
114
+ alert.innerHTML += `<div class="alert-timer" style="height: 5px; width: 0;"></div>`;
115
+ }
111
116
 
112
- document.getElementById('avalynx-alert-container-' + this.position).appendChild(alert);
117
+ document.getElementById('avalynx-alert-container-' + this.options.position).appendChild(alert);
113
118
 
114
- if (this.autoClose) {
115
- var timerBar = alert.querySelector('.alert-timer');
116
- timerBar.style.transition = `width ${this.duration}ms linear`;
119
+ if (this.options.autoClose) {
120
+ var timerBar = alert.querySelector('.alert-timer');
121
+ timerBar.style.transition = `width ${this.options.duration}ms linear`;
117
122
 
118
- requestAnimationFrame(() => {
119
- requestAnimationFrame(() => {
120
- timerBar.style.width = '100%';
121
- });
122
- });
123
+ requestAnimationFrame(() => {
124
+ requestAnimationFrame(() => {
125
+ timerBar.style.width = '100%';
126
+ });
127
+ });
123
128
 
124
- setTimeout(() => {
125
- alert.classList.remove('show');
126
- setTimeout(() => alert.remove(), 150);
127
- }, this.duration);
128
- }
129
- }
130
- }
129
+ setTimeout(() => {
130
+ alert.classList.remove('show');
131
+ setTimeout(() => {
132
+ alert.remove();
133
+ if (typeof this.options.onClose === 'function') {
134
+ this.options.onClose();
135
+ }
136
+ }, 150);
137
+ }, this.options.duration);
138
+ }
139
+ }
140
+ }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "avalynx-alert",
3
3
  "title": "AvalynxAlert",
4
4
  "description": "AvalynxAlert is a simple alert system for web applications. Based on Bootstrap >=5.3 without any framework dependencies.",
5
- "version": "0.0.4",
5
+ "version": "0.0.6",
6
6
  "license": "MIT",
7
7
  "main": "dist/js/avalynx-alert.js",
8
8
  "module": "dist/js/avalynx-alert.esm.js",
@@ -13,15 +13,14 @@
13
13
  "dist/js/avalynx-alert.esm.js"
14
14
  ],
15
15
  "keywords": [
16
+ "avalynx",
16
17
  "alert",
17
18
  "bootstrap",
19
+ "bootstrap5",
18
20
  "component",
19
21
  "notification",
20
22
  "popup"
21
23
  ],
22
- "dependencies": {
23
- "bootstrap": ">=5.3"
24
- },
25
24
  "ignore": [
26
25
  "package.json"
27
26
  ],
@@ -36,5 +35,18 @@
36
35
  },
37
36
  "bugs": {
38
37
  "url": "https://github.com/avalynx/avalynx-alert/issues"
38
+ },
39
+ "scripts": {
40
+ "test": "jest"
41
+ },
42
+ "dependencies": {
43
+ "bootstrap": ">=5.3"
44
+ },
45
+ "devDependencies": {
46
+ "@babel/core": "^7",
47
+ "@babel/preset-env": "^7",
48
+ "babel-jest": "^29",
49
+ "jest": "^29",
50
+ "jest-environment-jsdom": "^29"
39
51
  }
40
- }
52
+ }