Skip to content

ComponentsMessaging

SourceSource

Slash Command

An inline /command with its subcommand path and typed options, including the focused one.

An inline /command with its subcommand path and typed options, including the focused one. Compose it from the exported registry parts so local content and behaviour stay in the application while the visual contract stays token-driven.

Root class newt-command
import { SlashCommand } from "@/registry/bases/newt/ui/slash-command"

export default function SlashCommandDemo() {
  return (
    <div className="flex flex-col items-start gap-3">
      <SlashCommand name="command" />
      <SlashCommand
        name="ban"
        subcommand="member"
        options={[
          { name: "user", value: "@username" },
          {
            name: "reason",
            description: "why they were banned",
            focused: true,
          },
        ]}
      />
      <SlashCommand
        name="conf"
        subcommandGroup="menu"
        subcommand="save"
        options={[{ name: "key", value: "prefix" }]}
      />
      <SlashCommand name="remind" options={[{ name: "in", value: "10m" }]}>
        <span className="text-newt-text-muted">sends in this channel</span>
      </SlashCommand>
    </div>
  )
}
<script setup lang="ts">
import { SlashCommand } from "~~/registry/bases/newt/ui/slash-command"

const banOptions = [
  { name: "user", value: "@username" },
  { name: "reason", description: "why they were banned", focused: true },
]
</script>

<template>
  <div class="flex flex-col items-start gap-3">
    <SlashCommand name="command" />
    <SlashCommand name="ban" subcommand="member" :options="banOptions" />
    <SlashCommand
      name="conf"
      subcommand-group="menu"
      subcommand="save"
      :options="[{ name: 'key', value: 'prefix' }]"
    />
    <SlashCommand name="remind" :options="[{ name: 'in', value: '10m' }]">
      <span class="text-newt-text-muted">sends in this channel</span>
    </SlashCommand>
  </div>
</template>

Installation

npx newtui@latest add slash-commandnpx shadcn@latest add @newtui/slash-commandnpx shadcn-vue@latest add @newtui/slash-commandpnpm dlx newtui@latest add slash-commandpnpm dlx shadcn@latest add @newtui/slash-commandpnpm dlx shadcn-vue@latest add @newtui/slash-commandyarn dlx newtui@latest add slash-commandyarn dlx shadcn@latest add @newtui/slash-commandyarn dlx shadcn-vue@latest add @newtui/slash-commandbunx newtui@latest add slash-commandbunx shadcn@latest add @newtui/slash-commandbunx shadcn-vue@latest add @newtui/slash-commanddeno run -A npm:newtui@latest add slash-commanddeno run -A npm:shadcn@latest add @newtui/slash-commanddeno run -A npm:shadcn-vue@latest add @newtui/slash-commandvlx newtui@latest add slash-commandvlx shadcn@latest add @newtui/slash-commandvlx shadcn-vue@latest add @newtui/slash-commandvp dlx newtui@latest add slash-commandvp dlx shadcn@latest add @newtui/slash-commandvp dlx shadcn-vue@latest add @newtui/slash-commandnubx newtui@latest add slash-commandnubx shadcn@latest add @newtui/slash-commandnubx shadcn-vue@latest add @newtui/slash-commandnlx newtui@latest add slash-commandnlx shadcn@latest add @newtui/slash-commandnlx shadcn-vue@latest add @newtui/slash-commandupx newtui@latest add slash-commandupx shadcn@latest add @newtui/slash-commandupx shadcn-vue@latest add @newtui/slash-command

The shadcn CLI needs the @newtui registry in your components.json. Set it up.

Copy the source into your project

import * as React from "react"

import { cn } from "@/lib/utils"

export interface SlashCommandOption {
  name: string
  /** The value the user has entered, if any. */
  value?: string
  /** Stands in for a missing value, the way the client previews the option. */
  description?: string
  /** The option the caret currently sits in. */
  focused?: boolean
}

