|
| 1 | +import React, { useEffect } from "react"; |
| 2 | +import { Link, useSearchParams } from "react-router-dom"; |
| 3 | +import lunr from "lunr"; |
| 4 | +import { componentSearchIndex } from "../searchIndex"; |
| 5 | +import "@visual-framework/vf-search-client-side/vf-search-client-side.css"; |
| 6 | + |
| 7 | +function SearchResults() { |
| 8 | + const [searchParams] = useSearchParams(); |
| 9 | + const query = (searchParams.get("search_query") || "").trim(); |
| 10 | + |
| 11 | + useEffect(() => { |
| 12 | + // vf-search-client-side expects global `lunr` and `searchIndex` variables. |
| 13 | + window.lunr = lunr; |
| 14 | + window.searchIndex = { |
| 15 | + pages: componentSearchIndex.map((item, index) => ({ |
| 16 | + id: String(index), |
| 17 | + title: item.name, |
| 18 | + text: `${item.description}`, |
| 19 | + url: item.path, |
| 20 | + })), |
| 21 | + }; |
| 22 | + |
| 23 | + let isMounted = true; |
| 24 | + |
| 25 | + import("@visual-framework/vf-search-client-side/vf-search-client-side.js") |
| 26 | + .then(({ vfSearchClientSide }) => { |
| 27 | + if (isMounted) { |
| 28 | + vfSearchClientSide(); |
| 29 | + } |
| 30 | + }) |
| 31 | + .catch(() => { |
| 32 | + const resultsContainer = document.querySelector( |
| 33 | + "[data-vf-search-client-side-results]", |
| 34 | + ); |
| 35 | + if (resultsContainer) { |
| 36 | + resultsContainer.textContent = |
| 37 | + "Search is temporarily unavailable. Please try again."; |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + return () => { |
| 42 | + isMounted = false; |
| 43 | + }; |
| 44 | + }, []); |
| 45 | + |
| 46 | + return ( |
| 47 | + <div> |
| 48 | + <section class="vf-intro" id="an-id-for-anchor"> |
| 49 | + <div></div> |
| 50 | + |
| 51 | + <div class="vf-stack"> |
| 52 | + <h1 class="vf-intro__heading ">Search</h1> |
| 53 | + <p class="vf-lede">Search documentation and guidance.</p> |
| 54 | + |
| 55 | + <p class="vf-intro__text"></p> |
| 56 | + </div> |
| 57 | + </section> |
| 58 | + <div className="embl-grid embl-grid--has-centered-content"> |
| 59 | + <div></div> |
| 60 | + <div className="vf-stack vf-stack--400"> |
| 61 | + <form |
| 62 | + action="" |
| 63 | + method="GET" |
| 64 | + className="vf-form vf-form--search vf-form--search--responsive | vf-sidebar vf-sidebar--end" |
| 65 | + > |
| 66 | + <div className="vf-sidebar__inner"> |
| 67 | + <div className="vf-form__item | vf-search__item"> |
| 68 | + <label |
| 69 | + className="vf-form__label vf-u-sr-only | vf-search__label" |
| 70 | + htmlFor="search_query" |
| 71 | + > |
| 72 | + Search |
| 73 | + </label> |
| 74 | + <input |
| 75 | + type="search" |
| 76 | + id="search_query" |
| 77 | + placeholder="Search components and documentation" |
| 78 | + name="search_query" |
| 79 | + defaultValue={query} |
| 80 | + className="vf-search__input | vf-form__input" |
| 81 | + data-vf-search-client-side-input |
| 82 | + data-vf-search-client-side-destination-prefix="" |
| 83 | + /> |
| 84 | + </div> |
| 85 | + <button |
| 86 | + type="submit" |
| 87 | + className="vf-search__button | vf-button vf-button--primary" |
| 88 | + > |
| 89 | + <span className="vf-button__text">Search</span> |
| 90 | + <svg |
| 91 | + className="vf-icon vf-icon--search-btn | vf-button__icon" |
| 92 | + aria-hidden="true" |
| 93 | + xmlns="http://www.w3.org/2000/svg" |
| 94 | + version="1.1" |
| 95 | + viewBox="0 0 140 140" |
| 96 | + width="140" |
| 97 | + height="140" |
| 98 | + > |
| 99 | + <g transform="matrix(5.833333333333333,0,0,5.833333333333333,0,0)"> |
| 100 | + <path |
| 101 | + d="M23.414,20.591l-4.645-4.645a10.256,10.256,0,1,0-2.828,2.829l4.645,4.644a2.025,2.025,0,0,0,2.828,0A2,2,0,0,0,23.414,20.591ZM10.25,3.005A7.25,7.25,0,1,1,3,10.255,7.258,7.258,0,0,1,10.25,3.005Z" |
| 102 | + fill="#FFFFFF" |
| 103 | + stroke="none" |
| 104 | + strokeLinecap="round" |
| 105 | + strokeLinejoin="round" |
| 106 | + strokeWidth="0" |
| 107 | + /> |
| 108 | + </g> |
| 109 | + </svg> |
| 110 | + </button> |
| 111 | + </div> |
| 112 | + </form> |
| 113 | + |
| 114 | + <div className="vf-search-client-side vf-grid | vf-content"> |
| 115 | + <div |
| 116 | + className="results-container" |
| 117 | + data-vf-search-client-side-results |
| 118 | + > |
| 119 | + Loading... |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + <div></div> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + ); |
| 127 | +} |
| 128 | + |
| 129 | +export default SearchResults; |
0 commit comments