Synertech Web/ui v0.57.5 Site Storybook

Documentation / Dashboards

ChartPeriodPicker

Client@synertech/ui

The window a dashboard is read over. Calendar picks a date and hands it back; this one picks a period: the windows people ask for by name (last 7, 30 and 90 days, last 12 months, month, quarter and year to date) and, for everything else, a calendar in range mode. It belongs in a ChartFilterBar, above the charts — never inside one of them.

Controlled (value + onChange) or not (defaultValue, a preset or an already resolved period). Presets are resolved against now rather than a clock read inside: a report generated at 3 a.m. does not change window at midnight. Both bounds are inclusive (from 00:00 to the last millisecond of the day). The pure functions createPeriod, resolvePeriodPreset, formatPeriodRange, samePeriod, startOfDay / endOfDay are exported so the data can be filtered with the same rule.

A "use client" component: the panel is rendered in a portal, registered as a floating layer of its trigger (it stays usable inside a Modal), and closes on Escape or an outside click. Labels go through UILabelsProvider or presetLabels.

Examples

Basic usage

Uncontrolled: defaultValue accepts a preset name, resolved on mount.

Open in a tab
import { ChartPeriodPicker, UILabelsProvider, frenchUILabels } from "@synertech/ui";

const today = new Date(2026, 7, 29);

export function Basic() {
  return (
    <UILabelsProvider labels={frenchUILabels}>
      <ChartPeriodPicker defaultValue="30d" now={today} locale="fr-CA" />
    </UILabelsProvider>
  );
}

Sizes and chosen presets

presets sets the list and its order, presetLabels renames an entry without going through the provider, custom={false} removes the calendar.

Open in a tab
import { ChartPeriodPicker, UILabelsProvider, frenchUILabels } from "@synertech/ui";

const today = new Date(2026, 7, 29);

export function PresetsAndSizes() {
  return (
    <UILabelsProvider labels={frenchUILabels}>
      <div className="flex flex-wrap items-center gap-3">
        <ChartPeriodPicker size="sm" defaultValue="7d" now={today} />
        <ChartPeriodPicker
          presets={["mtd", "qtd", "ytd"]}
          presetLabels={{ ytd: "Exercice en cours" }}
          defaultValue="ytd"
          now={today}
        />
        <ChartPeriodPicker size="lg" presets={["7d", "30d"]} custom={false} now={today} />
        <ChartPeriodPicker presets={["7d", "30d"]} disabled />
      </div>
    </UILabelsProvider>
  );
}

Controlled, with a custom range

onChange receives a ChartPeriod (preset, from, to). The calendar is bounded by maxDate, shows two months and starts on Monday.

Open in a tab
import { useState } from "react";
import { ChartPeriodPicker, UILabelsProvider, createPeriod, formatPeriodRange, frenchUILabels, type ChartPeriod } from "@synertech/ui";

const today = new Date(2026, 7, 29);

export function Controlled() {
  const [period, setPeriod] = useState<ChartPeriod>(() => createPeriod("90d", today));

  return (
    <UILabelsProvider labels={frenchUILabels}>
      <div className="flex flex-col items-start gap-2">
        <ChartPeriodPicker
          value={period}
          onChange={setPeriod}
          now={today}
          maxDate={today}
          numberOfMonths={2}
          weekStartsOn={1}
          align="start"
          label="Période du rapport"
          locale="fr-CA"
        />
        <p className="text-sm text-gray-600 dark:text-gray-300">
          {period.preset === "custom" ? "Plage libre" : `Préréglage « ${period.preset} »`} :{" "}
          {formatPeriodRange(period.from, period.to, "fr-CA")}
        </p>
      </div>
    </UILabelsProvider>
  );
}

The period functions

resolvePeriodPreset gives a preset’s bounds against a reference instant; samePeriod compares two windows to the millisecond.

Open in a tab
import { createPeriod, formatPeriodRange, resolvePeriodPreset, samePeriod } from "@synertech/ui";

const today = new Date(2026, 7, 29);

export function PeriodUtilities() {
  const presets = ["7d", "30d", "12m", "qtd"] as const;
  const reference = createPeriod("30d", today);

  return (
    <ul className="space-y-1 text-sm text-gray-700 dark:text-gray-200">
      {presets.map((preset) => {
        const { from, to } = resolvePeriodPreset(preset, today);
        const isReference = samePeriod(createPeriod(preset, today), reference);
        return (
          <li key={preset}>
            <code>{preset}</code> → {formatPeriodRange(from, to, "fr-CA")}
            {isReference ? " (fenêtre courante)" : ""}
          </li>
        );
      })}
    </ul>
  );
}

Filtering a chart on the period

The resolved bounds filter the rows before they reach the chart; since from and to are inclusive, a simple comparison is enough.

Open in a tab
import { useMemo, useState } from "react";
import { ChartPeriodPicker, LineChart, UILabelsProvider, createPeriod, frenchUILabels, type ChartPeriod } from "@synertech/ui";

const today = new Date(2026, 7, 29);

