-
-
Notifications
You must be signed in to change notification settings - Fork 185
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
feat: sections/groups #59
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/timc/kbar/HkiZDG9FGSGNo7e5smKjSMQXaeZ3 |
b7a7362
to
3e3abeb
Compare
children: React.ReactNode; | ||
manager: Context; | ||
}) { | ||
props.manager.reset(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updates index for all nested useResultItem
s whenever children rerenders.
setActiveIndex(0); | ||
}, [search, currentRootActionId]); | ||
|
||
const getHandlers = (index: number, cb: () => void) => ({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Make sure results are visible to screen readers.
3e3abeb
to
4abe603
Compare
My main bit of feedback on the interface is it seems like there is an implicit dependency between Would there be a downside of making return (
<Results>
{(groupedResults) =>
groupedResults.map(...)
}
</Results>
) |
We should keep the matches and |
4abe603
to
cb64bf7
Compare
Alternative solution to #49.
Closes #36.
Background
While working on the initial version of groups, I noticed the current API required too much leg work for the consumer; i.e., currently a developer can import the standard
KBarResults
component, which handles everything from keyboard navigation to rendering results.However, if you wanted to segment results by groups, you'd have to write a component from scratch handling all pointer and keyboard events that
KBarResults
had built in. This requires the developer to write a lot of code – index management, event listeners, coordinating values fromuseKBar
, etc.For example, #49 introduced another component,
KBarGroupedResults
, but what happens when more feature requests roll in? How do we build an API that can easily enable developers to create any type of result view that they want, with all the complexity abstracted away?Solution
From a high level, we want to:
If a developer wanted to build sectioned/grouped results, they would do this:
wherein
ResultItem
can be rendered anywhere withinResults
, and events and index management will be implicitly handled.We will introduce a few components/hooks:
useMatches: () => ActionGroup[]
This hook uses
match-sorter
under the hood to take the current list of actions and search query, and returns a filtered list of actions grouped by theiraction.section
value. Additionally, asmatch-sorter
returns a new object on every call, we slightly throttle the call to improve the overall search performance.Results: ({ children }: { children: React.ReactNode }) => JSX.Element
All results must be wrapped with this components, which leverages a simple context implementation which implicitly handles all keyboard/pointer events alongside an index management solution inspired by @reach/descendants and use-descendants.
useResultItem: (action: Action) => { index: number; active: boolean; handlers: any; }
Every result rendered will need to use this hook, which works alongside
Results
to get the current index, whether it is active, and and event handlers that need to be spread onto the result.