Skip to content

Commit d108be2

Browse files
committed
fix(#1999): refactor to use SearchAutocomplete and v3 of AlgoliaSearch API
1 parent c0b2454 commit d108be2

File tree

4 files changed

+249
-7
lines changed

4 files changed

+249
-7
lines changed

packages/dev/docs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
"@spectrum-icons/illustrations": "^3.1.0",
2424
"@spectrum-icons/ui": "^3.1.0",
2525
"@spectrum-icons/workflow": "^4.0.0",
26+
"algoliasearch": "^4.14.1",
2627
"clsx": "^1.1.1",
2728
"globals-docs": "^2.4.1",
28-
"highlight.js": "9.18.1",
29+
"highlight.js": "^11.6.0",
2930
"markdown-to-jsx": "^6.11.0",
3031
"quicklink": "^2.0.0",
3132
"react": "^18.0.0",

packages/dev/docs/src/client.js

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {ActionButton} from '@react-spectrum/button';
13+
import {ActionButton, defaultTheme, Provider, Text} from '@adobe/react-spectrum';
14+
import algoliasearch from 'algoliasearch/lite';
1415
import docsStyle from './docs.css';
16+
import {Item, SearchAutocomplete, Section} from '@react-spectrum/autocomplete';
1517
import {listen} from 'quicklink';
1618
import React, {useEffect, useRef, useState} from 'react';
1719
import ReactDOM from 'react-dom';
@@ -183,8 +185,100 @@ function Hamburger() {
183185
);
184186
}
185187

188+
function DocSearch() {
189+
const client = algoliasearch('1V1Q59JVTR', '44a7e2e7508ff185f25ac64c0a675f98');
190+
const searchIndex = client.initIndex('react-spectrum');
191+
const searchOptions = {
192+
highlightPreTag: `<mark class="${docsStyle.docSearchBoxMark}">`,
193+
highlightPostTag: '</mark>'
194+
};
195+
196+
const [searchValue, setSearchValue] = useState('');
197+
const [predictions, setPredictions] = useState(null);
198+
const [suggestions, setSuggestions] = useState(null);
199+
200+
let updatePredictions = ({hits}) => {
201+
setPredictions(hits);
202+
let sections = [];
203+
hits.forEach(prediction => {
204+
let hierarchy = prediction.hierarchy;
205+
let objectID = prediction.objectID;
206+
let lvl0 = hierarchy.lvl0;
207+
let section = sections.find(section => section.title === lvl0);
208+
if (!section) {
209+
section = {title: lvl0, items: []};
210+
sections.push(section);
211+
}
212+
let text = [];
213+
let textValue = [];
214+
for (let i = 1; i < 6; i++) {
215+
if (hierarchy[`lvl${i}`]) {
216+
text.push(prediction._highlightResult.hierarchy[`lvl${i}`].value);
217+
textValue.push(hierarchy[`lvl${i}`]);
218+
}
219+
}
220+
section.items.push(
221+
<Item key={objectID} textValue={textValue.join(' | ')}>
222+
<Text><span dangerouslySetInnerHTML={{__html: text.join(' | ')}} /></Text>
223+
{
224+
prediction.content &&
225+
<Text slot="description">
226+
<span dangerouslySetInnerHTML={{__html: prediction._snippetResult.content.value}} />
227+
</Text>
228+
}
229+
</Item>
230+
);
231+
});
232+
let suggestions = sections.map((section, index) => <Section key={`${index}-lvl0-${section.title}`} title={section.title}>{section.items}</Section>);
233+
setSuggestions(suggestions);
234+
};
235+
236+
let onInputChange = (query) => {
237+
if (!query && predictions) {
238+
setPredictions(null);
239+
setSuggestions(null);
240+
}
241+
setSearchValue(query);
242+
searchIndex
243+
.search(
244+
query,
245+
searchOptions
246+
)
247+
.then(updatePredictions);
248+
};
249+
250+
let onSubmit = (value, key) => {
251+
if (key) {
252+
let prediction = predictions.find(prediction => key === prediction.objectID);
253+
let url = prediction.url;
254+
if (
255+
url.includes('https://react-spectrum.adobe.com') &&
256+
window.location.hostname === 'localhost') {
257+
url = url.replace('https://react-spectrum.adobe.com', window.location.origin);
258+
}
259+
window.location.href = url;
260+
}
261+
};
262+
263+
return (
264+
<Provider theme={defaultTheme} role="search">
265+
<SearchAutocomplete
266+
aria-label="Search"
267+
placeholder="Search"
268+
UNSAFE_className={docsStyle.docSearchBox}
269+
id="algolia-doc-search"
270+
value={searchValue}
271+
onInputChange={onInputChange}
272+
onSubmit={onSubmit}>
273+
{suggestions}
274+
</SearchAutocomplete>
275+
</Provider>
276+
);
277+
}
278+
186279
ReactDOM.render(<>
187280
<Hamburger />
281+
<DocSearch />
188282
<ThemeSwitcher />
189283
</>, document.querySelector('.' + docsStyle.pageHeader));
190284

