---
title: "Token Input · UI SDK — StyleX Blocks"
description: "Rich text field that mixes editable prose with removable atomic tokens in outline, subtle, and plain styles."
url: "https://stylexblocks.com/components/ui-sdk/token-input/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Token Input

Rich text field that mixes editable prose with removable atomic tokens in outline, subtle, and plain styles.

- Behavior engine: UI SDK
- Category: Form
- Variants: outline, subtle, plain
- Sizes: sm, md, lg
- Used in blocks: [AI Chat Landing](https://stylexblocks.com/block/ui-sdk/ai-chat-landing.md)
- HTML page: https://stylexblocks.com/components/ui-sdk/token-input/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style ui-sdk
```

Then add the item as source you own:

```sh
npx stylexblocks add token-input
```

Equivalent: `npx shadcn add @stylexblocks/token-input`. Registry item JSON: https://stylexblocks.com/r/ui-sdk/token-input.json

## Usage

```tsx
import { TokenInput, type TokenInputValue } from "#components/ui/ui-sdk/token-input.tsx";
import { bluePalette, violetPalette } from "@stylexblocks/ui/themes/palettes.ts";

const initialValue: TokenInputValue = [
  "Assign ",
  { type: "mention", value: "elena", label: "@Elena Park" },
  " to ",
  { type: "tag", value: "design-tokens", label: "#design-tokens" },
];

export function Example() {
  return (
    // variant/size resolve once on Root; inline tokens move, copy/paste,
    // undo, and delete as atomic units alongside editable text.
    <TokenInput.Root
      defaultValue={initialValue}
      variant="outline"
      size="md"
      renderToken={(token) => (
        <TokenInput.Token palette={token.type === "mention" ? bluePalette : violetPalette}>
          {token.label}
          <TokenInput.TokenRemove aria-label={`Remove ${token.label}`} />
        </TokenInput.Token>
      )}
    >
      <TokenInput.Editor
        aria-label="Message"
        placeholder="Write a message…"
      />
    </TokenInput.Root>
  );
}
```

## Examples

### Controller & select-to-delete

Insert atomic @mention and #tag tokens programmatically with createTokenInputController(). Press Backspace once next to a token to select it, and a second time to delete.

```tsx
const [controller] = useState(createTokenInputController);

<TokenInput.Root controller={controller} tokenDeletion="select">
  <TokenInput.Editor placeholder="Insert or edit tokens…" />
</TokenInput.Root>
```

## Source

`components/ui/ui-sdk/token-input.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import {
  createTokenInputController,
  defaultCodec,
  fromText,
  toLabel,
  toText,
  TokenInput as UiSdkTokenInput,
  TokenInputEditor,
  TokenInputToken,
  TokenInputTokenRemove,
  type TextEditorAdapter,
  type TextRange,
  type Token,
  type TokenCodec,
  type TokenDeletion,
  type TokenInputApi,
  type TokenInputChangeOptions,
  type TokenInputController,
  type TokenInputEditorProps,
  type TokenInputEditorState,
  type TokenInputOptions,
  type TokenInputPrimitive,
  type TokenInputProps,
  type TokenInputSubmitDetails,
  type TokenInputTokenProps,
  type TokenInputTokenState,
  type TokenInputValue,
  type TokenInputValueChangeDetails,
  type TokenInputValueChangeReason,
  type TokenInsert,
  type UndoStackOptions,
} from "@ui-sdk/react/token-input";
import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";
import {
  defineSlotRecipe,
  type SlotRecipeVariants,
  type StyleOverrideProps,
} from "#lib/recipe-props.ts";
import { colorPalette } from "@stylexblocks/ui/color-palette.stylex.ts";
import { semanticColors } from "@stylexblocks/ui/semantic-tokens/colors.stylex.ts";
import { semanticRadii } from "@stylexblocks/ui/semantic-tokens/radii.stylex.ts";
import { cursor } from "@stylexblocks/ui/tokens/cursor.stylex.ts";
import { durations } from "@stylexblocks/ui/tokens/durations.stylex.ts";
import { fontSizes } from "@stylexblocks/ui/tokens/font-sizes.stylex.ts";
import { fontWeights } from "@stylexblocks/ui/tokens/font-weights.stylex.ts";
import { fonts } from "@stylexblocks/ui/tokens/fonts.stylex.ts";
import { lineHeights } from "@stylexblocks/ui/tokens/line-heights.stylex.ts";
import { sizes } from "@stylexblocks/ui/tokens/sizes.stylex.ts";
import { spacing } from "@stylexblocks/ui/tokens/spacing.stylex.ts";

const tokenInputRoot = stylex.create({
  base: {
    position: "relative",
    width: sizes.full,
  },
});

const tokenInputEditor = stylex.create({
  base: {
    boxSizing: "border-box",
    color: semanticColors.fg,
    cursor: {
      default: "text",
      ":is([data-disabled])": cursor.disabled,
    },
    display: "block",
    fontFamily: fonts.body,
    opacity: {
      default: null,
      ":is([data-disabled])": 0.5,
    },
    outlineOffset: "0px",
    overflowWrap: "anywhere",
    position: "relative",
    transitionDuration: durations.moderate,
    transitionProperty: "border-color, background-color, box-shadow",
    whiteSpace: "pre-wrap",
    width: sizes.full,
    wordBreak: "break-word",
    "::before": {
      content: {
        default: null,
        "[data-empty]": "attr(aria-placeholder)",
      },
      color: semanticColors.fgSubtle,
      pointerEvents: "none",
      userSelect: "none",
    },
  },
});

const tokenInputEditorSizes = stylex.create({
  sm: {
    borderRadius: semanticRadii.controlSm,
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.lg,
    minHeight: sizes._8,
    paddingBlock: spacing._1,
    paddingInline: spacing._2_5,
  },
  md: {
    borderRadius: semanticRadii.control,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.lg,
    minHeight: sizes._10,
    paddingBlock: spacing._1_5,
    paddingInline: spacing._3,
  },
  lg: {
    borderRadius: semanticRadii.controlLg,
    fontSize: fontSizes.md,
    lineHeight: lineHeights.xl,
    minHeight: sizes._12,
    paddingBlock: spacing._2,
    paddingInline: spacing._3_5,
  },
});

const tokenInputEditorVariants = stylex.create({
  outline: {
    backgroundColor: semanticColors.bg,
    borderColor: {
      default: semanticColors.border,
      ":focus": colorPalette.focusRing,
      ':is([data-invalid], [aria-invalid="true"])': semanticColors.borderError,
    },
    borderStyle: "solid",
    borderWidth: "1px",
    outlineColor: {
      default: "transparent",
      ":focus": {
        default: colorPalette.focusRing,
        ':is([data-invalid], [aria-invalid="true"])': semanticColors.borderError,
      },
    },
    outlineStyle: {
      default: "none",
      ":focus": "solid",
    },
    outlineWidth: {
      default: "0px",
      ":focus": "1px",
    },
  },
  subtle: {
    backgroundColor: semanticColors.bgMuted,
    borderColor: {
      default: "transparent",
      ":focus": colorPalette.focusRing,
      ':is([data-invalid], [aria-invalid="true"])': semanticColors.borderError,
    },
    borderStyle: "solid",
    borderWidth: "1px",
    outlineColor: {
      default: "transparent",
      ":focus": {
        default: colorPalette.focusRing,
        ':is([data-invalid], [aria-invalid="true"])': semanticColors.borderError,
      },
    },
    outlineStyle: {
      default: "none",
      ":focus": "solid",
    },
    outlineWidth: {
      default: "0px",
      ":focus": "1px",
    },
  },
  plain: {
    backgroundColor: "transparent",
    borderColor: "transparent",
    borderRadius: 0,
    borderStyle: "none",
    borderWidth: 0,
    outlineColor: "transparent",
    outlineStyle: "none",
    outlineWidth: "0px",
    paddingBlock: spacing._0_5,
    paddingInline: 0,
  },
});

const tokenInputToken = stylex.create({
  base: {
    alignItems: "center",
    backgroundColor: {
      default: colorPalette.subtle,
      ":is([data-selected])": colorPalette.emphasized,
    },
    borderColor: {
      default: colorPalette.border,
      ":is([data-selected])": colorPalette.focusRing,
      ":is([data-unresolved])": semanticColors.borderWarning,
    },
    borderRadius: semanticRadii.indicator,
    borderStyle: "solid",
    borderWidth: "1px",
    boxSizing: "border-box",
    color: colorPalette.fg,
    display: "inline-flex",
    fontWeight: fontWeights.medium,
    gap: spacing._1,
    lineHeight: 1.2,
    marginInline: "1px",
    maxWidth: "100%",
    opacity: {
      default: null,
      ":is([data-pending])": 0.65,
      ":is([data-disabled])": 0.5,
    },
    outlineColor: {
      default: "transparent",
      ":is([data-selected])": colorPalette.focusRing,
    },
    outlineOffset: "1px",
    outlineStyle: {
      default: "none",
      ":is([data-selected])": "solid",
    },
    outlineWidth: {
      default: "0px",
      ":is([data-selected])": "2px",
    },
    userSelect: "none",
    verticalAlign: "baseline",
  },
});

const tokenInputTokenSizes = stylex.create({
  sm: {
    fontSize: fontSizes.xs,
    minHeight: sizes._5,
    paddingBlock: "1px",
    paddingInline: spacing._1_5,
  },
  md: {
    fontSize: fontSizes.xs,
    minHeight: sizes._5,
    paddingBlock: spacing._0_5,
    paddingInline: spacing._2,
  },
  lg: {
    fontSize: fontSizes.sm,
    minHeight: sizes._6,
    paddingBlock: spacing._0_5,
    paddingInline: spacing._2,
  },
});

const tokenInputTokenRemove = stylex.create({
  base: {
    alignItems: "center",
    backgroundColor: {
      default: "transparent",
      ":hover": colorPalette.emphasized,
    },
    borderRadius: semanticRadii.indicatorSm,
    color: "currentColor",
    cursor: {
      default: "pointer",
      ":is([data-disabled], [data-readonly])": "default",
    },
    display: {
      default: "inline-flex",
      ":is([data-readonly])": "none",
    },
    flexShrink: 0,
    justifyContent: "center",
    marginInlineEnd: "-0.125rem",
    opacity: {
      default: 0.65,
      ":hover": 1,
    },
    transitionDuration: durations.fast,
    transitionProperty: "opacity, background-color",
  },
});

const tokenInputTokenRemoveSizes = stylex.create({
  sm: {
    height: sizes._3_5,
    width: sizes._3_5,
  },
  md: {
    height: sizes._4,
    width: sizes._4,
  },
  lg: {
    height: sizes._4_5,
    width: sizes._4_5,
  },
});

const tokenInputRecipe = defineSlotRecipe({
  slots: {
    root: { styles: tokenInputRoot },
    editor: {
      styles: tokenInputEditor,
      sizes: tokenInputEditorSizes,
      variants: tokenInputEditorVariants,
    },
    token: { styles: tokenInputToken, sizes: tokenInputTokenSizes },
    tokenRemove: { styles: tokenInputTokenRemove, sizes: tokenInputTokenRemoveSizes },
  },
  defaultVariants: { size: "md", variant: "outline" },
});

function CloseIcon(props: React.ComponentProps<"svg">) {
  return (
    <svg width="11" height="11" viewBox="0 0 16 16" fill="none" aria-hidden="true" {...props}>
      <path d="m4 4 8 8m0-8-8 8" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" />
    </svg>
  );
}

const { withProvider, withContext } = createSlotRecipeContext(tokenInputRecipe);

export type TokenInputVariantProps = Partial<SlotRecipeVariants<typeof tokenInputRecipe>>;

const StyledToken = withContext(TokenInputToken, "token", "TokenInputToken");

const StyledTokenRemove = withContext(
  TokenInputTokenRemove,
  "tokenRemove",
  "TokenInputTokenRemove",
);

function TokenRemove(props: React.ComponentProps<typeof StyledTokenRemove>) {
  const { children, ...rest } = props;
  return (
    <StyledTokenRemove {...rest}>
      {children ?? <CloseIcon />}
    </StyledTokenRemove>
  );
}
TokenRemove.displayName = "TokenInputTokenRemove";

function defaultRenderToken(token: Token) {
  return (
    <StyledToken>
      {token.label}
      <TokenRemove />
    </StyledToken>
  );
}

type RootNodeProps = TokenInputProps & {
  className?: string;
  style?: React.CSSProperties;
};

function TokenInputRootNode({
  className,
  style,
  children,
  renderToken,
  ...rest
}: RootNodeProps) {
  return (
    <div className={className} style={style}>
      <UiSdkTokenInput {...rest} renderToken={renderToken ?? defaultRenderToken}>
        {children}
      </UiSdkTokenInput>
    </div>
  );
}

const Root = withProvider(TokenInputRootNode, "root", "TokenInputRoot");

const Editor = withContext(TokenInputEditor, "editor", "TokenInputEditor");

/**
 * TokenInput — UI SDK's rich text input with inline atomic tokens wearing
 * the StyleX slot recipe. Tokens move, delete, copy/paste, and undo as a
 * single unit alongside editable text. `<TokenInput.Root variant size>`
 * resolves the recipe once and shares it through context; pair with
 * `TriggerMenu` inside `Root` for `@` mention and `/` command suggestions.
 */
export const TokenInput = {
  Root,
  Editor,
  Token: StyledToken,
  TokenRemove,
};

export type TokenInputRootProps = React.ComponentProps<typeof TokenInput.Root>;
export type TokenInputEditorComponentProps = React.ComponentProps<typeof TokenInput.Editor>;
export type TokenInputTokenComponentProps = React.ComponentProps<typeof TokenInput.Token>;
export type TokenInputTokenRemoveProps = React.ComponentProps<typeof TokenInput.TokenRemove>;

export {
  createTokenInputController,
  defaultCodec,
  fromText,
  toLabel,
  toText,
};

export type {
  TextEditorAdapter,
  TextRange,
  Token,
  TokenCodec,
  TokenDeletion,
  TokenInputApi,
  TokenInputChangeOptions,
  TokenInputController,
  TokenInputEditorProps,
  TokenInputEditorState,
  TokenInputOptions,
  TokenInputPrimitive,
  TokenInputProps,
  TokenInputSubmitDetails,
  TokenInputTokenProps,
  TokenInputTokenState,
  TokenInputValue,
  TokenInputValueChangeDetails,
  TokenInputValueChangeReason,
  TokenInsert,
  UndoStackOptions,
  StyleOverrideProps,
};
```
