kbar
kbar copied to clipboard
[feature + pr?] allow user to pass additional arguments
As a user I'd like to pass some additional commands to an action, for example:
-
rename something
The matching Actions will receive an extra property: argv
, this can be used in the results listing or the action itself. Example for results (replacing %s
with the additional arguments):
const { results } = useMatches();
const { argv } = useKBar((state) => {
return {
argv: state.argv,
};
});
results.forEach((x) => {
x.argv = argv;
});
console.log(results);
return (
<KBarResults
items={results}
onRender={({ item, active }) =>
typeof item === "string" ? (
<div className="palette-result">{item}</div>
) : (
<div className={"palette-result" + (active ? " active" : "")}>
{item.name.replace("%s", argv.slice(1).join(" "))}
</div>
)
}
/>
);
Example action:
const actions = [
{
id: "rename",
name: 'Rename window to "%s"',
shortcut: ["r"],
keywords: "rename window page",
perform: (a) => {
console.log(a.argv);
window.document.title = a.argv.slice(1).join(" ");
},
}]
I've implemented this in https://github.com/Cygnusfear/kbar/tree/argv. There are a few caveats:
-
useMatches
only matches the first word (withsplit(' ')[0]
) - had to muck around with the action names in the tests and didn't write proper tests
It allows for fancier actions/action rendering (CLI like argument parsing --v --h
) and may address #210 and #227
Suggestions towards the implementation are welcome and can do a PR for this if that would appreciated 😁
p.s> this can also be used in fancy dynamic subtitles:
Results
{typeof item.subtitle === "string"
? item.subtitle
: item.subtitle(item)}
Action
subtitle: (a) => {
let sub = "Rename the current page";
if (a.argv.length > 1)
sub = `Rename page to '${a.argv.slice(1).join(" ")}'`;
return sub;
},
Hey! This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.