@docsearch/react 3.5.1 → 3.6.0

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.
@@ -51,12 +51,12 @@ export function DocSearch(props) {
51
51
  });
52
52
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(DocSearchButton, {
53
53
  ref: searchButtonRef,
54
- translations: props === null || props === void 0 ? void 0 : (_props$translations = props.translations) === null || _props$translations === void 0 ? void 0 : _props$translations.button,
54
+ translations: props === null || props === void 0 || (_props$translations = props.translations) === null || _props$translations === void 0 ? void 0 : _props$translations.button,
55
55
  onClick: onOpen
56
56
  }), isOpen && createPortal( /*#__PURE__*/React.createElement(DocSearchModal, _extends({}, props, {
57
57
  initialScrollY: window.scrollY,
58
58
  initialQuery: initialQuery,
59
- translations: props === null || props === void 0 ? void 0 : (_props$translations2 = props.translations) === null || _props$translations2 === void 0 ? void 0 : _props$translations2.modal,
59
+ translations: props === null || props === void 0 || (_props$translations2 = props.translations) === null || _props$translations2 === void 0 ? void 0 : _props$translations2.modal,
60
60
  onClose: onClose
61
61
  })), document.body));
62
62
  }
@@ -60,9 +60,50 @@ export var DocSearchButton = React.forwardRef(function (_ref, ref) {
60
60
  className: "DocSearch-Button-Placeholder"
61
61
  }, buttonText)), /*#__PURE__*/React.createElement("span", {
62
62
  className: "DocSearch-Button-Keys"
63
- }, key !== null && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("kbd", {
64
- className: "DocSearch-Button-Key"
65
- }, key === ACTION_KEY_DEFAULT ? /*#__PURE__*/React.createElement(ControlKeyIcon, null) : key), /*#__PURE__*/React.createElement("kbd", {
66
- className: "DocSearch-Button-Key"
63
+ }, key !== null && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(DocSearchButtonKey, {
64
+ reactsToKey: key === ACTION_KEY_DEFAULT ? ACTION_KEY_DEFAULT : 'Meta'
65
+ }, key === ACTION_KEY_DEFAULT ? /*#__PURE__*/React.createElement(ControlKeyIcon, null) : key), /*#__PURE__*/React.createElement(DocSearchButtonKey, {
66
+ reactsToKey: "k"
67
67
  }, "K"))));
