@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.cjs.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
  'use strict';
@@ -9989,6 +9989,42 @@ const defaultMultiPlaceholderFormatter = (items, labelProp) => {
9989
9989
  return title;
9990
9990
  };
9991
9991
 
9992
+
9993
+ function getFocusState(element) {
9994
+ if (!element)
9995
+ return null;
9996
+
9997
+ const state = { element };
9998
+
9999
+ if (typeof element.selectionStart === 'number' &&
10000
+ typeof element.selectionEnd === 'number') {
10001
+ state.selectionStart = element.selectionStart;
10002
+ state.selectionEnd = element.selectionEnd;
10003
+ state.selectionDirection = element.selectionDirection;
10004
+ }
10005
+
10006
+ return state;
10007
+ }
10008
+
10009
+ function restoreFocusState(state) {
10010
+ if (!(state !== null && state !== void 0 && state.element) || !document.body.contains(state.element))
10011
+ return false;
10012
+
10013
+ state.element.focus();
10014
+
10015
+ if (typeof state.selectionStart === 'number' &&
10016
+ typeof state.selectionEnd === 'number' &&
10017
+ typeof state.element.setSelectionRange === 'function') {var _state$selectionDirec;
10018
+ state.element.setSelectionRange(
10019
+ state.selectionStart,
10020
+ state.selectionEnd, (_state$selectionDirec =
10021
+ state.selectionDirection) !== null && _state$selectionDirec !== void 0 ? _state$selectionDirec : 'none'
10022
+ );
10023
+ }
10024
+
10025
+ return true;
10026
+ }
10027
+
9992
10028
  /*
9993
10029
  Emits the following events:
9994
10030
  ---------------------------
@@ -11253,6 +11289,8 @@ class SelectBox {
11253
11289
 
11254
11290
  this._trigger('open:before', { list: p.dropList });
11255
11291
 
11292
+ p.lastActiveElement = document.activeElement === document.body ? null : getFocusState(document.activeElement);
11293
+
11256
11294
  // Propagate direction to droplist
11257
11295
  p.dropList.setDirection(getComputedStyle(p.el).direction);
11258
11296
 
@@ -11287,6 +11325,11 @@ class SelectBox {
11287
11325
 
11288
11326
  p.dropList.hide();
11289
11327
 
11328
+ if (p.lastActiveElement) {
11329
+ restoreFocusState(p.lastActiveElement);
11330
+ p.lastActiveElement = null;
11331
+ }
11332
+
11290
11333
  return this;
11291
11334
  }
11292
11335
 
@@ -11879,9 +11922,10 @@ class SelectBox {
11879
11922
 
11880
11923
  this.closeList();
11881
11924
 
11882
- if (p.blurOnSingleSelection === 'touch' && hasTouchCapability ||
11883
- p.blurOnSingleSelection !== 'touch' && p.blurOnSingleSelection) {
11884
- p.input && p.input.blur();
11925
+ if ((p.blurOnSingleSelection === 'touch' && hasTouchCapability ||
11926
+ p.blurOnSingleSelection !== 'touch' && p.blurOnSingleSelection) &&
11927
+ p.input && document.activeElement === p.input) {
11928
+ p.input.blur();
11885
11929
  }
11886
11930
  }
11887
11931
  break;