- Accordion
- Agent Thinking
- Alert
- Alert Dialog
- Announcement
- Aspect Ratio
- Attachment
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Checkbox
- Checkbox Card
- Code Block
- Collapsible
- Color Picker
- Combobox
- Command
- Composer
- Composer Loader
- Context Menu
- Data Grid
- Data Table
- Date Picker
- Dialog
- Direction
- Drawer
- Dropdown Menu
- Empty
- Field
- File Upload
- Hover Card
- Input
- Input Group
- Input Otp
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Message Scroller
- Meter
- Native Select
- Navigation Menu
- Number Field
- Pagination
- Period Switcher
- Popover
- Progress
- Questionnaire
- Radio Card
- Radio Group
- Resizable
- Reveal
- Scroll Area
- Search Field
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Social Button
- Sonner
- Spinner
- Status Dot
- Switch
- Switch Card
- Table
- Tabs
- Tag Group
- Textarea
- Theme Toggle
- Time Field
- Toggle
- Toggle Group
- Tooltip
- Trace
- Tree View
- Agent Creator 01
- Agent Limits Card 01
- Agent Progress 01
- AI Chat 01
- AI Image Generation 01
- AI Profile 01
- Alerts Card 01
- Auth Card 01
- Calendar 01
- Composer 01
- Composer Attachments 01
- Composer Panel 01
- Finance Dashboard 01
- Home Dashboard 01
- Marketing Dashboard 01
- Medical Profile 01
- Meeting Scheduler 01
- Multi Agent Chat 01
- Notification Center 01
- Patient Info Card 01
- Project Board 01
- Questionnaire 01
- Settings Modal 01
- Sidebar 01
- Stat Cards 01
- Task List 01
- Web Search 01
- Activity Rings Card 01
- Area Chart Card 01
- Bar List Card 01
- Combo Chart Card 01
- Contributions Card 01
- Earnings Chart Card 01
- Funnel Chart Card 01
- Heatmap Chart Card 01
- Line Chart Card 01
- Most Active Days Card 01
- Orders Chart Card 01
- Radar Chart Card 01
- Radial Chart Card 01
- Revenue Chart Card 01
- Sankey Chart Card 01
- Scatter Chart Card 01
- Sleep Score Card 01
- Stage Bars Card 01
- Steps Card 01
29 Jun - 5 Jul
April
Installation
$ pnpm dlx shadcn@latest add @cobalt/period-switcher
Usage
The block owns the index. Set disabled on the buttons at the ends of the
range and pass the current label as the children of PeriodSwitcherLabel.
When the label changes, the old text slides up and fades out while the new
text rises in from below, whichever button was pressed. With reduced motion
the text swaps at once.
Source
"use client";
import { RiArrowLeftSLine, RiArrowRightSLine } from "@remixicon/react";
import { cn } from "cn";
import * as React from "react";
import { Button } from "@/components/ui/button";
/**
* A "‹ label ›" chip for stepping through periods such as weeks or months.
* The block owns the index: it sets `disabled` at the ends of its range and
* passes the new label, and the label swaps with a short upward slide.
*/
function PeriodSwitcher({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="period-switcher"
role="group"
className={cn(
"flex h-8 shrink-0 items-center rounded-[10px] border border-[var(--border-subtle,var(--border))] bg-[var(--surface-card,var(--popover))] shadow-[shadow:var(--shadow-control,0_1px_2px_0_rgb(0_0_0/0.05))]",
className
)}
{...props}
/>
);
}
function PeriodSwitcherPrevious({
children,
tooltip = "Previous period",
...props
}: React.ComponentProps<typeof Button>) {
return (
<Button
data-slot="period-switcher-previous"
variant="ghost-muted"
size="icon-2xs"
radius={6}
tooltip={tooltip}
{...props}
>
{children ?? <RiArrowLeftSLine aria-hidden className="size-4" />}
</Button>
);
}
function PeriodSwitcherNext({
children,
tooltip = "Next period",
...props
}: React.ComponentProps<typeof Button>) {
return (
<Button
data-slot="period-switcher-next"
variant="ghost-muted"
size="icon-2xs"
radius={6}
tooltip={tooltip}
{...props}
>
{children ?? <RiArrowRightSLine aria-hidden className="size-4" />}
</Button>
);
}
const swapMs = 200;
/**
* The centred label. When `children` changes, the old text slides up and
* fades out while the new text rises in from below, whichever button was
* pressed. With reduced motion the text swaps at once.
*/
function PeriodSwitcherLabel({
className,
children,
...props
}: React.ComponentProps<"span">) {
const [state, setState] = React.useState<{
current: React.ReactNode;
previous: React.ReactNode;
swaps: number;
}>({ current: children, previous: null, swaps: 0 });
if (!Object.is(state.current, children)) {
setState({
current: children,
previous: state.current,
swaps: state.swaps + 1,
});
}
React.useEffect(() => {
if (state.swaps === 0) {
return;
}
const timer = setTimeout(
() => setState((s) => ({ ...s, previous: null })),
swapMs
);
return () => clearTimeout(timer);
}, [state.swaps]);
const animating = state.previous !== null;
return (
<span
data-slot="period-switcher-label"
aria-live="polite"
aria-atomic="true"
className={cn(
"relative flex flex-1 items-center justify-center px-1 text-center text-sm leading-5 font-medium whitespace-nowrap text-foreground",
className
)}
{...props}
>
{animating && (
<span
key={`out-${state.swaps}`}
aria-hidden
className="absolute inset-0 flex animate-[period-switcher-out_200ms_ease-out_forwards] items-center justify-center motion-reduce:hidden"
>
{state.previous}
</span>
)}
<span
key={`in-${state.swaps}`}
className={cn(
animating &&
"animate-[period-switcher-in_200ms_ease-out] motion-reduce:animate-none"
)}
>
{state.current}
</span>
</span>
);
}
export {
PeriodSwitcher,
PeriodSwitcherLabel,
PeriodSwitcherNext,
PeriodSwitcherPrevious,
}; On this page