export interface SlashCommandProps extends React.HTMLAttributes<HTMLSpanElement> {
  /** Command name rendered after the leading slash. */
  name: string
  /** Subcommand segment, e.g. `/ban member`. */
  subcommand?: string
  /** Subcommand group segment, e.g. `/settings roles add`. */
  subcommandGroup?: string
  options?: readonly SlashCommandOption[]
  /** Content that follows the command inside the chip, after a space. */
  children?: React.ReactNode
  /** Builds the accessible name; `aria-label` still wins when both are set. */
  formatLabel?: (state: SlashCommandLabelState) => string
}

/** What the accessible name of a command chip is built from. */
export interface SlashCommandLabelState {
  /** The command path without the slash: `ban member`. */
  path: string
  options: readonly SlashCommandOption[]
}

/** `/name group sub` — the segments a caller can actually reach. */
function commandPath(
  props: Pick<SlashCommandProps, "name" | "subcommand" | "subcommandGroup">
) {
  return [props.name, props.subcommandGroup, props.subcommand]
    .filter(Boolean)
    .join(" ")
}

/** The English default. Pass `formatLabel` to translate it. */
export function defaultSlashCommandLabel({
  path,
  options,
}: SlashCommandLabelState): string {
  const spoken = options
    .map(
      (option) =>
        `${option.name}: ${option.value ?? option.description ?? option.name}`
    )
    .join(" ")
  return `Slash command /${path}${spoken ? ` ${spoken}` : ""}`
}

/*
 * The separators are real text: a space between two parts and a colon after
 * an option name, so a copy, a search and a test read
 * `/ban member user:@username`. The chip is inline content, because a flex row
 * drops the space between two items. `word-spacing` draws each space 4px wide
 * (a space is 1ch in a monospace font), and every child resets it, so only the
 * spaces between the parts are narrowed. Each part is its own run of text, so a
 * right-to-left chip orders its parts from the right, as a flex row did.
 */
const SlashCommand = React.forwardRef<HTMLSpanElement, SlashCommandProps>(
  (
    {
      className,
      name,
      subcommand,
      subcommandGroup,
      options = [],
      children,
      formatLabel = defaultSlashCommandLabel,
      ...props
    },
    ref
  ) => {
    const path = commandPath({ name, subcommand, subcommandGroup })
    const hasChildren =
      children !== undefined && children !== null && children !== false
    return (
      <span
        ref={ref}
        role="group"
        aria-label={formatLabel({ path, options })}
        className={cn(
          "inline-block rounded-[var(--newt-radius-sm)] bg-[color-mix(in_srgb,var(--newt-brand)_15%,transparent)] py-[2px] ps-[6px] pe-2 font-[family-name:var(--newt-font-mono)] text-[13px] text-newt-mention-text [word-spacing:calc(4px_-_1ch)] [&>*]:[word-spacing:normal] [&>*]:[unicode-bidi:isolate]",
          className
        )}
        {...props}
      >
        {/* No space follows the slash, so the text reads `/ban`. */}
        <span className="me-1 opacity-80" aria-hidden="true">
          /
        </span>
        <span className="font-semibold">{name}</span>
        {subcommandGroup ? (
          <>
            {" "}
            <span className="font-semibold">{subcommandGroup}</span>
          </>
        ) : null}
        {subcommand ? (
          <>
            {" "}
            <span className="font-semibold">{subcommand}</span>
          </>
        ) : null}
        {options.map((option) => (
          <React.Fragment key={option.name}>
            {" "}
            <span
              className={cn(
                "inline-block [&>*]:[unicode-bidi:isolate]",
                /* The option the caret currently sits in. */
                option.focused &&
                  "rounded-[var(--newt-radius-sm)] bg-[color-mix(in_srgb,var(--newt-brand)_35%,transparent)] px-0.5"
              )}
            >
              <span className="text-newt-text-muted">{`${option.name}:`}</span>
              {option.value ? (
                <span className="ms-1 font-medium text-newt-text-primary">
                  {option.value}
                </span>
              ) : (
                /* No value yet — the option's description stands in. */
                <span className="ms-1 italic text-newt-text-muted">
                  {option.description ?? option.name}
                </span>
              )}
            </span>
          </React.Fragment>
        ))}
        {hasChildren ? (
          <>
            {" "}
            <span>{children}</span>
          </>
        ) : null}
      </span>
    )
  }
)
SlashCommand.displayName = "SlashCommand"

