Date Range Compare

1 个变体

A dashboard date-range bar with presets, a range calendar, and a compare switch that drives a row of KPI cards.

Range and KPIs

Preset tabs, a two-month range calendar, and a compare switch drive a row of KPI cards. Values roll, change pills pop in when compare is on, and each sparkline redraws with a dashed previous-period line.

  • Visitors

    129,483up1%
  • Revenue

    $144,673down1%
  • Conversion

    4.20%down2%
  • Avg session

    1m 56sdown2%
"use client";

import { DateRangeCompare } from "@/components/premium/date-range-compare";

export function DateRangeCompareKpisPreview() {
  return (
    <div className="w-full px-4 py-10">
      <DateRangeCompare />
    </div>
  );
}

安装

$ bunx --bun shadcn add @beui-pro/date-range-compare-kpis

API 参考

除标注为必填的属性外,其余均为可选。数据、文案和颜色默认使用示例内容。

DateRangeCompare12 个属性
  • kpis

    KPI cards in display order; four fit the row.

    KpiDefinition[]
  • getKpiData

    Computes each KPI for a range and its comparison range. Defaults to deterministic sample data.

    KpiDataFunction
  • formatters

    Formatters for each KPI format; any left out keep their defaults (USD for currency).

    Partial<KpiFormatters>
  • value

    Controlled value: preset, range, compare switch and compare mode.

    DateRangeCompareValue
  • defaultValue

    Starting value when uncontrolled. Defaults to the 30d preset with compare on.

    DateRangeCompareValue
  • onChange

    Called with the whole next value whenever any part changes.

    ((value: DateRangeCompareValue) => void)
  • presets

    Quick ranges shown as tabs, before the Custom tab.

    CompareRangePreset[]
  • today

    The day presets end on and the last selectable day, `YYYY-MM-DD`.

    string

    默认值 2026-11-28

  • min

    Earliest selectable day, `YYYY-MM-DD`.

    string
  • locale

    BCP 47 locale for dates.

    string
  • labels

    Copy overrides; anything left out uses the English defaults.

    Partial<DateRangeCompareLabels>
  • className

    Extra classes for the bar.

    string
ChangePill3 个属性
  • from必填

    Value in the comparison range.

    number
  • to必填

    Value in the selected range.

    number
  • className

    Extra classes for the pill.

    string
CompareControl9 个属性
  • compare必填

    Whether comparison is on.

    boolean
  • compareMode必填

    What the range is compared against.

    "previous-period" | "previous-year"
  • range必填

    The selected range; each comparison option shows the range it would compare against.

    CompareDateRange
  • locale必填

    BCP 47 locale for the comparison ranges.

    string
  • currentYear

    Year of "today" as `YYYY`; ranges in other years show their year.

    string
  • labels必填

    Copy for the switch, menu and range text.

    Pick<DateRangeCompareLabels, "compare" | "compareMode" | "previousPeriod" | "previousYear" | "versus">
  • onCompareChange必填

    Called when the switch flips.

    (compare: boolean) => void
  • onCompareModeChange必填

    Called when a comparison mode is chosen.

    (mode: CompareMode) => void
  • className

    Extra classes for the group.

    string
DateRangeBar9 个属性
  • value

    Controlled value: preset, range, compare switch and compare mode.

    DateRangeCompareValue
  • defaultValue

    Starting value when uncontrolled. Defaults to the 30d preset with compare on.

    DateRangeCompareValue
  • onChange

    Called with the whole next value whenever any part changes.

    ((value: DateRangeCompareValue) => void)
  • presets

    Quick ranges shown as tabs, before the Custom tab.

    CompareRangePreset[]
  • today

    The day presets end on and the last selectable day, `YYYY-MM-DD`.

    string

    默认值 2026-11-28

  • min

    Earliest selectable day, `YYYY-MM-DD`.

    string
  • locale

    BCP 47 locale for dates.

    string

    默认值 en-US

  • labels

    Copy overrides; anything left out uses the English defaults.

    Partial<DateRangeCompareLabels>
  • className

    Extra classes for the bar.

    string
KpiCard7 个属性
  • kpi必填

    The KPI's label, format and colors.

    KpiDefinition
  • snapshot必填

    Values and trends for the selected and comparison ranges.

    KpiSnapshot
  • compare必填

    Whether the change pill and comparison line show.

    boolean
  • drawKey必填

    Changes with the range, so the sparkline draws in again.

    string
  • formatters必填

    Formatter for each KPI format.

    KpiFormatters
  • index必填

    Position in the row, for the mount stagger.

    number
  • className

    Extra classes for the card.

    string
