# Command

shadcn command reskinned for UI95.

Source: https://www.ui95.dev/ui/command

## Examples

```tsx
"use client"

import * as React from "react"

import { Button } from "@/components/ui/button"
import {
  Command,
  CommandDialog,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator,
  CommandShortcut,
} from "@/components/ui/command"
import {
  DocumentGlyph,
  FolderGlyph,
  TerminalGlyph,
} from "@/components/ui/pixel-glyphs"

import type { ShowcaseDemo } from "../types"

function Palette({ initial = "" }: { initial?: string }) {
  const [search, setSearch] = React.useState(initial)
  return (
    <Command className="h-auto">
      <CommandInput
        placeholder="Search disk..."
        value={search}
        onValueChange={setSearch}
      />
      <CommandList>
        <CommandEmpty>No file found.</CommandEmpty>
        <CommandGroup heading="Files">
          <CommandItem>
            <DocumentGlyph className="size-4" />
            README.TXT
            <CommandShortcut>^O</CommandShortcut>
          </CommandItem>
          <CommandItem>
            <FolderGlyph className="size-4" />
            GAMES
            <CommandShortcut>^G</CommandShortcut>
          </CommandItem>
          <CommandItem disabled>
            <DocumentGlyph className="size-4" />
            VIRUS.EXE
          </CommandItem>
        </CommandGroup>
        <CommandSeparator />
        <CommandGroup heading="System">
          <CommandItem>
            <TerminalGlyph className="size-4" />
            Dial modem
            <CommandShortcut>^D</CommandShortcut>
          </CommandItem>
          <CommandItem>
            <TerminalGlyph className="size-4" />
            Format floppy
          </CommandItem>
        </CommandGroup>
      </CommandList>
    </Command>
  )
}

function MountWhenFullyVisible({ height, children }: { height: number; children: React.ReactNode }) {
  const ref = React.useRef<HTMLDivElement>(null)
  const [isVisible, setIsVisible] = React.useState(false)

  React.useEffect(() => {
    const element = ref.current
    if (!element) {
      return
    }
    const observer = new IntersectionObserver(
      ([entry]) => {
        if (entry?.isIntersecting) {
          setIsVisible(true)
          observer.disconnect()
        }
      },
      { threshold: 1 }
    )
    observer.observe(element)
    return () => observer.disconnect()
  }, [])

  return (
    <div ref={ref} style={isVisible ? undefined : { height }}>
      {isVisible && children}
    </div>
  )
}

function DialogPalette() {
  const [open, setOpen] = React.useState(false)
  return (
    <>
      <Button onClick={() => setOpen(true)}>Open palette</Button>
      <CommandDialog open={open} onOpenChange={setOpen}>
        <Command>
          <CommandInput placeholder="Run a command..." />
          <CommandList>
            <CommandEmpty>No command found.</CommandEmpty>
            <CommandGroup heading="Run">
              <CommandItem onSelect={() => setOpen(false)}>
                <TerminalGlyph className="size-4" />
                Dial modem
                <CommandShortcut>^D</CommandShortcut>
              </CommandItem>
              <CommandItem onSelect={() => setOpen(false)}>
                <FolderGlyph className="size-4" />
                Open A:\
              </CommandItem>
            </CommandGroup>
          </CommandList>
        </Command>
      </CommandDialog>
    </>
  )
}

export const demo: ShowcaseDemo = {
  span: "wide",
  variants: [
    {
      label: "palette",
      render: () => (
        <MountWhenFullyVisible height={296}>
          <Palette />
        </MountWhenFullyVisible>
      ),
    },
    { label: "empty", render: () => <Palette initial="zzz" /> },
    { label: "dialog", render: () => <DialogPalette /> },
  ],
}
```

## Installation

```bash
npx shadcn@latest add https://www.ui95.dev/r/command.json
```

Run it in a Radix shadcn project that already has the UI95 theme.

Dependencies:

```bash
npm install cmdk cn
```

Add these items first:

