A command palette that filters actions as the user types. It extends Base UI's Autocomplete and Dialog. It adds the Command.Shortcut, Command.Toolbar, Command.Footer, and Command.Panel subcomponents and an endAddon prop on Command.Input.
Here is the component implementation code that you can copy to your project:
// https://devie-ui.com/components/command
// https://base-ui.com/react/components/autocomplete#command-palette
import { Autocomplete } from "@base-ui/react/autocomplete";
import { Dialog as BaseDialog } from "@base-ui/react/dialog";
import clsx from "clsx";
import { Search } from "lucide-react";
import type React from "react";
import styles from "./Command.module.scss";
function Root(props: Command.Root.Props) {
const {
autoHighlight = "always",
keepHighlight = true,
open = true,
inline = true,
...rest
} = props;
return (
<Autocomplete.Root
autoHighlight={autoHighlight}
keepHighlight={keepHighlight}
open={open}
inline={inline}
{...rest}
/>
);
}
const Dialog = BaseDialog.Root;
const DialogTrigger = BaseDialog.Trigger;
function DialogPopup({ className, ...props }: Command.DialogPopup.Props) {
return (
<BaseDialog.Portal>
<BaseDialog.Backdrop className={styles.backdrop} />
<BaseDialog.Popup
aria-label="Command palette"
className={clsx(styles.dialogPopup, className)}
{...props}
/>
</BaseDialog.Portal>
);
}
function Input({ className, endAddon, ...props }: Command.Input.Props) {
return (
<div className={styles.inputWrapper}>
<Search size={16} className={styles.inputIcon} />
<Autocomplete.Input
className={clsx(styles.input, className)}
{...props}
/>
{endAddon && <div className={styles.inputKbd}>{endAddon}</div>}
</div>
);
}
function List({ className, ...props }: Command.List.Props) {
return (
<div className={styles.listScroller}>
<Autocomplete.List className={clsx(styles.list, className)} {...props} />
</div>
);
}
function Item({ className, ...props }: Command.Item.Props) {
return (
<Autocomplete.Item className={clsx(styles.item, className)} {...props} />
);
}
function Empty({ className, ...props }: Command.Empty.Props) {
return (
<Autocomplete.Empty className={clsx(styles.empty, className)} {...props} />
);
}
function Group({ className, ...props }: Command.Group.Props) {
return (
<Autocomplete.Group className={clsx(styles.group, className)} {...props} />
);
}
function GroupLabel({ className, ...props }: Command.GroupLabel.Props) {
return (
<Autocomplete.GroupLabel
className={clsx(styles.groupLabel, className)}
{...props}
/>
);
}
function Separator({ className, ...props }: Command.Separator.Props) {
return (
<Autocomplete.Separator
className={clsx(styles.separator, className)}
{...props}
/>
);
}
function Shortcut({ className, ...props }: Command.Shortcut.Props) {
return <kbd className={clsx(styles.shortcut, className)} {...props} />;
}
function Toolbar({ className, ...props }: Command.Toolbar.Props) {
return <div className={clsx(styles.toolbar, className)} {...props} />;
}
function Footer({ className, ...props }: Command.Footer.Props) {
return <div className={clsx(styles.footer, className)} {...props} />;
}
function Panel({ className, ...props }: Command.Panel.Props) {
return <div className={clsx(styles.panel, className)} {...props} />;
}
const Collection = Autocomplete.Collection;
const Command = {
Root,
Dialog,
DialogTrigger,
DialogPopup,
Input,
List,
Item,
Empty,
Group,
GroupLabel,
Separator,
Shortcut,
Toolbar,
Footer,
Panel,
Collection,
};
namespace Command {
export namespace Root {
export type Props = React.ComponentProps<typeof Autocomplete.Root>;
}
export namespace Dialog {
export type Props = BaseDialog.Root.Props;
}
export namespace DialogTrigger {
export type Props = BaseDialog.Trigger.Props;
}
export namespace DialogPopup {
export type Props = Omit<BaseDialog.Popup.Props, "className"> & {
className?: string;
};
}
export namespace Input {
export interface Props extends Omit<Autocomplete.Input.Props, "className"> {
className?: string;
endAddon?: React.ReactNode;
}
}
export namespace List {
export type Props = Omit<Autocomplete.List.Props, "className"> & {
className?: string;
};
}
export namespace Item {
export type Props = Omit<Autocomplete.Item.Props, "className"> & {
className?: string;
};
export type State = Autocomplete.Item.State;
}
export namespace Empty {
export type Props = Omit<Autocomplete.Empty.Props, "className"> & {
className?: string;
};
}
export namespace Group {
export type Props = Omit<Autocomplete.Group.Props, "className"> & {
className?: string;
};
}
export namespace GroupLabel {
export type Props = Omit<Autocomplete.GroupLabel.Props, "className"> & {
className?: string;
};
}
export namespace Separator {
export type Props = Omit<Autocomplete.Separator.Props, "className"> & {
className?: string;
};
}
export namespace Shortcut {
export type Props = React.ComponentProps<"kbd">;
}
export namespace Toolbar {
export type Props = React.ComponentProps<"div">;
}
export namespace Footer {
export type Props = React.ComponentProps<"div">;
}
export namespace Panel {
export type Props = React.ComponentProps<"div">;
}
export namespace Collection {
export type Props = Autocomplete.Collection.Props;
}
}
export default Command;Pass items to Command.Root and render each one as a Command.Item inside Command.List. Wrap them in Command.Dialog and Command.DialogPopup to open the palette from a trigger.
Use Command.Group, Command.GroupLabel, and Command.Collection to organize items into sections.
Place Command.Toolbar between Command.Input and Command.List to add filters or other controls.
Control open on Command.Dialog to toggle the palette from a global shortcut such as ⌘J. Add Command.Shortcut to items, endAddon to Command.Input, and Kbd hints to Command.Footer.
Wrap Command.Root in Command.Panel instead of a dialog to embed the palette in a page.