KpiSparkline6 个属性
  • values必填

    Selected range trend, oldest first.

    readonly number[]
  • previousValues必填

    Comparison range trend on the same x positions as `values`.

    readonly number[]
  • showPrevious必填

    Whether to draw the dashed comparison line behind the trend.

    boolean
  • drawKey必填

    Changes whenever the range changes, so the line draws in again.

    string
  • strokeClassName必填

    Stroke class for the trend line, such as `stroke-sky-500`.

    string
  • className

    Extra classes for the svg; its size comes from here.

    string
RangeCalendar7 个属性
  • range必填

    Applied range shown on the trigger and selected when the calendar opens.

    CompareDateRange
  • max必填

    Latest selectable day, usually today, `YYYY-MM-DD`.

    string
  • min

    Earliest selectable day, `YYYY-MM-DD`.

    string
  • locale必填

    BCP 47 locale for dates.

    string
  • labels必填

    Copy for the calendar and its buttons.

    Pick<DateRangeCompareLabels, "calendar" | "previousMonth" | "nextMonth" | "day" | "days">
  • onRangeChange必填

    Called with a complete range picked in the calendar.

    (range: CompareDateRange) => void
  • className

    Extra classes for the trigger.

    string
useDateRangeValue3 个属性
  • value

    Controlled value.

    DateRangeCompareValue
  • defaultValue必填

    Starting value when uncontrolled.

    DateRangeCompareValue
  • onChange

    Called with the next value whenever any part of it changes.

    ((value: DateRangeCompareValue) => void)
类型6 个类型

这些属性使用的对象类型及其字段。

CompareRangePreset3 个字段
  • id必填

    Stable id stored in the value, such as `30d`. Never `custom`.

    string
  • label必填

    Tab label, such as "30d".

    string
  • days必填

    Days covered, ending on `today`. 1 means today only.

    number
DateRangeCompareValue4 个字段
  • preset必填

    Selected preset id, or `custom` after picking dates in the calendar.

    string
  • range必填

    Selected range.

    CompareDateRange
  • compare必填

    Whether the comparison range is shown.

    boolean
  • compareMode必填

    What the selected range is compared against.

    CompareMode
KpiDefinition8 个字段
  • id必填

    Stable id.

    string
  • label必填

    Card label, such as "Visitors".

    string
  • format必填

    Which formatter shows the value.

    KpiFormat
  • aggregate必填

    `sum` adds the days up (visitors, revenue); `average` averages them (rates, durations).

    "sum" | "average"
  • base必填

    Typical value for one day.

    number
  • trend必填

    Daily growth as a fraction, such as 0.002 for 0.2% a day.

    number
  • noise必填

    How far a day can drift from its trend line, as a fraction of it.

    number
  • strokeClassName必填

    Sparkline stroke class, such as `stroke-sky-500`.

    string
DateRangeCompareLabels13 个字段
  • presets必填

    Accessible name of the preset tabs.

    string
  • custom必填

    Tab label for a range picked in the calendar.

    string
  • calendar必填

    Accessible name of the calendar.

    string
  • compare必填

    Label beside the compare switch.

    string
  • previousPeriod必填

    Option label for the previous period.

    string
  • previousYear必填

    Option label for the same dates a year earlier.

    string
  • compareMode必填

    Accessible name of the comparison mode menu.

    string
  • versus必填

    Prefix for the comparison range text, such as "vs".

    string
  • day必填

    Unit after a one-day count on the range trigger.

    string
  • days必填

    Unit after a count of several days on the range trigger.

    string
  • previousMonth必填

    Previous month button.

    string
  • nextMonth必填

    Next month button.

    string
  • kpis必填

    Accessible name of the KPI row.

    string
CompareDateRange2 个字段
  • from必填

    First day of the range, `YYYY-MM-DD`.

    string
  • to必填

    Last day of the range, `YYYY-MM-DD`.

    string
KpiSnapshot5 个字段
  • id必填

    The KPI it belongs to.

    string
  • value必填

    Value for the selected range.

    number
  • previous必填

    Value for the comparison range.

    number
  • series必填

    Bucketed trend for the selected range, oldest first.

    number[]
  • previousSeries必填

    Bucketed trend for the comparison range, the same length as `series`.

    number[]