packages/dev/docs/src/docs.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ h2.sectionHeader {
608608
}
609609

610610
.pageHeader {
611+
gap: var(--spectrum-global-dimension-size-100);
611612
position: fixed;
612613
top: 0;
613614
display: inline-flex;
@@ -622,6 +623,22 @@ h2.sectionHeader {
622623
[dir='rtl'] & {
623624
left: 0;
624625
}
626+
627+
z-index: 1;
628+
}
629+
630+
.docSearchBox {
631+
margin-inline-start: auto;
632+
width: var(--spectrum-global-dimension-size-3600);
633+
> div {
634+
width: 100%;
635+
}
636+
}
637+
638+
.docSearchBoxMark {
639+
font-weight: bold;
640+
background: var(--spectrum-alias-text-highlight-color);
641+
color: var(--spectrum-global-color-blue-700);
625642
}
626643

627644
/* hamburger menu should be hidden so that it doesn't receive focus before the sidenav */
@@ -706,6 +723,12 @@ h2.sectionHeader {
706723
}
707724
}
708725

726+
@media (max-width: 569px) {
727+
.docSearchBox {
728+
width: var(--spectrum-global-dimension-size-2400);
729+
}
730+
}
731+
709732
@media (max-width: 1200px) {
710733
/* Hide the table of contents */
711734
main {

yarn.lock

Lines changed: 129 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,110 @@
5252
resolved "https://registry.yarnpkg.com/@adobe/spectrum-css-workflow-icons/-/spectrum-css-workflow-icons-1.0.0.tgz#913f3a251ef5ea3154929a7a23d4fbd6de1553da"
5353
integrity sha512-aJbk7XhsUNefoufMpneq2aIT3VuD++KUY0C08VKbt5p/WOW0aDX8WYavUxW4oiK5cAgsvapcaPaS+eWpBMwXLg==
5454

55+
"@algolia/[email protected]":
56+
version "4.14.1"
57+
resolved "https://registry.yarnpkg.com/@algolia/cache-browser-local-storage/-/cache-browser-local-storage-4.14.1.tgz#a0b85a6c3fe3d5c49fca01b16f00b41bf38a918c"
58+
integrity sha512-BBdibsPn3hLBajc/NRAtHEeoXsw+ziSGR/3bqRNB5puUuwKPQZSE2MaMVWSADnlc3KV3bEj4xsfKOVLJyfJSPQ==
59+
dependencies:
60+
"@algolia/cache-common" "4.14.1"
61+
62+
"@algolia/[email protected]":
63+
version "4.14.1"
64+
resolved "https://registry.yarnpkg.com/@algolia/cache-common/-/cache-common-4.14.1.tgz#11d44a6442f83deb3629a04c20df8408088f6449"
65+
integrity sha512-XhAzm0Sm3D3DuOWUyDoVSXZ/RjYMvI1rbki+QH4ODAVaHDWVhMhg3IJPv3gIbBQnEQdtPdBhsf2hyPxAu28E5w==
66+
67+
"@algolia/[email protected]":
68+
version "4.14.1"
69+
resolved "https://registry.yarnpkg.com/@algolia/cache-in-memory/-/cache-in-memory-4.14.1.tgz#68ede8520f054bc65938209b59962056ae5b56c7"
70+
integrity sha512-fVUu7N1hYb/zZYfV9Krlij70NwS+8bQm5vmDJyfp0+9FjSjz2V7wj1CUxvaY8ZcgoBPj9ehQ8sRuqSM2m5OPww==
71+
dependencies:
72+
"@algolia/cache-common" "4.14.1"
73+
74+
"@algolia/[email protected]":
75+
version "4.14.1"
76+
resolved "https://registry.yarnpkg.com/@algolia/client-account/-/client-account-4.14.1.tgz#b92e091f698630c49ec7df48816ae75af3cbac40"
77+
integrity sha512-Zm4+PN3bsBPhv1dKKwzBaRGzf0G1JcjjSTpE231L7Z7LsEDcFDW4E6L5ctwMz3SliSBeL/j1ghmaunJrZlkRIg==
78+
dependencies:
79+
"@algolia/client-common" "4.14.1"
80+
"@algolia/client-search" "4.14.1"
81+
"@algolia/transporter" "4.14.1"
82+
83+
"@algolia/[email protected]":
84+
version "4.14.1"
85+
resolved "https://registry.yarnpkg.com/@algolia/client-analytics/-/client-analytics-4.14.1.tgz#aca3436775f608a6141cc81899e1d75ef030efa2"
86+
integrity sha512-EhZLR0ezBZx7ZGkwzj7OTvnI8j2Alyv1ByC0Mx48qh3KqRhVwMFm/Uf34zAv4Dum2PTFin41Y4smAvAypth9nQ==
87+
dependencies:
88+
"@algolia/client-common" "4.14.1"
89+
"@algolia/client-search" "4.14.1"
90+
"@algolia/requester-common" "4.14.1"
91+
"@algolia/transporter" "4.14.1"
92+
93+
"@algolia/[email protected]":
94+
version "4.14.1"
95+
resolved "https://registry.yarnpkg.com/@algolia/client-common/-/client-common-4.14.1.tgz#2709bddf934a3545cd9feecc0591e9285fbed7c2"
96+
integrity sha512-WDwziD7Rt1yCRDfONmeLOfh1Lt8uOy6Vn7dma171KOH9NN3q8yDQpOhPqdFOCz1j3GC1FfIZxaC0YEOIobZ2lg==
97+
dependencies:
98+
"@algolia/requester-common" "4.14.1"
99+
"@algolia/transporter" "4.14.1"
100+
101+
"@algolia/[email protected]":
102+
version "4.14.1"
103+
resolved "https://registry.yarnpkg.com/@algolia/client-personalization/-/client-personalization-4.14.1.tgz#58f0b85b8f6d531e13877a099f54513ac2bec154"
104+
integrity sha512-D4eeW7bTi769PWcEYZO+QiKuUXFOC5zK5Iy83Ey6FHqS7m5TXws5MP1rmETE018lTXeYq2NSHWp/F07fRRg0RA==
105+
dependencies:
106+
"@algolia/client-common" "4.14.1"
107+
"@algolia/requester-common" "4.14.1"
108+
"@algolia/transporter" "4.14.1"
109+
110+
"@algolia/[email protected]":
111+
version "4.14.1"
112+
resolved "https://registry.yarnpkg.com/@algolia/client-search/-/client-search-4.14.1.tgz#44bfc65b3e6939b725f8f97aad725593f2a4ad7f"
113+
integrity sha512-K6XrdIIQq8a3o+kCedj5slUVzA1aKttae4mLzwnY0bS7tYduv1IQggi9Sz8gOG6/MMyKMB4IwYqr47t/0z4Vxw==
114+
dependencies:
115+
"@algolia/client-common" "4.14.1"
116+
"@algolia/requester-common" "4.14.1"
117+
"@algolia/transporter" "4.14.1"
118+
119+
"@algolia/[email protected]":
120+
version "4.14.1"
121+
resolved "https://registry.yarnpkg.com/@algolia/logger-common/-/logger-common-4.14.1.tgz#acbd36b66e3b408f99cacfb581ad3bd28defcc28"
122+
integrity sha512-58CK87wTjUWI1QNXc3nFDQ7EXBi28NoLufXE9sMjng2fAL1wPdyO+KFD8KTBoXOZnJWflPj5F7p6jLyGAfgvcQ==
123+
124+
"@algolia/[email protected]":
125+
version "4.14.1"
126+
resolved "https://registry.yarnpkg.com/@algolia/logger-console/-/logger-console-4.14.1.tgz#7e7d7486d71ccfe38e63234626931083592149d2"
127+
integrity sha512-not+VwH1Dx2B/BaN+4+4+YnGRBJ9lduNz2qbMCTxZ4yFHb+84j4ewHRPBTtEmibn7caVCPybdTKfHLQhimSBLQ==
128+
dependencies:
129+
"@algolia/logger-common" "4.14.1"
130+
131+
"@algolia/[email protected]":
132+
version "4.14.1"
133+
resolved "https://registry.yarnpkg.com/@algolia/requester-browser-xhr/-/requester-browser-xhr-4.14.1.tgz#9e683dc0916afae221bf946255a998b06830be78"
134+
integrity sha512-mpH6QsFBbXjTy9+iU86Rcdt9LxS7GA/tWhGMr0+Ap8+4Za5+ELToz0PC7euVeVOcclgGGi7gbjOAmf6k8b10iA==
135+
dependencies:
136+
"@algolia/requester-common" "4.14.1"
137+
138+
"@algolia/[email protected]":
139+
version "4.14.1"
140+
resolved "https://registry.yarnpkg.com/@algolia/requester-common/-/requester-common-4.14.1.tgz#b07ffa00ae0cf61442dcda71a3209051fed130d8"
141+
integrity sha512-EbXBKrfYcX5/JJfaw7IZxhWlbUtjd5Chs+Alrfc4tutgRQn4dmImWS07n3iffwJcYdOWY1eRrnfBK5BwopuN5A==
142+
143+
"@algolia/[email protected]":
144+
version "4.14.1"
145+
resolved "https://registry.yarnpkg.com/@algolia/requester-node-http/-/requester-node-http-4.14.1.tgz#5e5f4ff55deb5aa0e92f3105d77299de744b1471"
146+
integrity sha512-/sbRqL9P8aVuYUG50BgpCbdJyyCS7fia+sQIx9d1DiGPO7hunwLaEyR4H7JDHc/PLKdVEPygJx3rnbJWix4Btg==
147+
dependencies:
148+
"@algolia/requester-common" "4.14.1"
149+
150+
"@algolia/[email protected]":
151+
version "4.14.1"
152+
resolved "https://registry.yarnpkg.com/@algolia/transporter/-/transporter-4.14.1.tgz#7eca8568ff710d9d1a7bbd3c1dafbbf44a6143f5"
153+
integrity sha512-xbmoIqszFDOCCZqizBQ2TNHcGtjZX7EkJCzABsrokA0WqtfZzClFmtc+tZYgtEiyAfIF70alTegG19poQGdkvg==
154+
dependencies:
155+
"@algolia/cache-common" "4.14.1"
156+
"@algolia/logger-common" "4.14.1"
157+
"@algolia/requester-common" "4.14.1"
158+
55159
"@babel/cli@^7.12.10":
56160
version "7.12.10"
57161
resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.12.10.tgz#67a1015b1cd505bde1696196febf910c4c339a48"
@@ -5786,6 +5890,26 @@ ajv@^6.1.0, ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.2, ajv@^6.12.3, ajv@^6.12.4, ajv
57865890
json-schema-traverse "^0.4.1"
57875891
uri-js "^4.2.2"
57885892

5893+
algoliasearch@^4.14.1:
5894+
version "4.14.1"
5895+
resolved "https://registry.yarnpkg.com/algoliasearch/-/algoliasearch-4.14.1.tgz#7f24cabd264f8294b461d108e1603e673571e806"
5896+
integrity sha512-ZWqnbsGUgU03/IyG995pMCc+EmNVDA/4c9ntr8B0dWQwFqazOQ4ErvKZxarbgSNmyPo/eZcVsTb0bNplJMttGQ==
5897+
dependencies:
5898+
"@algolia/cache-browser-local-storage" "4.14.1"
5899+
"@algolia/cache-common" "4.14.1"
5900+
"@algolia/cache-in-memory" "4.14.1"
5901+
"@algolia/client-account" "4.14.1"
5902+
"@algolia/client-analytics" "4.14.1"
5903+
"@algolia/client-common" "4.14.1"
5904+
"@algolia/client-personalization" "4.14.1"
5905+
"@algolia/client-search" "4.14.1"
5906+
"@algolia/logger-common" "4.14.1"
5907+
"@algolia/logger-console" "4.14.1"
5908+
"@algolia/requester-browser-xhr" "4.14.1"
5909+
"@algolia/requester-common" "4.14.1"
5910+
"@algolia/requester-node-http" "4.14.1"
5911+
"@algolia/transporter" "4.14.1"
5912+
57895913
align-text@^0.1.1, align-text@^0.1.3:
57905914
version "0.1.4"
57915915
resolved "https://registry.yarnpkg.com/align-text/-/align-text-0.1.4.tgz#0cd90a561093f35d0a99256c22b7069433fad117"
@@ -12324,11 +12448,6 @@ header-case@^1.0.0:
1232412448
no-case "^2.2.0"
1232512449
upper-case "^1.1.3"
1232612450

12327-
12328-
version "9.18.1"
12329-
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-9.18.1.tgz#ed21aa001fe6252bb10a3d76d47573c6539fe13c"
12330-
integrity sha512-OrVKYz70LHsnCgmbXctv/bfuvntIKDz177h0Co37DQ5jamGZLVmoCVMtjMtNZY3X9DrCcKfklHPNeA0uPZhSJg==
12331-
1233212451
highlight.js@^10.1.1:
1233312452
version "10.5.0"
1233412453
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-10.5.0.tgz#3f09fede6a865757378f2d9ebdcbc15ba268f98f"
@@ -12339,6 +12458,11 @@ highlight.js@^10.4.1:
1233912458
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-10.7.3.tgz#697272e3991356e40c3cac566a74eef681756531"
1234012459
integrity sha512-tzcUFauisWKNHaRkN4Wjl/ZA07gENAjFl3J/c480dprkGTg5EQstgaNFqBfUqCq54kZRIEcreTsAgF/m2quD7A==
1234112460

12461+
highlight.js@^11.6.0:
12462+
version "11.6.0"
12463+
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-11.6.0.tgz#a50e9da05763f1bb0c1322c8f4f755242cff3f5a"
12464+
integrity sha512-ig1eqDzJaB0pqEvlPVIpSSyMaO92bH1N2rJpLMN/nX396wTpDA4Eq0uK+7I/2XG17pFaaKE0kjV/XPeGt7Evjw==
12465+
1234212466
highlight.js@~10.4.0:
1234312467
version "10.4.1"
1234412468
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-10.4.1.tgz#d48fbcf4a9971c4361b3f95f302747afe19dbad0"

0 commit comments

Comments
 (0)