- [dialog](https://www.ui95.dev/ui/dialog)
- [pixel-glyphs](https://www.ui95.dev/ui/pixel-glyphs)

`components/ui/command.tsx`

```tsx
"use client"

import { Command as CommandPrimitive } from "cmdk"
import { cn } from "cn"
import * as React from "react"

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog"
import { CheckGlyph, SearchGlyph } from "@/components/ui/pixel-glyphs"

function Command({
  className,
  ...props
}: React.ComponentProps<typeof CommandPrimitive>) {
  return (
    <CommandPrimitive
      data-slot="command"
      className={cn(
        "flex size-full flex-col overflow-hidden pixel-border border-border bg-popover p-(--pixel) text-popover-foreground bevel-raised offset-shadow-none",
        className
      )}
      {...props}
    />
  )
}

function CommandDialog({
  title = "Command Palette",
  description = "Search for a command to run...",
  children,
  className,
  showCloseButton = false,
  ...props
}: React.ComponentProps<typeof Dialog> & {
  title?: string
  description?: string
  className?: string
  showCloseButton?: boolean
}) {
  return (
    <Dialog {...props}>
      <DialogContent
        className={cn("top-1/3 translate-y-0 overflow-hidden p-0", className)}
        showCloseButton={showCloseButton}
      >
        <DialogHeader className="sr-only">
          <DialogTitle>{title}</DialogTitle>
          <DialogDescription>{description}</DialogDescription>
        </DialogHeader>
        {children}
      </DialogContent>
    </Dialog>
  )
}

function CommandInput({
  className,
  ...props
}: React.ComponentProps<typeof CommandPrimitive.Input>) {
  return (
    <div data-slot="command-input-wrapper" className="p-1 pb-0">
      <div className="flex h-10 items-center gap-2 pixel-border border-border well bg-input text-foreground px-2">
        <SearchGlyph className="size-4 shrink-0 text-well-muted" />
        <CommandPrimitive.Input
          data-slot="command-input"
          className={cn(
            "h-full w-full min-w-0 bg-transparent text-xl leading-none text-current outline-hidden placeholder:text-well-muted disabled:cursor-not-allowed disabled:text-muted-foreground",
            className
          )}
          {...props}
        />
      </div>
    </div>
  )
}

function CommandList({
  className,
  ...props
}: React.ComponentProps<typeof CommandPrimitive.List>) {
  return (
    <CommandPrimitive.List
      data-slot="command-list"
      className={cn(
        "no-scrollbar max-h-72 scroll-py-1 overflow-x-hidden overflow-y-auto outline-none",
        className
      )}
      {...props}
    />
  )
}

function CommandEmpty({
  className,
  ...props
}: React.ComponentProps<typeof CommandPrimitive.Empty>) {
  return (
    <CommandPrimitive.Empty
      data-slot="command-empty"
      className={cn(
        "py-6 text-center font-pixel text-[16px] leading-none text-muted-foreground uppercase",
        className
      )}
      {...props}
    />
  )
}

function CommandGroup({
  className,
  ...props
}: React.ComponentProps<typeof CommandPrimitive.Group>) {
  return (
    <CommandPrimitive.Group
      data-slot="command-group"
      className={cn(
        "overflow-hidden p-1 text-foreground **:[[cmdk-group-heading]]:px-2 **:[[cmdk-group-heading]]:pt-2 **:[[cmdk-group-heading]]:pb-1 **:[[cmdk-group-heading]]:font-pixel **:[[cmdk-group-heading]]:text-[16px] **:[[cmdk-group-heading]]:leading-none **:[[cmdk-group-heading]]:text-muted-foreground **:[[cmdk-group-heading]]:uppercase",
        className
      )}
      {...props}
    />
  )
}

function CommandSeparator({
  className,
  ...props
}: React.ComponentProps<typeof CommandPrimitive.Separator>) {
  return (
    <CommandPrimitive.Separator
      data-slot="command-separator"
      className={cn(
        "mx-1 my-1 border-t-(length:--pixel) border-b-(length:--pixel) border-t-bevel-lo border-b-bevel-hi",
        className
      )}
      {...props}
    />
  )
}

function CommandItem({
  className,
  children,
  ...props
}: React.ComponentProps<typeof CommandPrimitive.Item>) {
  return (
    <CommandPrimitive.Item
      data-slot="command-item"
      className={cn(
        "group/command-item relative flex h-8 items-center gap-2 px-2 text-xl leading-none outline-hidden select-none data-[disabled=true]:pointer-events-none data-[disabled=true]:text-muted-foreground data-[selected=true]:bg-foreground data-[selected=true]:text-popover [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-3.5",
        className
      )}
      {...props}
    >
      {children}
      <CheckGlyph className="ml-auto hidden group-has-data-[slot=command-shortcut]/command-item:hidden group-data-[checked=true]/command-item:block" />
    </CommandPrimitive.Item>
  )
}

function CommandShortcut({
  className,
  ...props
}: React.ComponentProps<"span">) {
  return (
    <span
      data-slot="command-shortcut"
      className={cn(
        "ml-auto pl-4 text-lg leading-none text-muted-foreground group-data-[selected=true]/command-item:text-popover",
        className
      )}
      {...props}
    />
  )
}

export {
  Command,
  CommandDialog,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator,
  CommandShortcut,
}
```

## API reference

### Command

No props of its own. It forwards the props of the element or primitive it wraps.

### CommandDialog

| Prop | Type | Default |
| --- | --- | --- |
| `title` | `string` | `"Command Palette"` |
| `description` | `string` | `"Search for a command to run..."` |
| `className` | `string` | - |
| `showCloseButton` | `boolean` | `false` |

### CommandEmpty

No props of its own. It forwards the props of the element or primitive it wraps.

### CommandGroup

No props of its own. It forwards the props of the element or primitive it wraps.

### CommandInput

No props of its own. It forwards the props of the element or primitive it wraps.

### CommandItem

No props of its own. It forwards the props of the element or primitive it wraps.

### CommandList

No props of its own. It forwards the props of the element or primitive it wraps.

### CommandSeparator

No props of its own. It forwards the props of the element or primitive it wraps.

### CommandShortcut

No props of its own. It forwards the props of the element or primitive it wraps.

## Sounds

- `menu.select`
- `typewriter.key`
- `typewriter.space`
- `typewriter.return`