const days = Array.from({ length: 120 }, (_, index) => ({
  day: new Date(2026, 4, 2 + index),
  visits: Math.round(1200 + Math.sin(index / 6) * 180 + index * 9),
}));

export function FilteringAChart() {
  const [period, setPeriod] = useState<ChartPeriod>(() => createPeriod("30d", today));
  const rows = useMemo(
    () => days.filter((row) => row.day >= period.from && row.day <= period.to),
    [period]
  );

  return (
    <UILabelsProvider labels={frenchUILabels}>
      <div className="flex flex-col gap-4">
        <ChartPeriodPicker value={period} onChange={setPeriod} now={today} locale="fr-CA" />
        <LineChart
          title="Visites"
          description={`${rows.length} jours`}
          data={rows}
          series={[{ key: "visits", label: "Visites" }]}
          x={{ key: "day", scale: "time" }}
          height={200}
          locale="fr-CA"
        />
      </div>
    </UILabelsProvider>
  );
}

API reference

Components

ChartPeriodPicker

ComponentClient

The window a dashboard is read over.

The one filter every dashboard has, and the one the library was missing: Calendar picks a date and hands it back, which is a different job. This picks a period, offers the windows people actually ask for by name, and falls back to the calendar for the rest.

It belongs in a ChartFilterBar with the other filters, above the charts and never inside one of them — a period picked per card is how two panels end up answering two questions without saying so.

The presets are resolved against now rather than against a clock read somewhere inside: a window is data, and data a component makes up on its own cannot be tested, frozen or shared between two charts.

import { ChartPeriodPicker } from "@synertech/ui";

Props — ChartPeriodPickerProps

PropTypeDefaultDescription
align "start" | "end" "start"

Which end of the trigger the panel lines up with.

custom boolean true

Offers the calendar, for a window the presets do not cover.

defaultValue ChartPeriod | ChartPeriodPreset —

Uncontrolled starting window — a resolved period, or a preset to resolve.

disabled boolean false
label string —

Names the control when the default "Period" is not specific enough.

locale string —

BCP 47 tag the numbers and the dates are written in. Defaults to the locale of UILabelsProvider, then to the language of the runtime.

maxDate Date —
minDate Date —

Bounds of the calendar. A dashboard usually caps it at today.

now Date —

The moment the presets are measured from.

Injected by a test, and by an application that freezes "today" — a report generated at 3am must not silently change window at midnight.

numberOfMonths number 1

Number of months shown side by side by the calendar.

onChange (period: ChartPeriod) => void —
presetLabels Partial<Record<ChartPeriodPreset, string>> —

Overrides a preset's caption, without going through the labels provider.

presets readonly ChartPeriodPreset[] DEFAULT_CHART_PERIOD_PRESETS

Which windows to offer, in the order they are listed.

size ChartSize "md"
value ChartPeriod —

Controlled window.

weekStartsOn CalendarWeekStart —

+ 284 inherited native attributes from ButtonHTMLAttributes, HTMLAttributes, AriaAttributes (className, id, aria-*, event handlers…).

Functions

createPeriod

Function

A named window, resolved into the period a chart is filtered by.

import { createPeriod } from "@synertech/ui";
createPeriod(preset: ChartPeriodPreset, now?: Date): ChartPeriod

endOfDay

Function

The last millisecond of a day.

A window ending at midnight drops everything that happened during its last day — the single most common off-by-one in a dashboard.

import { endOfDay } from "@synertech/ui";
endOfDay(date: Date): Date

The two ends of a window as one string.

formatRange is what a locale expects here — it collapses the parts the two dates share ("1 – 15 March 2026" rather than twice the month and the year).

import { formatPeriodRange } from "@synertech/ui";
formatPeriodRange(from: Date, to: Date, locale?: string): string

Resolves a named window against a reference moment.

"The last 7 days" counts today as one of them, which is what a reader expects from a figure that includes what happened this morning.

import { resolvePeriodPreset } from "@synertech/ui";
resolvePeriodPreset(preset: ChartPeriodPreset, now?: Date): { from: Date; to: Date; }

samePeriod

Function

true when two periods cover the same window, to the millisecond.

import { samePeriod } from "@synertech/ui";
samePeriod(a: ChartPeriod | undefined, b: ChartPeriod | undefined): boolean

startOfDay

Function

Midnight at the start of a day, in the reader's own time zone.

import { startOfDay } from "@synertech/ui";
startOfDay(date: Date): Date

Constants

The presets a picker offers when it is not told otherwise.

import { DEFAULT_CHART_PERIOD_PRESETS } from "@synertech/ui";
const DEFAULT_CHART_PERIOD_PRESETS: readonly ChartPeriodPreset[] = [ "7d", "30d", "90d", "12m", "mtd", "ytd", ]

Types

ChartPeriod

Interface

A resolved window: where it came from, and the two dates it covers.

interface ChartPeriod
FieldTypeDefaultDescription
from• Date —
preset• ChartPeriodPreset —

The entry that produced it. custom when the dates were picked by hand.

to• Date —

The windows a filter row offers by name.

type ChartPeriodPreset = "7d" | "30d" | "90d" | "12m" | "mtd" | "qtd" | "ytd" | "custom"