Tier Slider
Pick a quantity from fixed price tiers with a native range input. The thumb rests only on tier stops and the price cross-fades between them.
Preview
25 seats
The price covers every seat up to the selected tier.
Install
npx shadcn@latest add @dxbh-ui/tier-slider
Add the @dxbh-ui registry to components.json once:
{
"registries": {
"@dxbh-ui": "https://ui.dxbh.dev/r/{name}.json"
}
}Usage
"use client";
import { useState } from "react";
import { TierSlider, type Tier } from "@/components/ui/tier-slider/tier-slider";
const tiers: Tier[] = [
{ value: 5, price: 12, caption: "per month" },
{ value: 10, price: 20, caption: "per month" },
{ value: 25, price: 45, caption: "per month" },
{ value: 50, price: 80, caption: "per month" },
{ value: 100, price: 140, caption: "per month" },
{ value: 250, price: "Custom", caption: "talk to us" },
];
export default function TierSliderDemo() {
const [seats, setSeats] = useState(25);
return (
<div className="w-full max-w-sm">
<TierSlider
label="Seats"
tiers={tiers}
value={seats}
onValueChange={setSeats}
formatValue={(v) => `${v.toLocaleString("en-US")} seats`}
hint="The price covers every seat up to the selected tier."
/>
</div>
);
}Props
| Prop | Type | Default |
|---|---|---|
tiers*Stops in ascending order. | { value: number; price: number | string; caption?: ReactNode }[] | — |
label*Visible label and accessible name. | ReactNode | — |
valueSelected tier value when controlled. Snaps up to the next tier. | number | — |
defaultValueInitial value when uncontrolled. | number | first tier |
onValueChangeCalled when the thumb lands on another tier. | (value: number, tier: Tier) => void | — |
formatValueFormats tier values. | (value: number) => string | toLocaleString |
formatPriceFormats numeric prices. | (price: number) => string | whole USD |
hintDescription linked with aria-describedby. | ReactNode | — |
nameSubmits the selected tier value with a form. | string | — |
disabledDisables the input. | boolean | false |
Polish
- Radix slider replaced by a native <input type="range"> over tier indices. Keyboard, touch and form support come from the platform.
- Pricing rules moved out of the component into a tiers[] config. Product branding and pricing copy removed; the demo uses neutral seat tiers.
- Every price stays mounted in one grid cell and cross-fades with 2px of blur, so a change reads as one number morphing. Transitions retarget mid-fade.
- Keyboard steps and reduced motion swap the price instantly.
- The thumb presses to 0.97 on pointer-down with the press token and --ease-out.
- Values and prices use tabular-nums. aria-valuetext carries both ("25 seats, $45"), so the price isn't a chatty live region while dragging.
- The track fill is a gradient on the input itself, so no child recalculates styles.
- The 44px input height keeps the touch target large.
Source
"use client";
import { useId, useState, type ChangeEvent, type HTMLAttributes, type ReactNode } from "react";
import { cn } from "@/lib/utils";
import { useReducedMotion } from "@/hooks/use-reduced-motion";
export type Tier = {
/** The quantity this stop stands for, e.g. 25 seats. Must be unique. */
value: number;
/** Price at this stop. Numbers go through `formatPrice`. */
price: number | string;
/** Optional line under the price, e.g. "per month". */
caption?: ReactNode;
};
export type TierSliderProps = Omit<HTMLAttributes<HTMLDivElement>, "onChange" | "defaultValue"> & {
/** Stops in ascending order. The thumb rests only on these. */
tiers: readonly Tier[];
/** Selected tier value. Pass with `onValueChange` to control it. */
value?: number;
defaultValue?: number;
onValueChange?: (value: number, tier: Tier) => void;
/** Visible label and accessible name, e.g. "Seats". */
label: ReactNode;
/** Formats a tier value for the readout and screen readers. */
formatValue?: (value: number) => string;
/** Formats numeric prices. Defaults to whole US dollars. */
formatPrice?: (price: number) => string;
/** Extra description, linked with aria-describedby. */
hint?: ReactNode;
name?: string;
disabled?: boolean;
};
const defaultPrice = (price: number) =>
new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", maximumFractionDigits: 0 }).format(price);
const defaultValueFormat = (value: number) => value.toLocaleString("en-US");
/** Index of the first tier at or above `value`, so you always see what your money buys. */
export function tierIndexFor(tiers: readonly Tier[], value: number | undefined): number {
if (tiers.length === 0 || value === undefined) return 0;
const i = tiers.findIndex((t) => t.value >= value);
return i === -1 ? tiers.length - 1 : i;
}
// Every price stays mounted in one grid cell and cross-fades with a little
// blur, so a change reads as one number morphing. Transitions retarget when
// the thumb moves again mid-fade.
const swap =
"col-start-1 row-start-1 transition-[opacity,filter] duration-(--duration-popover,180ms) ease-out motion-reduce:blur-none";
/**
* Pick a quantity from a fixed set of price tiers with a native range input.
* The price cross-fades as the thumb moves between stops.
*/
export function TierSlider({
tiers,
value: valueProp,
defaultValue,
onValueChange,
label,
formatValue = defaultValueFormat,
formatPrice = defaultPrice,
hint,
name,
disabled = false,
className,
id: idProp,
...props
}: TierSliderProps) {
const reactId = useId();
const id = idProp ?? reactId;
const inputId = `${id}-input`;
const hintId = `${id}-hint`;
const [inner, setInner] = useState(() => tiers[tierIndexFor(tiers, defaultValue)]?.value);
const index = tierIndexFor(tiers, valueProp !== undefined ? valueProp : inner);
const tier = tiers[index];
// Keyboard steps repeat; the price swaps instantly for them.
const [instant, setInstant] = useState(false);
const reduced = useReducedMotion();
if (!tier) return null;
const price = (t: Tier) => (typeof t.price === "number" ? formatPrice(t.price) : t.price);
const last = tiers.length - 1;
const fill = last > 0 ? (index / last) * 100 : 100;
const onChange = (event: ChangeEvent<HTMLInputElement>) => {
const next = tiers[Number(event.currentTarget.value)];
if (!next || next.value === tier.value) return;
if (valueProp === undefined) setInner(next.value);
onValueChange?.(next.value, next);
};
return (
<div className={cn("flex flex-col gap-3", className)} id={id} {...props}>
<div className="flex items-end justify-between gap-4">
<div className="flex flex-col gap-1">
<label htmlFor={inputId} className="text-sm font-medium text-foreground">
{label}
</label>
<span className="text-2xl font-semibold text-foreground tabular-nums">{formatValue(tier.value)}</span>
</div>
<div aria-hidden="true" className="grid justify-items-end text-right">
{tiers.map((t, i) => (
<span
key={t.value}
data-active={i === index || undefined}
className={cn(
swap,
"flex flex-col items-end",
(instant || reduced) && "transition-none",
i === index ? "opacity-100 blur-none" : "opacity-0 blur-[2px]",
)}
>
<span className="text-2xl font-semibold text-foreground tabular-nums">{price(t)}</span>
{t.caption ? <span className="text-xs text-muted-foreground">{t.caption}</span> : null}
</span>
))}
</div>
</div>
<input
id={inputId}
type="range"
min={0}
max={last}
step={1}
value={index}
disabled={disabled}
aria-valuetext={`${formatValue(tier.value)}, ${price(tier)}`}
aria-describedby={hint ? hintId : undefined}
onChange={onChange}
onKeyDown={() => setInstant(true)}
onPointerDown={() => setInstant(false)}
style={{
// Set on the input itself, so no children recalculate styles.
background: `linear-gradient(to right, var(--foreground) ${fill}%, var(--muted) ${fill}%) center / 100% 0.5rem no-repeat`,
}}
className={cn(
"h-11 w-full cursor-pointer appearance-none rounded-full bg-transparent outline-none touch-pan-y",
"disabled:cursor-default disabled:opacity-50",
"[&::-webkit-slider-thumb]:size-6 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:border [&::-webkit-slider-thumb]:border-foreground/10 [&::-webkit-slider-thumb]:bg-background [&::-webkit-slider-thumb]:shadow-md",
"[&::-webkit-slider-thumb]:[transition:transform_var(--duration-press,120ms)_var(--ease-out,cubic-bezier(0.23,1,0.32,1))]",
"[&:active::-webkit-slider-thumb]:[transform:scale(0.97)]",
"[&::-moz-range-thumb]:size-6 [&::-moz-range-thumb]:rounded-full [&::-moz-range-thumb]:border [&::-moz-range-thumb]:border-foreground/10 [&::-moz-range-thumb]:bg-background [&::-moz-range-thumb]:shadow-md",
"[&::-moz-range-thumb]:[transition:transform_var(--duration-press,120ms)_var(--ease-out,cubic-bezier(0.23,1,0.32,1))]",
"[&:active::-moz-range-thumb]:[transform:scale(0.97)]",
"focus-visible:[&::-webkit-slider-thumb]:ring-2 focus-visible:[&::-webkit-slider-thumb]:ring-ring/50",
"focus-visible:[&::-moz-range-thumb]:ring-2 focus-visible:[&::-moz-range-thumb]:ring-ring/50",
)}
/>
{name !== undefined ? <input type="hidden" name={name} value={tier.value} /> : null}
<div aria-hidden="true" className="flex justify-between text-xs text-muted-foreground tabular-nums">
<span>{formatValue(tiers[0].value)}</span>
<span>{formatValue(tiers[last].value)}</span>
</div>
{hint ? (
<p id={hintId} className="text-sm text-muted-foreground">
{hint}
</p>
) : null}
</div>
);
}Extracted from moments-website/components/billing/seating-pricing-slider.tsx at f1d46a9.