Sidebar Dashboard Shell
Sidebar, top bar and scrollable content slot, with a mobile drawer that closes on Escape and marks the active route.
A reporting window with the comparison window spelled out beside it — resolved dates, UTC arithmetic, and an option to align weekdays so a weekend is never compared against a Tuesday.
components/dashboard-comparison-period.tsxnpx hoverlab add dashboard-comparison-period
Or over MCP, from your editor's agent — no account needed.
Free to read, copy and install, for personal and non-commercial projects. Shipping it in client work or a paid product needs Pro ($79 once). The source lands in your repo and stops being ours — no attribution, nothing to upgrade.
Start a page with this section — add more, order them, and leave with the page source.
Every percentage on this dashboard is measured against the comparison window below.
Both windows are inclusive of their end dates and computed in UTC, so a change of clocks never produces a week of six days and 23 hours.
Rendered live in your current theme — this is the same component whose source is below, not a screenshot of it.
'use client'
/**
* <DashboardComparisonPeriod> — the date range, and the range it is being
* compared against.
*
* Every dashboard in this catalog shows deltas — "+12.4%" beside a number
* — and not one of them says what the comparison is against. That is the
* single most load-bearing missing sentence in analytics UI: a figure is up
* 12% against *something*, and whether that something is the previous seven
* days or the same week last year changes whether it is good news.
*
* WHAT THIS GETS RIGHT THAT MOST PICKERS DO NOT
*
* The comparison is a first-class control, not a checkbox in a menu. Most
* products bury it, default it to "previous period", and then render deltas
* as if they were facts. Here both ranges are on screen at once, with their
* actual dates spelled out, because "Jan 1–7 vs Dec 25–31" is a claim a
* reader can check and "vs previous period" is not.
*
* THE CASE THAT IS USUALLY WRONG
*
* Period-over-period comparison across a week boundary is misleading for
* anything with a weekly rhythm — a Tuesday-to-Monday window compared with
* the seven days before it lands a weekend against a weekday. `alignWeekday`
* exists for that, and the panel says in words when it is on, rather than
* quietly shifting the dates by a day or two and hoping nobody reconciles.
*
* NO DATE LIBRARY. The ranges are computed with `Date` arithmetic in UTC so
* a block that ships as source does not drag `date-fns` or `dayjs` into a
* project that may already have the other one. UTC specifically: local-time
* arithmetic across a daylight-saving boundary silently produces a
* six-day-23-hour "week".
*
* ACCESSIBILITY: the presets are a real radiogroup rather than a row of
* buttons that look selected, so a screen reader announces "3 of 5" instead
* of five unrelated buttons; the resolved dates live in an `aria-live`
* region so changing a preset is announced rather than only seen.
*/
import * as React from 'react'
import { ArrowLeftRight, CalendarRange, Check, Info } from 'lucide-react'
export type ComparisonMode = 'previous' | 'year' | 'none'
export interface PeriodPreset {
id: string
label: string
/** Length of the window in days. */
days: number
}
export interface DashboardComparisonPeriodProps {
presets?: PeriodPreset[]
initialPresetId?: string
initialMode?: ComparisonMode
/**
* Anchor date the windows are measured back from. Fixed by default so the
* preview and the screenshots do not change every night.
*/
today?: Date
/**
* Shift the comparison window to start on the same weekday.
*
* Off by default because it is the surprising behaviour, and a block that
* silently moved a customer's reporting window would be worse than one
* that offers the option in the open.
*/
alignWeekday?: boolean
className?: string
}
const DEFAULT_PRESETS: PeriodPreset[] = [
{ id: '7d', label: 'Last 7 days', days: 7 },
{ id: '14d', label: 'Last 14 days', days: 14 },
{ id: '28d', label: 'Last 28 days', days: 28 },
{ id: '90d', label: 'Last quarter', days: 90 },
{ id: '365d', label: 'Last year', days: 365 },
]
const MS_PER_DAY = 86_400_000
/** UTC-only arithmetic — see the header for why local time is a trap here. */
function addDays(date: Date, days: number): Date {
return new Date(date.getTime() + days * MS_PER_DAY)
}
function formatRange(start: Date, end: Date): string {
const sameYear = start.getUTCFullYear() === end.getUTCFullYear()
const opts: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric', timeZone: 'UTC' }
const from = start.toLocaleDateString('en-US', opts)
const to = end.toLocaleDateString('en-US', {
...opts,
year: sameYear ? undefined : 'numeric',
})
return `${from} – ${to}${sameYear ? `, ${end.getUTCFullYear()}` : ''}`
}
interface Range {
start: Date
end: Date
}
function primaryRange(today: Date, days: number): Range {
const end = today
// Inclusive of both ends: "last 7 days" is seven days, not eight.
return { start: addDays(end, -(days - 1)), end }
}
function comparisonRange(
primary: Range,
days: number,
mode: ComparisonMode,
alignWeekday: boolean,
): Range | null {
if (mode === 'none') return null
if (mode === 'year') {
// Same calendar dates, one year back. Deliberately not "365 days back",
// which drifts a day every leap year and makes an annual comparison
// stop lining up with the thing it is comparing.
const shift = (date: Date) => {
const next = new Date(date)
next.setUTCFullYear(next.getUTCFullYear() - 1)
return next
}
return { start: shift(primary.start), end: shift(primary.end) }
}
let end = addDays(primary.start, -1)
if (alignWeekday) {
// Walk back whole weeks until the weekday matches, so a weekend never
// gets compared against a Tuesday.
while (end.getUTCDay() !== primary.end.getUTCDay()) end = addDays(end, -1)
}
return { start: addDays(end, -(days - 1)), end }
}
const MODES: { id: ComparisonMode; label: string; hint: string }[] = [
{ id: 'previous', label: 'Previous period', hint: 'The window immediately before this one.' },
{ id: 'year', label: 'Same period last year', hint: 'The same calendar dates, one year back.' },
{ id: 'none', label: 'No comparison', hint: 'Show absolute numbers with no deltas.' },
]
export function DashboardComparisonPeriod({
presets = DEFAULT_PRESETS,
initialPresetId = '28d',
initialMode = 'previous',
today = new Date(Date.UTC(2026, 7, 31)),
alignWeekday = false,
className = '',
}: DashboardComparisonPeriodProps) {
const [presetId, setPresetId] = React.useState(initialPresetId)
const [mode, setMode] = React.useState<ComparisonMode>(initialMode)
const [aligned, setAligned] = React.useState(alignWeekday)
const preset = presets.find((p) => p.id === presetId) ?? presets[0]
const primary = primaryRange(today, preset.days)
const comparison = comparisonRange(primary, preset.days, mode, aligned)
return (
<section
className={`rounded-2xl border border-border bg-card p-5 text-card-foreground sm:p-6 ${className}`}
>
<header className="flex flex-wrap items-start justify-between gap-3">
<div>
<h2 className="flex items-center gap-2 text-base font-semibold">
<CalendarRange aria-hidden className="h-4 w-4 text-muted-foreground" />
Reporting period
</h2>
<p className="mt-1 text-sm text-muted-foreground">
Every percentage on this dashboard is measured against the comparison
window below.
</p>
</div>
</header>
{/* ------------------------------------------------------------ *
* Window length
* ------------------------------------------------------------ */}
<div
role="radiogroup"
aria-label="Reporting window"
className="mt-5 flex flex-wrap gap-2"
>
{presets.map((option) => {
const selected = option.id === preset.id
return (
<button
key={option.id}
type="button"
role="radio"
aria-checked={selected}
onClick={() => setPresetId(option.id)}
className={`rounded-lg border px-3 py-1.5 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background ${
selected
? 'border-primary bg-primary/10 text-foreground'
: 'border-border text-muted-foreground hover:bg-muted hover:text-foreground'
}`}
>
{option.label}
</button>
)
})}
</div>
{/* ------------------------------------------------------------ *
* Comparison
* ------------------------------------------------------------ */}
<fieldset className="mt-6">
<legend className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">
Compare against
</legend>
<div className="mt-3 grid gap-2 sm:grid-cols-3">
{MODES.map((option) => {
const selected = option.id === mode
return (
<label
key={option.id}
className={`min-w-0 cursor-pointer rounded-xl border p-3 transition-colors ${
selected ? 'border-primary bg-primary/5' : 'border-border hover:bg-muted/50'
}`}
>
<span className="flex min-w-0 items-center gap-2 text-sm font-medium">
<input
type="radio"
name="comparison-mode"
value={option.id}
checked={selected}
onChange={() => setMode(option.id)}
className="h-3.5 w-3.5 shrink-0 accent-primary"
/>
<span className="min-w-0 flex-1 break-words">{option.label}</span>
</span>
<span className="mt-1 block break-words text-xs text-muted-foreground">{option.hint}</span>
</label>
)
})}
</div>
</fieldset>
{/* ------------------------------------------------------------ *
* The resolved dates — the whole point of the block
* ------------------------------------------------------------ */}
<div
aria-live="polite"
className="mt-6 rounded-xl border border-border bg-muted/40 p-4"
>
<div className="flex flex-wrap items-center gap-x-3 gap-y-2 text-sm">
<span className="inline-flex items-center gap-1.5 font-semibold">
<span aria-hidden className="h-2 w-2 rounded-full bg-primary border border-transparent" />
{formatRange(primary.start, primary.end)}
</span>
{comparison ? (
<>
<ArrowLeftRight aria-hidden className="h-3.5 w-3.5 text-muted-foreground" />
<span className="inline-flex items-center gap-1.5 text-muted-foreground">
<span aria-hidden className="h-2 w-2 rounded-full bg-muted-foreground/50 border border-transparent" />
{formatRange(comparison.start, comparison.end)}
</span>
</>
) : (
<span className="text-muted-foreground">— no deltas shown</span>
)}
</div>
{/*
Stated in words rather than expressed as a silently shifted date.
A reader reconciling these numbers against a spreadsheet needs to
know the window moved and why.
*/}
{mode === 'previous' ? (
<label className="mt-3 flex items-start gap-2 text-xs text-muted-foreground">
<input
type="checkbox"
checked={aligned}
onChange={(event) => setAligned(event.target.checked)}
className="mt-0.5 h-3.5 w-3.5 accent-primary"
/>
<span>
Align to the same weekdays.{' '}
{aligned
? 'On — the comparison window has been shifted back to whole weeks, so weekends line up.'
: 'Off — the comparison is the window immediately before this one, which can land a weekend against weekdays.'}
</span>
</label>
) : null}
</div>
<p className="mt-4 flex items-start gap-2 text-xs text-muted-foreground">
<Info aria-hidden className="mt-0.5 h-3.5 w-3.5 shrink-0" />
Both windows are inclusive of their end dates and computed in UTC, so a
change of clocks never produces a week of six days and 23 hours.
</p>
<div className="mt-5 flex justify-end gap-2">
<button
type="button"
className="rounded-lg border border-border px-3 py-1.5 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
>
Reset
</button>
<button
type="button"
className="inline-flex items-center gap-1.5 rounded-lg bg-primary px-3 py-1.5 text-sm font-semibold text-primary-foreground transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
>
<Check aria-hidden className="h-4 w-4" />
Apply
</button>
</div>
</section>
)
}
bg-card, text-muted-foreground) — it inherits your theme instead of overriding it.Drop it at components/dashboard-comparison-period.tsx and import it where you need the section:
import { DashboardComparisonPeriod } from '@/components/dashboard-comparison-period'2 of this block’s props are simple enough to drive from here. Change them and the block below re-renders — it is the same component whose source is above, not a mock of it. Everything else it accepts is in the table underneath.
Read out of the component’s own type and signature, so this cannot drift from the source below. Every prop has a default — the component renders standalone before you pass it anything.
| Prop | Type | Default |
|---|---|---|
presets | PeriodPreset[] | DEFAULT_PRESETS |
initialPresetId | string | '28d' |
initialMode | ComparisonMode | 'previous' |
todayAnchor date the windows are measured back from. Fixed by default so the preview and the screenshots do not change every night. | Date | new Date(Date.UTC(2026, 7, 31)) |
alignWeekdayShift the comparison window to start on the same weekday. Off by default because it is the surprising behaviour, and a block that silently moved a customer's reporting window would be worse than one that offers the option in the open. | boolean | false |
className | string | '' |
The same block rendered once to markup, wrapped as a file your framework compiles. Tailwind classes are framework-agnostic, so the design transfers intact — the behaviour does not.
This block is interactive. The markup below is its initial state with the event handlers stripped — you will need to re-wire the behaviour in your framework.
<!--
Comparison Period Picker — markup from the Hoverlab catalog.
This is the block rendered once to HTML and wrapped as a component
file. It is not a port of the React source: the Tailwind classes carry
the design, which is the part that took the work, and they are the same
in every framework.
This block is interactive in React and the handlers are NOT here.
Buttons, toggles and menus render in their initial state and do
nothing until you wire them up.
-->
<section class="rounded-2xl border border-border bg-card p-5 text-card-foreground sm:p-6 ">
<header class="flex flex-wrap items-start justify-between gap-3">
<div>
<h2 class="flex items-center gap-2 text-base font-semibold"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-calendar-range h-4 w-4 text-muted-foreground" aria-hidden="true"><rect width="18" height="18" x="3" y="4" rx="2"></rect><path d="M16 2v4"></path><path d="M3 10h18"></path><path d="M8 2v4"></path><path d="M17 14h-6"></path><path d="M13 18H7"></path><path d="M7 14h.01"></path><path d="M17 18h.01"></path></svg>Reporting period</h2>
<p class="mt-1 text-sm text-muted-foreground">Every percentage on this dashboard is measured against the comparison window below.</p>
</div>
</header>
<div role="radiogroup" aria-label="Reporting window" class="mt-5 flex flex-wrap gap-2">
<button type="button" role="radio" aria-checked="false" class="rounded-lg border px-3 py-1.5 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background border-border text-muted-foreground hover:bg-muted hover:text-foreground">Last 7 days</button>
<button type="button" role="radio" aria-checked="false" class="rounded-lg border px-3 py-1.5 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background border-border text-muted-foreground hover:bg-muted hover:text-foreground">Last 14 days</button>
<button type="button" role="radio" aria-checked="true" class="rounded-lg border px-3 py-1.5 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background border-primary bg-primary/10 text-foreground">Last 28 days</button>
<button type="button" role="radio" aria-checked="false" class="rounded-lg border px-3 py-1.5 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background border-border text-muted-foreground hover:bg-muted hover:text-foreground">Last quarter</button>
<button type="button" role="radio" aria-checked="false" class="rounded-lg border px-3 py-1.5 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background border-border text-muted-foreground hover:bg-muted hover:text-foreground">Last year</button>
</div>
<fieldset class="mt-6">
<legend class="text-xs font-semibold uppercase tracking-wider text-muted-foreground">Compare against</legend>
<div class="mt-3 grid gap-2 sm:grid-cols-3">
<label class="min-w-0 cursor-pointer rounded-xl border p-3 transition-colors border-primary bg-primary/5">
<span class="flex min-w-0 items-center gap-2 text-sm font-medium">
<input type="radio" class="h-3.5 w-3.5 shrink-0 accent-primary" name="comparison-mode" checked="" value="previous" />
<span class="min-w-0 flex-1 break-words">Previous period</span>
</span>
<span class="mt-1 block break-words text-xs text-muted-foreground">The window immediately before this one.</span>
</label>
<label class="min-w-0 cursor-pointer rounded-xl border p-3 transition-colors border-border hover:bg-muted/50">
<span class="flex min-w-0 items-center gap-2 text-sm font-medium">
<input type="radio" class="h-3.5 w-3.5 shrink-0 accent-primary" name="comparison-mode" value="year" />
<span class="min-w-0 flex-1 break-words">Same period last year</span>
</span>
<span class="mt-1 block break-words text-xs text-muted-foreground">The same calendar dates, one year back.</span>
</label>
<label class="min-w-0 cursor-pointer rounded-xl border p-3 transition-colors border-border hover:bg-muted/50">
<span class="flex min-w-0 items-center gap-2 text-sm font-medium">
<input type="radio" class="h-3.5 w-3.5 shrink-0 accent-primary" name="comparison-mode" value="none" />
<span class="min-w-0 flex-1 break-words">No comparison</span>
</span>
<span class="mt-1 block break-words text-xs text-muted-foreground">Show absolute numbers with no deltas.</span>
</label>
</div>
</fieldset>
<div aria-live="polite" class="mt-6 rounded-xl border border-border bg-muted/40 p-4">
<div class="flex flex-wrap items-center gap-x-3 gap-y-2 text-sm">
<span class="inline-flex items-center gap-1.5 font-semibold"><span aria-hidden="true" class="h-2 w-2 rounded-full bg-primary border border-transparent"></span>Aug 4 – Aug 31, 2026</span>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-arrow-left-right h-3.5 w-3.5 text-muted-foreground" aria-hidden="true">
<path d="M8 3 4 7l4 4"></path>
<path d="M4 7h16"></path>
<path d="m16 21 4-4-4-4"></path>
<path d="M20 17H4"></path>
</svg>
<span class="inline-flex items-center gap-1.5 text-muted-foreground"><span aria-hidden="true" class="h-2 w-2 rounded-full bg-muted-foreground/50 border border-transparent"></span>Jul 7 – Aug 3, 2026</span>
</div>
<label class="mt-3 flex items-start gap-2 text-xs text-muted-foreground">
<input type="checkbox" class="mt-0.5 h-3.5 w-3.5 accent-primary" />
<span>Align to the same weekdays. Off — the comparison is the window immediately before this one, which can land a weekend against weekdays.</span>
</label>
</div>
<p class="mt-4 flex items-start gap-2 text-xs text-muted-foreground"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-info mt-0.5 h-3.5 w-3.5 shrink-0" aria-hidden="true"><circle cx="12" cy="12" r="10"></circle><path d="M12 16v-4"></path><path d="M12 8h.01"></path></svg>Both windows are inclusive of their end dates and computed in UTC, so a change of clocks never produces a week of six days and 23 hours.</p>
<div class="mt-5 flex justify-end gap-2">
<button type="button" class="rounded-lg border border-border px-3 py-1.5 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">Reset</button>
<button type="button" class="inline-flex items-center gap-1.5 rounded-lg bg-primary px-3 py-1.5 text-sm font-semibold text-primary-foreground transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-check h-4 w-4" aria-hidden="true"><path d="M20 6 9 17l-5-5"></path></svg>Apply</button>
</div>
</section>
What each framework gets across the whole catalog — effects convert properly; this rung is markup.
The component, its props, the design tokens it expects and the command that installs it — as one prompt. Paste it into Claude, Cursor, v0 or ChatGPT and what they build around it will match the rest of the catalog instead of inventing its own system.
Want the whole screen instead of this one section? Open a page and copy it entire.
All of them free to read, copy and install — no account, no locked tiles, no watermarked preview. The whole catalog is open, and so are the API and the CLI.
Browse DashboardsCopying the code is free. Putting it in client work or a paid product is what Pro is for — the licence, not the access.
Sidebar, top bar and scrollable content slot, with a mobile drawer that closes on Escape and marks the active route.
Metric row where each card knows whether a rise is good news, so churn going up is red while revenue going up is green.
Recent-events list with typed icons and relative timestamps that keep the absolute value in a <time> element.
Breadcrumb, title, action buttons and a counted tab row that sits flush with the bottom rule.
Four columns of prioritised, tagged, assigned task cards with a blocked state — the board as a layout contract, leaving drag behaviour to your own state and library.
Events grouped by day on a vertical rail, icons varying by type and one entry expanded into a detail card — what keeps forty events scannable instead of forty gray dots.
Saved views with the third state most implementations drop: a shared view with unsaved edits, where Save changes what a colleague sees tomorrow. Two honest buttons instead of one, ownership on the tab, and dirty said in words.
Threshold alerts with the two facts most rule lists omit: how often each has fired, and whether its channel is muted — so a rule that is on but delivering nowhere reads as its own state.