export { SlashCommand }
<script setup lang="ts">
import type { HTMLAttributes, VNode } from "vue"
import { computed } from "vue"

import { cn } from "@/lib/utils"

import {
  defaultSlashCommandLabel,
  type SlashCommandLabelState,
  type SlashCommandOption,
} from "."

const props = withDefaults(
  defineProps<{
    class?: HTMLAttributes["class"]
    /** Command name rendered after the leading slash. */
    name: string
    /** Subcommand segment, e.g. `/ban member`. */
    subcommand?: string
    /** Subcommand group segment, e.g. `/settings roles add`. */
    subcommandGroup?: string
    options?: readonly SlashCommandOption[]
    /** Builds the accessible name; `aria-label` still wins when both are set. */
    formatLabel?: (state: SlashCommandLabelState) => string
  }>(),
  { options: () => [], formatLabel: defaultSlashCommandLabel }
)

defineSlots<{
  /** Content that follows the command inside the chip, after a space. */
  default?: () => VNode[]
}>()

/** `/name group sub` — the segments a caller can actually reach. */
const path = computed(() =>
  [props.name, props.subcommandGroup, props.subcommand]
    .filter(Boolean)
    .join(" ")
)

const label = computed(() =>
  props.formatLabel({ path: path.value, options: props.options })
)
</script>

<!--
  The separators are real text: a space between two parts and a colon after an
  option name, so a copy, a search and a test read `/ban member user:@username`.
  The chip is inline content, because a flex row drops the space between two
  items. `word-spacing` draws each space 4px wide (a space is 1ch in a
  monospace font), and every child resets it, so only the spaces between the
  parts are narrowed. Each part is its own run of text, so a right-to-left chip
  orders its parts from the right, as a flex row did.

  Each space is written as an interpolation. The compiler removes a line break
  between two elements, so a space typed in the template would not survive.
-->
<template>
  <span
    role="group"
    :aria-label="label"
    :class="
      cn(
        'inline-block rounded-[var(--newt-radius-sm)] bg-[color-mix(in_srgb,var(--newt-brand)_15%,transparent)] py-[2px] ps-[6px] pe-2 font-[family-name:var(--newt-font-mono)] text-[13px] text-newt-mention-text [word-spacing:calc(4px_-_1ch)] [&>*]:[word-spacing:normal] [&>*]:[unicode-bidi:isolate]',
        props.class
      )
    "
  >
    <!-- No space follows the slash, so the text reads `/ban`. -->
    <span class="me-1 opacity-80" aria-hidden="true">/</span>
    <span class="font-semibold">{{ props.name }}</span>
    <template v-if="props.subcommandGroup"
      >{{ " "
      }}<span class="font-semibold">{{ props.subcommandGroup }}</span></template
    >
    <template v-if="props.subcommand"
      >{{ " "
      }}<span class="font-semibold">{{ props.subcommand }}</span></template
    >
    <template v-for="option of props.options" :key="option.name"
      >{{ " "
      }}<span
        :class="
          cn(
            'inline-block [&>*]:[unicode-bidi:isolate]',
            // The option the caret currently sits in.
            option.focused &&
              'rounded-[var(--newt-radius-sm)] bg-[color-mix(in_srgb,var(--newt-brand)_35%,transparent)] px-0.5'
          )
        "
      >
        <span class="text-newt-text-muted">{{ `${option.name}:` }}</span>
        <span
          v-if="option.value"
          class="ms-1 font-medium text-newt-text-primary"
        >
          {{ option.value }}
        </span>
        <!-- No value yet — the option's description stands in. -->
        <span v-else class="ms-1 italic text-newt-text-muted">
          {{ option.description ?? option.name }}
        </span>
      </span></template
    >
    <template v-if="$slots.default"
      >{{ " " }}<span><slot /></span
    ></template>
  </span>
