---
title: "Tags Input · Base UI — StyleX Blocks"
description: "Enter-to-add token input with removable item previews in outline, subtle, and flushed styles."
url: "https://stylexblocks.com/components/base-ui/tags-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/"
---

# Tags Input

Enter-to-add token input with removable item previews in outline, subtle, and flushed styles.

- Behavior engine: Base UI
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/tags-input.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/tags-input.md)
- Category: Form
- Variants: outline, subtle, flushed
- Sizes: xs, sm, md, lg
- HTML page: https://stylexblocks.com/components/base-ui/tags-input/

## Install

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

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

Then add the item as source you own:

```sh
npx stylexblocks add tags-input
```

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

## Usage

```tsx
import { TagsInput } from "#components/ui/base-ui/tags-input.tsx";

export function Example() {
  return (
    // Base UI has no tags-input machine, so Root owns the tag list
    // (value/defaultValue/onValueChange); Enter adds, the delete trigger
    // removes, and parts compose through render
    <TagsInput.Root defaultValue={["Design", "Engineering"]} variant="outline" size="md">
      <TagsInput.Label>Teams</TagsInput.Label>
      <TagsInput.Control>
        <TagsInput.Context>
          {(api) =>
            api.value.map((tag) => (
              <TagsInput.Item key={tag} value={tag}>
                <TagsInput.ItemPreview>
                  <TagsInput.ItemText />
                  <TagsInput.ItemDeleteTrigger />
                </TagsInput.ItemPreview>
              </TagsInput.Item>
            ))
          }
        </TagsInput.Context>
        <TagsInput.Input placeholder="Add team" aria-label="Add team" />
      </TagsInput.Control>
    </TagsInput.Root>
  );
}
```

## Source

