-
Notifications
You must be signed in to change notification settings - Fork 22
feat: add search for criteria and how to pages using Fuse JS package #457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
bd33824
d9307a7
68a27b2
840f624
7d34a38
07aa21a
d7a3bc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| .searchbar { | ||
| display: flex; | ||
| justify-content: space-between; | ||
| align-items: center; | ||
|
|
||
| &__label { | ||
| margin-right: 1rem; | ||
| font: var(--magentaa11y-typeset-body-xl-strong); | ||
| } | ||
|
|
||
| &__input { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @silverli I think a custom CSS |
||
| height: 50px; | ||
| flex-grow: 1; | ||
| border-color: var(--color-text-link); | ||
| border-radius: 25px; | ||
| font: var(--magentaa11y-typeset-body-lg-normal); | ||
| padding: 0 1rem; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import React from 'react'; | ||
| import './search.scss'; | ||
|
|
||
| interface SearchBarProps { | ||
| controlsId: string; | ||
| resultCount: number; | ||
| query: string; | ||
| onQueryChange: (q: string) => void; | ||
| } | ||
|
|
||
| const SearchBar: React.FC<SearchBarProps> = ({ controlsId, resultCount, query, onQueryChange }) => { | ||
| const resultsString = resultCount === 1 ? 'No results found' : `${resultCount} result${resultCount !== 1 ? 's' : ''} found`; | ||
|
silverli marked this conversation as resolved.
|
||
|
|
||
| return ( | ||
| <div className="searchbar"> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @silverli |
||
| <label htmlFor="criteriaSearch" className="searchbar__label"> | ||
| Search to filter: | ||
| </label> | ||
| <input | ||
| className="searchbar__input" | ||
| role="combobox" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @silverli I'm a tad worried we might see screen reader conflicts with the ARIA Combobox approach here because the ARIA Menu items have nested links which is a no-no - they can only be |
||
| aria-autocomplete="list" | ||
| id="criteriaSearch" | ||
| type="search" | ||
| value={query} | ||
| onChange={(e) => onQueryChange(e.target.value)} // returns new value to setQuery in hook | ||
| aria-controls={controlsId} | ||
| aria-expanded="false" | ||
| aria-haspopup="false" | ||
| /> | ||
| <span className="hidden-visually" role="status"> | ||
| {resultsString} | ||
| </span> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default SearchBar; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { useMemo, useState } from 'react'; | ||
| import Fuse from 'fuse.js'; | ||
|
|
||
| interface ContentItem { | ||
| label: string; | ||
| name: string; | ||
| generalNotes?: string | null; | ||
| developerNotes?: string | null; | ||
| } | ||
|
|
||
| export const useSearch = (items: ContentItem[]) => { | ||
| const [query, setQuery] = useState(''); // start with query being '' | ||
|
|
||
| const fuse = useMemo( | ||
| () => | ||
| new Fuse(items, { | ||
| keys: ['label', 'developerNotes', 'generalNotes'], | ||
| threshold: 0.3, | ||
| // useTokenSearch: true, // can add in fuzzier search .. or perhaps toggle this at some point for pro folks who want to search "card" or "aria-busy" etc | ||
| includeScore: true, | ||
| }), | ||
| [items] | ||
| ); | ||
|
|
||
| const matches = query ? fuse.search(query).map((r) => r.item) : null; | ||
| const results = matches && matches.length === 0 | ||
| ? [{ label: 'No results found', name: 'no-results', generalNotes: "No criteria matches this search."}] | ||
| : matches; | ||
|
|
||
| return { query, setQuery, results }; | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.