68
- });
68
+ });
69
+
70
+ function DocSearchButtonKey(_ref2) {
71
+ var reactsToKey = _ref2.reactsToKey,
72
+ children = _ref2.children;
73
+
74
+ var _useState3 = useState(false),
75
+ _useState4 = _slicedToArray(_useState3, 2),
76
+ isKeyDown = _useState4[0],
77
+ setIsKeyDown = _useState4[1];
78
+
79
+ useEffect(function () {
80
+ if (!reactsToKey) {
81
+ return undefined;
82
+ }
83
+
84
+ function handleKeyDown(e) {
85
+ if (e.key === reactsToKey) {
86
+ setIsKeyDown(true);
87
+ }
88
+ }
89
+
90
+ function handleKeyUp(e) {
91
+ if (e.key === reactsToKey || // keyup doesn't fire when Command is held down,
92
+ // workaround is to mark key as also released when Command is released
93
+ // See https://stackoverflow.com/a/73419500
94
+ e.key === 'Meta') {
95
+ setIsKeyDown(false);
96
+ }
97
+ }
98
+
99
+ window.addEventListener('keydown', handleKeyDown);
100
+ window.addEventListener('keyup', handleKeyUp);
101
+ return function () {
102
+ window.removeEventListener('keydown', handleKeyDown);
103
+ window.removeEventListener('keyup', handleKeyUp);
104
+ };
105
+ }, [reactsToKey]);
106
+ return /*#__PURE__*/React.createElement("kbd", {
107
+ className: isKeyDown ? 'DocSearch-Button-Key DocSearch-Button-Key--pressed' : 'DocSearch-Button-Key'
108
+ }, children);
109
+ }
@@ -223,9 +223,9 @@ export function DocSearchModal(_ref) {
223
223
  throw error;
224
224
  }).then(function (_ref7) {
225
225
  var results = _ref7.results;
226
- var _results$ = results[0],
227
- hits = _results$.hits,
228
- nbHits = _results$.nbHits;
226
+ var firstResult = results[0];
227
+ var hits = firstResult.hits,
228
+ nbHits = firstResult.nbHits;
229
229
  var sources = groupBy(hits, function (hit) {
230
230
  return removeHighlightTags(hit);
231
231
  }, maxResultsPerGroup); // We store the `lvl0`s to display them as search suggestions
@@ -245,7 +245,7 @@ export function DocSearchModal(_ref) {
245
245
  if (insightsActive) {
246
246
  insightsParams = {
247
247
  __autocomplete_indexName: indexName,
248
- __autocomplete_queryID: results[0].queryID,
248
+ __autocomplete_queryID: firstResult.queryID,
249
249
  __autocomplete_algoliaCredentials: {
250
250
  appId: appId,
251
251
  apiKey: apiKey
@@ -315,7 +315,7 @@ export function DocSearchModal(_ref) {
315
315
  document.body.classList.remove('DocSearch--active'); // IE11 doesn't support `scrollTo` so we check that the method exists
316
316
  // first.
317
317
 
318
- (_window$scrollTo = (_window = window).scrollTo) === null || _window$scrollTo === void 0 ? void 0 : _window$scrollTo.call(_window, 0, initialScrollY);
318
+ (_window$scrollTo = (_window = window).scrollTo) === null || _window$scrollTo === void 0 || _window$scrollTo.call(_window, 0, initialScrollY);
319
319
  }; // eslint-disable-next-line react-hooks/exhaustive-deps
320
320
  }, []);
321
321
  React.useEffect(function () {
@@ -7,6 +7,7 @@ export declare type SearchBoxTranslations = Partial<{
7
7
  resetButtonAriaLabel: string;
8
8
  cancelButtonText: string;
9
9
  cancelButtonAriaLabel: string;
10
+ searchInputLabel: string;
10
11
  }>;
11
12
  interface SearchBoxProps extends AutocompleteApi<InternalDocSearchHit, React.FormEvent, React.MouseEvent, React.KeyboardEvent> {
12
13
  state: AutocompleteState<InternalDocSearchHit>;
@@ -23,7 +23,9 @@ export function SearchBox(_ref) {
23
23
  _translations$cancelB = translations.cancelButtonText,
24
24
  cancelButtonText = _translations$cancelB === void 0 ? 'Cancel' : _translations$cancelB,
25
25
  _translations$cancelB2 = translations.cancelButtonAriaLabel,
26
- cancelButtonAriaLabel = _translations$cancelB2 === void 0 ? 'Cancel' : _translations$cancelB2;
26
+ cancelButtonAriaLabel = _translations$cancelB2 === void 0 ? 'Cancel' : _translations$cancelB2,
27
+ _translations$searchI = translations.searchInputLabel,
28
+ searchInputLabel = _translations$searchI === void 0 ? 'Search' : _translations$searchI;
27
29
 
28
30
  var _props$getFormProps = props.getFormProps({
29
31
  inputElement: props.inputRef.current
@@ -48,7 +50,9 @@ export function SearchBox(_ref) {
48
50
  onReset: onReset
49
51
  }, /*#__PURE__*/React.createElement("label", _extends({
50
52
  className: "DocSearch-MagnifierLabel"
51
- }, props.getLabelProps()), /*#__PURE__*/React.createElement(SearchIcon, null)), /*#__PURE__*/React.createElement("div", {
53
+ }, props.getLabelProps()), /*#__PURE__*/React.createElement(SearchIcon, null), /*#__PURE__*/React.createElement("span", {
54
+ className: "DocSearch-VisuallyHiddenForAccessibility"
55
+ }, searchInputLabel)), /*#__PURE__*/React.createElement("div", {
52
56
  className: "DocSearch-LoadingIndicator"
53
57
  }, /*#__PURE__*/React.createElement(LoadingIcon, null)), /*#__PURE__*/React.createElement("input", _extends({
54
58
  className: "DocSearch-Input",
@@ -43,22 +43,15 @@ function noResultSearch(_queries, _requestOptions) {
43
43
  }
44
44
 
45
45
  describe('api', function () {
46
- var container;
47
46
  var docSearchSelector = '.DocSearch';
48
- beforeEach(function () {
49
- container = document.createElement('div');
50
- document.body.appendChild(container);
51
- });
52
- afterEach(function () {
53
- document.body.removeChild(container);
54
- container = null;
55
- });
56
47
  it('renders with minimal parameters', function () {
57
48
  render( /*#__PURE__*/React.createElement(DocSearch, null));
58
49
  expect(document.querySelector(docSearchSelector)).toBeInTheDocument();
59
50
  });
60
51
  describe('translations', function () {
61
52
  it('overrides the default DocSearchButton text', function () {
53
+ var _document$querySelect, _document$querySelect2;
54
+
62
55
  render( /*#__PURE__*/React.createElement(DocSearch, {
63
56
  translations: {
64
57
  button: {
@@ -68,8 +61,8 @@ describe('api', function () {
68
61
  }
69
62
  }));
70
63
  expect(document.querySelector(docSearchSelector)).toBeInTheDocument();
71
- expect(document.querySelector('.DocSearch-Button-Placeholder').innerHTML).toBe('Recherche');
72
- expect(document.querySelector('.DocSearch-Button').getAttribute('aria-label')).toBe('Recherche');
64
+ expect((_document$querySelect = document.querySelector('.DocSearch-Button-Placeholder')) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect.innerHTML).toBe('Recherche');
65
+ expect((_document$querySelect2 = document.querySelector('.DocSearch-Button')) === null || _document$querySelect2 === void 0 ? void 0 : _document$querySelect2.getAttribute('aria-label')).toBe('Recherche');
73
66
  });
74
67
  it('overrides the default DocSearchModal startScreen text', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
75
68
  return regeneratorRuntime.wrap(function _callee2$(_context2) {
@@ -202,6 +195,9 @@ describe('api', function () {
202
195
  }, _callee5);
203
196
  })));
204
197
  it('overrides the default DocSearchModal searchbox text', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() {
198
+ var _document$querySelect3, _document$querySelect4, _document$querySelect5, _document$querySelect6;
199
+
200
+ var searchInputLabel;
205
201
  return regeneratorRuntime.wrap(function _callee7$(_context7) {
206
202
  while (1) switch (_context7.prev = _context7.next) {
207
203
  case 0:
@@ -212,13 +208,13 @@ describe('api', function () {
212
208
  resetButtonTitle: 'Effacer',
213
209
  resetButtonAriaLabel: 'Effacer',
214
210
  cancelButtonText: 'Annuler',
215
- cancelButtonAriaLabel: 'Annuler'
211
+ cancelButtonAriaLabel: 'Annuler',
212
+ searchInputLabel: 'Recherche'
216
213
  }
217
214
  }
218
215
  }
219
216
  }));
220
- expect(document.querySelector(docSearchSelector)).toBeInTheDocument();
221
- _context7.next = 4;
217
+ _context7.next = 3;
222
218
  return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() {
223
219
  return regeneratorRuntime.wrap(function _callee6$(_context6) {
224
220
  while (1) switch (_context6.prev = _context6.next) {
@@ -239,13 +235,16 @@ describe('api', function () {
239
235
  }, _callee6);
240
236
  })));
241
237
 
242
- case 4:
243
- expect(document.querySelector('.DocSearch-Cancel').innerHTML).toBe('Annuler');
244
- expect(document.querySelector('.DocSearch-Cancel').getAttribute('aria-label')).toBe('Annuler');
245
- expect(document.querySelector('.DocSearch-Reset').getAttribute('title')).toBe('Effacer');
246
- expect(document.querySelector('.DocSearch-Reset').getAttribute('aria-label')).toBe('Effacer');
238
+ case 3:
239
+ searchInputLabel = document.querySelector('.DocSearch-MagnifierLabel');
240
+ expect(document.querySelector(docSearchSelector)).toBeInTheDocument();
241
+ expect((_document$querySelect3 = document.querySelector('.DocSearch-Cancel')) === null || _document$querySelect3 === void 0 ? void 0 : _document$querySelect3.innerHTML).toBe('Annuler');
242
+ expect((_document$querySelect4 = document.querySelector('.DocSearch-Cancel')) === null || _document$querySelect4 === void 0 ? void 0 : _document$querySelect4.getAttribute('aria-label')).toBe('Annuler');
243
+ expect((_document$querySelect5 = document.querySelector('.DocSearch-Reset')) === null || _document$querySelect5 === void 0 ? void 0 : _document$querySelect5.getAttribute('title')).toBe('Effacer');
244
+ expect((_document$querySelect6 = document.querySelector('.DocSearch-Reset')) === null || _document$querySelect6 === void 0 ? void 0 : _document$querySelect6.getAttribute('aria-label')).toBe('Effacer');
245
+ expect(searchInputLabel === null || searchInputLabel === void 0 ? void 0 : searchInputLabel.textContent).toBe('Recherche');
247
246
 
248
- case 8:
247
+ case 10:
249
248
  case "end":
250
249
  return _context7.stop();
251
250
  }
@@ -449,7 +448,7 @@ describe('api', function () {
449
448
  expect(screen.getByText(/No results for/)).toBeInTheDocument();
450
449
  link = document.querySelector('.DocSearch-Help a');
451
450
  expect(link).toBeInTheDocument();
452
- expect(link.getAttribute('href')).toBe('https://github.com/algolia/docsearch/issues/new?title=q');
451
+ expect(link === null || link === void 0 ? void 0 : link.getAttribute('href')).toBe('https://github.com/algolia/docsearch/issues/new?title=q');
453
452
 
454
453
  case 9:
455
454
  case "end":
@@ -4,7 +4,8 @@ export function SearchIcon() {
4
4
  width: "20",
5
5
  height: "20",
6
6
  className: "DocSearch-Search-Icon",
7
- viewBox: "0 0 20 20"
7
+ viewBox: "0 0 20 20",
8
+ "aria-hidden": "true"
8
9
  }, /*#__PURE__*/React.createElement("path", {
9
10
  d: "M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z",
10
11
  stroke: "currentColor",
@@ -1,7 +1,7 @@
1
1
  var regexHighlightTags = /(<mark>|<\/mark>)/g;
2
2
  var regexHasHighlightTags = RegExp(regexHighlightTags.source);
3
3
  export function removeHighlightTags(hit) {
4
- var _internalDocSearchHit, _internalDocSearchHit2, _internalDocSearchHit3, _hit$_highlightResult, _hit$_highlightResult2;
4
+ var _internalDocSearchHit, _hit$_highlightResult;
5
5
 
6
6
  var internalDocSearchHit = hit;
7
7
 
@@ -9,7 +9,7 @@ export function removeHighlightTags(hit) {
9
9
  return hit.hierarchy.lvl0;
10
10
  }
11
11
 
12
- var _ref = (internalDocSearchHit.__docsearch_parent ? (_internalDocSearchHit = internalDocSearchHit.__docsearch_parent) === null || _internalDocSearchHit === void 0 ? void 0 : (_internalDocSearchHit2 = _internalDocSearchHit._highlightResult) === null || _internalDocSearchHit2 === void 0 ? void 0 : (_internalDocSearchHit3 = _internalDocSearchHit2.hierarchy) === null || _internalDocSearchHit3 === void 0 ? void 0 : _internalDocSearchHit3.lvl0 : (_hit$_highlightResult = hit._highlightResult) === null || _hit$_highlightResult === void 0 ? void 0 : (_hit$_highlightResult2 = _hit$_highlightResult.hierarchy) === null || _hit$_highlightResult2 === void 0 ? void 0 : _hit$_highlightResult2.lvl0) || {},
12
+ var _ref = (internalDocSearchHit.__docsearch_parent ? (_internalDocSearchHit = internalDocSearchHit.__docsearch_parent) === null || _internalDocSearchHit === void 0 || (_internalDocSearchHit = _internalDocSearchHit._highlightResult) === null || _internalDocSearchHit === void 0 || (_internalDocSearchHit = _internalDocSearchHit.hierarchy) === null || _internalDocSearchHit === void 0 ? void 0 : _internalDocSearchHit.lvl0 : (_hit$_highlightResult = hit._highlightResult) === null || _hit$_highlightResult === void 0 || (_hit$_highlightResult = _hit$_highlightResult.hierarchy) === null || _hit$_highlightResult === void 0 ? void 0 : _hit$_highlightResult.lvl0) || {},
13
13
  value = _ref.value;
14
14
 
15
15
  return value && regexHasHighlightTags.test(value) ? value.replace(regexHighlightTags, '') : value;
@@ -1 +1 @@
1
- export declare const version = "3.5.1";
1
+ export declare const version = "3.6.0";
@@ -1 +1 @@
1
- export var version = '3.5.1';
1
+ export var version = '3.6.0';