`components/ui/base-ui/tags-input.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { createContext, useContext, useMemo, useState } from "react";
import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import { createSlotRecipeContext } from "#lib/base-ui/create-slot-recipe-context.tsx";
import {
  defineSlotRecipe,
  resolveSlotStyles,
  type SlotRecipeVariants,
} from "#lib/recipe-props.ts";
import {
  tagsInputItemDeleteTriggerIconVars,
  tagsInputClearTriggerIconVars,
} from '@stylexblocks/ui/slot-recipes/tags-input.stylex.ts'

import { colorPalette } from '@stylexblocks/ui/color-palette.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 { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'


const tagsInputRoot = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'column',
    gap: spacing._1_5,
    width: sizes.full,
  },
})

const tagsInputRootSizes = stylex.create({
  xs: {
    '--tags-input-height': sizes._8,
    '--tags-input-px': spacing._1_5,
    '--tags-input-py': spacing._1,
    '--tags-input-gap': spacing._1,
    '--tags-input-item-height': sizes._6,
    '--tags-input-item-px': spacing._2,
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
  },
  sm: {
    '--tags-input-height': sizes._9,
    '--tags-input-px': spacing._1_5,
    '--tags-input-py': spacing._1,
    '--tags-input-gap': spacing._1,
    '--tags-input-item-height': sizes._6,
    '--tags-input-item-px': spacing._2,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
  },
  md: {
    '--tags-input-height': sizes._10,
    '--tags-input-px': spacing._1_5,
    '--tags-input-py': spacing._1,
    '--tags-input-gap': spacing._1,
    '--tags-input-item-height': sizes._7,
    '--tags-input-item-px': spacing._2,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
  },
  lg: {
    '--tags-input-height': sizes._10,
    '--tags-input-px': spacing._1_5,
    '--tags-input-py': spacing._1,
    '--tags-input-gap': spacing._1,
    '--tags-input-item-height': sizes._8,
    '--tags-input-item-px': spacing._2,
    fontSize: fontSizes.md,
    lineHeight: lineHeights.md,
  },
})

const tagsInputLabel = stylex.create({
  base: {
    fontWeight: fontWeights.medium,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
  },
})

const tagsInputControl = stylex.create({
  base: {
    '--focus-color': colorPalette.focusRing,
    '--error-color': semanticColors.borderError,
    minHeight: 'var(--tags-input-height)',
    '--input-height': 'var(--tags-input-height)',
    paddingInline: 'var(--tags-input-px)',
    paddingBlock: 'var(--tags-input-py)',
    gap: 'var(--tags-input-gap)',
    display: 'flex',
    flexWrap: 'wrap',
    alignItems: 'center',
    borderRadius: semanticRadii.l2,
    position: 'relative',
    transitionProperty: 'border-color, box-shadow',
    transitionDuration: durations.moderate,
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
    borderColor: {
      default: null,
      ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
    },
  },
})

const tagsInputControlVariants = stylex.create({
  outline: {
    borderWidth: '1px',
    backgroundColor: semanticColors.bg,
    outlineWidth: {
      default: null,
      ':focus': '1px',
    },
    outlineStyle: {
      default: null,
      ':focus': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus': {
        default: 'var(--focus-color)',
        ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
      },
    },
    borderColor: {
      default: null,
      ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
      ':focus': {
        default: 'var(--focus-color)',
        ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
      },
    },
  },
  subtle: {
    backgroundColor: semanticColors.bgMuted,
    borderWidth: '1px',
    borderColor: {
      default: 'transparent',
      ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
      ':focus': {
        default: 'var(--focus-color)',
        ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
      },
    },
    outlineWidth: {
      default: null,
      ':focus': '1px',
    },
    outlineStyle: {
      default: null,
      ':focus': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus': {
        default: 'var(--focus-color)',
        ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
      },
    },
  },
  flushed: {
    borderRadius: 0,
    paddingInline: 0,
    backgroundColor: 'transparent',
    borderBottomWidth: '1px',
    borderBottomColor: semanticColors.border,
    borderColor: {
      default: null,
      ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)',
      ':focus': 'var(--focus-color)',
    },
    boxShadow: {
      default: null,
      ':focus': '0px 1px 0px 0px var(--focus-color)',
    },
  },
})

const tagsInputInput = stylex.create({
  base: {
    flex: 1,
    minWidth: sizes._20,
    outline: 'none',
    backgroundColor: 'transparent',
    color: semanticColors.fg,
    paddingInline: 'calc(var(--tags-input-item-px) / 1.25)',
    height: 'var(--tags-input-item-height)',
    display: {
      default: null,
      ':is([data-readonly], [aria-readonly="true"], [readonly])': 'none',
    },
  },
})

const tagsInputItemText = stylex.create({
  base: {
    lineClamp: 1,
  },
})

const tagsInputItemInput = stylex.create({
  base: {
    outline: 'none',
    backgroundColor: 'transparent',
    minWidth: '2ch',
    color: 'inherit',
    paddingInline: 'var(--tags-input-item-px)',
    height: 'var(--tags-input-item-height)',
  },
})

const tagsInputItemPreview = stylex.create({
  base: {
    height: 'var(--tags-input-item-height)',
    userSelect: 'none',
    display: 'inline-flex',
    alignItems: 'center',
    gap: spacing._1,
    borderRadius: semanticRadii.l1,
    paddingInline: 'var(--tags-input-item-px)',
  },
})

const tagsInputItemPreviewVariants = stylex.create({
  outline: {
    backgroundColor: colorPalette.subtle,
  },
  subtle: {
    backgroundColor: semanticColors.bg,
    borderWidth: '1px',
  },
  flushed: {
    backgroundColor: colorPalette.subtle,
  },
})

const tagsInputItemDeleteTrigger = stylex.create({
  base: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    width: 'calc(var(--tags-input-item-height) / 1.5)',
    height: 'calc(var(--tags-input-item-height) / 1.5)',
    marginInlineEnd: `calc(${spacing._1} * -1)`,
    opacity: {
      default: 0.4,
      ':hover': 1,
    },
    [tagsInputItemDeleteTriggerIconVars.width]: '80%',
    [tagsInputItemDeleteTriggerIconVars.height]: '80%',
  },
})

const tagsInputItemDeleteTriggerIcon = stylex.create({
  base: {
    width: tagsInputItemDeleteTriggerIconVars.width,
    height: tagsInputItemDeleteTriggerIconVars.height,
  },
})

const tagsInputClearTrigger = stylex.create({
  base: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    width: 'calc(var(--tags-input-item-height) / 1.5)',
    height: 'calc(var(--tags-input-item-height) / 1.5)',
    color: semanticColors.fgMuted,
    outlineWidth: {
      default: null,
      ':focus-visible': 'var(--focus-ring-width, 0)',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '0px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
    focusRingWidth: '2px',
    borderRadius: semanticRadii.l1,
    [tagsInputClearTriggerIconVars.width]: sizes._5,
    [tagsInputClearTriggerIconVars.height]: sizes._5,
  },
})

const tagsInputClearTriggerIcon = stylex.create({
  base: {
    width: tagsInputClearTriggerIconVars.width,
    height: tagsInputClearTriggerIconVars.height,
  },
})

export type TagsInputSize = keyof typeof tagsInputRootSizes

export type TagsInputVariant = keyof typeof tagsInputControlVariants

const tagsInputRecipe = defineSlotRecipe({
  slots: {
    root: { styles: tagsInputRoot, sizes: tagsInputRootSizes },
    label: { styles: tagsInputLabel },
    control: { styles: tagsInputControl, variants: tagsInputControlVariants },
    input: { styles: tagsInputInput },
    itemText: { styles: tagsInputItemText },
    itemInput: { styles: tagsInputItemInput },
    itemPreview: { styles: tagsInputItemPreview, variants: tagsInputItemPreviewVariants },
    itemDeleteTrigger: { styles: tagsInputItemDeleteTrigger },
    clearTrigger: { styles: tagsInputClearTrigger },
  },
  defaultVariants: { size: "md", variant: "outline" },
});

const chrome = stylex.create({
  btnReset: {
    appearance: "none",
    backgroundColor: "transparent",
    borderWidth: 0,
    borderStyle: "none",
    padding: 0,
    margin: 0,
    fontFamily: "inherit",
    fontSize: "inherit",
    color: "inherit",
    cursor: "pointer",
    textAlign: "inherit",
  },
});

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

interface TagsInputContextValue {
  value: string[];
  addValue: (tag: string) => void;
  removeValue: (tag: string) => void;
  clear: () => void;
}

const TagsInputContext = createContext<TagsInputContextValue>({
  value: [],
  addValue: () => {},
  removeValue: () => {},
  clear: () => {},
});

const ItemContext = createContext<{ value: string }>({ value: "" });

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

export type TagsInputVariantProps = Partial<SlotRecipeVariants<typeof tagsInputRecipe>>;

function RootNode({
  render,
  value,
  defaultValue = [],
  onValueChange,
  ...rest
}: useRender.ComponentProps<"div"> & {
  value?: string[];
  defaultValue?: string[];
  onValueChange?: (value: string[]) => void;
}) {
  const [inner, setInner] = useState<string[]>(defaultValue);
  const current = value !== undefined ? value : inner;
  const ctx = useMemo<TagsInputContextValue>(() => {
    const commit = (next: string[]) => {
      setInner(next);
      onValueChange?.(next);
    };
    return {
      value: current,
      addValue: (tag: string) => {
        const trimmed = tag.trim();
        if (trimmed && !current.includes(trimmed)) commit([...current, trimmed]);
      },
      removeValue: (tag: string) => commit(current.filter((t) => t !== tag)),
      clear: () => commit([]),
    };
  }, [current, onValueChange]);
  const element = useRender({
    defaultTagName: "div",
    render: render as useRender.RenderProp,
    props: rest,
  });
  return <TagsInputContext.Provider value={ctx}>{element}</TagsInputContext.Provider>;
}

function Item({ value, children }: { value: string; children?: React.ReactNode }) {
  const ctx = useMemo(() => ({ value }), [value]);
  return <ItemContext.Provider value={ctx}>{children}</ItemContext.Provider>;
}

function ItemTextNode({
  render,
  children,
  ...rest
}: useRender.ComponentProps<"span">) {
  const { value } = useContext(ItemContext);
  return useRender({
    defaultTagName: "span",
    render: render as useRender.RenderProp,
    props: mergeProps({ children: children ?? value }, rest),
  });
}

function ItemDeleteTriggerNode({
  render,
  children,
  onClick,
  ...rest
}: useRender.ComponentProps<"button">) {
  const { removeValue } = useContext(TagsInputContext);
  const { value } = useContext(ItemContext);
  return useRender({
    defaultTagName: "button",
    render: render as useRender.RenderProp,
    props: mergeProps(
      {
        type: "button" as const,
        "aria-label": `Remove ${value}`,
        onClick: (event: React.MouseEvent<HTMLButtonElement>) => {
          (onClick as ((event: React.MouseEvent<HTMLButtonElement>) => void) | undefined)?.(event);
          removeValue(value);
        },
        children: children ?? (
          <CloseIcon {...stylex.props(tagsInputItemDeleteTriggerIcon.base)} />
        ),
      },
      rest,
    ),
  });
}

function InputNode({
  render,
  onChange,
  onKeyDown,
  ...rest
}: Omit<useRender.ComponentProps<"input">, "value">) {
  const ctx = useContext(TagsInputContext);
  const [draft, setDraft] = useState("");
  return useRender({
    defaultTagName: "input",
    render: render as useRender.RenderProp,
    props: mergeProps(
      {
        value: draft,
        onChange: (event: React.ChangeEvent<HTMLInputElement>) => {
          (onChange as ((event: React.ChangeEvent<HTMLInputElement>) => void) | undefined)?.(event);
          setDraft(event.target.value);
        },
        onKeyDown: (event: React.KeyboardEvent<HTMLInputElement>) => {
          (onKeyDown as ((event: React.KeyboardEvent<HTMLInputElement>) => void) | undefined)?.(
            event,
          );
          if (event.key === "Enter" && draft.trim()) {
            event.preventDefault();
            ctx.addValue(draft);
            setDraft("");
          } else if (event.key === "Backspace" && draft === "" && ctx.value.length > 0) {
            ctx.removeValue(ctx.value[ctx.value.length - 1]!);
          }
        },
      },
      rest,
    ),
  });
}

function ClearTriggerNode({
  render,
  children,
  onClick,
  ...rest
}: useRender.ComponentProps<"button">) {
  const { clear } = useContext(TagsInputContext);
  return useRender({
    defaultTagName: "button",
    render: render as useRender.RenderProp,
    props: mergeProps(
      {
        type: "button" as const,
        "aria-label": "Clear tags",
        onClick: (event: React.MouseEvent<HTMLButtonElement>) => {
          (onClick as ((event: React.MouseEvent<HTMLButtonElement>) => void) | undefined)?.(event);
          clear();
        },
        children: children ?? <CloseIcon {...stylex.props(tagsInputClearTriggerIcon.base)} />,
      },
      rest,
    ),
  });
}

function Context({
  children,
}: {
  children: (ctx: TagsInputContextValue) => React.ReactNode;
}) {
  return children(useContext(TagsInputContext));
}

/**
 * TagsInput — Enter-to-add token input. Base UI ships no tags-input machine,
 * so Root owns the tag list (controlled via `value`/`onValueChange` or
 * uncontrolled via `defaultValue`). `<TagsInput.Root variant size>` resolves
 * the recipe once; every part inherits it through context. Composition per
 * part via `render`.
 */
export const TagsInput = {
  Root: withProvider(RootNode, "root", "TagsInputRoot"),
  Label: withContext("span", "label", "TagsInputLabel"),
  Control: withContext("div", "control", "TagsInputControl"),
  Item,
  ItemPreview: withContext("span", "itemPreview", "TagsInputItemPreview"),
  ItemText: withContext(ItemTextNode, "itemText", "TagsInputItemText"),
  ItemDeleteTrigger: withContext(
    ItemDeleteTriggerNode,
    (v) => [chrome.btnReset, ...resolveSlotStyles(tagsInputRecipe.slots.itemDeleteTrigger, v)],
    "TagsInputItemDeleteTrigger",
  ),
  Input: withContext(InputNode, "input", "TagsInputInput"),
  ClearTrigger: withContext(
    ClearTriggerNode,
    (v) => [chrome.btnReset, ...resolveSlotStyles(tagsInputRecipe.slots.clearTrigger, v)],
    "TagsInputClearTrigger",
  ),
  Context,
};

export type TagsInputRootProps = React.ComponentProps<typeof TagsInput.Root>;
```
