@danielgindi/selectbox 2.0.48 → 2.0.50

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/lib/SelectBox.js CHANGED
@@ -179,6 +179,42 @@ const defaultMultiPlaceholderFormatter = (items, labelProp) => {
179
179
  return title;
180
180
  };
181
181
 
182
+
183
+ function getFocusState(element) {
184
+ if (!element)
185
+ return null;
186
+
187
+ const state = { element };
188
+
189
+ if (typeof element.selectionStart === 'number' &&
190
+ typeof element.selectionEnd === 'number') {
191
+ state.selectionStart = element.selectionStart;
192
+ state.selectionEnd = element.selectionEnd;
193
+ state.selectionDirection = element.selectionDirection;
194
+ }
195
+
196
+ return state;
197
+ }
198
+
199
+ function restoreFocusState(state) {
200
+ if (!state?.element || !document.body.contains(state.element))
201
+ return false;
202
+
203
+ state.element.focus();
204
+
205
+ if (typeof state.selectionStart === 'number' &&
206
+ typeof state.selectionEnd === 'number' &&
207
+ typeof state.element.setSelectionRange === 'function') {
208
+ state.element.setSelectionRange(
209
+ state.selectionStart,
210
+ state.selectionEnd,
211
+ state.selectionDirection ?? 'none',
212
+ );
213
+ }
214
+
215
+ return true;
216
+ }
217
+
182
218
  /*
183
219
  Emits the following events:
184
220
  ---------------------------
@@ -1443,6 +1479,9 @@ class SelectBox {
1443
1479
 
1444
1480
  this._trigger('open:before', { list: p.dropList });
1445
1481
 
1482
+ if (document.activeElement !== p.input)
1483
+ p.lastActiveElement = document.activeElement === document.body ? null : getFocusState(document.activeElement);
1484
+
1446
1485
  // Propagate direction to droplist
1447
1486
  p.dropList.setDirection(getComputedStyle(p.el).direction);
1448
1487
 
@@ -1477,6 +1516,12 @@ class SelectBox {
1477
1516
 
1478
1517
  p.dropList.hide();
1479
1518
 
1519
+ if (p.lastActiveElement) {
1520
+ if (!p.multi && !p.searchable)
1521
+ restoreFocusState(p.lastActiveElement);
1522
+ p.lastActiveElement = null;
1523
+ }
1524
+
1480
1525
  return this;
1481
1526
  }
1482
1527
 
@@ -2069,9 +2114,10 @@ class SelectBox {
2069
2114
 
2070
2115
  this.closeList();
2071
2116
 
2072
- if (p.blurOnSingleSelection === 'touch' && hasTouchCapability ||
2073
- p.blurOnSingleSelection !== 'touch' && p.blurOnSingleSelection) {
2074
- p.input && p.input.blur();
2117
+ if ((p.blurOnSingleSelection === 'touch' && hasTouchCapability ||
2118
+ p.blurOnSingleSelection !== 'touch' && p.blurOnSingleSelection) &&
2119
+ p.input && document.activeElement === p.input) {
2120
+ p.input.blur();
2075
2121
  }
2076
2122
  }
2077
2123
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danielgindi/selectbox",
3
- "version": "2.0.48",
3
+ "version": "2.0.50",
4
4
  "description": "A collection of dom utilities. So you can work natively with the dom without dom frameworks.",
5
5
  "main": "dist/lib.cjs.min.js",
6
6
  "module": "lib/index.js",