@citygross/components 0.8.92 → 0.8.93

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.
@@ -440,6 +440,45 @@ var makeCateringPortionsString = function (variant, cartItem, unit) {
440
440
  return qString;
441
441
  };
442
442
 
443
+ var focusTrap = function (_a) {
444
+ var _b;
445
+ var exitFunction = _a.exitFunction, ref = _a.ref;
446
+ var elements = 'button:not([disabled]), [href]:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
447
+ var focusableElements = (_b = ref.current) === null || _b === void 0 ? void 0 : _b.querySelectorAll(elements);
448
+ if (focusableElements &&
449
+ focusableElements.length > 0 &&
450
+ focusableElements[0] instanceof HTMLElement) {
451
+ focusableElements[0].focus();
452
+ }
453
+ var handleKeyDown = function (e) {
454
+ if (!ref.current)
455
+ return;
456
+ var focusable = Array.from(ref.current.querySelectorAll(elements));
457
+ if (focusable.length === 0)
458
+ return;
459
+ var firstElement = focusable[0];
460
+ var lastElement = focusable[focusable.length - 1];
461
+ if (e.key === 'Tab') {
462
+ if (e.shiftKey && document.activeElement === firstElement) {
463
+ e.preventDefault();
464
+ lastElement.focus();
465
+ }
466
+ else if (!e.shiftKey && document.activeElement === lastElement) {
467
+ e.preventDefault();
468
+ firstElement.focus();
469
+ }
470
+ }
471
+ else if (e.key === 'Escape')
472
+ exitFunction();
473
+ };
474
+ if (ref.current)
475
+ ref.current.addEventListener('keydown', handleKeyDown);
476
+ return function () {
477
+ if (ref.current)
478
+ ref.current.removeEventListener('keydown', handleKeyDown);
479
+ };
480
+ };
481
+
443
482
  var formatLeadingZero = function (day) { return day.replace(/^0+/, ''); };
444
483
 
445
484
  var formatPostalCode = function (postalCode, spaceIndex) {
@@ -18841,6 +18880,7 @@ build.capitalizeFirstLetter = capitalizeFirstLetter;
18841
18880
  build.capitalizedAllWords = capitalizedAllWords;
18842
18881
  build.comparisonTypes = comparisonTypes;
18843
18882
  build.emailRegex = emailRegex;
18883
+ build.focusTrap = focusTrap;
18844
18884
  build.formatDateToLocale = formatDateToLocale;
18845
18885
  build.formatLeadingZero = formatLeadingZero;
18846
18886
  build.formatPostalCode = formatPostalCode;