1 "use client"
2
3 import { Popover, Portal } from "radix-ui"
4 import * as React from "react"
5
6 import { Kbd } from "@/components/kbd"
7 import { clx } from "@/utils/clx"
8 import { isInputElement } from "@/utils/is-input-element"
9
10 interface CommandBarProps extends React.PropsWithChildren {
11 open?: boolean
12 onOpenChange?: (open: boolean) => void
13 defaultOpen?: boolean
14 disableAutoFocus?: boolean
15 }
16
17 /**
18 * The root component of the command bar. This component manages the state of the command bar.
19 */
20 const Root = ({
21 /**
22 * Whether to open (show) the command bar.
23 */
24 open = false,
25 /**
26 * Specify a function to handle the change of `open`'s value.
27 */
28 onOpenChange,
29 /**
30 * Whether the command bar is open by default.
31 */
32 defaultOpen = false,
33 /**
34 * Whether to disable focusing automatically on the command bar when it's opened.
35 */
36 disableAutoFocus = true,
37 children,
38 }: CommandBarProps) => {
39 return (
40 <Popover.Root
41 open={open}
42 onOpenChange={onOpenChange}
43 defaultOpen={defaultOpen}
44 >
45 <Portal.Root>
46 <Popover.Anchor
47 className={clx("fixed bottom-8 left-1/2 h-px w-px -translate-x-1/2")}
48 />
49 </Portal.Root>
50 <Popover.Portal>
51 <Popover.Content
52 side="top"
53 sideOffset={0}
54 onOpenAutoFocus={(e) => {
55 if (disableAutoFocus) {
56 e.preventDefault()
57 }
58 }}
59 className={clx(
60 "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2"
61 )}
62 >
63 {children}
64 </Popover.Content>
65 </Popover.Portal>
66 </Popover.Root>
67 )
68 }
69 Root.displayName = "CommandBar"
70
71 /**
72 * The value component of the command bar. This component is used to display a value,
73 * such as the number of selected items which the commands will act on.
74 */
75 const Value = React.forwardRef<
76 HTMLDivElement,
77 React.ComponentPropsWithoutRef<"div">
78 >(({ className, ...props }, ref) => {
79 return (
80 <div
81 ref={ref}
82 className={clx(
83 "txt-compact-small-plus text-ui-contrast-fg-secondary px-3 py-2.5",
84 className
85 )}
86 {...props}
87 />
88 )
89 })
90 Value.displayName = "CommandBar.Value"
91
92 /**
93 * The bar component of the command bar. This component is used to display the commands.
94 */
95 const Bar = React.forwardRef<
96 HTMLDivElement,
97 React.ComponentPropsWithoutRef<"div">
98 >(({ className, ...props }, ref) => {
99 return (
100 <div
101 ref={ref}
102 className={clx(
103 "bg-ui-contrast-bg-base relative flex items-center overflow-hidden rounded-full px-1",
104 "after:shadow-elevation-flyout after:pointer-events-none after:absolute after:inset-0 after:rounded-full after:content-['']",
105 className
106 )}
107 {...props}
108 />
109 )
110 })
111 Bar.displayName = "CommandBar.Bar"
112
113 /**
114 * The separator component of the command bar. This component is used to display a separator between commands.
115 */
116 const Seperator = React.forwardRef<
117 HTMLDivElement,
118 Omit<React.ComponentPropsWithoutRef<"div">, "children">
119 >(({ className, ...props }, ref) => {
120 return (
121 <div
122 ref={ref}
123 className={clx("bg-ui-contrast-border-base h-10 w-px", className)}
124 {...props}
125 />
126 )
127 })
128 Seperator.displayName = "CommandBar.Seperator"
129
130 interface CommandProps
131 extends Omit<
132 React.ComponentPropsWithoutRef<"button">,
133 "children" | "onClick"
134 > {
135 action: () => void | Promise<void>
136 label: string
137 shortcut: string
138 }
139
140 /**
141 * The command component of the command bar. This component is used to display a command, as well as registering the keyboad shortcut.
142 */
143 const Command = React.forwardRef<HTMLButtonElement, CommandProps>(
144 (
145 {
146 className,
147 /**
148 * @ignore
149 */
150 type = "button",
151 /**
152 * The command's label.
153 */
154 label,
155 /**
156 * The function to execute when the command is triggered.
157 */
158 action,
159 /**
160 * The command's shortcut
161 */
162 shortcut,
163 disabled,
164 ...props
165 }: CommandProps,
166 ref
167 ) => {
168 React.useEffect(() => {
169 const handleKeyDown = (event: KeyboardEvent) => {
170 if (isInputElement(document.activeElement)) {
171 return
172 }
173
174 if (event.key.toLowerCase() === shortcut.toLowerCase()) {
175 event.preventDefault()
176 event.stopPropagation()
177 action()
178 }
179 }
180
181 if (!disabled) {
182 document.addEventListener("keydown", handleKeyDown)
183 }
184
185 return () => {
186 document.removeEventListener("keydown", handleKeyDown)
187 }
188 }, [action, shortcut, disabled])
189
190 return (
191 <button
192 ref={ref}
193 className={clx(
194 "bg-ui-contrast-bg-base txt-compact-small-plus transition-fg text-ui-contrast-fg-primary flex items-center gap-x-2 px-3 py-2.5 outline-none",
195 "focus-visible:bg-ui-contrast-bg-highlight focus-visible:hover:bg-ui-contrast-bg-base-hover hover:bg-ui-contrast-bg-base-hover active:bg-ui-contrast-bg-base-pressed focus-visible:active:bg-ui-contrast-bg-base-pressed disabled:!bg-ui-bg-disabled disabled:!text-ui-fg-disabled",
196 "last-of-type:-mr-1 last-of-type:pr-4",
197 className
198 )}
199 type={type}
200 onClick={action}
201 {...props}
202 >
203 <span>{label}</span>
204 <Kbd className="bg-ui-contrast-bg-subtle border-ui-contrast-border-base text-ui-contrast-fg-secondary">
205 {shortcut.toUpperCase()}
206 </Kbd>
207 </button>
208 )
209 }
210 )
211 Command.displayName = "CommandBar.Command"
212
213 const CommandBar = Object.assign(Root, {
214 Command,
215 Value,
216 Bar,
217 Seperator,
218 })
219
220 export { CommandBar }