Skip to content

Maximum Tag Limit

Learn how to set and enforce a maximum number of tags.

Live Demo

Try adding more than 3 tags (the input will be disabled):

Overview

The maxTags prop limits the number of tags users can add. This is useful for:

  • Form field constraints
  • API limitations
  • UX simplification
  • Data validation
  • Preventing data overload

Basic Usage

Props

maxTags

  • Type: number
  • Default: 20
  • Description: Maximum number of tags that can be added

How It Works

Before Limit Reached

  • Input field is visible
  • Users can add tags normally
  • ARIA label shows progress: "Add tags (2 of 5)"

At Limit

  • Input field automatically hides
  • No more tags can be added
  • Error message shown if user tries: "Maximum 5 tags allowed"
  • Input field reappears when tags are removed

Exceeding Limit with Paste

  • Paste only adds tags up to the limit
  • Excess tags are ignored
  • Error message shown for overflow

Practical Examples

Example 1: Survey with Tag Limit

Live Demo - Try selecting up to 5 skills:

What are your top skills?

Select up to 5 skills

Example 2: Shopping Cart Tags (Wishlist)

Live Demo - Build your wishlist (max 10 items):

My Wishlist

Your wishlist can hold up to 10 items. Try adding items and editing them!

Example 3: Image Tagging with Limit

Live Demo - Tag an image (max 8 tags):

Mountain Landscape

Tag This Image

Add up to 8 tags to describe this image

Example 4: API Request with Tag Limits

Live Demo - Submit a request with tags:

Add at least 1 tag to submit

Behavior Notes

Input Field Visibility

  • Hidden when: Tag count >= maxTags
  • Visible when: Tag count < maxTags
  • Reappears: When tags are removed below limit

Error Message

When user tries to exceed limit:

"Maximum X tags allowed"
Auto-dismisses after 4 seconds

Paste Operation

When pasting exceeds limit:

  • Only tags up to limit are added
  • Excess tags ignored
  • Error message shown for overflow
  • User can paste remaining items after removing tags

ARIA Label

Input field ARIA label updates:

"Add tags (2 of 5)"  <- 2 tags added, max is 5
"Add tags (5 of 5)"  <- at limit
"Add tags (0 of 5)"  <- cleared

Accessibility

  • ARIA labels show progress: "X of Y"
  • Tag limit shown in label
  • Input hidden at limit, announced to screen readers
  • Error messages announced to screen readers

TypeScript Support

typescript
interface SmartTagzProps {
  maxTags?: number
  allowDuplicates?: boolean
  allowPaste?: { delimiter: string }
}

See Also

Released under the MIT License.