@y14e/disclosure-css 2.2.0 → 2.2.2

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.
@@ -345,6 +345,64 @@ function isFocusable(element, options = {}) {
345
345
 
346
346
  // node_modules/@y14e/roving-tabindex/dist/index.js
347
347
  var DIRECTIONS = ["both", "grid", "horizontal", "vertical"];
348
+ function getCellCoords(cell) {
349
+ const { left, top, width, height } = cell.getBoundingClientRect();
350
+ return { x: left + width / 2, y: top + height / 2 };
351
+ }
352
+ function getCellsOfActiveRow(cells) {
353
+ const active = getActiveElement();
354
+ if (!(active instanceof Element)) {
355
+ return [];
356
+ }
357
+ const activeRect = active.getBoundingClientRect();
358
+ return cells.filter((cell) => {
359
+ const cellRect = cell.getBoundingClientRect();
360
+ return cellRect.top < activeRect.bottom && cellRect.bottom > activeRect.top;
361
+ });
362
+ }
363
+ function getNextCell(cells, key, wrap) {
364
+ const active = getActiveElement();
365
+ if (!(active instanceof Element)) {
366
+ return null;
367
+ }
368
+ const activeCoords = getCellCoords(active);
369
+ let min = Infinity;
370
+ let result = null;
371
+ for (const cell of cells) {
372
+ if (cell === active) {
373
+ continue;
374
+ }
375
+ const cellCoords = getCellCoords(cell);
376
+ const distances = {
377
+ x: Math.abs(cellCoords.x - activeCoords.x),
378
+ y: Math.abs(cellCoords.y - activeCoords.y)
379
+ };
380
+ if (key === "ArrowUp" && cellCoords.y < activeCoords.y && distances.x < distances.y || key === "ArrowDown" && cellCoords.y > activeCoords.y && distances.x < distances.y) {
381
+ const distance = distances.x ** 2 + distances.y ** 2;
382
+ if (distance < min) {
383
+ min = distance;
384
+ result = cell;
385
+ }
386
+ }
387
+ }
388
+ if (!result && wrap) {
389
+ let max = -Infinity;
390
+ for (const cell of cells) {
391
+ if (cell === active) {
392
+ continue;
393
+ }
394
+ const cellCoords = getCellCoords(cell);
395
+ if (Math.abs(cellCoords.x - activeCoords.x) < cell.getBoundingClientRect().width / 2) {
396
+ const distance = key === "ArrowUp" ? cellCoords.y - activeCoords.y : activeCoords.y - cellCoords.y;
397
+ if (distance > max) {
398
+ max = distance;
399
+ result = cell;
400
+ }
401
+ }
402
+ }
403
+ }
404
+ return result;
405
+ }
348
406
  function resolveOptions3(options) {
349
407
  let {
350
408
  direction = "both",
@@ -358,7 +416,7 @@ function resolveOptions3(options) {
358
416
  if (typeof direction === "string") {
359
417
  direction = direction.toLowerCase();
360
418
  }
361
- if (!DIRECTIONS.includes(direction)) {
419
+ if (typeof direction === "string" && !DIRECTIONS.includes(direction) || typeof direction === "function" && !DIRECTIONS.includes(direction())) {
362
420
  console.warn("Invalid direction option. Fallback: 'both'.");
363
421
  direction = "both";
364
422
  }
@@ -458,7 +516,7 @@ var RovingTabIndex = class _RovingTabIndex {
458
516
  (!(target instanceof Element) || !this.#focusables.has(target)) && this.#update();
459
517
  };
460
518
  #onKeyDown = (event) => {
461
- const { direction, typeahead, wrap = false } = this.#settings;
519
+ const direction = this.#getDirection();
462
520
  const isGrid = direction === "grid";
463
521
  const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
464
522
  if (!(isGrid && ["End", "Home"].includes(key)) && ctrlKey || altKey || metaKey || shiftKey) {
@@ -466,6 +524,7 @@ var RovingTabIndex = class _RovingTabIndex {
466
524
  }
467
525
  const isBoth = direction === "both";
468
526
  const isHorizontal = direction === "horizontal";
527
+ const { typeahead, wrap = false } = this.#settings;
469
528
  if (![
470
529
  "End",
471
530
  "Home",
@@ -601,20 +660,16 @@ var RovingTabIndex = class _RovingTabIndex {
601
660
  const { selector } = this.#settings;
602
661
  return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
603
662
  }
604
- #getCellCoords(cell) {
605
- const { left, top, width, height } = cell.getBoundingClientRect();
606
- return { x: left + width / 2, y: top + height / 2 };
607
- }
608
663
  #getCellsOfActiveRow() {
609
- const active = getActiveElement();
610
- if (!(active instanceof Element)) {
611
- return [];
664
+ return getCellsOfActiveRow(this.#getFocusables());
665
+ }
666
+ #getDirection() {
667
+ const { direction } = this.#settings;
668
+ if (typeof direction === "string") {
669
+ return direction;
612
670
  }
613
- const activeRect = active.getBoundingClientRect();
614
- return this.#getFocusables().filter((cell) => {
615
- const cellRect = cell.getBoundingClientRect();
616
- return cellRect.top < activeRect.bottom && cellRect.bottom > activeRect.top;
617
- });
671
+ const d = direction();
672
+ return DIRECTIONS.includes(d) ? d : "both";
618
673
  }
619
674
  #getFocusables() {
620
675
  return getFocusables(this.#container, {
@@ -625,48 +680,7 @@ var RovingTabIndex = class _RovingTabIndex {
625
680
  });
626
681
  }
627
682
  #getNextCell(key, wrap) {
628
- const cells = this.#getFocusables();
629
- const active = getActiveElement();
630
- if (!(active instanceof Element)) {
631
- return null;
632
- }
633
- const activeCoords = this.#getCellCoords(active);
634
- let min = Infinity;
635
- let result = null;
636
- for (const cell of cells) {
637
- if (cell === active) {
638
- continue;
639
- }
640
- const cellCoords = this.#getCellCoords(cell);
641
- const distances = {
642
- x: Math.abs(cellCoords.x - activeCoords.x),
643
- y: Math.abs(cellCoords.y - activeCoords.y)
644
- };
645
- if (key === "ArrowUp" && cellCoords.y < activeCoords.y && distances.x < distances.y || key === "ArrowDown" && cellCoords.y > activeCoords.y && distances.x < distances.y) {
646
- const distance = distances.x ** 2 + distances.y ** 2;
647
- if (distance < min) {
648
- min = distance;
649
- result = cell;
650
- }
651
- }
652
- }
653
- if (!result && wrap) {
654
- let max = -Infinity;
655
- for (const cell of cells) {
656
- if (cell === active) {
657
- continue;
658
- }
659
- const cellCoords = this.#getCellCoords(cell);
660
- if (Math.abs(cellCoords.x - activeCoords.x) < cell.getBoundingClientRect().width / 2) {
661
- const distance = key === "ArrowUp" ? cellCoords.y - activeCoords.y : activeCoords.y - cellCoords.y;
662
- if (distance > max) {
663
- max = distance;
664
- result = cell;
665
- }
666
- }
667
- }
668
- }
669
- return result;
683
+ return getNextCell(this.#getFocusables(), key, wrap);
670
684
  }
671
685
  };
672
686
 
@@ -688,7 +702,6 @@ function resolveOptions4(defaults2, source) {
688
702
  var Disclosure = class _Disclosure {
689
703
  static defaults = {};
690
704
  #rootElement;
691
- #defaults = defaults;
692
705
  #settings;
693
706
  #detailsElements;
694
707
  #summaryElements;
@@ -706,8 +719,10 @@ var Disclosure = class _Disclosure {
706
719
  return;
707
720
  }
708
721
  this.#rootElement = root;
709
- this.#defaults = resolveOptions4(this.#defaults, _Disclosure.defaults);
710
- this.#settings = resolveOptions4(this.#defaults, options);
722
+ this.#settings = resolveOptions4(
723
+ resolveOptions4(defaults, _Disclosure.defaults),
724
+ options
725
+ );
711
726
  const NOT_NESTED = ":not(:scope summary + * *)";
712
727
  this.#detailsElements = [
713
728
  ...this.#rootElement.querySelectorAll(
@@ -743,9 +758,9 @@ var Disclosure = class _Disclosure {
743
758
  return;
744
759
  }
745
760
  const binding = this.#createBinding(details, summary, content);
746
- this.#bindings.set(details, binding);
747
- this.#bindings.set(summary, binding);
748
- this.#bindings.set(content, binding);
761
+ [details, summary, content].forEach((element) => {
762
+ this.#bindings.set(element, binding);
763
+ });
749
764
  });
750
765
  this.#initialize();
751
766
  }
@@ -814,12 +829,8 @@ var Disclosure = class _Disclosure {
814
829
  if (!(summary instanceof HTMLElement)) {
815
830
  return;
816
831
  }
817
- const binding = this.#bindings.get(summary);
818
- if (!binding) {
819
- return;
820
- }
821
- const { details } = binding;
822
- this.#toggle(details, !details.open);
832
+ const details = this.#bindings.get(summary)?.details;
833
+ details && this.#toggle(details, !details.open);
823
834
  };
824
835
  #toggle(details, isExpand) {
825
836
  if (details.open === isExpand) {
@@ -343,6 +343,64 @@ function isFocusable(element, options = {}) {
343
343
 
344
344
  // node_modules/@y14e/roving-tabindex/dist/index.js
345
345
  var DIRECTIONS = ["both", "grid", "horizontal", "vertical"];
346
+ function getCellCoords(cell) {
347
+ const { left, top, width, height } = cell.getBoundingClientRect();
348
+ return { x: left + width / 2, y: top + height / 2 };
349
+ }
350
+ function getCellsOfActiveRow(cells) {
351
+ const active = getActiveElement();
352
+ if (!(active instanceof Element)) {
353
+ return [];
354
+ }
355
+ const activeRect = active.getBoundingClientRect();
356
+ return cells.filter((cell) => {
357
+ const cellRect = cell.getBoundingClientRect();
358
+ return cellRect.top < activeRect.bottom && cellRect.bottom > activeRect.top;
359
+ });
360
+ }
361
+ function getNextCell(cells, key, wrap) {
362
+ const active = getActiveElement();
363
+ if (!(active instanceof Element)) {
364
+ return null;
365
+ }
366
+ const activeCoords = getCellCoords(active);
367
+ let min = Infinity;
368
+ let result = null;
369
+ for (const cell of cells) {
370
+ if (cell === active) {
371
+ continue;
372
+ }
373
+ const cellCoords = getCellCoords(cell);
374
+ const distances = {
375
+ x: Math.abs(cellCoords.x - activeCoords.x),
376
+ y: Math.abs(cellCoords.y - activeCoords.y)
377
+ };
378
+ if (key === "ArrowUp" && cellCoords.y < activeCoords.y && distances.x < distances.y || key === "ArrowDown" && cellCoords.y > activeCoords.y && distances.x < distances.y) {
379
+ const distance = distances.x ** 2 + distances.y ** 2;
380
+ if (distance < min) {
381
+ min = distance;
382
+ result = cell;
383
+ }
384
+ }
385
+ }
386
+ if (!result && wrap) {
387
+ let max = -Infinity;
388
+ for (const cell of cells) {
389
+ if (cell === active) {
390
+ continue;
391
+ }
392
+ const cellCoords = getCellCoords(cell);
393
+ if (Math.abs(cellCoords.x - activeCoords.x) < cell.getBoundingClientRect().width / 2) {
394
+ const distance = key === "ArrowUp" ? cellCoords.y - activeCoords.y : activeCoords.y - cellCoords.y;
395
+ if (distance > max) {
396
+ max = distance;
397
+ result = cell;
398
+ }
399
+ }
400
+ }
401
+ }
402
+ return result;
403
+ }
346
404
  function resolveOptions3(options) {
347
405
  let {
348
406
  direction = "both",
@@ -356,7 +414,7 @@ function resolveOptions3(options) {
356
414
  if (typeof direction === "string") {
357
415
  direction = direction.toLowerCase();
358
416
  }
359
- if (!DIRECTIONS.includes(direction)) {
417
+ if (typeof direction === "string" && !DIRECTIONS.includes(direction) || typeof direction === "function" && !DIRECTIONS.includes(direction())) {
360
418
  console.warn("Invalid direction option. Fallback: 'both'.");
361
419
  direction = "both";
362
420
  }
@@ -456,7 +514,7 @@ var RovingTabIndex = class _RovingTabIndex {
456
514
  (!(target instanceof Element) || !this.#focusables.has(target)) && this.#update();
457
515
  };
458
516
  #onKeyDown = (event) => {
459
- const { direction, typeahead, wrap = false } = this.#settings;
517
+ const direction = this.#getDirection();
460
518
  const isGrid = direction === "grid";
461
519
  const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
462
520
  if (!(isGrid && ["End", "Home"].includes(key)) && ctrlKey || altKey || metaKey || shiftKey) {
@@ -464,6 +522,7 @@ var RovingTabIndex = class _RovingTabIndex {
464
522
  }
465
523
  const isBoth = direction === "both";
466
524
  const isHorizontal = direction === "horizontal";
525
+ const { typeahead, wrap = false } = this.#settings;
467
526
  if (![
468
527
  "End",
469
528
  "Home",
@@ -599,20 +658,16 @@ var RovingTabIndex = class _RovingTabIndex {
599
658
  const { selector } = this.#settings;
600
659
  return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
601
660
  }
602
- #getCellCoords(cell) {
603
- const { left, top, width, height } = cell.getBoundingClientRect();
604
- return { x: left + width / 2, y: top + height / 2 };
605
- }
606
661
  #getCellsOfActiveRow() {
607
- const active = getActiveElement();
608
- if (!(active instanceof Element)) {
609
- return [];
662
+ return getCellsOfActiveRow(this.#getFocusables());
663
+ }
664
+ #getDirection() {
665
+ const { direction } = this.#settings;
666
+ if (typeof direction === "string") {
667
+ return direction;
610
668
  }
611
- const activeRect = active.getBoundingClientRect();
612
- return this.#getFocusables().filter((cell) => {
613
- const cellRect = cell.getBoundingClientRect();
614
- return cellRect.top < activeRect.bottom && cellRect.bottom > activeRect.top;
615
- });
669
+ const d = direction();
670
+ return DIRECTIONS.includes(d) ? d : "both";
616
671
  }
617
672
  #getFocusables() {
618
673
  return getFocusables(this.#container, {
@@ -623,48 +678,7 @@ var RovingTabIndex = class _RovingTabIndex {
623
678
  });
624
679
  }
625
680
  #getNextCell(key, wrap) {
626
- const cells = this.#getFocusables();
627
- const active = getActiveElement();
628
- if (!(active instanceof Element)) {
629
- return null;
630
- }
631
- const activeCoords = this.#getCellCoords(active);
632
- let min = Infinity;
633
- let result = null;
634
- for (const cell of cells) {
635
- if (cell === active) {
636
- continue;
637
- }
638
- const cellCoords = this.#getCellCoords(cell);
639
- const distances = {
640
- x: Math.abs(cellCoords.x - activeCoords.x),
641
- y: Math.abs(cellCoords.y - activeCoords.y)
642
- };
643
- if (key === "ArrowUp" && cellCoords.y < activeCoords.y && distances.x < distances.y || key === "ArrowDown" && cellCoords.y > activeCoords.y && distances.x < distances.y) {
644
- const distance = distances.x ** 2 + distances.y ** 2;
645
- if (distance < min) {
646
- min = distance;
647
- result = cell;
648
- }
649
- }
650
- }
651
- if (!result && wrap) {
652
- let max = -Infinity;
653
- for (const cell of cells) {
654
- if (cell === active) {
655
- continue;
656
- }
657
- const cellCoords = this.#getCellCoords(cell);
658
- if (Math.abs(cellCoords.x - activeCoords.x) < cell.getBoundingClientRect().width / 2) {
659
- const distance = key === "ArrowUp" ? cellCoords.y - activeCoords.y : activeCoords.y - cellCoords.y;
660
- if (distance > max) {
661
- max = distance;
662
- result = cell;
663
- }
664
- }
665
- }
666
- }
667
- return result;
681
+ return getNextCell(this.#getFocusables(), key, wrap);
668
682
  }
669
683
  };
670
684
 
@@ -686,7 +700,6 @@ function resolveOptions4(defaults2, source) {
686
700
  var Disclosure = class _Disclosure {
687
701
  static defaults = {};
688
702
  #rootElement;
689
- #defaults = defaults;
690
703
  #settings;
691
704
  #detailsElements;
692
705
  #summaryElements;
@@ -704,8 +717,10 @@ var Disclosure = class _Disclosure {
704
717
  return;
705
718
  }
706
719
  this.#rootElement = root;
707
- this.#defaults = resolveOptions4(this.#defaults, _Disclosure.defaults);
708
- this.#settings = resolveOptions4(this.#defaults, options);
720
+ this.#settings = resolveOptions4(
721
+ resolveOptions4(defaults, _Disclosure.defaults),
722
+ options
723
+ );
709
724
  const NOT_NESTED = ":not(:scope summary + * *)";
710
725
  this.#detailsElements = [
711
726
  ...this.#rootElement.querySelectorAll(
@@ -741,9 +756,9 @@ var Disclosure = class _Disclosure {
741
756
  return;
742
757
  }
743
758
  const binding = this.#createBinding(details, summary, content);
744
- this.#bindings.set(details, binding);
745
- this.#bindings.set(summary, binding);
746
- this.#bindings.set(content, binding);
759
+ [details, summary, content].forEach((element) => {
760
+ this.#bindings.set(element, binding);
761
+ });
747
762
  });
748
763
  this.#initialize();
749
764
  }
@@ -812,12 +827,8 @@ var Disclosure = class _Disclosure {
812
827
  if (!(summary instanceof HTMLElement)) {
813
828
  return;
814
829
  }
815
- const binding = this.#bindings.get(summary);
816
- if (!binding) {
817
- return;
818
- }
819
- const { details } = binding;
820
- this.#toggle(details, !details.open);
830
+ const details = this.#bindings.get(summary)?.details;
831
+ details && this.#toggle(details, !details.open);
821
832
  };
822
833
  #toggle(details, isExpand) {
823
834
  if (details.open === isExpand) {
package/dist/index.cjs CHANGED
@@ -23,7 +23,6 @@ function resolveOptions(defaults2, source) {
23
23
  var Disclosure = class _Disclosure {
24
24
  static defaults = {};
25
25
  #rootElement;
26
- #defaults = defaults;
27
26
  #settings;
28
27
  #detailsElements;
29
28
  #summaryElements;
@@ -41,8 +40,10 @@ var Disclosure = class _Disclosure {
41
40
  return;
42
41
  }
43
42
  this.#rootElement = root;
44
- this.#defaults = resolveOptions(this.#defaults, _Disclosure.defaults);
45
- this.#settings = resolveOptions(this.#defaults, options);
43
+ this.#settings = resolveOptions(
44
+ resolveOptions(defaults, _Disclosure.defaults),
45
+ options
46
+ );
46
47
  const NOT_NESTED = ":not(:scope summary + * *)";
47
48
  this.#detailsElements = [
48
49
  ...this.#rootElement.querySelectorAll(
@@ -78,9 +79,9 @@ var Disclosure = class _Disclosure {
78
79
  return;
79
80
  }
80
81
  const binding = this.#createBinding(details, summary, content);
81
- this.#bindings.set(details, binding);
82
- this.#bindings.set(summary, binding);
83
- this.#bindings.set(content, binding);
82
+ [details, summary, content].forEach((element) => {
83
+ this.#bindings.set(element, binding);
84
+ });
84
85
  });
85
86
  this.#initialize();
86
87
  }
@@ -149,12 +150,8 @@ var Disclosure = class _Disclosure {
149
150
  if (!(summary instanceof HTMLElement)) {
150
151
  return;
151
152
  }
152
- const binding = this.#bindings.get(summary);
153
- if (!binding) {
154
- return;
155
- }
156
- const { details } = binding;
157
- this.#toggle(details, !details.open);
153
+ const details = this.#bindings.get(summary)?.details;
154
+ details && this.#toggle(details, !details.open);
158
155
  };
159
156
  #toggle(details, isExpand) {
160
157
  if (details.open === isExpand) {
package/dist/index.js CHANGED
@@ -21,7 +21,6 @@ function resolveOptions(defaults2, source) {
21
21
  var Disclosure = class _Disclosure {
22
22
  static defaults = {};
23
23
  #rootElement;
24
- #defaults = defaults;
25
24
  #settings;
26
25
  #detailsElements;
27
26
  #summaryElements;
@@ -39,8 +38,10 @@ var Disclosure = class _Disclosure {
39
38
  return;
40
39
  }
41
40
  this.#rootElement = root;
42
- this.#defaults = resolveOptions(this.#defaults, _Disclosure.defaults);
43
- this.#settings = resolveOptions(this.#defaults, options);
41
+ this.#settings = resolveOptions(
42
+ resolveOptions(defaults, _Disclosure.defaults),
43
+ options
44
+ );
44
45
  const NOT_NESTED = ":not(:scope summary + * *)";
45
46
  this.#detailsElements = [
46
47
  ...this.#rootElement.querySelectorAll(
@@ -76,9 +77,9 @@ var Disclosure = class _Disclosure {
76
77
  return;
77
78
  }
78
79
  const binding = this.#createBinding(details, summary, content);
79
- this.#bindings.set(details, binding);
80
- this.#bindings.set(summary, binding);
81
- this.#bindings.set(content, binding);
80
+ [details, summary, content].forEach((element) => {
81
+ this.#bindings.set(element, binding);
82
+ });
82
83
  });
83
84
  this.#initialize();
84
85
  }
@@ -147,12 +148,8 @@ var Disclosure = class _Disclosure {
147
148
  if (!(summary instanceof HTMLElement)) {
148
149
  return;
149
150
  }
150
- const binding = this.#bindings.get(summary);
151
- if (!binding) {
152
- return;
153
- }
154
- const { details } = binding;
155
- this.#toggle(details, !details.open);
151
+ const details = this.#bindings.get(summary)?.details;
152
+ details && this.#toggle(details, !details.open);
156
153
  };
157
154
  #toggle(details, isExpand) {
158
155
  if (details.open === isExpand) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@y14e/disclosure-css",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "WAI-ARIA compliant disclosure pattern implementation in TypeScript",
5
5
  "keywords": [
6
6
  "a11y",
@@ -54,7 +54,7 @@
54
54
  "esbuild": true
55
55
  },
56
56
  "dependencies": {
57
- "@y14e/attribute-utils": "^2.0.16",
58
- "@y14e/roving-tabindex": "^3.4.0"
57
+ "@y14e/attribute-utils": "^2.0.18",
58
+ "@y14e/roving-tabindex": "^3.5.1"
59
59
  }
60
60
  }