@danielgindi/selectbox 2.0.48 → 2.0.49

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/dist/lib.es6.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @danielgindi/selectbox 2.0.48
2
+ * @danielgindi/selectbox 2.0.49
3
3
  * git://github.com/danielgindi/selectbox.git
4
4
  */
5
5
  import { createElement, closestUntil, setElementAttrs, next, prev } from '@danielgindi/dom-utils/lib/Dom';
@@ -3496,6 +3496,42 @@ const defaultMultiPlaceholderFormatter = (items, labelProp) => {
3496
3496
  return title;
3497
3497
  };
3498
3498
 
3499
+
3500
+ function getFocusState(element) {
3501
+ if (!element)
3502
+ return null;
3503
+
3504
+ const state = { element };
3505
+
3506
+ if (typeof element.selectionStart === 'number' &&
3507
+ typeof element.selectionEnd === 'number') {
3508
+ state.selectionStart = element.selectionStart;
3509
+ state.selectionEnd = element.selectionEnd;
3510
+ state.selectionDirection = element.selectionDirection;
3511
+ }
3512
+
3513
+ return state;
3514
+ }
3515
+
3516
+ function restoreFocusState(state) {
3517
+ if (!state?.element || !document.body.contains(state.element))
3518
+ return false;
3519
+
3520
+ state.element.focus();
3521
+
3522
+ if (typeof state.selectionStart === 'number' &&
3523
+ typeof state.selectionEnd === 'number' &&
3524
+ typeof state.element.setSelectionRange === 'function') {
3525
+ state.element.setSelectionRange(
3526
+ state.selectionStart,
3527
+ state.selectionEnd,
3528
+ state.selectionDirection ?? 'none',
3529
+ );
3530
+ }
3531
+
3532
+ return true;
3533
+ }
3534
+
3499
3535
  /*
3500
3536
  Emits the following events:
3501
3537
  ---------------------------
@@ -4760,6 +4796,8 @@ class SelectBox {
4760
4796
 
4761
4797
  this._trigger('open:before', { list: p.dropList });
4762
4798
 
4799
+ p.lastActiveElement = document.activeElement === document.body ? null : getFocusState(document.activeElement);
4800
+
4763
4801
  // Propagate direction to droplist
4764
4802
  p.dropList.setDirection(getComputedStyle(p.el).direction);
4765
4803
 
@@ -4794,6 +4832,11 @@ class SelectBox {
4794
4832
 
4795
4833
  p.dropList.hide();
4796
4834
 
4835
+ if (p.lastActiveElement) {
4836
+ restoreFocusState(p.lastActiveElement);
4837
+ p.lastActiveElement = null;
4838
+ }
4839
+
4797
4840
  return this;
4798
4841
  }
4799
4842
 
@@ -5386,9 +5429,10 @@ class SelectBox {
5386
5429
 
5387
5430
  this.closeList();
5388
5431
 
5389
- if (p.blurOnSingleSelection === 'touch' && hasTouchCapability ||
5390
- p.blurOnSingleSelection !== 'touch' && p.blurOnSingleSelection) {
5391
- p.input && p.input.blur();
5432
+ if ((p.blurOnSingleSelection === 'touch' && hasTouchCapability ||
5433
+ p.blurOnSingleSelection !== 'touch' && p.blurOnSingleSelection) &&
5434
+ p.input && document.activeElement === p.input) {
5435
+ p.input.blur();
5392
5436
  }
5393
5437
  }
5394
5438
  break;