Version Range Input

Reads npm semver ranges back in plain English and shows which published versions match and which one installs.

StopsSemver range installs the wrong version

Installation

pnpm dlx shadcn@latest add https://bollard-ui.vercel.app/r/version-range-input.json

The files land in your project. They're yours to edit.

Usage

version-range-input-usage.tsx
import { VersionRangeInput } from "@/components/ui/version-range-input"

<VersionRangeInput label="react" defaultValue="^18.2.0" versions={react} />

What it catches

  • Ranges that match no published version
  • ^0.x and ^0.0.x, which lock to a minor or patch
  • A hyphen range without spaces
  • Commas used where npm expects spaces (AND)
  • Latest version falling outside the range
  • Which version npm would install

Props

VersionRangeInput props
PropTypeDefault
label

Visible label.

string"Version range"
value

Controlled value.

string—
defaultValue

Initial value when uncontrolled.

string""
onValueChange

Called with the text on every change.

(value: string) => void—
versions

Published versions to test the range against. Invalid entries are ignored.

string[][]
delay

Pause in ms before warnings appear while typing. Leaving the field shows them at once.

number500
describe

Turns a parsed range into words. Override to translate.

(sets: ComparatorSet[]) => stringdescribeRange
allowsText

Templates: `{range}`, `{allowed}`, `{token}`, `{version}`, `{count}`, `{total}` are filled in.

string"{range} allows {allowed}."
matchCountText

Count of matching versions.

string"{count} of {total} published versions match"
invalidText

Shown when the range can't be parsed.

string"That isn't a valid range."
andText

Warning when commas are used for AND.

string"npm ranges use a space for AND, not a comma or &&."
hyphenText

Warning for a hyphen range without spaces.

string"{token} is a prerelease of one version, not a range."
zeroMinorText

Warning for ^0.x ranges.

string"{token} won't reach the next 0.x minor: below 1.0, every…
zeroPatchText

Warning for ^0.0.x ranges.

string"{token} allows only that one version: below 0.1, every p…
noMatchText

Shown when no published version matches.

string"No published version matches."
neverText

Shown when the range can never match.

string"This range can never match any version."
latestOutsideText

Shown when the latest version is outside the range.

string"Excludes the latest, {version}."
installsLabel

Badge on the version a fresh install would resolve to.

string"installs"
fixAriaLabel

Accessible label of a suggested-range fix button.

(fix: string) => string(fix) => `Use ${fix}`
versionsLabel

Accessible name of the published-versions list.

string"Published versions"
matchSrText

Screen-reader note on each listed version.

(matches: boolean) => string(matches) => (matches ? " matches" : " does not match")

* required. Also accepts the native props of <input> (className, aria-*, data-*…), passed through to it.

Accessibility

  • Visible <label>; aria-invalid with hint via aria-describedby
  • Hint in a polite live region; parse errors use role=alert
  • Versions list has an aria-label; each row states matches/does not match in text
  • Fix buttons have aria-labels

Only what the source does. Test with your own screen reader and content before shipping.

Limitations

  • Implements node-semver default options; includePrerelease and loose modes are not modelled
  • Needs the published versions list you pass

What it stops

Semver range installs the wrong version

Also known as

semver · version range · package.json · caret · tilde · npm · dependencies · peer deps

Field type

Dependencies

Pairs well with