frayerjj-frontend 0.8.8 → 0.8.10

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/package.json +1 -1
  2. package/src/modal.js +23 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "frayerjj-frontend",
3
- "version": "0.8.8",
3
+ "version": "0.8.10",
4
4
  "description": "My base frontend for various projects",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/modal.js CHANGED
@@ -32,11 +32,14 @@ export const modal = {
32
32
  }));
33
33
  let ajaxModal = document.getElementById(randomId),
34
34
  ajaxModalBody = ajaxModal.querySelector('.modal-body');
35
- ajaxModal.addEventListener('hidden.bs.modal', ev => {
35
+ ajaxModal.addEventListener('hide.bs.modal', ev => {
36
36
  modal.focusFix();
37
+ });
38
+ ajaxModal.addEventListener('hidden.bs.modal', ev => {
37
39
  ev.target.remove();
38
40
  });
39
41
  let bsAjaxModal = Modal.getOrCreateInstance(ajaxModal);
42
+ modal._lastFocus = document.activeElement;
40
43
  bsAjaxModal.show();
41
44
  loading.start(0, ajaxModalBody);
42
45
  msg.verbose('Loading AJAX Modal');
@@ -91,7 +94,7 @@ export const modal = {
91
94
 
92
95
  alert: (messsage, title = "Alert", button = "OK") => {
93
96
  msg.verbose('Building Alert Modal');
94
- let randomId = modal.randomId('confirm');
97
+ let randomId = modal.randomId('alert');
95
98
  document.querySelector('body').insertAdjacentHTML('beforeend', modal.build.modal({
96
99
  id: randomId,
97
100
  title: title,
@@ -100,11 +103,13 @@ export const modal = {
100
103
  }));
101
104
  let alertModal = document.getElementById(randomId),
102
105
  bsAlertModal = Modal.getOrCreateInstance(alertModal);
103
- alertModal.addEventListener('hidden.bs.modal', ev => {
106
+ alertModal.addEventListener('hide.bs.modal', ev => {
104
107
  modal.focusFix();
108
+ });
109
+ alertModal.addEventListener('hidden.bs.modal', ev => {
105
110
  ev.target.remove();
106
111
  });
107
- modal.close();
112
+ modal._lastFocus = document.activeElement;
108
113
  bsAlertModal.show();
109
114
  },
110
115
 
@@ -276,8 +281,10 @@ export const modal = {
276
281
  }));
277
282
  let confirmModal = document.getElementById(randomId),
278
283
  bsConfirmModal = Modal.getOrCreateInstance(confirmModal);
279
- confirmModal.addEventListener('hidden.bs.modal', ev => {
284
+ confirmModal.addEventListener('hide.bs.modal', ev => {
280
285
  modal.focusFix();
286
+ });
287
+ confirmModal.addEventListener('hidden.bs.modal', ev => {
281
288
  ev.target.remove();
282
289
  });
283
290
  confirmModal.querySelector('.btn-confirm').addEventListener('click', () => {
@@ -286,13 +293,21 @@ export const modal = {
286
293
  confirmModal.querySelector('.btn-outline-danger').addEventListener('click', () => {
287
294
  onCancel();
288
295
  });
289
- modal.close();
296
+ modal._lastFocus = document.activeElement;
290
297
  bsConfirmModal.show();
291
298
  },
292
299
 
293
300
  focusFix: () => {
294
- if (document.activeElement === document.body || !document.activeElement)
295
- document.querySelector('main') ?.focus() || document.body.focus();
301
+ setTimeout(() => {
302
+ if (modal._lastFocus && document.contains(modal._lastFocus)) {
303
+ modal._lastFocus.focus();
304
+ return;
305
+ }
306
+ const fallback = document.querySelector('main') || document.body;
307
+ if (!fallback.hasAttribute('tabindex'))
308
+ fallback.setAttribute('tabindex', '-1');
309
+ fallback.focus();
310
+ }, 0);
296
311
  },
297
312
 
298
313
  randomId: (base = 'modal') => {