Skip to content

Commit

Permalink
feat: nested search (#105)
Browse files Browse the repository at this point in the history
* feat: actions rework

* chore: tests

* fix: package-lock

* fix: dedupe children

* chore: tests

* chore: add test to ci

* fix: package-lock

* feat: nested search

* remove throttle for now

* perf: for loop faster

* perf: large data sets

* feat: display ancestors

* ancestor flicker between navigating rootActionId

* cleanup

* style nits

* fix ci, maybe

* fix: return new actions object

* faster

* fix: move flattening logic into lib
  • Loading branch information
timc1 authored Nov 5, 2021
1 parent dde8f0e commit eacf193
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 437 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: npm install
run: npm install
run: npx npm@7 install
- name: eslint
run: npm run lint
- name: tsc
run: npm run typecheck
- name: test
run: npm run test
86 changes: 61 additions & 25 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from "react";
import { KBarAnimator } from "../../src/KBarAnimator";
import { KBarProvider } from "../../src/KBarContextProvider";
import KBarPortal from "../../src/KBarPortal";
import useMatches, { NO_GROUP } from "../../src/useMatches";
import useDeepMatches from "../../src/useDeepMatches";
import KBarPositioner from "../../src/KBarPositioner";
import KBarSearch from "../../src/KBarSearch";
import KBarResults from "../../src/KBarResults";
Expand All @@ -14,8 +14,9 @@ import Docs from "./Docs";
import SearchDocsActions from "./SearchDocsActions";
import { createAction } from "../../src/utils";
import { useAnalytics } from "./utils";
import { Action } from "../../src";
import Blog from "./Blog";
import { ActionImpl } from "../../src/action";
import { ActionId } from "../../src";

const searchStyle = {
padding: "12px 16px",
Expand Down Expand Up @@ -43,7 +44,6 @@ const groupNameStyle = {
fontSize: "10px",
textTransform: "uppercase" as const,
opacity: 0.5,
background: "var(--background)",
};

const App = () => {
Expand Down Expand Up @@ -102,7 +102,7 @@ const App = () => {
{
id: "darkTheme",
name: "Dark",
keywords: "dark",
keywords: "dark theme",
section: "",
perform: () =>
document.documentElement.setAttribute("data-theme-dark", ""),
Expand All @@ -111,7 +111,7 @@ const App = () => {
{
id: "lightTheme",
name: "Light",
keywords: "light",
keywords: "light theme",
section: "",
perform: () =>
document.documentElement.removeAttribute("data-theme-dark"),
Expand All @@ -123,10 +123,7 @@ const App = () => {
<KBarPortal>
<KBarPositioner>
<KBarAnimator style={animatorStyle}>
<KBarSearch
style={searchStyle}
placeholder="Type a command or search…"
/>
<KBarSearch style={searchStyle} />
<RenderResults />
</KBarAnimator>
</KBarPositioner>
Expand All @@ -152,25 +149,20 @@ const App = () => {
};

function RenderResults() {
const groups = useMatches();
const flattened = React.useMemo(
() =>
groups.reduce((acc, curr) => {
acc.push(curr.name);
acc.push(...curr.actions);
return acc;
}, []),
[groups]
);
const { results, rootActionId } = useDeepMatches();

return (
<KBarResults
items={flattened.filter((i) => i !== NO_GROUP)}
items={results}
onRender={({ item, active }) =>
typeof item === "string" ? (
<div style={groupNameStyle}>{item}</div>
) : (
<ResultItem action={item} active={active} />
<ResultItem
action={item}
active={active}
currentRootActionId={rootActionId}
/>
)
}
/>
Expand All @@ -182,18 +174,32 @@ const ResultItem = React.forwardRef(
{
action,
active,
currentRootActionId,
}: {
action: Action;
action: ActionImpl;
active: boolean;
currentRootActionId: ActionId;
},
ref: React.Ref<HTMLDivElement>
) => {
const ancestors = React.useMemo(() => {
return (function collect(action: ActionImpl, ancestors = []) {
if (action.parent && action.parent.id !== currentRootActionId) {
ancestors.push(action.parent);
if (action.parent.parent) {
collect(action.parent.parent, ancestors);
}
}
return ancestors;
})(action);
}, [action, currentRootActionId]);

return (
<div
ref={ref}
style={{
padding: "12px 16px",
background: active ? "var(--a1)" : "var(--background)",
background: active ? "var(--a1)" : "transparent",
borderLeft: `2px solid ${
active ? "var(--foreground)" : "transparent"
}`,
Expand All @@ -203,10 +209,39 @@ const ResultItem = React.forwardRef(
cursor: "pointer",
}}
>
<div style={{ display: "flex", gap: "8px", alignItems: "center" }}>
<div
style={{
display: "flex",
gap: "8px",
alignItems: "center",
fontSize: 14,
}}
>
{action.icon && action.icon}
<div style={{ display: "flex", flexDirection: "column" }}>
<span>{action.name}</span>
<div>
{ancestors.length > 0 &&
ancestors.map((ancestor) => (
<React.Fragment key={ancestor.id}>
<span
style={{
opacity: 0.5,
marginRight: 8,
}}
>
{ancestor.name}
</span>
<span
style={{
marginRight: 8,
}}
>
&rsaquo;
</span>
</React.Fragment>
))}
<span>{action.name}</span>
</div>
{action.subtitle && (
<span style={{ fontSize: 12 }}>{action.subtitle}</span>
)}
Expand All @@ -221,6 +256,7 @@ const ResultItem = React.forwardRef(
padding: "4px 6px",
background: "rgba(0 0 0 / .1)",
borderRadius: "4px",
fontSize: 14,
}}
>
{sc}
Expand Down
2 changes: 1 addition & 1 deletion example/src/Blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const greatgrandchild = createAction({

export default function Blog() {
const [actions, setActions] = React.useState([
...Array.from(Array(4)).map((_, i) =>
...Array.from(Array(100000)).map((_, i) =>
createAction({
name: i.toString(),
shortcut: [],
Expand Down
2 changes: 1 addition & 1 deletion example/src/SearchDocsActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function SearchDocsActions() {
name: "Search docs…",
shortcut: ["?"],
keywords: "find",
section: "",
section: "Documentation",
}
: null,
[searchActions]
Expand Down
Loading

1 comment on commit eacf193

@vercel
Copy link

@vercel vercel bot commented on eacf193 Nov 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.