</template>
export { default as SlashCommand } from "./SlashCommand.vue"

export interface SlashCommandOption {
  name: string
  /** The value the user has entered, if any. */
  value?: string
  /** Stands in for a missing value, the way the client previews the option. */
  description?: string
  /** The option the caret currently sits in. */
  focused?: boolean
}

/** What the accessible name of a command chip is built from. */
export interface SlashCommandLabelState {
  /** The command path without the slash: `ban member`. */
  path: string
  options: readonly SlashCommandOption[]
}

/** The English default. Pass `formatLabel` to translate it. */
export function defaultSlashCommandLabel({
  path,
  options,
}: SlashCommandLabelState): string {
  const spoken = options
    .map(
      (option) =>
        `${option.name}: ${option.value ?? option.description ?? option.name}`
    )
    .join(" ")
  return `Slash command /${path}${spoken ? ` ${spoken}` : ""}`
}

Update the import paths

The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.

No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Slash Command to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.

No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Slash Command to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.

Usage

import { SlashCommand } from "@/components/ui/slash-command"

export function Example() {
  return <SlashCommand />
}
<script setup lang="ts">
import { SlashCommand } from "@/components/ui/slash-command"
</script>

<template>
  <SlashCommand />
</template>

Pass the command as name, its path as subcommandGroup and subcommand, and the typed options as options. An option with no value shows its description in italics. Set focused on the option the caret sits in.

The text of the chip

The separators are real text. A space stands between two parts, and a colon follows each option name. The third chip of the preview reads /conf menu save key:prefix, in textContent, in a copy and in a search of the page.

The chip draws each space 4px wide, so the picture does not change with the text. It measures that gap against the width of a space in a monospace font. Keep a monospace font on the chip.

Content after the command

children in React and the default slot in Vue follow the last option inside the chip, after a space. The last chip of the preview adds a note this way.

API reference

SlashCommand

PropType
name?string

Command name rendered after the leading slash.

Typestring
subcommand?string | undefined

Subcommand segment, e.g. `/ban member`.

Typestring | undefined
subcommandGroup?string | undefined

Subcommand group segment, e.g. `/settings roles add`.

Typestring | undefined
options?readonly SlashCommandOption[] | undefined

The typed options, in order. Each one has a `name`, and optionally a `value`, a `description` and `focused`.

Typereadonly SlashCommandOption[] | undefined
children / default slot?ReactNode / Vue slot

Content that follows the command inside the chip, after a space.

TypeReactNode / Vue slot
formatLabel?(state: { path, options }) => string

Builds the accessible name from the command path and the options. `aria-label` still wins.

Type(state: { path, options }) => string
DefaultdefaultSlashCommandLabel

The registry exports SlashCommandOption, SlashCommand. The chip also accepts the native attributes of a <span>; React uses className, while Vue uses class and fallthrough attributes.

HTML and CSS

Load tokens.css and slash-command.css with the markup. Write one space between two parts, and no space after the slash or after a colon. The colon is part of the option name: <span class="newt-command__option-name">user:</span>.

A formatter can move a part to its own line. The picture stays the same, but textContent then holds a line break in place of the space. The reference markup keeps each space single with an oxfmt-ignore comment. To add content after the command, write a space and an element after the last option.

Accessibility

The chip is a group, and its aria-label gives the full command. The slash is hidden from assistive technology, because the label already says it. The colon is text, not generated content, so a screen reader reads it once.

Pass formatLabel to translate the chip’s name, Slash command /ban member. Translating labels lists every English default.

Pass through ARIA attributes when the surrounding context needs a label or description.

Tokens

Slash Command reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-command.

  • --newt-radius-sm
  • --newt-brand
  • --newt-mention-text
  • --newt-font-mono
  • --newt-text-muted
  • --newt-text-primary