mirror of
https://github.com/outline/outline.git
synced 2026-11-25 23:24:06 +03:00
* fix: Command bar options changing as the menu animates away Freeze the rendered results while the command bar is animating out, so that navigation triggered by the selected action cannot reorder or resize the list that is still on screen. Also order sections by their declared priority in all cases – a section that declares no priority was falling back to the fuzzy match score of its first result, and recent searches shared a heading with recently viewed documents, so both groups moved around unpredictably. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix: Record the last command bar matches after render, not during Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
79 lines
2.0 KiB
TypeScript
79 lines
2.0 KiB
TypeScript
import { useMatches, useKBar, KBarResults, VisualState } from "kbar";
|
||
import { useEffect, useRef } from "react";
|
||
import styled from "styled-components";
|
||
import Text from "~/components/Text";
|
||
import CommandBarItem from "./CommandBarItem";
|
||
|
||
export default function CommandBarResults() {
|
||
const { results, rootActionId } = useFrozenMatches();
|
||
|
||
if (results.length === 0) {
|
||
return null;
|
||
}
|
||
|
||
return (
|
||
<Container>
|
||
<KBarResults
|
||
items={results}
|
||
maxHeight={400}
|
||
onRender={({ item, active }) =>
|
||
typeof item === "string" ? (
|
||
<Header type="tertiary" size="xsmall" ellipsis>
|
||
{item}
|
||
</Header>
|
||
) : (
|
||
<CommandBarItem
|
||
action={item}
|
||
active={active}
|
||
currentRootActionId={rootActionId}
|
||
/>
|
||
)
|
||
}
|
||
/>
|
||
</Container>
|
||
);
|
||
}
|
||
|
||
/**
|
||
* Returns the matching results, frozen for as long as the command bar is
|
||
* animating away. Performing an action navigates, which registers a different
|
||
* set of actions – without freezing, the options visibly change while the menu
|
||
* is still on screen.
|
||
*
|
||
* @returns the results to render and the current root action id.
|
||
*/
|
||
function useFrozenMatches() {
|
||
const matches = useMatches();
|
||
const { isClosing } = useKBar((state) => ({
|
||
isClosing: state.visualState === VisualState.animatingOut,
|
||
}));
|
||
|
||
// Only what was committed is worth freezing, so the last matches are recorded
|
||
// after render rather than during it.
|
||
const lastCommitted = useRef(matches);
|
||
|
||
useEffect(() => {
|
||
if (!isClosing) {
|
||
lastCommitted.current = matches;
|
||
}
|
||
}, [isClosing, matches]);
|
||
|
||
return isClosing ? lastCommitted.current : matches;
|
||
}
|
||
|
||
// Cannot style KBarResults unfortunately, so we must wrap and target the inner
|
||
const Container = styled.div`
|
||
> div {
|
||
padding-bottom: 8px;
|
||
}
|
||
`;
|
||
|
||
const Header = styled(Text).attrs({ as: "h3" })`
|
||
letter-spacing: 0.03em;
|
||
margin: 0;
|
||
padding-block: 16px 4px;
|
||
padding-inline: 20px 0;
|
||
height: 36px;
|
||
cursor: default;
|
||
`;
|