All components outlinesubtleplainsmmdlgAssign [@@Elena Park](mention:elena) to [@#design-tokens](tag:design-tokens) before launch.
Token Input
Rich text field that mixes editable prose with removable atomic tokens in outline, subtle, and plain styles.
Assign @Elena Park to #design-tokens before launch.
1234567891011121314151617181920212223242526272829303132import { 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> );}Variants
Assign @Elena Park to #design-tokens before launch.
Assign @Elena Park to #design-tokens before launch.
Assign @Elena Park to #design-tokens before launch.
Sizes
Assign @Elena Park to #design-tokens before launch.
Assign @Elena Park to #design-tokens before launch.
Assign @Elena Park to #design-tokens before launch.
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.
Assign @Elena Park to #design-tokens before launch.
12345const [controller] = useState(createTokenInputController);
<TokenInput.Root controller={controller} tokenDeletion="select"> <TokenInput.Editor placeholder="Insert or edit tokens…" /></TokenInput.Root>Install
1npx stylexblocks add token-inputSource
components/ui/ui-sdk/token-input.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425import * 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,};
Examples
Installable blocks built with Token Input — real use cases, delivered as source you own.
AI Chat Landing
Zero-state AI entry surface centered on a single composer with a collapsible attachment drawer, @ mention and / command typeahead, mode menus, and category-driven suggestion cards.