@marianmeres/stuic 1.13.0 → 1.14.0

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.
@@ -1,21 +1,20 @@
1
- // copied from skeleton
1
+ // initially copied from skeleton, adjusted
2
2
  const defaults = { enabled: true, autoFocusFirst: true };
3
- // Action: Focus Trap
4
3
  export function focusTrap(node, options = {}) {
5
4
  let { enabled, autoFocusFirst } = { ...defaults, ...(options || {}) };
6
5
  const focusableSelectors = [
7
- '[contentEditable=true]:not([tabindex^="-"])',
6
+ '[contentEditable=true]',
8
7
  //
9
- 'button:not([disabled]):not([tabindex^="-"])',
10
- 'input:not([disabled]):not([tabindex^="-"])',
11
- 'select:not([disabled]):not([tabindex^="-"])',
12
- 'textarea:not([disabled]):not([tabindex^="-"])',
13
- //
14
- 'a[href]:not([tabindex^="-"])',
15
- 'area[href]:not([tabindex^="-"])',
16
- 'details:not([tabindex^="-"])',
17
- 'iframe:not([tabindex^="-"])',
8
+ 'button:not([disabled])',
9
+ 'input:not([disabled])',
10
+ 'select:not([disabled])',
11
+ 'textarea:not([disabled])',
18
12
  //
13
+ 'a[href]',
14
+ 'area[href]',
15
+ 'details',
16
+ 'iframe',
17
+ // see more below on tabindexes
19
18
  '[tabindex]:not([tabindex^="-"])',
20
19
  ].join(',');
21
20
  let first;
@@ -37,10 +36,13 @@ export function focusTrap(node, options = {}) {
37
36
  const queryElements = (fromObserver) => {
38
37
  if (enabled === false)
39
38
  return;
40
- const focusable = [...node.querySelectorAll(focusableSelectors)]
41
- // in case I didn't get the selectors right, make sure to manually check as well...
42
- // (negligible overhead, if any...)
39
+ let maxTabindex = 0;
40
+ let focusable = [...node.querySelectorAll(focusableSelectors)]
41
+ // filter negative tabindexes (afaik there is no :not([disabled] OR [tabindex^="-"]))
43
42
  .filter((e) => {
43
+ // reusing loop for a side job here... see sort below
44
+ maxTabindex = Math.max(maxTabindex, parseInt(e.getAttribute('tabindex') || '0'));
45
+ //
44
46
  if (e.getAttribute('disabled') === '')
45
47
  return false;
46
48
  if ((e.getAttribute('tabindex') || '').startsWith('-'))
@@ -48,9 +50,11 @@ export function focusTrap(node, options = {}) {
48
50
  return true;
49
51
  })
50
52
  // important to sort by tabindex, so the first/last will work as expected
53
+ // but must increase zero to max + 1 first, because browsers focus zeros as last...
54
+ // https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex
51
55
  .sort((e1, e2) => {
52
- const a = parseInt(e1.getAttribute('tabindex') || '0');
53
- const b = parseInt(e2.getAttribute('tabindex') || '0');
56
+ const a = parseInt(e1.getAttribute('tabindex') || '0') || maxTabindex + 1;
57
+ const b = parseInt(e2.getAttribute('tabindex') || '0') || maxTabindex + 1;
54
58
  return a - b;
55
59
  });
56
60
  if (focusable.length) {
@@ -66,20 +70,17 @@ export function focusTrap(node, options = {}) {
66
70
  };
67
71
  queryElements(false);
68
72
  function cleanup() {
69
- if (first)
70
- first.removeEventListener('keydown', onFirstElemKeydown);
71
- if (last)
72
- last.removeEventListener('keydown', onLastElemKeydown);
73
+ first && first.removeEventListener('keydown', onFirstElemKeydown);
74
+ last && last.removeEventListener('keydown', onLastElemKeydown);
73
75
  }
74
76
  // When children of node are changed (added or removed)
75
- const onObservationChange = (mutationRecords, observer) => {
76
- if (mutationRecords.length) {
77
+ const observer = new MutationObserver((mutations, observer) => {
78
+ if (mutations.length) {
77
79
  cleanup();
78
80
  queryElements(true);
79
81
  }
80
82
  return observer;
81
- };
82
- const observer = new MutationObserver(onObservationChange);
83
+ });
83
84
  observer.observe(node, { childList: true, subtree: true });
84
85
  // Lifecycle
85
86
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marianmeres/stuic",
3
- "version": "1.13.0",
3
+ "version": "1.14.0",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package && node ./scripts/date.js",